From 1f7cc3e854533f0cc093344b3e61ab434c3839c9 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 20 Dec 2017 11:52:10 -0800 Subject: [PATCH 1/2] 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.) --- circuitpython_build_tools/build.py | 86 +++++++++++++------ .../scripts/build_bundles.py | 11 +-- setup.py | 2 +- 3 files changed, 65 insertions(+), 34 deletions(-) diff --git a/circuitpython_build_tools/build.py b/circuitpython_build_tools/build.py index 7562297..c6cb5e9 100644 --- a/circuitpython_build_tools/build.py +++ b/circuitpython_build_tools/build.py @@ -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: - - 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) + 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() + + 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) diff --git a/circuitpython_build_tools/scripts/build_bundles.py b/circuitpython_build_tools/scripts/build_bundles.py index f48b35b..4fdaa5d 100755 --- a/circuitpython_build_tools/scripts/build_bundles.py +++ b/circuitpython_build_tools/scripts/build_bundles.py @@ -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, diff --git a/setup.py b/setup.py index 0ad5709..873ca16 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ 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 From 6dac14141a9b59a15ddad29d1870f3730aed4471 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 20 Dec 2017 13:43:53 -0800 Subject: [PATCH 2/2] Add requirements. --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..27d31cb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Click +semver