Skip to content

Commit 88731d5

Browse files
authored
Merge pull request #3 from tannewt/semver_versions
Add support for replacing placeholder version string with git tag
2 parents 073de8b + 6dac141 commit 88731d5

File tree

4 files changed

+67
-34
lines changed

4 files changed

+67
-34
lines changed

circuitpython_build_tools/build.py

+62-24
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,33 @@
2424

2525
import os
2626
import os.path
27+
import semver
2728
import shutil
2829
import sys
2930
import subprocess
31+
import tempfile
3032

3133
IGNORE_PY = ["setup.py", "conf.py", "__init__.py"]
3234

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+
3354
def mpy_cross(mpy_cross_filename, circuitpython_tag, quiet=False):
3455
if os.path.isfile(mpy_cross_filename):
3556
return
@@ -91,34 +112,51 @@ def library(library_path, output_directory, mpy_cross=None):
91112
if mpy_cross:
92113
new_extension = ".mpy"
93114

115+
library_version = version_string(library_path, valid_semver=True)
116+
94117
for filename in py_files:
95118
full_path = os.path.join(library_path, filename)
96119
output_file = os.path.join(output_directory,
97120
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)
108139

109140
for filename in package_files:
110141
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)

circuitpython_build_tools/scripts/build_bundles.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,7 @@ def _find_libraries(current_path, depth):
118118
def build_bundles(filename_prefix, output_directory, library_location, library_depth):
119119
os.makedirs(output_directory, exist_ok=True)
120120

121-
bundle_version = None
122-
tag = subprocess.run('git describe --tags --exact-match', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
123-
if tag.returncode == 0:
124-
bundle_version = tag
125-
else:
126-
commitish = subprocess.run("git log --pretty=format:'%h' -n 1", shell=True, stdout=subprocess.PIPE)
127-
bundle_version = commitish
128-
bundle_version = bundle_version.stdout.strip().decode("utf-8", "strict")
121+
bundle_version = build.version_string()
129122

130123
libs = _find_libraries(os.path.abspath(library_location), library_depth)
131124
print(libs)
@@ -156,7 +149,7 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
156149
mpy_cross = "build_deps/mpy-cross-" + version["name"]
157150
build.mpy_cross(mpy_cross, version["tag"])
158151
zip_filename = os.path.join(output_directory,
159-
filename_prefix + '-{TAG}-{VERSION}.zip'.format(
152+
filename_prefix + '-{TAG}-mpy-{VERSION}.zip'.format(
160153
TAG=version["name"],
161154
VERSION=bundle_version))
162155
build_bundle(libs, bundle_version, zip_filename, mpy_cross=mpy_cross,

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Click
2+
semver

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package_data={'circuitpython_build_tools': ['data/mpy-cross-*']},
1515
zip_safe=False,
1616
python_requires='>=3.4',
17-
install_requires=['Click'],
17+
install_requires=['Click', 'semver'],
1818
entry_points='''
1919
[console_scripts]
2020
circuitpython-build-bundles=circuitpython_build_tools.scripts.build_bundles:build_bundles

0 commit comments

Comments
 (0)