forked from dolfinus/setuptools-git-versioning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
332 lines (254 loc) · 8.28 KB
/
util.py
File metadata and controls
332 lines (254 loc) · 8.28 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from __future__ import annotations
import logging
import os
import subprocess
import sys
import textwrap
from datetime import datetime
from pathlib import Path
from secrets import token_hex
from typing import Any, Callable
import tomli_w
log = logging.getLogger(__name__)
root = Path(__file__).parent.parent
def rand_str() -> str:
return token_hex()
def rand_full_sha() -> str:
return token_hex(40)
def rand_sha() -> str:
return token_hex(8)
def execute(cwd: str | os.PathLike, cmd: str, **kwargs) -> str:
log.info(f"Executing '{cmd}' at '{cwd}'")
if "env" in kwargs:
kwargs["env"]["PATH"] = os.environ["PATH"]
pythonpath = os.getenv("PYTHONPATH", None)
if pythonpath:
kwargs["env"]["PYTHONPATH"] = pythonpath
return subprocess.check_output(cmd, cwd=cwd, shell=True, universal_newlines=True, **kwargs) # nosec
def get_full_sha(cwd: str | os.PathLike, **kwargs) -> str:
return execute(cwd, "git rev-list -n 1 HEAD", **kwargs).strip()
def get_sha(cwd: str | os.PathLike, **kwargs) -> str:
return get_full_sha(cwd, **kwargs)[:8]
def create_commit(
cwd: str | os.PathLike,
message: str,
dt: datetime | None = None,
**kwargs,
) -> str:
options = ""
if dt is not None:
# Store committer date in case it was set somewhere else
original_committer_date = os.environ.get("GIT_COMMITTER_DATE", None)
options += f"--date {dt.isoformat()}"
# The committer date is what is used to determine sort order for tags, etc
os.environ["GIT_COMMITTER_DATE"] = dt.isoformat()
try:
return_value = execute(cwd, f'git commit -m "{message}" {options}', **kwargs)
finally:
# Return committer date env var to prior value if set
if dt is not None:
if original_committer_date is None:
# unset the var
del os.environ["GIT_COMMITTER_DATE"]
else:
# restore previous value
os.environ["GIT_COMMITTER_DATE"] = original_committer_date
return return_value
def create_tag(
cwd: str | os.PathLike,
tag: str,
message: str | None = None,
commit: str | None = None,
**kwargs,
) -> str:
options = ""
if message:
options += f' -a -m "{message}"'
if not commit:
commit = ""
return execute(cwd, f"git tag {options} {tag} {commit}", **kwargs)
def checkout_branch(cwd: str | os.PathLike, branch: str, new: bool = True, **kwargs) -> str:
options = ""
if new:
options += " -b"
return execute(cwd, f"git checkout {options} {branch}", **kwargs)
def create_folder(
cwd: str | os.PathLike,
name: str | None = None,
add: bool = True,
commit: bool = True,
**kwargs,
) -> str | None:
result = None
if not name:
name = rand_str()
path = Path(cwd).joinpath(name)
# create dir with some random file
path.mkdir(parents=True, exist_ok=True)
path.joinpath(rand_str()).touch()
if add:
execute(cwd, f"git add {name}")
log.info(execute(cwd, "git status"))
log.info(execute(cwd, "git diff"))
if commit:
create_commit(cwd, f"Add {name}")
result = get_sha(cwd)
return result
def create_file(
cwd: str | os.PathLike,
name: str | None = None,
content: str | None = None,
add: bool = True,
commit: bool = True,
**kwargs,
) -> str | None:
result = None
if not name:
name = rand_str()
if content is None:
content = rand_str()
log.info(content)
Path(cwd).joinpath(name).write_text(content)
if add:
execute(cwd, f"git add {name}")
log.info(execute(cwd, "git status"))
log.info(execute(cwd, "git diff"))
if commit:
create_commit(cwd, f"Add {name}")
result = get_sha(cwd)
return result
def create_pyproject_toml(
cwd: str | os.PathLike,
config: dict | None = None,
commit: bool = True,
**kwargs,
) -> str | None:
# well, using pyproject.toml+setup.cfg is more classic
# but it is not easy to check code coverage in such a case
# so we're using pyproject.toml+setup.py
create_file(
cwd,
"setup.py",
textwrap.dedent(
"""
from coverage.control import Coverage
coverage = Coverage()
coverage.start()
try:
import setuptools
setuptools.setup(
name="mypkg",
)
finally:
coverage.stop()
coverage.save()
"""
),
commit=False,
**kwargs,
)
cfg: dict[str, Any] = {}
cfg["build-system"] = {
"requires": [
"setuptools>=41",
"wheel",
"setuptools-git-versioning",
"coverage",
],
# with default "setuptools.build_meta" it is not possible to build package
# which uses its own source code to get version number,
# e.g. `version_callback` or `branch_formatter`
# mote details: https://github.com/pypa/setuptools/issues/1642#issuecomment-457673563
"build-backend": "setuptools.build_meta:__legacy__",
}
if config is None:
config = {"enabled": True}
if config != NotImplemented:
cfg["tool"] = {"setuptools-git-versioning": config}
return create_file(cwd, "pyproject.toml", tomli_w.dumps(cfg), commit=commit, **kwargs)
def create_setup_py(
cwd: str | os.PathLike,
config: dict | None = None,
**kwargs,
) -> str | None:
if config is None:
config = {"enabled": True}
if config == NotImplemented:
cfg = ""
else:
cfg = f"setuptools_git_versioning={config},"
return create_file(
cwd,
"setup.py",
textwrap.dedent(
f"""
from coverage.control import Coverage
coverage = Coverage()
coverage.start()
try:
import setuptools
setuptools.setup(
name="mypkg",
{cfg}
setup_requires=[
"setuptools>=41",
"wheel",
"coverage",
"setuptools-git-versioning",
]
)
finally:
coverage.stop()
coverage.save()
"""
),
**kwargs,
)
def typed_config(
repo: str | os.PathLike,
config_creator: Callable,
config_type: str,
template: str | None = None,
template_name: str | None = None,
config: dict | None = None,
) -> None:
if config_type == "tag":
cfg = {}
else:
cfg = {"version_file": "VERSION.txt", "count_commits_from_version_file": True}
if template_name is None:
if config_type == "tag":
template_name = "template"
else:
template_name = "dev_template"
if template:
cfg[template_name] = template
if config:
cfg.update(config)
config_creator(repo, cfg)
if config_type == "tag":
create_tag(repo, "1.2.3")
else:
create_file(repo, "VERSION.txt", "1.2.3")
def get_version_setup_py(cwd: str | os.PathLike, **kwargs) -> str:
return execute(cwd, f"{sys.executable} setup.py --version", **kwargs).strip()
def get_version_module(cwd: str | os.PathLike, args: list[str] | None = None, **kwargs) -> str:
args_str = " ".join(args or [])
return execute(
cwd,
f"{sys.executable} -m coverage run -m setuptools_git_versioning {args_str} -vv",
**kwargs,
).strip()
def get_version_script(cwd: str | os.PathLike, args: list[str] | None = None, **kwargs) -> str:
args_str = " ".join(args or [])
return execute(cwd, f"setuptools-git-versioning {args_str} -vv", **kwargs).strip()
def get_version(cwd: str | os.PathLike, isolated: bool = False, **kwargs) -> str:
cmd = f"{sys.executable} -m build -s"
if not isolated:
cmd += " --no-isolation"
execute(cwd, cmd, **kwargs)
content = Path(cwd).joinpath("mypkg.egg-info/PKG-INFO").read_text().splitlines()
for line in content:
if line.startswith("Version: "):
return line.replace("Version: ", "").strip()
raise RuntimeError("Cannot get package version")