Debian Codegraph @

texk/gregorio/gregorio-src/VersionManager.py

raw
1 #! /usr/bin/env python3
2
3 """
4 A script that manages the VERSION of gregorio.
5
6 See VersionUpdate.py -h for help
7
8 Copyright (C) 2015-2025 The Gregorio Project (see CONTRIBUTORS.md)
9
10 This file is part of Gregorio.
11
12 Gregorio is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 Gregorio is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with Gregorio. If not, see <http://www.gnu.org/licenses/>.
24 """
25
26
27 import sys
28 import re
29 import argparse
30 import subprocess
31 import time
32 import os
33 import locale
34 import linecache
35 from datetime import date
36
37 locale.setlocale(locale.LC_TIME, 'C')
38
39 os.chdir(sys.path[0])
40
41 VERSION_FILE = '.gregorio-version'
42 CURRENTYEAR = str(date.today().year)
43 GREGORIO_FILES = ["configure.ac",
44 "ctan-o-mat.config",
45 "windows/gregorio-resources.rc",
46 "macosx/Gregorio.pkgproj",
47 "macosx/douninstall.sh",
48 "windows/gregorio.iss",
49 "doc/GregorioRef.tex",
50 "tex/gregoriotex.sty",
51 "tex/gregoriotex.lua",
52 "tex/gregoriotex.tex",
53 "tex/gregoriotex-chars.tex",
54 "tex/gregoriotex-signs.tex",
55 "tex/gregoriotex-signs.lua",
56 "tex/gregoriotex-spaces.tex",
57 "tex/gregoriotex-symbols.tex",
58 "tex/gregoriotex-symbols.lua",
59 "tex/gregoriotex-syllable.tex",
60 "tex/gregoriotex-main.tex",
61 "tex/gregoriotex-nabc.tex",
62 "tex/gregoriotex-nabc.lua",
63 "tex/gregoriosyms.sty",
64 "tex/gregoriotex-common.tex",
65 "tex/gregoriotex-gsp-default.tex",
66 "fonts/squarize.py",
67 ]
68 COPYRIGHT_FILES = ["install-gtex.sh",
69 "build-ctan.sh",
70 "ctan_upload.sh",
71 "tex/gregoriotex-signs.tex",
72 "tex/gregorio-vowels.dat",
73 "tex/gregoriotex-gsp-default.tex",
74 "tex/gregoriotex-nabc.lua",
75 "tex/gregoriotex-symbols.lua",
76 "tex/gregoriotex-chars.tex",
77 "tex/gregoriotex-main.tex",
78 "tex/gregoriotex-spaces.tex",
79 "tex/Makefile.am",
80 "tex/gregoriotex-common.tex",
81 "tex/gregoriotex-syllable.tex",
82 "tex/gregoriotex.lua",
83 "tex/gregoriotex.sty",
84 "tex/gregoriosyms.sty",
85 "tex/gregoriotex-nabc.tex",
86 "tex/gregoriotex.tex",
87 "tex/gregoriotex-signs.lua",
88 "tex/gregoriotex-symbols.tex",
89 "contrib/TeXShop/Makefile.am",
90 "contrib/vim/Makefile.am",
91 "contrib/vim/ftdetect/Makefile.am",
92 "contrib/vim/syntax/Makefile.am",
93 "contrib/900_gregorio.xml",
94 "contrib/Makefile.am",
95 "contrib/gabc.lang",
96 "contrib/gprocess",
97 "contrib/checkSyllabation.py",
98 "configure.ac",
99 "examples/Makefile.am",
100 "Makefile.am",
101 "install.sh",
102 "debian/copyright",
103 "debian/manpage.xml",
104 "doc/Command_Index_User.tex",
105 "doc/Makefile.am",
106 "doc/GregorioRef.tex",
107 "doc/Command_Index_gregorio.tex",
108 "doc/Command_Index_internal.tex",
109 "doc/GregorioNabcRef.tex",
110 "doc/Gabc.tex",
111 "doc/GregorioRef.lua",
112 "doc/Appendix_Font_Tables.tex",
113 "doc/gsp-sample.tex",
114 "VersionManager.py",
115 "coverage.sh",
116 "COPYING.md",
117 "src/characters.h",
118 "src/plugins.h",
119 "src/gregoriotex/gregoriotex-write.c",
120 "src/gregoriotex/gregoriotex.h",
121 "src/gregoriotex/gregoriotex-position.c",
122 "src/unicode.c",
123 "src/unicode.h",
124 "src/messages.h",
125 "src/support.c",
126 "src/sha1.h",
127 "src/messages.c",
128 "src/support.h",
129 "src/sha1.c",
130 "src/struct.h",
131 "src/bool.h",
132 "src/struct_iter.h",
133 "src/Makefile.am",
134 "src/dump/dump.c",
135 "src/encode_utf8strings.c",
136 "src/enum_generator.h",
137 "src/gabc/gabc-score-determination.c",
138 "src/gabc/gabc-elements-determination.c",
139 "src/gabc/gabc-write.c",
140 "src/gabc/gabc-notes-determination.l",
141 "src/gabc/gabc.h",
142 "src/gabc/gabc-score-determination.l",
143 "src/gabc/gabc-score-determination.y",
144 "src/gabc/gabc-score-determination.h",
145 "src/gabc/gabc-glyphs-determination.c",
146 "src/utf8strings.h.in",
147 "src/config.h",
148 "src/characters.c",
149 "src/vowel/vowel-rules.y",
150 "src/vowel/vowel.h",
151 "src/vowel/vowel-rules.h",
152 "src/vowel/vowel-rules.l",
153 "src/vowel/vowel.c",
154 "src/gregorio-utils.c",
155 "src/struct.c",
156 "fonts/gregorio-base.sfd",
157 "fonts/granapadano-base.sfd",
158 "fonts/squarize.py",
159 "fonts/convertsfdtottf.py",
160 "fonts/Makefile.am",
161 "fonts/simplify.py",
162 "fonts/stemsschemas.py",
163 "fonts/greciliae-base.sfd",
164 "fonts/gregall.sfd",
165 "fonts/grelaon.sfd",
166 "fonts/gresgmodern.sfd",
167 "fonts/install_supp_fonts.lua",
168 "windows/gregorio.iss",
169 "windows/install.lua",
170 "windows/uninstall.lua",
171 "macosx/Makefile.am",
172 ]
173
174 def strtobool(val):
175 """Convert a string representation of truth to true (1) or false (0).
176 True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
177 are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
178 'val' is anything else.
179 """
180 val = val.lower()
181 if val in ('y', 'yes', 't', 'true', 'on', '1'):
182 return 1
183 if val in ('n', 'no', 'f', 'false', 'off', '0'):
184 return 0
185 raise ValueError(f'invalid truth value: {val}')
186
187 def get_parser():
188 "Return command line parser"
189 parser = argparse.ArgumentParser(
190 description='A script to manage the VERSION of gregorio.')
191 parser.add_argument('-ni', '--not-interactive',
192 help='Do not ask confirmation',
193 action='store_true', default=False)
194 parser.add_argument('-c', '--get-current',
195 help='Prints the current gregorio version',
196 action='store_true', default=False)
197 parser.add_argument('-d', '--get-debian-stable',
198 help='Prints the version for Debian stable package',
199 action='store_true', default=False)
200 parser.add_argument('-dg', '--get-debian-git',
201 help='Prints the version for Debian git package',
202 action='store_true', default=False)
203
204 modify = parser.add_mutually_exclusive_group()
205 modify.add_argument('-b', '--beta',
206 help='Increment -betax+1',
207 action='store_true', default=False,
208 dest='beta')
209 modify.add_argument('--manual=',
210 help='Manually set the version.',
211 action='store',
212 dest='manual_version')
213 modify.add_argument('-m', '--major',
214 help='Increment the major version: x+1.0.0-beta1',
215 action='store_true', default=False,
216 dest='major')
217 modify.add_argument('-e', '--enhancement',
218 help='Increment the minor version: x.y+1.0-beta1',
219 action='store_true', default=False,
220 dest='minor')
221 modify.add_argument('-p', '--patch',
222 help='Increment the patch version: x.y.z+1',
223 action='store_true', default=False,
224 dest='patch')
225 modify.add_argument('-rc', '--release-candidate',
226 help='Change version to a -rc1, or increment -rcx+1',
227 action='store_true', default=False,
228 dest='release_candidate')
229 modify.add_argument('-r', '--release',
230 help='Make a release. Removes -rcx',
231 action='store_true', default=False,
232 dest='release')
233 return parser
234
235 class Version():
236 "Class for version manipulation."
237
238 def __init__(self, versionfile):
239 self.versionfile = versionfile
240 self.version = linecache.getline(self.versionfile, 1).strip('\n')
241 self.filename_version = self.filename_version_from_version(self.version)
242 self.short_tag = None
243 self.date = None
244 self.binary_version = self.binary_version_from_version(self.version)
245
246 def filename_version_from_version(self, version):
247 "Return filename-compatible version"
248 return version.replace('.', '_')
249
250 def binary_version_from_version(self, version):
251 "Return binary version number for Windows FILEVERSION"
252 binary = version.replace('.', ',')
253 if '-' in binary:
254 binary = binary.replace('-beta', ',1')
255 binary = binary.replace('-rc', ',2')
256 else:
257 binary += ',30'
258 return binary
259
260 def fetch_version(self):
261 "Prints version"
262 print(self.version)
263 sys.exit(0)
264
265 def fetch_version_debian_stable(self):
266 "Prints version for Debian stable package and exits"
267 print(self.version.replace('-', '~'))
268 sys.exit(0)
269
270 def fetch_version_debian_git(self):
271 "Prints version for Debian git package and exits"
272 self.short_tag = subprocess.check_output(
273 ['git', 'rev-parse', '--short', 'HEAD'])
274 self.short_tag = self.short_tag.strip('\n')
275 self.date = time.strftime("%Y%m%d%H%M%S")
276 print(f"{self.version.replace('-', '~')}+git{self.date}+{self.short_tag}")
277 sys.exit(0)
278
279 def update_version(self, newversion):
280 "Update self.version and .gregorio-version with the new version."
281 self.version = newversion
282 self.filename_version = self.filename_version_from_version(newversion)
283 self.binary_version = self.binary_version_from_version(newversion)
284 print(f'Updating {self.versionfile} with the new version: {self.version}\n')
285 with open(self.versionfile, 'w', encoding='utf-8') as verfile:
286 verfile.write(f'{self.version}\n{CURRENTYEAR}')
287 verfile.write('\n\n*** Do not modify this file. ***\n')
288 verfile.write('Use VersionManager.py to change the version.\n')
289
290 def replace_version(version_obj):
291 "Change version in file according to heuristics."
292 newver = version_obj.version
293 newver_filename = version_obj.filename_version
294 newbinver = version_obj.binary_version
295 today = date.today()
296 print(f'Updating source files to version {newver}\n')
297 for myfile in GREGORIO_FILES:
298 result = []
299 following_line_filename = False
300 with open(myfile, 'r', encoding='utf-8') as infile:
301 for line in infile:
302 if 'AC_INIT([' in line:
303 result.append(re.sub(r'(\d+\.\d+\.\d+(?:[-+~]\w+)*)', newver, line, 1))
304 elif 'AppVersion' in line:
305 result.append(re.sub(r'(\d+\.\d+\.\d+(?:[-+~]\w+)*)', newver, line, 1))
306 elif 'FILENAME_VERSION' in line:
307 result.append(re.sub(r'(\d+\_\d+\_\d+(?:[-+~]\w+)*)', newver_filename, line, 1))
308 elif 'FileVersion' in line:
309 result.append(re.sub(r'(\d+\.\d+\.\d+(?:[-+~]\w+)*)', newver, line, 1))
310 elif 'ProductVersion' in line:
311 result.append(re.sub(r'(\d+\.\d+\.\d+(?:[-+~]\w+)*)', newver, line, 1))
312 elif 'GREGORIO_VERSION' in line:
313 result.append(re.sub(r'(\d+\.\d+\.\d+(?:[-+~]\w+)*)', newver, line, 1))
314 elif 'GREGORIO_DATE_LTX' in line:
315 result.append(re.sub(r'(\d+\/\d+/\d+)', today.strftime("%Y/%m/%d"), line, 1))
316 elif 'PARSE_VERSION_DATE_LTX' in line:
317 newline = re.sub(r'(\d+\.\d+\.\d+(?:[-+~]\w+)*)', newver, line, 1)
318 result.append(re.sub(r'(\d+\/\d+/\d+)', today.strftime("%Y/%m/%d"), newline, 1))
319 elif 'PARSE_VERSION_DATE' in line:
320 newline = re.sub(r'(\d+\.\d+\.\d+(?:[-+~]\w+)*)', newver, line, 1)
321 result.append(re.sub(r'(\d{1,2} [A-Z][a-z]+ \d{4})',
322 today.strftime("%-d %B %Y"), newline, 1))
323 elif 'FILEVERSION' in line:
324 result.append(re.sub(r'\d+,\d+,\d+,\d+', newbinver, line, 1))
325 elif 'PRODUCTVERSION' in line:
326 result.append(re.sub(r'\d+,\d+,\d+,\d+', newbinver, line, 1))
327 elif 'PARSE_VERSION_FILE_NEXTLINE' in line:
328 result.append(line)
329 following_line_filename = True
330 elif following_line_filename:
331 result.append(re.sub(r'(\d+\_\d+\_\d+(?:[-+~]\w+)*)', newver_filename, line, 1))
332 following_line_filename = False
333 else:
334 result.append(line)
335 with open(myfile, 'w', encoding='utf-8') as outfile:
336 outfile.write(''.join(result))
337 sys.exit(0)
338
339 def update_changelog(newver, upgradetype):
340 "Insert the version number into CHANGELOG"
341 today = date.today()
342 with open('CHANGELOG.md', 'r', encoding='utf-8') as infile:
343 result = []
344 develop = False
345 for line in infile:
346 if upgradetype == "patch":
347 if '[Unreleased][develop]' in line:
348 print("Found an unreleased develop section.")
349 print("Patch releases should be based on ctan branch.")
350 sys.exit(1)
351 if '[Unreleased][CTAN]' in line:
352 result.append(line)
353 result.append('\n')
354 result.append('\n')
355 newline = '## [' + newver + '] - ' + today.strftime("%Y-%m-%d") + '\n'
356 result.append(newline)
357 else:
358 result.append(line)
359 else:
360 if '[Unreleased][develop]' in line:
361 develop = True
362 result.append(line)
363 result.append('\n')
364 result.append('\n')
365 result.append('## [Unreleased][CTAN]\n')
366 result.append('\n')
367 result.append('\n')
368 newline = '## [' + newver + '] - ' + today.strftime("%Y-%m-%d") + '\n'
369 result.append(newline)
370 elif '[Unreleased][CTAN]' in line and develop:
371 continue
372 else:
373 result.append(line)
374 if not develop and upgradetype != "patch":
375 print("I didn't find a unreleased develop section.")
376 print("Non-patch releases should be based on develop branch.")
377 sys.exit(1)
378 with open('CHANGELOG.md', 'w', encoding='utf-8') as outfile:
379 outfile.write(''.join(result))
380
381 def confirm_replace(oldver, newver):
382 "Query the user to confirm action"
383 query = f'Update version from {oldver} --> {newver} [y/n]?'
384 print(query)
385 consent = None
386 while True:
387 try:
388 consent = strtobool(input().lower())
389 break
390 except ValueError:
391 print('Answer with y or n.')
392 if not consent:
393 print('Aborting.')
394 sys.exit(1)
395
396 def release_candidate(version_obj, not_interactive):
397 "Changes x.y.z-beta to x.y.z-rc1 OR increments x.y.z-rcx+1"
398 oldversion = version_obj.version
399 if '-rc' in oldversion:
400 newversion = re.sub(r'\d+$', lambda x: str(int(x.group()) +1),
401 oldversion)
402 elif '-' in oldversion:
403 newversion = re.sub(r'-.*', '-rc1', oldversion)
404 if (not not_interactive):
405 confirm_replace(oldversion, newversion)
406 update_changelog(newversion,"releasecandidate")
407 version_obj.update_version(newversion)
408 replace_version(version_obj)
409
410 def beta(version_obj, not_interactive):
411 "Increments x.y.z-betax+1"
412 oldversion = version_obj.version
413 if '-beta' in oldversion:
414 newversion = re.sub(r'\d+$', lambda x: str(int(x.group()) +1),
415 oldversion)
416 else:
417 print('Version number not in beta stage')
418 print('Use --major (-m) or --enhancement (-e) to start a new beta')
419 sys.exit(1)
420 if (not not_interactive):
421 confirm_replace(oldversion, newversion)
422 update_changelog(newversion,"beta")
423 version_obj.update_version(newversion)
424 replace_version(version_obj)
425
426 def bump_major(version_obj, not_interactive):
427 "Changed the major version number: x.y.z --> x+1.0.0-beta1"
428 oldversion = version_obj.version
429 nums = re.search(r'(\d+)(\.\d+)(\.\d+)', oldversion)
430 newversion = str(int(nums.group(1)) +1) + '.0.0-beta1'
431 if (not not_interactive):
432 confirm_replace(oldversion, newversion)
433 update_changelog(newversion,"major")
434 version_obj.update_version(newversion)
435 replace_version(version_obj)
436
437 def bump_minor(version_obj, not_interactive):
438 "Changed the minor version number: x.y.z --> x.y+1.0-beta1"
439 oldversion = version_obj.version
440 nums = re.search(r'(\d+\.)(\d+)(\.\d+)', oldversion)
441 newversion = nums.group(1) + str(int(nums.group(2)) +1) + '.0-beta1'
442 if (not not_interactive):
443 confirm_replace(oldversion, newversion)
444 update_changelog(newversion,"minor")
445 version_obj.update_version(newversion)
446 replace_version(version_obj)
447
448 def bump_patch(version_obj, not_interactive):
449 "Changed the patch version number: x.y.z --> x.y.z+1"
450 oldversion = version_obj.version
451 nums = re.search(r'(\d+\.\d+\.)(\d+)', oldversion)
452 newversion = nums.group(1) + str(int(nums.group(2)) +1)
453 if (not not_interactive):
454 confirm_replace(oldversion, newversion)
455 update_changelog(newversion,"patch")
456 version_obj.update_version(newversion)
457 replace_version(version_obj)
458
459 def set_manual_version(version_obj, user_version, not_interactive):
460 "Changed the version number to a user supplied value"
461 oldversion = version_obj.version
462 if not re.match(r'(\d+\.\d+\.\d+(?:[-+~]\w+)*)$', user_version):
463 print('Bad version string. Use this style: x.y.z or x.y.z-betax')
464 sys.exit(1)
465 newversion = user_version
466 if (not not_interactive):
467 confirm_replace(oldversion, newversion)
468 update_changelog(newversion,"manual")
469 version_obj.update_version(newversion)
470 replace_version(version_obj)
471
472 def do_release(version_obj, not_interactive):
473 "Strips extra characters from the version leaving x.y.z"
474 oldversion = version_obj.version
475 newversion = re.sub(r'([\d.]+)-?.*', r'\1', oldversion)
476 if (not not_interactive):
477 confirm_replace(oldversion, newversion)
478 update_changelog(newversion,"release")
479 version_obj.update_version(newversion)
480 replace_version(version_obj)
481
482 def copyright_year():
483 "Check and update copyright year as needed"
484 fileyear = linecache.getline(VERSION_FILE, 2).strip()
485 def year_range(matchobj):
486 "Check and add a year range to the copyright"
487 if matchobj.group(1) is not None:
488 return re.sub(fileyear, CURRENTYEAR, matchobj.group(0))
489 return re.sub(fileyear, fileyear+'-'+CURRENTYEAR, matchobj.group(0))
490
491 if int(fileyear) != int(CURRENTYEAR):
492 print('Updating copyright year.')
493 for myfile in COPYRIGHT_FILES:
494 result = []
495 with open(myfile, 'r', encoding='utf-8') as infile:
496 for line in infile:
497 if re.search(r'[C|c]opyright.*Gregorio Project', line):
498 result.append(re.sub(r'(\d{4}-)?(\d{4})', year_range, line))
499 elif re.search(r'[C|c]opyright.*Elie Roux', line):
500 result.append(re.sub(r'(\d{4}-)?(\d{4})', year_range, line))
501 elif re.search(r'[C|c]opyright.*Richard Chonak', line):
502 result.append(re.sub(r'(\d{4}-)?(\d{4})', year_range, line))
503 elif re.search(r'[C|c]opyright.*Jakub Jelinek', line):
504 result.append(re.sub(r'(\d{4}-)?(\d{4})', year_range, line))
505 else:
506 result.append(line)
507 with open(myfile, 'w', encoding='utf-8') as outfile:
508 outfile.write(''.join(result))
509
510 def main():
511 "Main function"
512 parser = get_parser()
513 if len(sys.argv) == 1:
514 parser.print_help()
515 sys.exit(1)
516 args = parser.parse_args()
517 gregorio_version = Version(VERSION_FILE)
518 not_interactive = False
519 if args.not_interactive:
520 not_interactive = True
521 if args.get_current:
522 gregorio_version.fetch_version()
523 elif args.get_debian_stable:
524 gregorio_version.fetch_version_debian_stable()
525 elif args.get_debian_git:
526 gregorio_version.fetch_version_debian_git()
527 copyright_year()
528 if args.beta:
529 beta(gregorio_version, not_interactive)
530 elif args.major:
531 bump_major(gregorio_version, not_interactive)
532 elif args.minor:
533 bump_minor(gregorio_version, not_interactive)
534 elif args.patch:
535 bump_patch(gregorio_version, not_interactive)
536 elif args.release_candidate:
537 release_candidate(gregorio_version, not_interactive)
538 elif args.release:
539 do_release(gregorio_version, not_interactive)
540 elif args.manual_version:
541 set_manual_version(gregorio_version, args.manual_version, not_interactive)
542
543 if __name__ == "__main__":
544 main()