-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_project.py
More file actions
executable file
·208 lines (167 loc) · 7.01 KB
/
Copy pathmake_project.py
File metadata and controls
executable file
·208 lines (167 loc) · 7.01 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
#!/usr/bin/env python3
import json
import requests
import os
import shutil
import sys
from jinja2 import Template
from subprocess import call
from zipfile import ZipFile
AUTH_CONFIG = "auth-config.json"
PGDP_URL = "https://www.pgdp.net"
GITHUB_REMOTE = "origin"
GITHUB_BRANCH = "main"
class MakeProject():
def __init__(self):
self.dp_base = f"{os.environ['HOME']}/dp"
self.projects_base = f"{self.dp_base}/pp"
self.template_dir = f"{self.dp_base}/util/templates"
self.params = {}
with open(f"{self.dp_base}/util/{AUTH_CONFIG}") as file:
self.auth = json.loads(file.read())
def get_param(self, param_name, prompt_text):
param_answer = input(f"{prompt_text}: ")
if param_name == "project_id":
param_answer = param_answer.replace("projectID", "")
self.params[param_name] = param_answer
def get_params(self):
self.get_param("project_name", 'Project name, e.g. "missfairfax"')
self.get_param("project_id", 'Project ID, e.g. "projectID5351bd1e5eca9"')
self.project_dir = f"{self.projects_base}/{self.params['project_name']}"
def get_project_info(self):
r = requests.get(
f"{PGDP_URL}/api/v1/projects/projectID{self.params['project_id']}",
headers={
"Accept": "application/json",
"X-Api-Key": self.auth["pgdp"]["api_key"],
},
)
if r.status_code != 200:
print("Error: unable to use PGDP REST API")
sys.exit(1)
j = r.json()
self.params["title"] = j["title"]
print(f"Title: {self.params['title']}")
self.params["author"] = j["author"]
print(f"Author: {self.params['author']}")
self.params["project_comments"] = j["comments"].replace("\r", "")
self.params["forum_link"] = j["forum_url"]
print(f"Forum: {self.params['forum_link']}")
def get_project_file_info(self):
r = requests.get(
f"{PGDP_URL}/api/v1/projects/projectID{self.params['project_id']}/artifacts/PP",
headers={
"Accept": "application/json",
"X-Api-Key": self.auth["pgdp"]["api_key"],
}
)
if r.status_code != 200:
print("Error: unable to use PGDP REST API")
sys.exit(1)
for item in r.json():
if item["name"] == "text_zip":
self.params["text_zip"] = item["url"]
elif item["name"] == "images_zip":
self.params["images_zip"] = item["url"]
if "text_zip" not in self.params:
print("Unable to find text zipfile link")
sys.exit(1)
if "images_zip" not in self.params:
print("Unable to find images zipfile link")
sys.exit(1)
def create_directories(self):
os.mkdir(self.project_dir, mode=0o755)
os.chdir(self.project_dir)
os.mkdir("images", mode=0o755)
os.mkdir("pngs", mode=0o755)
print("Created directory structure")
def create_git_repository(self):
call(["git", "init", "-q"])
call(["git", "add", "."])
call(["git", "commit", "-q", "-m", "Initial import from DP"])
print(f"Git repository created")
call(["git", "remote", "add", GITHUB_REMOTE, self.git_remote_url])
call(["git", "push", "-u", GITHUB_REMOTE, GITHUB_BRANCH])
print(f"Git repository created")
def process_template(self, src_filename, dst_filename=None):
if not dst_filename:
dst_filename = src_filename
with open(f"{self.template_dir}/{src_filename}") as file:
template = Template(file.read(), autoescape=False)
with open(f"{self.project_dir}/{dst_filename}", "w") as file:
file.write(template.render(self.params))
print(f"Created: {dst_filename}")
def copy_text_file(self):
project_id = self.params["project_id"]
project_name = self.params["project_name"]
project_dir = self.project_dir
input_file = f"{project_dir}/projectID{project_id}.txt"
output_file = f"{project_dir}/{project_name}-utf8.txt"
shutil.copyfile(input_file, output_file)
print(f"Copied text file to {project_name}-utf8.txt")
def make_github_repo(self):
headers = {
"Accept": "application/vnd.github.v3+json",
"Content-Type": "application/json",
}
payload = {
"name": f"DP_{self.params['project_name']}",
"description": f"DP PP project \"{self.params['title']}\" ID {self.params['project_id']}",
"private": False,
"has_issues": False,
"has_wiki": False,
"has_downloads": False,
"auto_init": False,
}
auth_data = (
self.auth["github"]["username"],
self.auth["github"]["password"],
)
r = requests.post("https://api.github.com/user/repos",
auth=auth_data, headers=headers,
data=json.dumps(payload))
if r.status_code == 201:
print("Created GitHub repository")
json_response = json.loads(r.text)
self.git_remote_url = json_response["ssh_url"]
else:
print(f"ERROR: GitHub response code {r.status_code} unexpected.")
def unzip_file(self, filename, path):
with ZipFile(filename, "r") as zip_ref:
zip_ref.extractall(path)
os.remove(filename)
def download_text(self):
print("Downloading text from DP ...", end="", flush=True)
zipfile = f"projectID{self.params['project_id']}.zip"
r = requests.get(self.params["text_zip"],
headers={"X-Api-Key": self.auth["pgdp"]["api_key"],
"Accept": "application/zip"})
with open(zipfile, "wb") as file:
file.write(r.content)
self.unzip_file(zipfile, self.project_dir)
print(" done.")
def download_images(self):
print("Downloading images from DP ...", end="", flush=True)
zipfile = f"projectID{self.params['project_id']}images.zip"
r = requests.get(self.params["images_zip"],
headers={"X-Api-Key": self.auth["pgdp"]["api_key"],
"Accept": "application/zip"})
with open(zipfile, "wb") as file:
file.write(r.content)
self.unzip_file(zipfile, f"{self.project_dir}/pngs")
print(" done.")
if __name__ == "__main__":
project = MakeProject()
project.get_params()
project.get_project_info()
project.get_project_file_info()
project.create_directories()
project.make_github_repo()
project.process_template("README.md.j2", "README.md")
project.process_template("checklist.org.j2", "checklist.org")
project.process_template("Makefile.j2", "Makefile")
project.process_template("gitignore.j2", ".gitignore")
project.download_text()
project.download_images()
project.copy_text_file()
project.create_git_repository()