11#!/usr/bin/env python3
22"""Bump version of selected packages or core requirements (JupyterLab)"""
33import sys
4+ from argparse import ArgumentParser
45from dataclasses import dataclass
6+ from difflib import context_diff
57from pathlib import Path
68from typing import List
79
@@ -50,7 +52,13 @@ def maybe_change_version(self, dry: bool):
5052 def change_version (self , new_version : str , dry : bool ):
5153
5254 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+ )
5462
5563 for location in self .locations :
5664 replace_version (
@@ -63,12 +71,21 @@ def change_version(self, new_version: str, dry: bool):
6371
6472
6573def 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 (
6776 template .format (version = old ), template .format (version = new )
6877 )
6978 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 " )
7289 else :
7390 path .write_text (new_content )
7491
@@ -127,4 +144,11 @@ def update_versions(dry: bool):
127144
128145
129146if __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