|
| 1 | +#!/usr/bin/env python3 |
| 2 | +importargparse |
| 3 | +importos |
| 4 | +importos.path |
| 5 | +frompathlibimportPath |
| 6 | +importsemver |
| 7 | +importsubprocess |
| 8 | + |
| 9 | +# Compatible with Python 3.4 due to travis using trusty as default. |
| 10 | + |
| 11 | +defversion_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 | +exceptsubprocess.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 | +ifvalid_semver: |
| 21 | +version_info=semver.parse_version_info(tag) |
| 22 | +ifnotversion_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 | +returnversion |
| 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 | +defcopy_and_process(in_dir,out_dir): |
| 33 | +forroot,subdirs,filesinos.walk(in_dir): |
| 34 | + |
| 35 | +# Skip library examples directories. |
| 36 | +ifPath(root).name=='examples': |
| 37 | +continue |
| 38 | + |
| 39 | +forfileinfiles: |
| 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)andfilein ('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 | +iffile.endswith(".py"): |
| 49 | +ifnotoutput_file_path.parent.exists(): |
| 50 | +output_file_path.parent.mkdir(parents=True) |
| 51 | +withinput_file_path.open("r")asinput,output_file_path.open("w")asoutput: |
| 52 | +forlineininput: |
| 53 | +ifline.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 | +forin_dirinargs.in_dirs: |
| 71 | +copy_and_process(in_dir,args.out_dir) |