Huiran Yu commited on
Commit
4e3ad8c
·
1 Parent(s): 8129c83

fix future

Browse files
Files changed (2) hide show
  1. Dockerfile +3 -79
  2. patch_madmom.py +106 -0
Dockerfile CHANGED
@@ -22,86 +22,10 @@ RUN pip install --no-cache-dir -r /app/requirements.txt
22
  RUN pip install --no-cache-dir --no-build-isolation madmom
23
 
24
  # madmom patch
25
- RUN python - <<'PY'
26
- import site, pathlib, re
27
-
28
- # locate madmom package
29
- pkg_root = None
30
- for p in site.getsitepackages():
31
- cand = pathlib.Path(p) / "madmom"
32
- if cand.exists():
33
- pkg_root = cand
34
- break
35
- if pkg_root is None:
36
- raise SystemExit("madmom package not found")
37
-
38
- # 1) Fix processors.py: MutableSequence import for py3.10+
39
- proc = pkg_root / "processors.py"
40
- if proc.exists():
41
- s = proc.read_text(encoding="utf-8")
42
- s2 = s.replace("from collections import MutableSequence",
43
- "from collections.abc import MutableSequence")
44
- if s2 != s:
45
- proc.write_text(s2, encoding="utf-8")
46
- print("Patched", proc)
47
-
48
- # 2) Fix numpy aliases if present (np.float etc.)
49
- for f in [pkg_root / "io" / "__init__.py"]:
50
- if f.exists():
51
- s = f.read_text(encoding="utf-8")
52
- s2 = (s.replace("np.float", "float")
53
- .replace("np.int", "int")
54
- .replace("np.bool", "bool")
55
- .replace("np.object", "object"))
56
- if s2 != s:
57
- f.write_text(s2, encoding="utf-8")
58
- print("Patched", f)
59
-
60
- # 3) Fix Python2 type names in madmom/utils/__init__.py
61
- u = pkg_root / "utils" / "__init__.py"
62
- if not u.exists():
63
- raise SystemExit("madmom/utils/__init__.py not found")
64
-
65
- s = u.read_text(encoding="utf-8")
66
-
67
- # Inject compatibility definitions near the top (after imports)
68
- # We'll add them right after the first import block.
69
- lines = s.splitlines(True)
70
- insert_at = 0
71
- for i, line in enumerate(lines):
72
- # after the last consecutive import line at the top
73
- if line.startswith("import ") or line.startswith("from "):
74
- insert_at = i + 1
75
- elif i > 0:
76
- break
77
-
78
- compat = """
79
- # --- Compatibility for Python 3 ---
80
- try:
81
- basestring
82
- except NameError:
83
- basestring = str
84
-
85
- try:
86
- integer
87
- except NameError:
88
- integer = int
89
- # ---------------------------------
90
- """
91
-
92
- # Only insert if not already present
93
- if "basestring = str" not in s:
94
- lines.insert(insert_at, compat)
95
- s = "".join(lines)
96
-
97
- # Ensure string_types / integer_types are well-defined even if old code is weird
98
- s = re.sub(r"string_types\s*=\s*basestring", "string_types = (str,)", s)
99
- s = re.sub(r"integer_types\s*=\s*\(int,\s*integer\)", "integer_types = (int,)", s)
100
-
101
- u.write_text(s, encoding="utf-8")
102
- print("Patched", u)
103
- PY
104
 
 
 
105
 
106
  RUN python -c "import madmom; print('madmom import OK')"
107
 
 
22
  RUN pip install --no-cache-dir --no-build-isolation madmom
23
 
24
  # madmom patch
25
+ COPY patch_madmom.py /app/scripts/patch_madmom.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # ... after installing madmom ...
28
+ RUN python /app/scripts/patch_madmom.py
29
 
30
  RUN python -c "import madmom; print('madmom import OK')"
31
 
patch_madmom.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # scripts/patch_madmom.py
2
+ import re
3
+ import site
4
+ from pathlib import Path
5
+
6
+ FUTURE_LINE = "from __future__ import absolute_import, division, print_function"
7
+
8
+ def find_madmom_root() -> Path:
9
+ for p in site.getsitepackages():
10
+ cand = Path(p) / "madmom"
11
+ if cand.exists():
12
+ return cand
13
+ raise RuntimeError("madmom package not found in site-packages")
14
+
15
+ def patch_processors(madmom_root: Path) -> None:
16
+ proc = madmom_root / "processors.py"
17
+ if not proc.exists():
18
+ return
19
+ s = proc.read_text(encoding="utf-8")
20
+ s2 = s.replace(
21
+ "from collections import MutableSequence",
22
+ "from collections.abc import MutableSequence",
23
+ )
24
+ if s2 != s:
25
+ proc.write_text(s2, encoding="utf-8")
26
+
27
+ def patch_numpy_aliases(madmom_root: Path) -> None:
28
+ # Patch the known failing location; you can broaden to rglob if needed.
29
+ f = madmom_root / "io" / "__init__.py"
30
+ if not f.exists():
31
+ return
32
+ s = f.read_text(encoding="utf-8")
33
+ s2 = (
34
+ s.replace("np.float", "float")
35
+ .replace("np.int", "int")
36
+ .replace("np.bool", "bool")
37
+ .replace("np.object", "object")
38
+ )
39
+ if s2 != s:
40
+ f.write_text(s2, encoding="utf-8")
41
+
42
+ def patch_utils_future_safe(madmom_root: Path) -> None:
43
+ u = madmom_root / "utils" / "__init__.py"
44
+ if not u.exists():
45
+ raise RuntimeError("madmom/utils/__init__.py not found")
46
+
47
+ s = u.read_text(encoding="utf-8")
48
+
49
+ # Remove any existing future import (we'll reinsert correctly)
50
+ s = re.sub(
51
+ r"^\s*from __future__ import absolute_import,\s*division,\s*print_function\s*\n",
52
+ "",
53
+ s,
54
+ flags=re.M,
55
+ )
56
+
57
+ lines = s.splitlines(True)
58
+
59
+ # Find insertion point after shebang/encoding/docstring
60
+ i = 0
61
+ if i < len(lines) and lines[i].startswith("#!"):
62
+ i += 1
63
+ if i < len(lines) and re.match(r"^#.*coding[:=]\s*[-\w.]+", lines[i]):
64
+ i += 1
65
+ if i < len(lines) and re.match(r'^\s*[ruRU]?[\'"]{3}', lines[i]):
66
+ quote = lines[i].lstrip()[0:3] # ''' or """
67
+ i += 1
68
+ while i < len(lines) and quote not in lines[i]:
69
+ i += 1
70
+ if i < len(lines):
71
+ i += 1
72
+
73
+ # Insert future import at the right place
74
+ lines.insert(i, FUTURE_LINE + "\n")
75
+ s = "".join(lines)
76
+
77
+ compat = """
78
+ # --- Compatibility for Python 3 ---
79
+ try:
80
+ basestring
81
+ except NameError:
82
+ basestring = str
83
+
84
+ try:
85
+ integer
86
+ except NameError:
87
+ integer = int
88
+ # ---------------------------------
89
+ """
90
+ if "basestring = str" not in s:
91
+ s = s.replace(FUTURE_LINE + "\n", FUTURE_LINE + "\n" + compat)
92
+
93
+ s = re.sub(r"string_types\s*=\s*basestring", "string_types = (str,)", s)
94
+ s = re.sub(r"integer_types\s*=\s*\(int,\s*integer\)", "integer_types = (int,)", s)
95
+
96
+ u.write_text(s, encoding="utf-8")
97
+
98
+ def main() -> None:
99
+ madmom_root = find_madmom_root()
100
+ patch_processors(madmom_root)
101
+ patch_numpy_aliases(madmom_root)
102
+ patch_utils_future_safe(madmom_root)
103
+ print("madmom patched OK:", madmom_root)
104
+
105
+ if __name__ == "__main__":
106
+ main()