-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsync_version.py
More file actions
397 lines (356 loc) · 13.4 KB
/
Copy pathsync_version.py
File metadata and controls
397 lines (356 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
"""
OpenCut Version Sync Script
Reads __version__ from opencut/__init__.py and updates all other files
that contain version strings to match.
Usage:
python scripts/sync_version.py # Sync current version to all files
python scripts/sync_version.py --set 1.3.0 # Set new version, then sync
"""
import argparse
import re
import sys
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
if str(SCRIPT_DIR) not in sys.path:
sys.path.insert(0, str(SCRIPT_DIR))
from release_gate import DEFAULT_RECEIPT, ReleaseGateError, validate_receipt # noqa: E402
ROOT = Path(__file__).resolve().parent.parent
INIT_PY = ROOT / "opencut" / "__init__.py"
# Each entry: (relative path, regex pattern, replacement template)
# The replacement template uses {v} for the version string and may also use
# computed release-series placeholders from version_tokens().
TARGETS = [
(
"pyproject.toml",
r'^(version\s*=\s*")[^"]+(")',
r"\g<1>{v}\g<2>",
),
(
"opencut/server.py",
r"(OpenCut Backend Server v)[0-9]+\.[0-9]+\.[0-9]+",
r"\g<1>{v}",
),
(
"extension/com.opencut.panel/client/main.js",
r"(OpenCut CEP Panel - Main Controller v)[0-9]+\.[0-9]+\.[0-9]+",
r"\g<1>{v}",
),
(
"extension/com.opencut.panel/client/style.css",
r"(OpenCut CEP Panel v)[0-9]+\.[0-9]+\.[0-9]+",
r"\g<1>{v}",
),
# CEP manifest (both bundle and extension version)
(
"extension/com.opencut.panel/CSXS/manifest.xml",
r'(ExtensionBundleVersion=")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
(
"extension/com.opencut.panel/CSXS/manifest.xml",
r'(<Extension Id="com\.opencut\.panel\.main" Version=")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
# Installer C# project
(
"installer/src/OpenCut.Installer/OpenCut.Installer.csproj",
r"(<Version>)[0-9]+\.[0-9]+\.[0-9]+(</Version>)",
r"\g<1>{v}\g<2>",
),
(
"installer/src/OpenCut.Installer/OpenCut.Installer.csproj",
r"(<FileVersion>)[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(</FileVersion>)",
r"\g<1>{v}.0\g<2>",
),
(
"installer/src/OpenCut.Installer/OpenCut.Installer.csproj",
r"(<AssemblyVersion>)[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(</AssemblyVersion>)",
r"\g<1>{v}.0\g<2>",
),
# Installer AppConstants.cs
(
"installer/src/OpenCut.Installer/Models/AppConstants.cs",
r'(Version\s*=\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
# Installer app.manifest
(
"installer/src/OpenCut.Installer/Properties/app.manifest",
r'(assemblyIdentity version=")[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}.0\g<2>",
),
(
"installer/src/OpenCut.Installer/Properties/app.smoke.manifest",
r'(assemblyIdentity version=")[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}.0\g<2>",
),
# Install.ps1 dev installer banner
(
"Install.ps1",
r"(Installer v)[0-9]+\.[0-9]+\.[0-9]+",
r"\g<1>{v}",
),
# install.py dev installer
(
"install.py",
r'(VERS\s*=\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
# requirements.txt header comment
(
"requirements.txt",
r"(# OpenCut v)[0-9]+\.[0-9]+\.[0-9]+",
r"\g<1>{v}",
),
# Inno Setup
(
"OpenCut.iss",
r'(#define MyAppVersion\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
# CEP package.json
(
"extension/com.opencut.panel/package.json",
r'("version":\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
# CEP package-lock.json root package metadata. Keep these patterns scoped
# to the package root so dependency versions such as lightningcss 1.32.0
# are not rewritten as OpenCut releases.
(
"extension/com.opencut.panel/package-lock.json",
r'(^\s*"version":\s*")[0-9]+\.[0-9]+\.[0-9]+(",\s*$)',
r"\g<1>{v}\g<2>",
),
(
"extension/com.opencut.panel/package-lock.json",
r'("":\s*\{\s*"name":\s*"opencut-panel",\s*"version":\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
# UXP manifest.json
(
"extension/com.opencut.uxp/manifest.json",
r'("version":\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
# UXP main.js VERSION constant
(
"extension/com.opencut.uxp/main.js",
r'(const VERSION\s*=\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r"\g<1>{v}\g<2>",
),
# UXP index.html version display
(
"extension/com.opencut.uxp/index.html",
r'(<span class="oc-version"[^>]*>v)[0-9]+\.[0-9]+\.[0-9]+(</span>)',
r"\g<1>{v}\g<2>",
),
(
"extension/com.opencut.uxp/index.html",
r'(<span id="uxpVersionDisplay">)[0-9]+\.[0-9]+\.[0-9]+( \(UXP\)</span>)',
r"\g<1>{v}\g<2>",
),
# Security support policy tracks minor series, not full patch releases.
(
"SECURITY.md",
r"(latest minor\*\* \(`)[0-9]+\.[0-9]+\.x(`\) and the one immediately preceding it \(`)[0-9]+\.[0-9]+\.x(`\))",
r"\g<1>{series}\g<2>{previous_series}\g<3>",
),
# NOTE: this row is matched and rebuilt as a full single line. The previous
# pattern used `\|\s*)[^\n|]+(\s*\|` for the last cell; `\s*` could swallow
# the newline and match the next row's leading pipe when the row carried
# trailing whitespace, leaving `|—` residue after the closing pipe.
#
# The end of the row is asserted with a lookahead rather than consumed. On a
# CRLF checkout `[^\n]*$` swallows the `\r` into the match while the rebuilt
# replacement has none, so `--check` compared `row + "\r"` against `row` and
# reported a permanent mismatch that no `--set` could clear.
(
"SECURITY.md",
r"^\| [0-9]+\.[0-9]+\.x\s+\| ✅ Active\s+\|[^\n\r]*(?=\r?$)",
r"| {series} | ✅ Active | — |",
),
(
"SECURITY.md",
r"(\| )[0-9]+\.[0-9]+\.x(\s+\|[^\n]*Previous[^\n]*\|\s*)\+90 days after [0-9]+\.[0-9]+(\s*\|)",
r"\g<1>{previous_series}\g<2>+90 days after {latest_minor}\g<3>",
),
(
"SECURITY.md",
r"(\| )[0-9]+\.[0-9]+\.x(\s+\|[^\n]*Critical only[^\n]*\|\s*)\+30 days after [0-9]+\.[0-9]+(\s*\|)",
r"\g<1>{critical_series}\g<2>+30 days after {latest_minor}\g<3>",
),
(
"SECURITY.md",
r"(\| )[≤<=>\s]*[0-9]+\.[0-9]+(\s+\|[^\n]*End of life[^\n]*\|\s*)n/a(\s*\|)",
r"\g<1>≤ {eol_minor}\g<2>n/a\g<3>",
),
(
"opencut/core/c2pa_sidecar.py",
r'(CLAIM_GENERATOR_DEFAULT\s*=\s*"OpenCut/)[0-9]+\.[0-9]+\.[0-9]+( \(sidecar; c2pa-spec 2\.4\)")',
r"\g<1>{v}\g<2>",
),
# NOTE: packaging/linux/*.metainfo.xml is deliberately NOT a sync target.
# Its newest <release> entry tracks the last Flathub-PUBLISHED build
# (pinned by tests/test_linux_distribution_packaging.py), not the source
# version; add a dated <release> block there only when a Flathub build
# actually ships.
# CEP update card current version and About dialog version. Keep these
# selectors scoped so one sync updates both numeric surfaces.
(
"extension/com.opencut.panel/client/index.html",
r'(<span class="settings-value" id="updateCurrentVersion">)[0-9]+\.[0-9]+\.[0-9]+(</span>)',
r"\g<1>{v}\g<2>",
),
(
"extension/com.opencut.panel/client/index.html",
r'(<span class="settings-label" data-i18n="settings.version">Version</span>\s*<span class="settings-value">)[0-9]+\.[0-9]+\.[0-9]+(</span>)',
r"\g<1>{v}\g<2>",
),
(
"extension/com.opencut.uxp/index.html",
r'(<strong class="oc-inline-value" id="uxpUpdateCurrentVersion">)[0-9]+\.[0-9]+\.[0-9]+(</strong>)',
r"\g<1>{v}\g<2>",
),
# README.md version badge and feature-overview heading
(
"README.md",
r"(!\[Version\]\(https://img\.shields\.io/badge/version-)[0-9]+\.[0-9]+\.[0-9]+(-blue\))",
r"\g<1>{v}\g<2>",
),
(
"README.md",
r"(OpenCut v)[0-9]+\.[0-9]+\.[0-9]+( includes \*\*)",
r"\g<1>{v}\g<2>",
),
]
def _read_text(path: Path) -> str:
"""Read UTF-8 without normalizing the file's existing line endings."""
with path.open("r", encoding="utf-8", newline="") as handle:
return handle.read()
def _write_text(path: Path, text: str) -> None:
"""Write UTF-8 without translating line endings on Windows."""
with path.open("w", encoding="utf-8", newline="") as handle:
handle.write(text)
def version_tokens(version: str) -> dict[str, str]:
"""Return replacement tokens derived from an X.Y.Z version string."""
major_s, minor_s, patch_s = version.split(".")
major = int(major_s)
minor = int(minor_s)
def minor_at(offset: int) -> int:
return max(minor + offset, 0)
return {
"v": version,
"major": str(major),
"minor": str(minor),
"patch": patch_s,
"series": f"{major}.{minor}.x",
"previous_series": f"{major}.{minor_at(-1)}.x",
"critical_series": f"{major}.{minor_at(-2)}.x",
"latest_minor": f"{major}.{minor}",
"previous_minor": f"{major}.{minor_at(-1)}",
"critical_minor": f"{major}.{minor_at(-2)}",
"eol_minor": f"{major}.{minor_at(-3)}",
}
def render_replacement(replacement: str, version: str) -> str:
"""Expand sync_version replacement placeholders for a target."""
rendered = replacement
for key, value in version_tokens(version).items():
rendered = rendered.replace(f"{{{key}}}", value)
return rendered
def read_version() -> str:
"""Read __version__ from opencut/__init__.py."""
text = _read_text(INIT_PY)
m = re.search(r'__version__\s*=\s*"([^"]+)"', text)
if not m:
print(f"ERROR: Could not find __version__ in {INIT_PY}")
sys.exit(1)
return m.group(1)
def set_version(new_ver: str) -> None:
"""Write __version__ into opencut/__init__.py."""
text = _read_text(INIT_PY)
updated = re.sub(
r'(__version__\s*=\s*")[^"]+(")',
rf"\g<1>{new_ver}\g<2>",
text,
)
_write_text(INIT_PY, updated)
print(f" SET {INIT_PY.relative_to(ROOT)} -> {new_ver}")
def check_file(rel_path: str, pattern: str, replacement: str, version: str) -> bool:
"""Check if a file's version matches. Returns True if in sync."""
fpath = ROOT / rel_path
if not fpath.exists():
return True # Missing files are OK (optional targets)
text = _read_text(fpath)
m = re.search(pattern, text, flags=re.MULTILINE)
if not m:
return True # Pattern not found — nothing to check
matched = m.group(0)
repl = render_replacement(replacement, version)
expected = re.sub(pattern, repl, matched, count=1, flags=re.MULTILINE)
if expected == matched:
return True
print(f" MISMATCH {rel_path} (expected {version})")
return False
def sync_file(rel_path: str, pattern: str, replacement: str, version: str) -> bool:
"""Update a single version occurrence in a file. Returns True if changed."""
fpath = ROOT / rel_path
if not fpath.exists():
print(f" SKIP {rel_path} (file not found)")
return False
text = _read_text(fpath)
repl = render_replacement(replacement, version)
updated, count = re.subn(pattern, repl, text, count=1, flags=re.MULTILINE)
if count == 0:
print(f" SKIP {rel_path} (pattern not matched)")
return False
_write_text(fpath, updated)
print(f" SYNC {rel_path} -> {version}")
return True
def main() -> None:
parser = argparse.ArgumentParser(description="Sync OpenCut version across all files")
parser.add_argument(
"--set", dest="new_version", metavar="X.Y.Z", help="Set a new version in __init__.py before syncing"
)
parser.add_argument(
"--receipt",
type=Path,
default=DEFAULT_RECEIPT,
help="Fresh successful release-gate receipt required with --set",
)
parser.add_argument(
"--check", action="store_true", help="Check all files are in sync (exit 1 if not). Does not modify files."
)
args = parser.parse_args()
if args.new_version:
if not re.match(r"^\d+\.\d+\.\d+$", args.new_version):
print(f"ERROR: Invalid version format '{args.new_version}' (expected X.Y.Z)")
sys.exit(1)
try:
validate_receipt(args.receipt)
except ReleaseGateError as exc:
print(f"ERROR: Version bump refused: {exc}")
sys.exit(1)
set_version(args.new_version)
version = read_version()
if args.check:
print(f"\nChecking version {version} across project files:\n")
all_ok = True
for rel_path, pattern, replacement in TARGETS:
if not check_file(rel_path, pattern, replacement, version):
all_ok = False
if all_ok:
print(f"\nAll files in sync at v{version}.")
else:
print("\nVersion mismatch detected! Run: python scripts/sync_version.py")
sys.exit(1)
return
print(f"\nSyncing version {version} across project files:\n")
changed = 0
for rel_path, pattern, replacement in TARGETS:
if sync_file(rel_path, pattern, replacement, version):
changed += 1
print(f"\nDone. {changed} file(s) updated to v{version}.")
if __name__ == "__main__":
main()