forked from SuperTux/supertux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-addon-index.py
executable file
·141 lines (114 loc) · 4.98 KB
/
build-addon-index.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
#!/usr/bin/env python3
#
# SuperTux
# Copyright (C) 2014 Ingo Ruhnke <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import glob
import hashlib
import os
import subprocess
import sys
import sexpr
def escape_str(string):
return "\"%s\"" % string.replace("\"", "\\\"")
class Addon:
def __init__(self, filename):
lst = sexpr.parse(filename)
if lst[0][0] != "supertux-addoninfo":
raise Exception("not a supertux-addoninfo: %s" % lst[0][0])
else:
for k, v in lst[0][1:]:
if k == "id":
self.id = v
elif k == "version":
self.version = int(v)
elif k == "type":
self.type = v
elif k == "title":
self.title = v
elif k == "author":
self.author = v
elif k == "license":
self.license = v
else:
raise Exception("unknown tag: %s" % k)
self.md5 = ""
self.url = ""
def write(self, fout):
fout.write(" (supertux-addoninfo\n")
fout.write(" (id %s)\n" % escape_str(self.id))
fout.write(" (version %d)\n" % self.version)
fout.write(" (type %s)\n" % escape_str(self.type))
fout.write(" (title %s)\n" % escape_str(self.title))
fout.write(" (author %s)\n" % escape_str(self.author))
fout.write(" (license %s)\n" % escape_str(self.license))
fout.write(" (url %s)\n" % escape_str(self.url))
fout.write(" (md5 %s)\n" % escape_str(self.md5))
fout.write(" )\n")
def process_addon(fout, addon_dir, nfo, base_url, zipdir):
# print addon_dir, nfo
with open(nfo) as fin:
addon = Addon(fin.read())
zipfile = addon.id + "_v" + str(addon.version) + ".zip"
# see http://pivotallabs.com/barriers-deterministic-reproducible-zip-files/
os.remove(os.path.join(zipdir, zipfile))
zipout = os.path.relpath(os.path.join(zipdir, zipfile), addon_dir)
subprocess.call(["zip", "-X", "-r", "--quiet", zipout, "."], cwd=addon_dir)
with open(os.path.join(zipdir, zipfile), 'rb') as fin:
addon.md5 = hashlib.md5(fin.read()).hexdigest()
addon.url = base_url + zipfile
addon.write(fout)
def generate_index(fout, directory, base_url, zipdir):
fout.write(";; automatically generated by build-addon-index.py\n")
fout.write("(supertux-addons\n")
for addon_dir in os.listdir(directory):
addon_dir = os.path.join(directory, addon_dir)
if os.path.isdir(addon_dir):
# print(addon_dir)
nfos = glob.glob(os.path.join(addon_dir, "*.nfo"))
if len(nfos) == 0:
raise Exception(".nfo file missing from %s" % addon_dir)
elif len(nfos) > 1:
raise Exception("to many .nfo files in %s" % addon_dir)
else:
try:
process_addon(fout, addon_dir, nfos[0], base_url, zipdir)
except Exception as e:
sys.stderr.write("%s: ignoring addon because: %s\n" % (addon_dir, e))
fout.write(")\n\n;; EOF ;;\n")
EXAMPLE_TEXT="""Example:
./build-addon-index.py -z ../../addons/repository/ ../../addons/src/"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Addon Index/Zip Generator",
epilog=EXAMPLE_TEXT,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('DIRECTORY', type=str, nargs=1,
help="directory containing the mods")
parser.add_argument('-o', '--output', metavar='FILE', type=str, required=False,
help="output file")
parser.add_argument('-z', '--zipdir', metavar="DIR", type=str, required=True,
help="generate zip files")
parser.add_argument('-u', '--url', metavar='FILE', type=str,
default="https://raw.githubusercontent.com/SuperTux/addons/master/repository/",
help="base url")
args = parser.parse_args()
if args.output is None:
fout = sys.stdout
generate_index(fout, args.DIRECTORY[0], args.url, args.zipdir)
else:
with open(args.output, "w") as fout:
generate_index(fout, args.DIRECTORY[0], args.url, args.zipdir)
# EOF #