1
1
#!/usr/bin/env python3
2
2
"""Bump version of selected packages or core requirements (JupyterLab)"""
3
3
import sys
4
+ from argparse import ArgumentParser
4
5
from dataclasses import dataclass
6
+ from difflib import context_diff
5
7
from pathlib import Path
6
8
from typing import List
7
9
@@ -50,7 +52,13 @@ def maybe_change_version(self, dry: bool):
50
52
def change_version (self , new_version : str , dry : bool ):
51
53
52
54
changelog = CHANGELOG .read_text ()
53
- assert new_version in changelog
55
+ if new_version not in changelog :
56
+ raise Exception (
57
+ (
58
+ f"{ new_version } is absent in CHANGELOG.md file."
59
+ f" Please update the changelog first."
60
+ ).format (new_version = new_version )
61
+ )
54
62
55
63
for location in self .locations :
56
64
replace_version (
@@ -63,12 +71,21 @@ def change_version(self, new_version: str, dry: bool):
63
71
64
72
65
73
def replace_version (path : Path , template : str , old : str , new : str , dry : bool ):
66
- new_content = path .read_text ().replace (
74
+ old_content = path .read_text ()
75
+ new_content = old_content .replace (
67
76
template .format (version = old ), template .format (version = new )
68
77
)
69
78
if dry :
70
- print (path )
71
- print (new_content )
79
+ diff = context_diff (
80
+ old_content .splitlines (),
81
+ new_content .splitlines (),
82
+ fromfile = "current" ,
83
+ tofile = "new (proposed update)" ,
84
+ n = 4 ,
85
+ )
86
+ relative_path = path .relative_to (ROOT ) if path .is_absolute () else path
87
+ print ("\n ## Summary of changes proposed to {path}" .format (path = relative_path ))
88
+ print ("\n " .join (diff ) + "\n " )
72
89
else :
73
90
path .write_text (new_content )
74
91
@@ -127,4 +144,11 @@ def update_versions(dry: bool):
127
144
128
145
129
146
if __name__ == "__main__" :
130
- update_versions (dry = False )
147
+ parser = ArgumentParser ()
148
+ parser .add_argument (
149
+ "--dry" ,
150
+ action = "store_true" ,
151
+ help = "do not perform the update, only show the changes" ,
152
+ )
153
+ args = parser .parse_args ()
154
+ update_versions (dry = args .dry )
0 commit comments