Add support for replacing placeholder version string with git tag
based versions that are semver compatible. If a prerelease identifier is already present we'll bump ahead by adding `.plus.` and the number of subsequent commits. If its not, we'll bump the patch digit and call it `alpha.0` and add the remaining `.plus.` bit. This also changes the file names of the mpy zips to include "-mpy-" which should help space out two potentially confusing version strings (that of circuitpython and that of the library.)
This commit is contained in:
parent
073de8bdb6
commit
1f7cc3e854
3 changed files with 64 additions and 33 deletions
|
|
@ -24,12 +24,33 @@
|
|||
|
||||
import os
|
||||
import os.path
|
||||
import semver
|
||||
import shutil
|
||||
import sys
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
IGNORE_PY = ["setup.py", "conf.py", "__init__.py"]
|
||||
|
||||
def version_string(path=None, *, valid_semver=False):
|
||||
version = None
|
||||
tag = subprocess.run('git describe --tags --exact-match', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=path)
|
||||
if tag.returncode == 0:
|
||||
version = tag.stdout.strip().decode("utf-8", "strict")
|
||||
else:
|
||||
describe = subprocess.run("git describe --tags", shell=True, stdout=subprocess.PIPE, cwd=path)
|
||||
tag, additional_commits, commitish = describe.stdout.strip().decode("utf-8", "strict").rsplit("-", maxsplit=2)
|
||||
commitish = commitish[1:]
|
||||
if valid_semver:
|
||||
version_info = semver.parse_version_info(tag)
|
||||
if not version_info.prerelease:
|
||||
version = semver.bump_patch(tag) + "-alpha.0.plus." + additional_commits + "+" + commitish
|
||||
else:
|
||||
version = tag + ".plus." + additional_commits + "+" + commitish
|
||||
else:
|
||||
version = commitish
|
||||
return version
|
||||
|
||||
def mpy_cross(mpy_cross_filename, circuitpython_tag, quiet=False):
|
||||
if os.path.isfile(mpy_cross_filename):
|
||||
return
|
||||
|
|
@ -91,34 +112,51 @@ def library(library_path, output_directory, mpy_cross=None):
|
|||
if mpy_cross:
|
||||
new_extension = ".mpy"
|
||||
|
||||
library_version = version_string(library_path, valid_semver=True)
|
||||
|
||||
for filename in py_files:
|
||||
full_path = os.path.join(library_path, filename)
|
||||
output_file = os.path.join(output_directory,
|
||||
filename.replace(".py", new_extension))
|
||||
if mpy_cross:
|
||||
with tempfile.NamedTemporaryFile() as temp_file:
|
||||
with open(full_path, "rb") as original_file:
|
||||
for line in original_file:
|
||||
line = line.decode("utf-8").strip("\n")
|
||||
if line.startswith("__version__"):
|
||||
line = line.replace("0.0.0-auto.0", library_version)
|
||||
temp_file.write(line.encode("utf-8") + b"\r\n")
|
||||
temp_file.flush()
|
||||
|
||||
mpy_success = subprocess.call([mpy_cross,
|
||||
"-o", output_file,
|
||||
"-s", filename,
|
||||
full_path])
|
||||
if mpy_success != 0:
|
||||
raise RuntimeError("mpy-cross failed on", full_path)
|
||||
else:
|
||||
shutil.copyfile(full_path, output_file)
|
||||
if mpy_cross:
|
||||
mpy_success = subprocess.call([mpy_cross,
|
||||
"-o", output_file,
|
||||
"-s", filename,
|
||||
temp_file.name])
|
||||
if mpy_success != 0:
|
||||
raise RuntimeError("mpy-cross failed on", full_path)
|
||||
else:
|
||||
shutil.copyfile(temp_file.name, output_file)
|
||||
|
||||
for filename in package_files:
|
||||
full_path = os.path.join(library_path, filename)
|
||||
if (not mpy_cross or
|
||||
os.stat(full_path).st_size == 0 or
|
||||
filename.endswith("__init__.py")):
|
||||
output_file = os.path.join(output_directory, filename)
|
||||
shutil.copyfile(full_path, output_file)
|
||||
else:
|
||||
output_file = os.path.join(output_directory,
|
||||
filename.replace(".py", new_extension))
|
||||
mpy_success = subprocess.call([mpy_cross,
|
||||
"-o", output_file,
|
||||
"-s", filename,
|
||||
full_path])
|
||||
if mpy_success != 0:
|
||||
raise RuntimeError("mpy-cross failed on", full_path)
|
||||
with tempfile.NamedTemporaryFile() as temp_file:
|
||||
with open(full_path) as original_file:
|
||||
for line in original_file:
|
||||
if line.startswith("__version__"):
|
||||
line = line.replace("0.0.0-auto.0", library_version)
|
||||
temp_file.write(line + "\r\n")
|
||||
temp_file.flush()
|
||||
if (not mpy_cross or
|
||||
os.stat(full_path).st_size == 0 or
|
||||
filename.endswith("__init__.py")):
|
||||
output_file = os.path.join(output_directory, filename)
|
||||
shutil.copyfile(temp_file.name, output_file)
|
||||
else:
|
||||
output_file = os.path.join(output_directory,
|
||||
filename.replace(".py", new_extension))
|
||||
mpy_success = subprocess.call([mpy_cross,
|
||||
"-o", output_file,
|
||||
"-s", filename,
|
||||
temp_file.name])
|
||||
if mpy_success != 0:
|
||||
raise RuntimeError("mpy-cross failed on", full_path)
|
||||
|
|
|
|||
|
|
@ -118,14 +118,7 @@ def _find_libraries(current_path, depth):
|
|||
def build_bundles(filename_prefix, output_directory, library_location, library_depth):
|
||||
os.makedirs(output_directory, exist_ok=True)
|
||||
|
||||
bundle_version = None
|
||||
tag = subprocess.run('git describe --tags --exact-match', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
if tag.returncode == 0:
|
||||
bundle_version = tag
|
||||
else:
|
||||
commitish = subprocess.run("git log --pretty=format:'%h' -n 1", shell=True, stdout=subprocess.PIPE)
|
||||
bundle_version = commitish
|
||||
bundle_version = bundle_version.stdout.strip().decode("utf-8", "strict")
|
||||
bundle_version = build.version_string()
|
||||
|
||||
libs = _find_libraries(os.path.abspath(library_location), library_depth)
|
||||
print(libs)
|
||||
|
|
@ -156,7 +149,7 @@ def build_bundles(filename_prefix, output_directory, library_location, library_d
|
|||
mpy_cross = "build_deps/mpy-cross-" + version["name"]
|
||||
build.mpy_cross(mpy_cross, version["tag"])
|
||||
zip_filename = os.path.join(output_directory,
|
||||
filename_prefix + '-{TAG}-{VERSION}.zip'.format(
|
||||
filename_prefix + '-{TAG}-mpy-{VERSION}.zip'.format(
|
||||
TAG=version["name"],
|
||||
VERSION=bundle_version))
|
||||
build_bundle(libs, bundle_version, zip_filename, mpy_cross=mpy_cross,
|
||||
|
|
|
|||
2
setup.py
2
setup.py
|
|
@ -14,7 +14,7 @@ setup(name='circuitpython-build-tools',
|
|||
package_data={'circuitpython_build_tools': ['data/mpy-cross-*']},
|
||||
zip_safe=False,
|
||||
python_requires='>=3.4',
|
||||
install_requires=['Click'],
|
||||
install_requires=['Click', 'semver'],
|
||||
entry_points='''
|
||||
[console_scripts]
|
||||
circuitpython-build-bundles=circuitpython_build_tools.scripts.build_bundles:build_bundles
|
||||
|
|
|
|||
Loading…
Reference in a new issue