Skip to content

Commit

Permalink
Merge pull request #3 from tannewt/semver_versions
Browse files Browse the repository at this point in the history
Add support for replacing placeholder version string with git tag
  • Loading branch information
dhalbert authored Dec 20, 2017
2 parents 073de8b + 6dac141 commit 88731d5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 34 deletions.
86 changes: 62 additions & 24 deletions circuitpython_build_tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
11 changes: 2 additions & 9 deletions circuitpython_build_tools/scripts/build_bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Click
semver
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 88731d5

Please sign in to comment.