|
| 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) |
0 commit comments