... | ... |
@@ -203,7 +203,17 @@ def install(args): |
203 | 203 |
|
204 | 204 |
def ver(args): |
205 | 205 |
package_dir = args.package_directory |
206 |
- version_location = glob.glob(f"{package_dir}/**/_version.py")[0] |
|
206 |
+ try: |
|
207 |
+ version_location, = glob.glob(f"{package_dir}/**/_version.py", recursive=True) |
|
208 |
+ except ValueError as err: |
|
209 |
+ if "not enough" in str(err): |
|
210 |
+ print(f"'_version.py' not found in '{package_dir}'", file=sys.stderr) |
|
211 |
+ sys.exit(1) |
|
212 |
+ elif "too many" in str(err): |
|
213 |
+ print(f"More than 1 '_version.py' found in '{package_dir}'", file=sys.stderr) |
|
214 |
+ sys.exit(1) |
|
215 |
+ else: |
|
216 |
+ raise |
|
207 | 217 |
version_spec = spec_from_file_location("version", version_location) |
208 | 218 |
version = module_from_spec(version_spec) |
209 | 219 |
version_spec.loader.exec_module(version) |
... | ... |
@@ -8,8 +8,9 @@ import argparse |
8 | 8 |
import tempfile |
9 | 9 |
import shutil |
10 | 10 |
import textwrap |
11 |
+import glob |
|
11 | 12 |
from zipfile import ZipFile |
12 |
-from importlib.util import find_spec |
|
13 |
+from importlib.util import find_spec, spec_from_file_location, module_from_spec |
|
13 | 14 |
from urllib.request import urlretrieve |
14 | 15 |
|
15 | 16 |
if sys.version_info < (3, 5): |
... | ... |
@@ -146,6 +147,14 @@ def get_parser(): |
146 | 147 |
"package_directory", help="Directory to install 'miniver' into." |
147 | 148 |
) |
148 | 149 |
install_parser.set_defaults(dispatch=install) |
150 |
+ # 'ver' command |
|
151 |
+ ver_parser = subparsers.add_parser( |
|
152 |
+ "ver", help="Get generated version" |
|
153 |
+ ) |
|
154 |
+ ver_parser.add_argument( |
|
155 |
+ "package_directory", help="Directory 'miniver' was installed." |
|
156 |
+ ) |
|
157 |
+ ver_parser.set_defaults(dispatch=ver) |
|
149 | 158 |
return parser |
150 | 159 |
|
151 | 160 |
|
... | ... |
@@ -192,6 +201,15 @@ def install(args): |
192 | 201 |
print("\n".join((msg, _setup_template)).format(package_dir=package_dir)) |
193 | 202 |
|
194 | 203 |
|
204 |
+def ver(args): |
|
205 |
+ package_dir = args.package_directory |
|
206 |
+ version_location = glob.glob(f"{package_dir}/**/_version.py")[0] |
|
207 |
+ version_spec = spec_from_file_location("version", version_location) |
|
208 |
+ version = module_from_spec(version_spec) |
|
209 |
+ version_spec.loader.exec_module(version) |
|
210 |
+ print(version.get_version()) |
|
211 |
+ |
|
212 |
+ |
|
195 | 213 |
def main(): |
196 | 214 |
parser = get_parser() |
197 | 215 |
args = parser.parse_args() |
... | ... |
@@ -44,14 +44,15 @@ _setup_template = textwrap.dedent( |
44 | 44 |
""" |
45 | 45 |
import os |
46 | 46 |
from importlib.util import module_from_spec, spec_from_file_location |
47 |
- spec = spec_from_file_location('version', |
|
48 |
- os.path.join(package_path, '_version.py')) |
|
47 |
+ |
|
48 |
+ spec = spec_from_file_location("version", os.path.join(package_path, "_version.py")) |
|
49 | 49 |
module = module_from_spec(spec) |
50 | 50 |
spec.loader.exec_module(module) |
51 | 51 |
return module.__version__, module.cmdclass |
52 | 52 |
|
53 | 53 |
|
54 |
- version, cmdclass = get_version_and_cmdclass('{package_dir}') |
|
54 |
+ version, cmdclass = get_version_and_cmdclass("{package_dir}") |
|
55 |
+ |
|
55 | 56 |
|
56 | 57 |
setup( |
57 | 58 |
..., |
1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,206 @@ |
1 |
+#!/usr/bin/env python3 |
|
2 |
+# This file is part of 'miniver': https://github.com/jbweston/miniver |
|
3 |
+ |
|
4 |
+import sys |
|
5 |
+import os |
|
6 |
+import os.path |
|
7 |
+import argparse |
|
8 |
+import tempfile |
|
9 |
+import shutil |
|
10 |
+import textwrap |
|
11 |
+from zipfile import ZipFile |
|
12 |
+from importlib.util import find_spec |
|
13 |
+from urllib.request import urlretrieve |
|
14 |
+ |
|
15 |
+if sys.version_info < (3, 5): |
|
16 |
+ print("Miniver needs at least Python 3.5") |
|
17 |
+ sys.exit(1) |
|
18 |
+ |
|
19 |
+try: |
|
20 |
+ import miniver |
|
21 |
+ |
|
22 |
+ _miniver_version = miniver.__version__ |
|
23 |
+ del miniver |
|
24 |
+ _miniver_is_installed = True |
|
25 |
+except ImportError: |
|
26 |
+ _miniver_version = "unknown" |
|
27 |
+ _miniver_is_installed = False |
|
28 |
+ |
|
29 |
+# When we fetch miniver from local files |
|
30 |
+_miniver_modules = ("_version",) |
|
31 |
+ |
|
32 |
+ |
|
33 |
+# When we fetch miniver from GitHub |
|
34 |
+_miniver_zip_url = "https://github.com/jbweston/miniver/archive/master.zip" |
|
35 |
+_zipfile_root = "miniver-master" # tied to the fact that we fetch master.zip |
|
36 |
+ |
|
37 |
+# File templates |
|
38 |
+_setup_template = textwrap.dedent( |
|
39 |
+ ''' |
|
40 |
+ def get_version_and_cmdclass(package_path): |
|
41 |
+ """Load version.py module without importing the whole package. |
|
42 |
+ |
|
43 |
+ Template code from miniver |
|
44 |
+ """ |
|
45 |
+ import os |
|
46 |
+ from importlib.util import module_from_spec, spec_from_file_location |
|
47 |
+ spec = spec_from_file_location('version', |
|
48 |
+ os.path.join(package_path, '_version.py')) |
|
49 |
+ module = module_from_spec(spec) |
|
50 |
+ spec.loader.exec_module(module) |
|
51 |
+ return module.__version__, module.cmdclass |
|
52 |
+ |
|
53 |
+ |
|
54 |
+ version, cmdclass = get_version_and_cmdclass('{package_dir}') |
|
55 |
+ |
|
56 |
+ setup( |
|
57 |
+ ..., |
|
58 |
+ version=version, |
|
59 |
+ cmdclass=cmdclass, |
|
60 |
+ ) |
|
61 |
+''' |
|
62 |
+) |
|
63 |
+ |
|
64 |
+_static_version_template = textwrap.dedent( |
|
65 |
+ """\ |
|
66 |
+ # -*- coding: utf-8 -*- |
|
67 |
+ # This file is part of 'miniver': https://github.com/jbweston/miniver |
|
68 |
+ # |
|
69 |
+ # This file will be overwritten by setup.py when a source or binary |
|
70 |
+ # distribution is made. The magic value "__use_git__" is interpreted by |
|
71 |
+ # version.py. |
|
72 |
+ |
|
73 |
+ version = "__use_git__" |
|
74 |
+ |
|
75 |
+ # These values are only set if the distribution was created with 'git archive' |
|
76 |
+ refnames = "$Format:%D$" |
|
77 |
+ git_hash = "$Format:%h$" |
|
78 |
+""" |
|
79 |
+) |
|
80 |
+ |
|
81 |
+_init_template = "from ._version import __version__" |
|
82 |
+_gitattribute_template = "{package_dir}/_static_version.py export-subst" |
|
83 |
+ |
|
84 |
+ |
|
85 |
+def _line_in_file(to_find, filename): |
|
86 |
+ """Return True if the specified line exists in the named file.""" |
|
87 |
+ assert "\n" not in to_find |
|
88 |
+ try: |
|
89 |
+ with open(filename) as f: |
|
90 |
+ for line in f: |
|
91 |
+ if to_find in line: |
|
92 |
+ return True |
|
93 |
+ return False |
|
94 |
+ except FileNotFoundError: |
|
95 |
+ return False |
|
96 |
+ |
|
97 |
+ |
|
98 |
+def _write_line(content, filename): |
|
99 |
+ assert "\n" not in content |
|
100 |
+ if not _line_in_file(content, filename): |
|
101 |
+ with open(filename, "a") as f: |
|
102 |
+ f.write(content) |
|
103 |
+ |
|
104 |
+ |
|
105 |
+def _write_content(content, filename): |
|
106 |
+ with open(filename, "w") as f: |
|
107 |
+ f.write(content) |
|
108 |
+ |
|
109 |
+ |
|
110 |
+def _fail(msg): |
|
111 |
+ print(msg, file=sys.stderr) |
|
112 |
+ print("Miniver was not installed", file=sys.stderr) |
|
113 |
+ sys.exit(1) |
|
114 |
+ |
|
115 |
+ |
|
116 |
+def extract_miniver_from_github(): |
|
117 |
+ filename, _ = urlretrieve(_miniver_zip_url) |
|
118 |
+ z = ZipFile(filename) |
|
119 |
+ tmpdir = tempfile.mkdtemp() |
|
120 |
+ input_paths = [ |
|
121 |
+ "/".join((_zipfile_root, "miniver", module + ".py")) |
|
122 |
+ for module in _miniver_modules |
|
123 |
+ ] |
|
124 |
+ for p in input_paths: |
|
125 |
+ z.extract(p, path=tmpdir) |
|
126 |
+ return [os.path.join(tmpdir, *p.split()) for p in input_paths] |
|
127 |
+ |
|
128 |
+ |
|
129 |
+def extract_miniver_from_local(): |
|
130 |
+ return [ |
|
131 |
+ find_spec("." + module, package="miniver").origin for module in _miniver_modules |
|
132 |
+ ] |
|
133 |
+ |
|
134 |
+ |
|
135 |
+def get_parser(): |
|
136 |
+ parser = argparse.ArgumentParser(description="Interact with miniver") |
|
137 |
+ parser.add_argument("-v", "--version", action="version", version=_miniver_version) |
|
138 |
+ # TODO: when we can depend on Python 3.7 make this "add_subparsers(required=True)" |
|
139 |
+ subparsers = parser.add_subparsers() |
|
140 |
+ # 'install' command |
|
141 |
+ install_parser = subparsers.add_parser( |
|
142 |
+ "install", help="Install miniver into the current Python package" |
|
143 |
+ ) |
|
144 |
+ install_parser.add_argument( |
|
145 |
+ "package_directory", help="Directory to install 'miniver' into." |
|
146 |
+ ) |
|
147 |
+ install_parser.set_defaults(dispatch=install) |
|
148 |
+ return parser |
|
149 |
+ |
|
150 |
+ |
|
151 |
+def install(args): |
|
152 |
+ package_dir = args.package_directory |
|
153 |
+ if not os.path.isdir(package_dir): |
|
154 |
+ _fail("Directory '{}' does not exist".format(package_dir)) |
|
155 |
+ if package_dir != os.path.relpath(package_dir): |
|
156 |
+ _fail("'{}' is not a relative directory".format(package_dir)) |
|
157 |
+ |
|
158 |
+ # Get miniver files |
|
159 |
+ if _miniver_is_installed: |
|
160 |
+ miniver_paths = extract_miniver_from_local() |
|
161 |
+ else: |
|
162 |
+ miniver_paths = extract_miniver_from_github() |
|
163 |
+ output_paths = [ |
|
164 |
+ os.path.join(package_dir, os.path.basename(path)) for path in miniver_paths |
|
165 |
+ ] |
|
166 |
+ |
|
167 |
+ for path in output_paths: |
|
168 |
+ if os.path.exists(path): |
|
169 |
+ _fail("'{}' already exists".format(path)) |
|
170 |
+ |
|
171 |
+ # Write content to local package directory |
|
172 |
+ for path, output_path in zip(miniver_paths, output_paths): |
|
173 |
+ shutil.copy(path, output_path) |
|
174 |
+ _write_content( |
|
175 |
+ _static_version_template, os.path.join(package_dir, "_static_version.py") |
|
176 |
+ ) |
|
177 |
+ _write_line( |
|
178 |
+ _gitattribute_template.format(package_dir=package_dir), ".gitattributes" |
|
179 |
+ ) |
|
180 |
+ _write_line( |
|
181 |
+ _init_template.format(package_dir=package_dir), |
|
182 |
+ os.path.join(package_dir, "__init__.py"), |
|
183 |
+ ) |
|
184 |
+ |
|
185 |
+ msg = "\n".join( |
|
186 |
+ textwrap.wrap( |
|
187 |
+ "Miniver is installed into '{package_dir}/'. " |
|
188 |
+ "You still have to copy the following snippet into your 'setup.py':" |
|
189 |
+ ) |
|
190 |
+ ) |
|
191 |
+ print("\n".join((msg, _setup_template)).format(package_dir=package_dir)) |
|
192 |
+ |
|
193 |
+ |
|
194 |
+def main(): |
|
195 |
+ parser = get_parser() |
|
196 |
+ args = parser.parse_args() |
|
197 |
+ # TODO: remove this check when we can rely on Python 3.7 and |
|
198 |
+ # can make subparsers required. |
|
199 |
+ if "dispatch" in args: |
|
200 |
+ args.dispatch(args) |
|
201 |
+ else: |
|
202 |
+ parser.parse_args(['-h']) |
|
203 |
+ |
|
204 |
+ |
|
205 |
+if __name__ == "__main__": |
|
206 |
+ main() |