1 #!/usr/bin/env python3
2
3 "This tool is intended to be used from meson"
4
5 import os, sys, shutil, re
6
7 if len (sys.argv) < 4:
8 sys.exit(__doc__)
9
10 version = sys.argv[1]
11 major, minor, micro = version.split (".")
12
13 OUTPUT = sys.argv[2]
14 INPUT = sys.argv[3]
15 CURRENT_SOURCE_DIR = os.path.dirname(INPUT)
16
17 try:
18 with open (OUTPUT, "r", encoding='utf-8') as old_output:
19 for line in old_output:
20 old_version = re.match (r"#define HB_VERSION_STRING \"(\d.\d.\d)\"", line)
21 if old_version and old_version[1] == version:
22 sys.exit ()
23 except IOError:
24 pass
25
26 with open (INPUT, "r", encoding='utf-8') as template:
27 with open (OUTPUT, "wb") as output:
28 output.write (template.read ()
29 .replace ("@HB_VERSION_MAJOR@", major)
30 .replace ("@HB_VERSION_MINOR@", minor)
31 .replace ("@HB_VERSION_MICRO@", micro)
32 .replace ("@HB_VERSION@", version)
33 .encode ())
34
35 # copy it also to the source tree, but only if it has changed
36 baseline_filename = os.path.join (CURRENT_SOURCE_DIR, os.path.basename (OUTPUT))
37 with open(baseline_filename, "rb") as baseline:
38 with open(OUTPUT, "rb") as generated:
39 if baseline.read() != generated.read():
40 shutil.copyfile (OUTPUT, baseline_filename)