14
14
import zipfile
15
15
16
16
import requests
17
+ import semver
18
+
17
19
18
20
def get_git_command (command ):
21
+ """Execute and return the result of a git command without error."""
19
22
path = os .getcwd ()
20
23
procs = subprocess .run (
21
24
command ,
22
25
stdout = subprocess .PIPE ,
23
26
stderr = subprocess .PIPE ,
27
+ check = False ,
24
28
cwd = path ,
25
29
)
26
30
if procs .returncode != 0 :
27
31
return None
28
32
return procs .stdout .decode ("utf8" ).strip ()
29
33
34
+
30
35
def get_current_version ():
36
+ """Get current version string."""
31
37
return get_git_command ("git describe --tags --exact-match" .split ())
32
38
39
+
33
40
def 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 ):
36
43
year = int (tag [2 :4 ]) - 20
37
44
month = int (tag [4 :6 ])
38
45
day = int (tag [6 :8 ])
39
46
return f"{ year } .{ month } .{ day } "
40
- else :
41
- return tag
47
+ return tag
48
+
42
49
43
50
# the date tag for the generated files and stuff
44
51
# TODO: retrieve the version number from git or something
@@ -101,10 +108,15 @@ def date_to_version(tag):
101
108
102
109
103
110
def 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
+ )
108
120
cdate = re .split (r"[~-]" , ctag )[0 ]
109
121
if "." in cdate :
110
122
ver = cdate
@@ -116,13 +128,13 @@ def file_version_tag(path):
116
128
117
129
118
130
def fmt (path , platform = "py" ):
119
- """shortcut for the py directory"""
131
+ """Shortcut for the py directory. """
120
132
return path .format (platform = PLATFORM_NAMES [platform ])
121
133
122
134
123
135
# find in python
124
136
def list_all_files (path ):
125
- """clean list of all files in sub folders"""
137
+ """Clean list of all files in sub folders. """
126
138
pwd = os .getcwd ()
127
139
os .chdir (path )
128
140
liste = [
@@ -135,7 +147,7 @@ def list_all_files(path):
135
147
136
148
137
149
def init_directories ():
138
- """erase and create build directories"""
150
+ """Erase and create build directories. """
139
151
# create build directories
140
152
os .makedirs (BUILD_DIR , exist_ok = True )
141
153
os .makedirs (BUILD_DEPS , exist_ok = True )
@@ -152,24 +164,46 @@ def init_directories():
152
164
os .unlink (zip_file )
153
165
154
166
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
+
155
184
def make_bundle_files ():
156
- """create the .py bundle directory"""
185
+ """Create the .py bundle directory. """
157
186
# copy all the layouts and keycodes
158
187
shutil .copytree (MODULES_DIR , fmt (BUNDLE_LIB_DIR ))
159
188
160
189
# 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 )
173
207
174
208
# list of the modules
175
209
all_modules = [
@@ -200,7 +234,7 @@ def make_bundle_files():
200
234
# add the dependency to keyboard_layout
201
235
if module .startswith ("keyboard_layout_" ):
202
236
json_data [module ]["dependencies" ].append ("keyboard_layout" )
203
- with open (target ,"a" ) as fp :
237
+ with open (target , "a" ) as fp :
204
238
fp .write ("\r \n keyboard_layout\r \n " )
205
239
206
240
# create the json file
@@ -209,7 +243,7 @@ def make_bundle_files():
209
243
210
244
211
245
def 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. """
213
247
# copy for the zips
214
248
shutil .copy (BUNDLE_JSON , fmt (BUNDLE_ZIP_JSON ))
215
249
@@ -238,7 +272,7 @@ def make_the_mpy_bundles():
238
272
239
273
240
274
def do_the_zips ():
241
- """finally create the zip files for release"""
275
+ """Finally create the zip files for release. """
242
276
# now do the zips
243
277
for platform in ["py" ] + PLATFORMS :
244
278
in_path = BUNDLE_PATH_NAME .format (platform = PLATFORM_NAMES [platform ])
0 commit comments