-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
118 lines (106 loc) · 4.63 KB
/
setup.py
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
#!/bin/python3
import subprocess
from pathlib import Path
import re
README_TEMPLATE = """# {repo}
This is a simple collection of mostly
[Creative Commons Non-Commercial (No Derivatives) Licence](https://creativecommons.org/licenses/by-nc-nd/4.0/)d
content used by the [Open Buddhist University](https://www.buddhistuniversity.net/).
For more information about an item's source,
[look up its entry on the website](https://www.buddhistuniversity.net/search/)
or cross reference these files against [our BibTex database](https://buddhistuniversity.net/content.bib).
"""
HOMEPAGE_TEMPLAGE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{repo} @ The Open Buddhist University</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css">
</head>
<body>
<div class="container">
<h1 class="title">The {repo} Content Bucket</h1>
<h2 class="subtitle">@ The Open Buddhist University</h2>
<p>
<a href="https://github.com/{username}/{repo}">https://github.com/{username}/{repo}</a>
</p>
</div>
</body>
</html>
"""
ZENODO_TEMPLATE = """{{
"title": "The Open Buddhist University {repo}",
"keywords": ["buddhism"],
"upload_type": "lesson",
"description": "<p>A collection of free-distribution files for teaching Buddhism and related topics.</p>",
"creators": [{{
"name": "Khemarato Bhikkhu",
"orcid": "0000-0003-4738-7882"
}}],
"access_right": "open",
"license": "cc-by-nc-4.0",
"related_identifiers": [
{{
"relation": "isPartOf",
"identifier": "https://www.buddhistuniversity.net"
}},{{
"relation": "isPreviousVersionOf",
"identifier": "https://github.com/{username}/{repo}"
}},{{
"relation": "isRequiredBy",
"identifier": "https://doi.org/10.5281/zenodo.4448510"
}}
],
"subjects": [{{
"term": "Buddhism",
"identifier": "https://id.loc.gov/authorities/subjects/sh85017454.html",
"scheme": "url"
}}],
"language": "eng"
}}
"""
def get_repo_name() -> tuple[str, str]:
"""Returns the GitHub username and repository name from the current git repository."""
try:
result = subprocess.run(['git', 'remote', '-v'],
capture_output=True,
text=True,
check=True)
remote_url = result.stdout.split('\n')[0]
# Define patterns for different GitHub URL formats
# Handles both HTTPS and SSH URLs:
# https://github.com/username/repo.git
# git@github.com:username/repo.git
patterns = [
r'github\.com[:/]([^/]+)/([^/\.]+)(?:\.git)?',
r'github\.com/([^/]+)/([^/\.]+)(?:\.git)?'
]
for pattern in patterns:
match = re.search(pattern, remote_url)
if match:
username = match.group(1)
repo = match.group(2)
return username, repo
raise ValueError("Could not parse GitHub URL from git remote output")
except subprocess.CalledProcessError as e:
raise Exception("git command failed with error code: " + str(e.returncode) + "\n---\n" + e.stderr + "\n---\n\n(Not a git repository?)")
if __name__ == "__main__":
username, repo = get_repo_name()
print(f"GitHub Username: {username}")
print(f"Repository Name: {repo}")
print("Setting up GitHub Pages...")
# Have to do this as two requests due to this bug: https://github.com/orgs/community/discussions/144171
subprocess.run(["gh", "api", "--method", "POST", "-H", "Accept: application/vnd.github+json", "-H", "X-GitHub-Api-Version: 2022-11-28", "/repos/{username}/{repo}/pages".format(username=username, repo=repo), "-f", 'source[branch]=main', "-f", "build_type=workflow"], check=True)
subprocess.run(["gh", "api", "--method", "PUT", "-H", "Accept: application/vnd.github+json", "-H", "X-GitHub-Api-Version: 2022-11-28", "/repos/{username}/{repo}/pages".format(username=username, repo=repo), "-f", 'source[branch]=main', "-f", "source[path]=/", "-f", "build_type=legacy"], check=True)
print("Writing files...")
Path(".nojekyll").touch()
Path("index.html").write_text(HOMEPAGE_TEMPLAGE.format(repo=repo, username=username))
Path("README.md").write_text(README_TEMPLATE.format(repo=repo))
Path(".zenodo.json").write_text(ZENODO_TEMPLATE.format(repo=repo, username=username))
print("Committing files to GitHub...")
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "rm", "-f", "setup.py"], check=True) # self-destruct
subprocess.run(["git", "commit", "-m", "Initial (automated) commit"], check=True)
subprocess.run(["git", "push", "origin", "main"], check=True)
print("Done! This script will now self-destruct :)")