-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.py
More file actions
103 lines (94 loc) · 3.06 KB
/
new.py
File metadata and controls
103 lines (94 loc) · 3.06 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
import argparse
import os
import subprocess
import time
from pathlib import Path
TEMPLATE_REPOSITORY_URL = "https://github.com/smkent/cookie-python"
def parse_args() -> tuple[argparse.Namespace, list[str]]:
ap = argparse.ArgumentParser()
ap.add_argument(
"project_dir", help="New project parent directory", type=str
)
ap.add_argument(
"--local",
"-l",
action="store_true",
dest="local_template",
help=(
"Create project from local template instead of "
"template repository URL"
),
)
return ap.parse_known_args()
def locate_new_project(parent_dir: str) -> str:
paths = sorted(
Path(parent_dir).iterdir(), key=os.path.getmtime, reverse=True
)
for path in paths:
if os.path.isfile(os.path.join(parent_dir, path, ".cruft.json")):
sub_dir = os.path.join(parent_dir, path)
assert (
len(
subprocess.check_output(
["git", "log", "--oneline"], cwd=sub_dir
)
.decode("utf-8")
.strip()
.splitlines()
)
== 1
), (
f"Detected project directory {sub_dir} "
"should have exactly 1 commit"
)
assert (
subprocess.check_output(
["git", "status", "--porcelain=v1"], cwd=sub_dir
)
.decode("utf-8")
.strip()
== "?? .cruft.json"
), f"Detected project directory {sub_dir} has unexpected contents"
assert time.time() - os.stat(sub_dir).st_ctime < 60, (
f"Detected project directory {sub_dir}"
" was not created just now"
)
return sub_dir
raise Exception("Unable to locate newly-created project directory")
def commit_cruft_json(project_dir: str) -> None:
new_project_dir = locate_new_project(project_dir)
subprocess.check_call(["git", "add", ".cruft.json"], cwd=new_project_dir)
author_name, author_email = (
subprocess.check_output(
["git", "log", "-n", "1", "--pretty=%an|%ae"], cwd=new_project_dir
)
.decode("utf-8")
.split("|", 1)
)
environ = os.environ.copy()
environ.update(
dict(
GIT_AUTHOR_NAME=author_name,
GIT_AUTHOR_EMAIL=author_email,
GIT_COMMITTER_NAME=author_name,
GIT_COMMITTER_EMAIL=author_email,
)
)
subprocess.check_call(
["git", "commit", "--amend", "--no-edit"],
cwd=new_project_dir,
env=environ,
)
def main() -> None:
args, more_args = parse_args()
template_path = (
os.path.dirname(os.path.dirname(__file__))
if args.local_template
else TEMPLATE_REPOSITORY_URL
)
subprocess.check_call(
["cruft", "create", "--output-dir", args.project_dir]
+ more_args
+ [template_path]
)
commit_cruft_json(args.project_dir)