|
24 | 24 |
|
25 | 25 | import os
|
26 | 26 | import os.path
|
| 27 | +import semver |
27 | 28 | import shutil
|
28 | 29 | import sys
|
29 | 30 | import subprocess
|
| 31 | +import tempfile |
30 | 32 |
|
31 | 33 | IGNORE_PY = ["setup.py", "conf.py", "__init__.py"]
|
32 | 34 |
|
| 35 | +def version_string(path=None, *, valid_semver=False): |
| 36 | + version = None |
| 37 | + tag = subprocess.run('git describe --tags --exact-match', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=path) |
| 38 | + if tag.returncode == 0: |
| 39 | + version = tag.stdout.strip().decode("utf-8", "strict") |
| 40 | + else: |
| 41 | + describe = subprocess.run("git describe --tags", shell=True, stdout=subprocess.PIPE, cwd=path) |
| 42 | + tag, additional_commits, commitish = describe.stdout.strip().decode("utf-8", "strict").rsplit("-", maxsplit=2) |
| 43 | + commitish = commitish[1:] |
| 44 | + if valid_semver: |
| 45 | + version_info = semver.parse_version_info(tag) |
| 46 | + if not version_info.prerelease: |
| 47 | + version = semver.bump_patch(tag) + "-alpha.0.plus." + additional_commits + "+" + commitish |
| 48 | + else: |
| 49 | + version = tag + ".plus." + additional_commits + "+" + commitish |
| 50 | + else: |
| 51 | + version = commitish |
| 52 | + return version |
| 53 | + |
33 | 54 | def mpy_cross(mpy_cross_filename, circuitpython_tag, quiet=False):
|
34 | 55 | if os.path.isfile(mpy_cross_filename):
|
35 | 56 | return
|
@@ -91,34 +112,51 @@ def library(library_path, output_directory, mpy_cross=None):
|
91 | 112 | if mpy_cross:
|
92 | 113 | new_extension = ".mpy"
|
93 | 114 |
|
| 115 | + library_version = version_string(library_path, valid_semver=True) |
| 116 | + |
94 | 117 | for filename in py_files:
|
95 | 118 | full_path = os.path.join(library_path, filename)
|
96 | 119 | output_file = os.path.join(output_directory,
|
97 | 120 | filename.replace(".py", new_extension))
|
98 |
| - if mpy_cross: |
99 |
| - |
100 |
| - mpy_success = subprocess.call([mpy_cross, |
101 |
| - "-o", output_file, |
102 |
| - "-s", filename, |
103 |
| - full_path]) |
104 |
| - if mpy_success != 0: |
105 |
| - raise RuntimeError("mpy-cross failed on", full_path) |
106 |
| - else: |
107 |
| - shutil.copyfile(full_path, output_file) |
| 121 | + with tempfile.NamedTemporaryFile() as temp_file: |
| 122 | + with open(full_path, "rb") as original_file: |
| 123 | + for line in original_file: |
| 124 | + line = line.decode("utf-8").strip("\n") |
| 125 | + if line.startswith("__version__"): |
| 126 | + line = line.replace("0.0.0-auto.0", library_version) |
| 127 | + temp_file.write(line.encode("utf-8") + b"\r\n") |
| 128 | + temp_file.flush() |
| 129 | + |
| 130 | + if mpy_cross: |
| 131 | + mpy_success = subprocess.call([mpy_cross, |
| 132 | + "-o", output_file, |
| 133 | + "-s", filename, |
| 134 | + temp_file.name]) |
| 135 | + if mpy_success != 0: |
| 136 | + raise RuntimeError("mpy-cross failed on", full_path) |
| 137 | + else: |
| 138 | + shutil.copyfile(temp_file.name, output_file) |
108 | 139 |
|
109 | 140 | for filename in package_files:
|
110 | 141 | full_path = os.path.join(library_path, filename)
|
111 |
| - if (not mpy_cross or |
112 |
| - os.stat(full_path).st_size == 0 or |
113 |
| - filename.endswith("__init__.py")): |
114 |
| - output_file = os.path.join(output_directory, filename) |
115 |
| - shutil.copyfile(full_path, output_file) |
116 |
| - else: |
117 |
| - output_file = os.path.join(output_directory, |
118 |
| - filename.replace(".py", new_extension)) |
119 |
| - mpy_success = subprocess.call([mpy_cross, |
120 |
| - "-o", output_file, |
121 |
| - "-s", filename, |
122 |
| - full_path]) |
123 |
| - if mpy_success != 0: |
124 |
| - raise RuntimeError("mpy-cross failed on", full_path) |
| 142 | + with tempfile.NamedTemporaryFile() as temp_file: |
| 143 | + with open(full_path) as original_file: |
| 144 | + for line in original_file: |
| 145 | + if line.startswith("__version__"): |
| 146 | + line = line.replace("0.0.0-auto.0", library_version) |
| 147 | + temp_file.write(line + "\r\n") |
| 148 | + temp_file.flush() |
| 149 | + if (not mpy_cross or |
| 150 | + os.stat(full_path).st_size == 0 or |
| 151 | + filename.endswith("__init__.py")): |
| 152 | + output_file = os.path.join(output_directory, filename) |
| 153 | + shutil.copyfile(temp_file.name, output_file) |
| 154 | + else: |
| 155 | + output_file = os.path.join(output_directory, |
| 156 | + filename.replace(".py", new_extension)) |
| 157 | + mpy_success = subprocess.call([mpy_cross, |
| 158 | + "-o", output_file, |
| 159 | + "-s", filename, |
| 160 | + temp_file.name]) |
| 161 | + if mpy_success != 0: |
| 162 | + raise RuntimeError("mpy-cross failed on", full_path) |
0 commit comments