Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94e645d

Browse files
authored
Merge pull request adafruit#499 from dhalbert/2.x_frozen_module_version_info
Add git version info to frozen modules.
2 parents e44fbca + 31be207 commit 94e645d

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@
2929
[submodule "frozen/Adafruit_CircuitPython_BusDevice"]
3030
path = frozen/Adafruit_CircuitPython_BusDevice
3131
url = https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git
32+
[submodule "tools/python-semver"]
33+
path = tools/python-semver
34+
url = https://github.com/k-bx/python-semver.git

py/mkenv.mk

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ endif
7070
MAKE_FROZEN = $(TOP)/tools/make-frozen.py
7171
MPY_CROSS = $(TOP)/mpy-cross/mpy-cross
7272
MPY_TOOL = $(TOP)/tools/mpy-tool.py
73+
PREPROCESS_FROZEN_MODULES = PYTHONPATH=$(TOP)/tools/python-semver $(TOP)/tools/preprocess_frozen_modules.py
7374

7475
all:
7576
.PHONY: all

py/mkrules.mk

+3-4
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,13 @@ $(MPY_CROSS): $(TOP)/py/*.[ch] $(TOP)/mpy-cross/*.[ch] $(TOP)/windows/fmode.c
114114
$(Q)$(MAKE) -C $(TOP)/mpy-cross
115115

116116
# Copy all the modules and single python files to freeze to a common area, omitting top-level dirs (the repo names).
117-
# Remove any conf.py (sphinx config) and setup.py (module install info) files, which are not meant to be frozen.
118-
# Also remove the library examples directory, so it won't be included.
117+
# Do any preprocessing necessary: currently, this adds version information, removes examples, and
118+
# non-library .py files in the modules (setup.py and conf.py)
119119
# Then compile .mpy files from all the .py files, placing them in the same directories as the .py files.
120120
$(BUILD)/frozen_mpy: $(FROZEN_MPY_DIRS)
121121
$(ECHO) FREEZE $(FROZEN_MPY_DIRS)
122122
$(Q)$(MKDIR) -p $@
123-
$(Q)$(RSYNC) -rL --include="*/" --include='*.py' --exclude="*" $(addsuffix /*,$(FROZEN_MPY_DIRS)) $@
124-
$(Q)$(RM) -rf $@/conf.py $@/setup.py $@/examples
123+
$(Q)$(PREPROCESS_FROZEN_MODULES) -o $@ $(FROZEN_MPY_DIRS)
125124
$(Q)$(CD) $@ && \
126125
$(FIND) -L . -type f -name '*.py' | sed 's=^\./==' | \
127126
xargs -n1 $(abspath $(MPY_CROSS)) $(MPY_CROSS_FLAGS)

tools/preprocess_frozen_modules.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import os
4+
import os.path
5+
from pathlib import Path
6+
import semver
7+
import subprocess
8+
9+
# Compatible with Python 3.4 due to travis using trusty as default.
10+
11+
def version_string(path=None, *, valid_semver=False):
12+
version = None
13+
try:
14+
tag = subprocess.check_output('git describe --tags --exact-match', shell=True, cwd=path)
15+
version = tag.strip().decode("utf-8", "strict")
16+
except subprocess.CalledProcessError:
17+
describe = subprocess.check_output("git describe --tags", shell=True, cwd=path)
18+
tag, additional_commits, commitish = describe.strip().decode("utf-8", "strict").rsplit("-", maxsplit=2)
19+
commitish = commitish[1:]
20+
if valid_semver:
21+
version_info = semver.parse_version_info(tag)
22+
if not version_info.prerelease:
23+
version = semver.bump_patch(tag) + "-alpha.0.plus." + additional_commits + "+" + commitish
24+
else:
25+
version = tag + ".plus." + additional_commits + "+" + commitish
26+
else:
27+
version = commitish
28+
return version
29+
30+
# Visit all the .py files in topdir. Replace any __version__ = "0.0.0-auto.0" type of info
31+
# with actual version info derived from git.
32+
def copy_and_process(in_dir, out_dir):
33+
for root, subdirs, files in os.walk(in_dir):
34+
35+
# Skip library examples directories.
36+
if Path(root).name == 'examples':
37+
continue
38+
39+
for file in files:
40+
# Skip top-level setup.py (module install info) and conf.py (sphinx config),
41+
# which are not part of the library
42+
if (root == in_dir) and file in ('conf.py', 'setup.py'):
43+
continue
44+
45+
input_file_path = Path(root, file)
46+
output_file_path = Path(out_dir, input_file_path.relative_to(in_dir))
47+
48+
if file.endswith(".py"):
49+
if not output_file_path.parent.exists():
50+
output_file_path.parent.mkdir(parents=True)
51+
with input_file_path.open("r") as input, output_file_path.open("w") as output:
52+
for line in input:
53+
if line.startswith("__version__"):
54+
module_version = version_string(root, valid_semver=True)
55+
line = line.replace("0.0.0-auto.0", module_version)
56+
output.write(line)
57+
58+
if __name__ == '__main__':
59+
argparser = argparse.ArgumentParser(description="""\
60+
Copy and pre-process .py files into output directory, before freezing.
61+
1. Remove top-level repo directory.
62+
2. Update __version__ info.
63+
3. Remove examples.
64+
4. Remove non-library setup.py and conf.py""")
65+
argparser.add_argument("in_dirs", metavar="input-dir", nargs="+",
66+
help="top-level code dirs (may be git repo dirs)")
67+
argparser.add_argument("-o", "--out_dir", help="output directory")
68+
args = argparser.parse_args()
69+
70+
for in_dir in args.in_dirs:
71+
copy_and_process(in_dir, args.out_dir)

tools/python-semver

Submodule python-semver added at 2001c62

0 commit comments

Comments
 (0)