generated from ScoopInstaller/BucketTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatescript.py
173 lines (154 loc) · 6.12 KB
/
updatescript.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
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
import requests, json, hashlib, os, random
def git_project_latest_commit_hash_check(
provider: str,
name_and_repo: str = "",
project_id: str = "",
) -> str:
# print(provider)
# print(name_and_repo)
# print(project_id)
# print()
"""
This checks a git project and returns the latest commit hash.
the name_and_repo is for the name and the repo.
the provider is a git hosting provider, it currently supports github, and gitlab
if the provider is gitlab, you need to give the project_id.
"""
provider = provider.strip().lower()
if provider == "github" and name_and_repo != "":
return requests.get(
f"https://api.github.com/repos/{name_and_repo}/commits"
).json()[0]["sha"]
elif provider == "github" and name_and_repo == "":
raise Exception("provider was github but name_and_repo was not provided")
elif provider == "gitlab" and project_id != "":
return requests.get(
f"https://gitlab.com/api/v4/projects/{project_id}/repository/commits"
).json()[0]["id"]
elif provider == "gitlab" and project_id == "":
raise Exception("provider was gitlab but a project_id was not provided")
else:
raise NotImplementedError(f"this function doesn't support {provider}")
def read_project_commit_hash(package_name: str) -> tuple:
"""
this reads the manifest and return the commit hash and zip file's hash.
package_name should be the manifest name. it should not contain the the .json extension or bucket/
it returns a tuple.
the first item from the tuple is the version, aka, the commit hash.
"""
with open(f"bucket/{package_name}-git.json", "r") as manifest:
data = json.load(manifest)
return data["version"]
def update_decoy_and_get_hash(package_name: str) -> str:
with open(f"decoy/{package_name}-decoy", "w") as decoy:
decoy.write(
f"This is a decoy file\nIt should not be touched\nRandom bytes to register a scoop update:\n{random.randbytes(100)}\n"
)
os.system(f'git commit decoy/{package_name}-decoy -m "{package_name}-decoy update"')
os.system("git push")
# chatgpt generated code structure starts here
hash_obj = hashlib.new("sha256")
with requests.get(
f"https://raw.githubusercontent.com/ingenarel/ingenarel-scoop-bucket/refs/heads/master/decoy/{package_name}-decoy",
stream=True,
) as response:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
hash_obj.update(chunk)
return hash_obj.hexdigest()
# chatgpt generated code structure ends here
def update_project(package_name: str, commit_hash: str) -> None:
file_hash = update_decoy_and_get_hash(package_name)
with open(f"bucket/{package_name}-git.json", "r") as manifest:
data = json.load(manifest)
data["version"] = commit_hash
data["hash"] = file_hash
with open(f"bucket/{package_name}-git.json", "w") as manifest:
manifest.write(json.dumps(data, indent=4))
print(f"{package_name} commit hash changed to {commit_hash}")
with open(f"bucket/{package_name}-git-ssh.json", "r") as manifest:
data = json.load(manifest)
data["version"] = commit_hash
data["hash"] = file_hash
with open(f"bucket/{package_name}-git-ssh.json", "w") as manifest:
manifest.write(json.dumps(data, indent=4))
os.system(
f"git add bucket/{package_name}-git.json bucket/{package_name}-git-ssh.json"
)
os.system(f'git commit -m "{package_name} updated to {commit_hash}"')
def git_project_check_and_fix(list_of_git_projects) -> None:
for provider_name in list_of_git_projects:
# print(provider_name)
for projectdata in list_of_git_projects[provider_name]:
# print(projectdata)
if provider_name == "github":
git_project = projectdata["name_and_repo"]
elif provider_name == "gitlab":
git_project = projectdata["projectid"]
else:
raise NotImplementedError(
f'given a git provider "{provider_name}" that isn\'t supported'
)
# print(git_project)
package = projectdata["package_name"]
last_commit_hash = git_project_latest_commit_hash_check(
provider=provider_name,
name_and_repo=git_project,
project_id=git_project,
)
# print(f"{provider_name} | {git_project} commit hash: {last_commit_hash}")
manifest_commit_hash = read_project_commit_hash(package)
if last_commit_hash != manifest_commit_hash:
update_project(package, last_commit_hash)
# print(projectname)
def main():
list_of_git_projects = {
"github": (
{
"name_and_repo": "SpacingBat3/WebCord",
"package_name": "webcord",
},
{
"name_and_repo": "ayn2op/discordo",
"package_name": "discordo",
},
{
"name_and_repo": "jesseduffield/lazygit",
"package_name": "lazygit",
},
{
"name_and_repo": "ajeetdsouza/zoxide",
"package_name": "zoxide",
},
{
"name_and_repo": "junegunn/fzf",
"package_name": "fzf",
},
{
"name_and_repo": "cli/cli",
"package_name": "gh",
},
{
"name_and_repo": "BurntSushi/ripgrep",
"package_name": "ripgrep",
},
{
"name_and_repo": "Vencord/Vesktop",
"package_name": "vesktop",
},
),
"gitlab": (
{
"projectid": "20825969",
"package_name": "sheepit",
},
{
"projectid": "34675721",
"package_name": "glab",
},
),
}
git_project_check_and_fix(list_of_git_projects=list_of_git_projects)
if __name__ == "__main__":
main()
# change_decoy()