-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdater.py
More file actions
103 lines (89 loc) · 3.34 KB
/
Copy pathupdater.py
File metadata and controls
103 lines (89 loc) · 3.34 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
"""
Updater script for ACOS.
"""
import json
import requests
import os
import zipfile
import shutil
def download_repo(URL:str, folder:str, files_to_delete:(list, tuple)=(".gitattributes", "README.md"), log:bool=True, notify_function=print, *args, **kwargs):
"""
Downloads the specified repository into the specified folder.
:param URL: The URL of the repository main archive.
:param folder: The folder to place the repository in.
:param files_to_delete: The files to delete after downloading the repository. DEFAULT : '.gitattributes', 'README.md'
:param log: Log the informations in the console. DEFAULT : True
:param notify_function: The function that will get the log as argument. DEFAULT : print()
"""
if log: notify_function("Downloading... This might take a while.", *args, **kwargs)
r = requests.get(URL)
assert r.status_code == 200, "Something happened.\nStatus code : " + str(r.status_code)
if folder == "":
folder = "_"
if log: notify_function("Writing the zip...", *args, **kwargs)
# Writing the zip
with open(f"{folder}.zip", "wb") as code:
code.write(r.content)
code.close()
# Creating a folder for the zip content
if not os.path.exists(folder):
os.mkdir(folder)
if log: notify_function("Extracting...", *args, **kwargs)
# Extracting the zip
with zipfile.ZipFile(f"{folder}.zip", "r") as zip_ref:
zip_ref.extractall(folder)
if log: notify_function("Moving the files...", *args, **kwargs)
suffix = URL.split("/")[-1].replace(".zip", "", 1)
repo_name = URL.split("/")[4]
# Moving the file to parent
for filename in os.listdir(os.path.join(folder, f'{repo_name}-{suffix}')):
shutil.move(os.path.join(folder, f'{repo_name}-{suffix}', filename), os.path.join(folder, filename))
# Deleting unnecessary files
shutil.rmtree(f"{folder}/{repo_name}-{suffix}")
os.remove(f"{folder}.zip")
for file in files_to_delete:
try:
os.remove(f"{folder}/{file}")
except FileNotFoundError:
pass
if log: notify_function("Download complete !", *args, **kwargs)
def get_zip_link(username:str, repository:str, branch:str="main"):
"""
Gets the link to the main zip of a repository.
:param username: The username of the repository owner.
:param repository: The repository name.
:param branch: The branch to download. Default : 'main'.
:return: The link to the zip.
"""
return f"https://github.com/{username}/{repository}/archive/refs/heads/{branch}.zip"
def move_files(source_dir:str):
for filename in os.listdir("_"):
try: # Folder
if "ROOT" in filename:
continue
shutil.rmtree(filename)
except NotADirectoryError: # File
os.remove(filename)
except PermissionError:
continue
if filename is not "registry.json":
shutil.move(os.path.join(source_dir, filename), filename)
else:
f = open("registry.json", "r")
registry = json.load(f)
f.close()
f = open("_/registry.json", "r")
new_registry = json.load(f)
f.close()
for key in new_registry:
if key not in registry:
registry[key] = new_registry[key]
f = open("registry.json", "w")
json.dump(registry, f, indent=4)
f.close()
if __name__ == "__main__":
download_repo(get_zip_link("megat69", "ACOS"), "", (".gitattributes", "README.md", "LICENSE"))
print("Replacing files...")
move_files("_/")
shutil.rmtree("_/")
print("Update applied done !")