1414import zipfile
1515
1616import requests
17+ import semver
18+
1719
1820def get_git_command (command ):
21+ """Execute and return the result of a git command without error."""
1922 path = os .getcwd ()
2023 procs = subprocess .run (
2124 command ,
2225 stdout = subprocess .PIPE ,
2326 stderr = subprocess .PIPE ,
27+ check = False ,
2428 cwd = path ,
2529 )
2630 if procs .returncode != 0 :
2731 return None
2832 return procs .stdout .decode ("utf8" ).strip ()
2933
34+
3035def get_current_version ():
36+ """Get current version string."""
3137 return get_git_command ("git describe --tags --exact-match" .split ())
3238
39+
3340def date_to_version (tag ):
34- # YYYYMMDD
35- if re .match (' \d\d\d\d\d\d\d\d' , tag ):
41+ """Convert a tag from YYYYMMDD to y.M.D where y is years after 2020."""
42+ if re .match (r" \d\d\d\d\d\d\d\d" , tag ):
3643 year = int (tag [2 :4 ]) - 20
3744 month = int (tag [4 :6 ])
3845 day = int (tag [6 :8 ])
3946 return f"{ year } .{ month } .{ day } "
40- else :
41- return tag
47+ return tag
48+
4249
4350# the date tag for the generated files and stuff
4451# TODO: retrieve the version number from git or something
@@ -101,10 +108,15 @@ def date_to_version(tag):
101108
102109
103110def file_version_tag (path ):
104- hash = get_git_command (["git" , "log" , "-1" , '--pretty=%H' , path ])
105- #ptag = get_git_command(["git", "describe", "--tags", "--always", hash])
106- #pdate = re.split(r"[~-]", ptag)[0]
107- ctag = get_git_command (["git" , "describe" , "--tags" , "--always" , "--contains" , hash ])
111+ """
112+ Find a suitable version tag for a file using commit dates.
113+ """
114+ hash = get_git_command (["git" , "log" , "-1" , "--pretty=%H" , path ])
115+ # ptag = get_git_command(["git", "describe", "--tags", "--always", hash])
116+ # pdate = re.split(r"[~-]", ptag)[0]
117+ ctag = get_git_command (
118+ ["git" , "describe" , "--tags" , "--always" , "--contains" , hash ]
119+ )
108120 cdate = re .split (r"[~-]" , ctag )[0 ]
109121 if "." in cdate :
110122 ver = cdate
@@ -116,13 +128,13 @@ def file_version_tag(path):
116128
117129
118130def fmt (path , platform = "py" ):
119- """shortcut for the py directory"""
131+ """Shortcut for the py directory. """
120132 return path .format (platform = PLATFORM_NAMES [platform ])
121133
122134
123135# find in python
124136def list_all_files (path ):
125- """clean list of all files in sub folders"""
137+ """Clean list of all files in sub folders. """
126138 pwd = os .getcwd ()
127139 os .chdir (path )
128140 liste = [
@@ -135,7 +147,7 @@ def list_all_files(path):
135147
136148
137149def init_directories ():
138- """erase and create build directories"""
150+ """Erase and create build directories. """
139151 # create build directories
140152 os .makedirs (BUILD_DIR , exist_ok = True )
141153 os .makedirs (BUILD_DEPS , exist_ok = True )
@@ -152,24 +164,46 @@ def init_directories():
152164 os .unlink (zip_file )
153165
154166
167+ def write_version_to (module_local , file_tag ):
168+ """Write the version tag to the __version__ property of the module file."""
169+ module_file = os .path .join (
170+ fmt (BUNDLE_LIB_DIR ),
171+ os .path .relpath (module_local , MODULES_DIR ),
172+ )
173+ with open (module_file , "r" ) as fp :
174+ data = fp .read ()
175+ if "__version__" in data :
176+ data = data .replace (
177+ '\n __version__ = "0.0.0-auto.0"\n ' ,
178+ SET_VERSION_PATTERN .format (file_tag ),
179+ )
180+ with open (module_file , "w" ) as fp :
181+ fp .write (data )
182+
183+
155184def make_bundle_files ():
156- """create the .py bundle directory"""
185+ """Create the .py bundle directory. """
157186 # copy all the layouts and keycodes
158187 shutil .copytree (MODULES_DIR , fmt (BUNDLE_LIB_DIR ))
159188
160189 # change the version number of all the bundles
161- for module_local in list_all_files (MODULES_DIR ):
162- module_file = os .path .join (fmt (BUNDLE_LIB_DIR ), module_local )
163- file_tag = file_version_tag (os .path .join (MODULES_DIR , module_local ))
164- with open (module_file , "r" ) as fp :
165- data = fp .read ()
166- if "__version__" in data :
167- data = data .replace (
168- '\n __version__ = "0.0.0-auto.0"\n ' ,
169- SET_VERSION_PATTERN .format (file_tag ),
170- )
171- with open (module_file , "w" ) as fp :
172- fp .write (data )
190+ for module_local in glob .glob (MODULES_DIR + "/*" ):
191+ if os .path .isdir (module_local ):
192+ # get all versions
193+ versions = []
194+ for sub_module in list_all_files (module_local ):
195+ sub_local_file = os .path .join (module_local , sub_module )
196+ file_tag = file_version_tag (sub_local_file )
197+ versions .append (semver .VersionInfo .parse (file_tag ))
198+ # keep the highest one
199+ file_tag = max (versions )
200+ # set all versions
201+ for sub_module in list_all_files (module_local ):
202+ sub_local_file = os .path .join (module_local , sub_module )
203+ write_version_to (sub_local_file , file_tag )
204+ elif module_local .endswith (".py" ):
205+ file_tag = file_version_tag (module_local )
206+ write_version_to (module_local , file_tag )
173207
174208 # list of the modules
175209 all_modules = [
@@ -200,7 +234,7 @@ def make_bundle_files():
200234 # add the dependency to keyboard_layout
201235 if module .startswith ("keyboard_layout_" ):
202236 json_data [module ]["dependencies" ].append ("keyboard_layout" )
203- with open (target ,"a" ) as fp :
237+ with open (target , "a" ) as fp :
204238 fp .write ("\r \n keyboard_layout\r \n " )
205239
206240 # create the json file
@@ -209,7 +243,7 @@ def make_bundle_files():
209243
210244
211245def make_the_mpy_bundles ():
212- """create the mpy bundle(s) directory(ies) and mpy-cross the modules"""
246+ """Create the mpy bundle(s) directory(ies) and mpy-cross the modules. """
213247 # copy for the zips
214248 shutil .copy (BUNDLE_JSON , fmt (BUNDLE_ZIP_JSON ))
215249
@@ -238,7 +272,7 @@ def make_the_mpy_bundles():
238272
239273
240274def do_the_zips ():
241- """finally create the zip files for release"""
275+ """Finally create the zip files for release. """
242276 # now do the zips
243277 for platform in ["py" ] + PLATFORMS :
244278 in_path = BUNDLE_PATH_NAME .format (platform = PLATFORM_NAMES [platform ])
0 commit comments