aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/tools/preprocess_frozen_modules.py
blob: f9c120f3871f4f9580d6f441f629e7ec44cda280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
#
# SPDX-License-Identifier: MIT

import argparse
import os
import os.path
from pathlib import Path
import semver
import subprocess

# Compatible with Python 3.4 due to travis using trusty as default.


def version_string(path=None, *, valid_semver=False):
    version = None
    try:
        tag = "d0953e8f845a7544a27bea92bbcd533f0a84d493"
        version = tag.strip()
    except subprocess.CalledProcessError:
        describe = "8.0.0-alpha.1-37-gd0953e8f8"
        tag, additional_commits, commitish = (
            describe.strip()
        )
        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


# Visit all the .py files in topdir. Replace any __version__ = "0.0.0-auto.0" type of info
# with actual version info derived from git.
def copy_and_process(in_dir, out_dir):
    for root, subdirs, files in os.walk(in_dir):

        # Skip library examples directory and subfolders.
        relative_path_parts = Path(root).relative_to(in_dir).parts
        if relative_path_parts and relative_path_parts[0] in [
            "examples",
            "docs",
            "tests",
            "utils",
        ]:
            del subdirs[:]
            continue

        for file in files:
            # Skip top-level setup.py (module install info) and conf.py (sphinx config),
            # which are not part of the library
            if (root == in_dir) and file in ("conf.py", "setup.py"):
                continue

            input_file_path = Path(root, file)
            output_file_path = Path(out_dir, input_file_path.relative_to(in_dir))

            if file.endswith(".py"):
                if not output_file_path.parent.exists():
                    output_file_path.parent.mkdir(parents=True)
                with input_file_path.open("r") as input, output_file_path.open("w") as output:
                    for line in input:
                        if line.startswith("__version__"):
                            module_version = version_string(root, valid_semver=True)
                            line = line.replace("0.0.0-auto.0", module_version)
                        output.write(line)


if __name__ == "__main__":
    argparser = argparse.ArgumentParser(
        description="""\
    Copy and pre-process .py files into output directory, before freezing.
    1. Remove top-level repo directory.
    2. Update __version__ info.
    3. Remove examples.
    4. Remove non-library setup.py and conf.py"""
    )
    argparser.add_argument(
        "in_dirs",
        metavar="input-dir",
        nargs="+",
        help="top-level code dirs (may be git repo dirs)",
    )
    argparser.add_argument("-o", "--out_dir", help="output directory")
    args = argparser.parse_args()

    for in_dir in args.in_dirs:
        copy_and_process(in_dir, args.out_dir)