-
Notifications
You must be signed in to change notification settings - Fork 12
/
repogen.py
169 lines (139 loc) · 6 KB
/
repogen.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
#!/usr/bin/python
import os, json, zipfile, time, datetime, hashlib, re
print("Content-type: text/html\n\n")
SCREEN_REGEX = re.compile("^screen[1-9].png$")
def zipdir(path, ziph):
for root, dirs, files in os.walk(path):
for file in files:
if root == "." and (file == "icon.png" or file == "screen.png" or file == ".deletetoupdate"):
continue
ziph.write(os.path.join(root, file))
try:
os.mkdir("zips")
except:
pass
curdir = os.getcwd()
packages = {}
packages["packages"] = []
for package in os.listdir("packages"):
if os.path.isfile("packages/" + package) or (len(package) > 0 and package[0] == "."):
continue
# if the .deletetoupdate file exists, don't compress
# also if the zip doesn't exist already
skipRebuild = os.path.exists(curdir + "/packages/%s/.deletetoupdate" % package) and os.path.exists(curdir + "/zips/%s.zip" % package)
if not skipRebuild:
zipf = zipfile.ZipFile("zips/" + package + ".zip", 'w', zipfile.ZIP_DEFLATED)
os.chdir(curdir + "/packages/" + package)
screen_count = 0
if not skipRebuild:
# generate a manifest to go inside of this zip
# pull in any existing manifest and only write U entries
# omitted files
existing = {}
try:
manifest_file = open("manifest.install", "r")
for line in manifest_file:
mode, name = line.split(": ")
if mode == "U":
continue
existing[name.strip()] = line
manifest_file.close()
except:
pass
manifest = ""
for root, dirs, files in os.walk("."):
for file in files:
if root == "." and (file == "manifest.install" or file == "icon.png" or file == "info.json" or file == "screen.png" or file == ".deletetoupdate" or SCREEN_REGEX.match(file)):
continue
relPath = os.path.join(root, file)[2:]
relPath.replace("\\", "/")
if relPath in existing:
manifest += existing[relPath]
else:
manifest += "U: %s\n" % relPath
manifest_file = open("manifest.install", "w")
manifest_file.write(manifest)
manifest_file.close()
print("Zipping %s...<br>" % package)
print(".speak #hbas-updates WIIU : https://apps.fortheusers.org/switch/%s <br>" % package)
zipdir(".", zipf)
zipf.close()
cached_info = {}
needsCaching = True
try:
del_to_update_file = open(".deletetoupdate", "r")
cached_info = json.load(del_to_update_file)
del_to_update_file.close()
filesize = cached_info["filesize"]
folder_size = cached_info["extracted"]
binary = cached_info["binary"]
updated = cached_info["updated"]
mdhex = cached_info["md5"]
screen_count = cached_info["screens"]
needsCaching = False
except:
# data in .deletetoupdate wasn't useful, let's calculate that info now
# Detail zip package size in KB's
filesize = os.path.getsize(curdir + "/zips/" + package + ".zip")/1024
# Detail extracted directory size in KB's
folder_size = 0
for (root, dirs, files) in os.walk('.'):
for file in files:
fname = os.path.join(root, file)
folder_size += os.path.getsize(fname)/1024
# Include the nro name in json if exists
binary = "none"
for (root, dirs, files) in os.walk('.'):
for file in files:
if file.endswith(".nro"):
binary = (root + "/" + file)[1:]
if SCREEN_REGEX.match(file):
screen_count += 1
# Date last updated (assumption is that if the app is updated the info.json would be)
updated = time.strftime('%d/%m/%Y', time.gmtime(os.path.getmtime(curdir + "/packages/" + package + "/info.json")))
#md5 of package zip
filehash = hashlib.md5()
filehash.update(open(curdir + "/zips/" + package + ".zip").read())
mdhex = filehash.hexdigest()
cached_info = { "filesize": filesize, "extracted": folder_size, "binary": binary, "updated": updated, "md5": mdhex, "screens": screen_count}
# this line isn't confusing at all (additional info makes it less so)
packages["packages"].append({"name": package, "filesize": filesize, "updated": updated, "extracted": folder_size, "binary": binary, "md5": mdhex, "screens": screen_count})
# if a info.json file exists, load properties from it
if os.path.exists("info.json"):
target = packages["packages"][-1]
props = json.load(open("info.json", "r"))
vals = ["title", "author", "category", "version", "description", "details", "url", "license", "changelog"]
for val in vals:
if val in props and props[val]:
target[val] = props[val]
else:
target[val] = "n/a"
if needsCaching: # no deletetoupdate loaded, we need to make one
del_to_update = open(".deletetoupdate", 'w')
json.dump(cached_info, del_to_update, sort_keys=True, indent=4, separators=(',', ': '))
del_to_update.close()
os.chdir(curdir)
# do download counts for app and web for all packages
wiiu = False
targets = ["app", "web"]
root = "../"
if wiiu:
targets = ["wiiu"]
root = ""
try:
for target in targets:
# open all stats from the cron job'd stats json file for this target
statsfile = open("%s../history/logs/%s/output.json" % (root, target), "r")
stats = json.load(statsfile)
statsfile.close()
for package in packages["packages"]:
if package["name"] in stats:
if wiiu:
target = "all"
package["%s_dls" % target] = stats[package["name"]]
except:
print("Encountered an issue getting stats<br>")
out = open("repo.json", "w")
json.dump(packages, out, indent=4)
out.close()
print("All Done!<br>")