-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprebuild.py
More file actions
77 lines (55 loc) · 2.29 KB
/
Copy pathprebuild.py
File metadata and controls
77 lines (55 loc) · 2.29 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
import requests
import os
import argparse
import shutil
GITHUB_REPO = 'https://github.com/informatics-sa/WebsiteBuilder'
parser = argparse.ArgumentParser()
parser.add_argument("--path", required=False)
args = parser.parse_args()
from_github = False
if args.path is None:
print("Collecting from github")
from_github = True
else:
print(f"Path: {args.path}")
def repo_to_api(github_repo: str) -> str:
# Convert https://github.com/owner/repo → https://api.github.com/repos/owner/repo
parts = github_repo.rstrip('/').split('github.com/')[-1]
return f"https://api.github.com/repos/{parts}"
def list_files(github_repo: str, path: str = "") -> list[dict]:
"""Recursively list all files in the repo, returns list of {path, download_url}."""
api_url = f"{repo_to_api(github_repo)}/contents/{path}"
response = requests.get(api_url)
response.raise_for_status()
files = []
for item in response.json():
if item['type'] == 'file':
files.append({'path': item['path'], 'download_url': item['download_url']})
elif item['type'] == 'dir':
files.extend(list_files(github_repo, item['path']))
return files
def download_file(file: dict, dest_dir: str) -> None:
"""Download a single file and save it relative to dest_dir."""
response = requests.get(file['download_url'])
response.raise_for_status()
dest_path = os.path.join(dest_dir, file['path'])
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
with open(dest_path, 'wb') as f:
f.write(response.content)
print(f"[COPY] {file['path']}")
def main():
dest_dir = os.path.dirname(os.path.abspath(__file__))
print(f"[INFO] Destination: {dest_dir}")
if from_github:
print(f"[INFO] Listing files from {GITHUB_REPO} ...")
files = list_files(GITHUB_REPO)
print(f"[INFO] Found {len(files)} file(s)\n")
for f in files:
if os.path.basename(f["path"]).lower() == "readme.md": continue
download_file(f, dest_dir)
else:
source_dir = args.path
print(f"[INFO] Listing files from {source_dir} ...")
shutil.copytree(source_dir, dest_dir, dirs_exist_ok=True, ignore=shutil.ignore_patterns(".git", "README.md")) # copy from source_dir to dest_dir
print(f"\n[DONE]")
main()