Skip to content

Commit 9717f12

Browse files
committed
better error handling: print failure message and exit 1 if version bump fails
1 parent 482a979 commit 9717f12

File tree

1 file changed

+98
-23
lines changed

1 file changed

+98
-23
lines changed

commands.py

Lines changed: 98 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -299,30 +299,46 @@ def update_plotlyjs_dev(args, outdir):
299299

300300

301301
def bump_version_pyproject_toml(new_version):
302-
"""Bump the version in pyproject.toml to new_version"""
303-
302+
"""
303+
Bump the version in pyproject.toml to new_version
304+
Returns True on success, False on failure
305+
"""
304306
pyproject_toml_path = "pyproject.toml"
305307
pattern = r'(^\s*version\s*=\s*")([\w.]+)"(\s*$)'
306308
with open(pyproject_toml_path, "r") as f:
307309
content = f.read()
308-
new_content = re.sub(
310+
new_content, n_subs = re.subn(
309311
pattern,
310312
rf'\g<1>{new_version}"\g<3>',
311313
content,
312314
count=1, # replace only the first match
313315
flags=re.MULTILINE,
314316
)
317+
if n_subs < 1:
318+
print(
319+
f"FAILED to update version in {pyproject_toml_path}.",
320+
"Please update manually and then run `uv lock`.",
321+
)
322+
return False
323+
315324
with open(pyproject_toml_path, "w") as f:
316325
f.write(new_content)
317-
326+
318327
# Run `uv lock` to update the version number in the `uv.lock` file (do not update manually)
319328
subprocess.run(["uv", "lock"], check=True)
320329

321-
print(f"Updated version in {pyproject_toml_path} to {new_version}, and updated uv lockfile")
330+
print(
331+
f"Updated version in {pyproject_toml_path} to {new_version},",
332+
"and updated uv lockfile",
333+
)
334+
return True
322335

323336

324337
def bump_version_package_json(new_version):
325-
"""Bump the version in package.json to new_version"""
338+
"""
339+
Bump the version in package.json to new_version
340+
Returns True on success, False on failure
341+
"""
326342
js_dir = "js"
327343
subprocess.run(
328344
["npm", "version", new_version, "--no-git-tag-version", "--allow-same-version"],
@@ -332,66 +348,125 @@ def bump_version_package_json(new_version):
332348
print(
333349
f"Updated version in {js_dir}/package.json and {js_dir}/package-lock.json to {new_version}"
334350
)
351+
return True
335352

336353

337354
def bump_version_citation_cff(new_version, new_date):
338-
"""Bump the version in CITATION.cff to new_version and date-released to new_date"""
355+
"""
356+
Bump the version in CITATION.cff to new_version and date-released to new_date
357+
Returns True on success, False on failure
358+
"""
339359
citation_cff_path = "CITATION.cff"
340-
pattern_version = r'(^\s*version\s*:\s*)([\w.]+)(\s*$)'
341-
pattern_date = r'(^\s*date-released\s*:\s*)([0-9\-]+)(\s*$)'
360+
pattern_version = r"(^\s*version\s*:\s*)([\w.]+)(\s*$)"
361+
pattern_date = r"(^\s*date-released\s*:\s*)([0-9\-]+)(\s*$)"
342362

343363
with open(citation_cff_path, "r") as f:
344364
content = f.read()
345-
new_content = re.sub(
365+
new_content, n_subs = re.subn(
346366
pattern_version,
347-
rf'\g<1>{new_version}\g<3>',
367+
rf"\g<1>{new_version}\g<3>",
348368
content,
349369
count=1, # replace only the first match
350370
flags=re.MULTILINE,
351371
)
352-
new_content = re.sub(
372+
if n_subs < 1:
373+
print(
374+
f"FAILED to update version in {citation_cff_path}.",
375+
"Please update manually.",
376+
)
377+
return False
378+
new_content, n_subs = re.subn(
353379
pattern_date,
354-
rf'\g<1>{new_date}\g<3>',
380+
rf"\g<1>{new_date}\g<3>",
355381
new_content,
356382
count=1, # replace only the first match
357383
flags=re.MULTILINE,
358384
)
385+
if n_subs < 1:
386+
print(
387+
f"FAILED to update date-released in {citation_cff_path}.",
388+
"Please update manually.",
389+
)
390+
return False
391+
359392
with open(citation_cff_path, "w") as f:
360393
f.write(new_content)
361394
print(
362-
f"Updated version in {citation_cff_path} to {new_version} and date-released to {new_date}"
395+
f"Updated version in {citation_cff_path} to {new_version}",
396+
f"and date-released to {new_date}",
363397
)
398+
return True
399+
364400

365401
def bump_version_changelog_md(new_version, new_date):
366-
"""Bump the version in CHANGELOG.md to new_version and date to new_date"""
402+
"""
403+
Bump the version in CHANGELOG.md to new_version and date to new_date
404+
Returns True on success, False on failure
405+
"""
367406
changelog_md_path = "CHANGELOG.md"
368-
pattern = r'(^\s*##\s*Unreleased\s*$)'
407+
pattern = r"(^\s*##\s*Unreleased\s*$)"
369408
new_header = f"\n\n## [{new_version}] - {new_date}\n"
409+
370410
with open(changelog_md_path, "r") as f:
371411
content = f.read()
372-
new_content = re.sub(
412+
413+
# Check if the header already exists, so that we don't add a double header
414+
already_exists_pattern = rf"(^\s*##\s*\[ *{re.escape(new_version)} *\])"
415+
if re.search(already_exists_pattern, content, flags=re.MULTILINE):
416+
print(
417+
f"Header for version {new_version} already exists ",
418+
f"in {changelog_md_path}.",
419+
)
420+
return True
421+
422+
new_content, n_subs = re.subn(
373423
pattern,
374-
rf'\g<1>{new_header}',
424+
rf"\g<1>{new_header}",
375425
content,
376426
count=1, # replace only the first match
377427
flags=re.MULTILINE,
378428
)
429+
if n_subs < 1:
430+
print(
431+
f"FAILED to update version in {changelog_md_path}.",
432+
"Please update manually.",
433+
)
434+
return False
435+
379436
with open(changelog_md_path, "w") as f:
380437
f.write(new_content)
381-
print(f"Updated version in {changelog_md_path} to {new_version}")
438+
print(
439+
f"Added header in {changelog_md_path} with version {new_version}",
440+
f"and release date {new_date}",
441+
)
442+
return True
382443

383444

384445
def bump_version(args):
385446
"""Bump the version of plotly.py everywhere it needs to be updated."""
386447
new_version = args.version
387448
new_date = time.strftime("%Y-%m-%d")
388449

389-
# bump_version_citation_cff(new_version, new_date)
390-
# bump_version_changelog_md(new_version, new_date)
391-
# bump_version_package_json(new_version)
450+
success = True
451+
452+
success = success and bump_version_citation_cff(new_version, new_date)
453+
success = success and bump_version_changelog_md(new_version, new_date)
454+
success = success and bump_version_package_json(new_version)
392455
# Do this one last since it's the most visible,
393456
# so that if one of the above commands fails it will be easier to notice
394-
bump_version_pyproject_toml(new_version)
457+
success = success and bump_version_pyproject_toml(new_version)
458+
459+
if not success:
460+
print(
461+
"\n\n*** FAILED to update version for at least one file.",
462+
"Please check the output above for details. ***\n\n",
463+
)
464+
exit(1)
465+
466+
print(
467+
f"\n\n*** SUCCESSFULLY updated version to {new_version}.",
468+
"Please verify the result using `git diff`. ***\n\n",
469+
)
395470

396471

397472
def make_parser():

0 commit comments

Comments
 (0)