... | ... |
@@ -13,28 +13,30 @@ from importlib.util import find_spec |
13 | 13 |
from urllib.request import urlretrieve |
14 | 14 |
|
15 | 15 |
if sys.version_info < (3, 5): |
16 |
- print('Miniver needs at least Python 3.5') |
|
16 |
+ print("Miniver needs at least Python 3.5") |
|
17 | 17 |
sys.exit(1) |
18 | 18 |
|
19 | 19 |
try: |
20 | 20 |
import miniver |
21 |
+ |
|
21 | 22 |
_miniver_version = miniver.__version__ |
22 | 23 |
del miniver |
23 | 24 |
_miniver_is_installed = True |
24 | 25 |
except ImportError: |
25 |
- _miniver_version = 'unknown' |
|
26 |
+ _miniver_version = "unknown" |
|
26 | 27 |
_miniver_is_installed = False |
27 | 28 |
|
28 | 29 |
# When we fetch miniver from local files |
29 |
-_miniver_modules = ('_version',) |
|
30 |
+_miniver_modules = ("_version",) |
|
30 | 31 |
|
31 | 32 |
|
32 | 33 |
# When we fetch miniver from GitHub |
33 |
-_miniver_zip_url = 'https://github.com/jbweston/miniver/archive/master.zip' |
|
34 |
-_zipfile_root = 'miniver-master' # tied to the fact that we fetch master.zip |
|
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 |
|
35 | 36 |
|
36 | 37 |
# File templates |
37 |
-_setup_template = textwrap.dedent(''' |
|
38 |
+_setup_template = textwrap.dedent( |
|
39 |
+ ''' |
|
38 | 40 |
def get_version_and_cmdclass(package_path): |
39 | 41 |
"""Load version.py module without importing the whole package. |
40 | 42 |
|
... | ... |
@@ -56,9 +58,11 @@ _setup_template = textwrap.dedent(''' |
56 | 58 |
version=version, |
57 | 59 |
cmdclass=cmdclass, |
58 | 60 |
) |
59 |
-''') |
|
61 |
+''' |
|
62 |
+) |
|
60 | 63 |
|
61 |
-_static_version_template = textwrap.dedent('''\ |
|
64 |
+_static_version_template = textwrap.dedent( |
|
65 |
+ """\ |
|
62 | 66 |
# -*- coding: utf-8 -*- |
63 | 67 |
# This file is part of 'miniver': https://github.com/jbweston/miniver |
64 | 68 |
# |
... | ... |
@@ -71,15 +75,16 @@ _static_version_template = textwrap.dedent('''\ |
71 | 75 |
# These values are only set if the distribution was created with 'git archive' |
72 | 76 |
refnames = "$Format:%D$" |
73 | 77 |
git_hash = "$Format:%h$" |
74 |
-''') |
|
78 |
+""" |
|
79 |
+) |
|
75 | 80 |
|
76 |
-_init_template = 'from ._version import __version__' |
|
77 |
-_gitattribute_template = '{package_dir}/_static_version.py export-subst' |
|
81 |
+_init_template = "from ._version import __version__" |
|
82 |
+_gitattribute_template = "{package_dir}/_static_version.py export-subst" |
|
78 | 83 |
|
79 | 84 |
|
80 | 85 |
def _line_in_file(to_find, filename): |
81 | 86 |
"""Return True if the specified line exists in the named file.""" |
82 |
- assert '\n' not in to_find |
|
87 |
+ assert "\n" not in to_find |
|
83 | 88 |
try: |
84 | 89 |
with open(filename) as f: |
85 | 90 |
for line in f: |
... | ... |
@@ -91,20 +96,20 @@ def _line_in_file(to_find, filename): |
91 | 96 |
|
92 | 97 |
|
93 | 98 |
def _write_line(content, filename): |
94 |
- assert '\n' not in content |
|
99 |
+ assert "\n" not in content |
|
95 | 100 |
if not _line_in_file(content, filename): |
96 |
- with open(filename, 'a') as f: |
|
101 |
+ with open(filename, "a") as f: |
|
97 | 102 |
f.write(content) |
98 | 103 |
|
99 | 104 |
|
100 | 105 |
def _write_content(content, filename): |
101 |
- with open(filename, 'w') as f: |
|
106 |
+ with open(filename, "w") as f: |
|
102 | 107 |
f.write(content) |
103 | 108 |
|
104 | 109 |
|
105 | 110 |
def _fail(msg): |
106 | 111 |
print(msg, file=sys.stderr) |
107 |
- print('Miniver was not installed', file=sys.stderr) |
|
112 |
+ print("Miniver was not installed", file=sys.stderr) |
|
108 | 113 |
sys.exit(1) |
109 | 114 |
|
110 | 115 |
|
... | ... |
@@ -112,25 +117,29 @@ def extract_miniver_from_github(): |
112 | 117 |
filename, _ = urlretrieve(_miniver_zip_url) |
113 | 118 |
z = ZipFile(filename) |
114 | 119 |
tmpdir = tempfile.mkdtemp() |
115 |
- input_paths = ['/'.join((_zipfile_root, 'miniver', module+'.py')) |
|
116 |
- for module in _miniver_modules] |
|
120 |
+ input_paths = [ |
|
121 |
+ "/".join((_zipfile_root, "miniver", module + ".py")) |
|
122 |
+ for module in _miniver_modules |
|
123 |
+ ] |
|
117 | 124 |
for p in input_paths: |
118 | 125 |
z.extract(p, path=tmpdir) |
119 | 126 |
return [os.path.join(tmpdir, *p.split()) for p in input_paths] |
120 | 127 |
|
121 | 128 |
|
122 | 129 |
def extract_miniver_from_local(): |
123 |
- return [find_spec('.' + module, package='miniver').origin |
|
124 |
- for module in _miniver_modules] |
|
130 |
+ return [ |
|
131 |
+ find_spec("." + module, package="miniver").origin for module in _miniver_modules |
|
132 |
+ ] |
|
125 | 133 |
|
126 | 134 |
|
127 | 135 |
def get_args(): |
128 | 136 |
parser = argparse.ArgumentParser( |
129 |
- description="Install 'miniver' into the current Python package") |
|
130 |
- parser.add_argument("-v", "--version", action="version", |
|
131 |
- version=_miniver_version) |
|
132 |
- parser.add_argument('package_directory', |
|
133 |
- help="Directory to install 'miniver' into.") |
|
137 |
+ description="Install 'miniver' into the current Python package" |
|
138 |
+ ) |
|
139 |
+ parser.add_argument("-v", "--version", action="version", version=_miniver_version) |
|
140 |
+ parser.add_argument( |
|
141 |
+ "package_directory", help="Directory to install 'miniver' into." |
|
142 |
+ ) |
|
134 | 143 |
return parser.parse_args().package_directory |
135 | 144 |
|
136 | 145 |
|
... | ... |
@@ -146,8 +155,9 @@ def main(): |
146 | 155 |
miniver_paths = extract_miniver_from_local() |
147 | 156 |
else: |
148 | 157 |
miniver_paths = extract_miniver_from_github() |
149 |
- output_paths = [os.path.join(package_dir, os.path.basename(path)) |
|
150 |
- for path in miniver_paths] |
|
158 |
+ output_paths = [ |
|
159 |
+ os.path.join(package_dir, os.path.basename(path)) for path in miniver_paths |
|
160 |
+ ] |
|
151 | 161 |
|
152 | 162 |
for path in output_paths: |
153 | 163 |
if os.path.exists(path): |
... | ... |
@@ -156,19 +166,25 @@ def main(): |
156 | 166 |
# Write content to local package directory |
157 | 167 |
for path, output_path in zip(miniver_paths, output_paths): |
158 | 168 |
shutil.copy(path, output_path) |
159 |
- _write_content(_static_version_template, |
|
160 |
- os.path.join(package_dir, '_static_version.py')) |
|
161 |
- _write_line(_gitattribute_template.format(package_dir=package_dir), |
|
162 |
- '.gitattributes') |
|
163 |
- _write_line(_init_template.format(package_dir=package_dir), |
|
164 |
- os.path.join(package_dir, '__init__.py')) |
|
169 |
+ _write_content( |
|
170 |
+ _static_version_template, os.path.join(package_dir, "_static_version.py") |
|
171 |
+ ) |
|
172 |
+ _write_line( |
|
173 |
+ _gitattribute_template.format(package_dir=package_dir), ".gitattributes" |
|
174 |
+ ) |
|
175 |
+ _write_line( |
|
176 |
+ _init_template.format(package_dir=package_dir), |
|
177 |
+ os.path.join(package_dir, "__init__.py"), |
|
178 |
+ ) |
|
165 | 179 |
|
166 |
- msg = '\n'.join(textwrap.wrap( |
|
167 |
- "Miniver is installed into '{package_dir}/'. " |
|
168 |
- "You still have to copy the following snippet into your 'setup.py':" |
|
169 |
- )) |
|
170 |
- print('\n'.join((msg, _setup_template)).format(package_dir=package_dir)) |
|
180 |
+ msg = "\n".join( |
|
181 |
+ textwrap.wrap( |
|
182 |
+ "Miniver is installed into '{package_dir}/'. " |
|
183 |
+ "You still have to copy the following snippet into your 'setup.py':" |
|
184 |
+ ) |
|
185 |
+ ) |
|
186 |
+ print("\n".join((msg, _setup_template)).format(package_dir=package_dir)) |
|
171 | 187 |
|
172 | 188 |
|
173 |
-if __name__ == '__main__': |
|
189 |
+if __name__ == "__main__": |
|
174 | 190 |
main() |