Updated addon repository URL and improved debug output on download
[supertux.git] / tools / build-addon-index.py
1 #!/usr/bin/env python
2 #
3 # SuperTux
4 # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 import argparse
21 import glob
22 import hashlib
23 import os
24 import subprocess
25 import sys
26
27 import sexpr
28
29
30 def escape_str(str):
31     return "\"%s\"" % str.replace("\"", "\\\"")
32
33
34 class Addon:
35     def __init__(self, filename):
36         lst = sexpr.parse(filename)
37         if lst[0][0] != "supertux-addoninfo":
38             raise Exception("not a supertux-addoninfo: %s" % lst[0][0])
39         else:
40             tags = {}
41             for k, v in lst[0][1:]:
42                 if k == "id":
43                     self.id = v
44                 elif k == "version":
45                     self.version = int(v)
46                 elif k == "type":
47                     self.type = v
48                 elif k == "title":
49                     self.title = v
50                 elif k == "author":
51                     self.author = v
52                 elif k == "license":
53                     self.license = v
54                 else:
55                     raise Exception("unknown tag: %s" % k)
56
57             self.md5 = ""
58             self.url = ""
59                     
60     def write(self, fout):
61         fout.write("  (supertux-addoninfo\n")
62         fout.write("    (id %s)\n" % escape_str(self.id))
63         fout.write("    (version %d)\n" % self.version)
64         fout.write("    (type %s)\n" % escape_str(self.type))
65         fout.write("    (title %s)\n" % escape_str(self.title))
66         fout.write("    (author %s)\n" % escape_str(self.author))
67         fout.write("    (license %s)\n" % escape_str(self.license))
68         fout.write("    (url %s)\n" % escape_str(self.url))
69         fout.write("    (md5 %s)\n" % escape_str(self.md5))
70         fout.write("   )\n")
71
72   
73 def process_addon(fout, addon_dir, nfo, base_url, zipdir):
74     # print addon_dir, nfo
75     with open(nfo) as fin:
76         addon = Addon(fin.read())
77
78     zipfile = addon.id + "_v" + str(addon.version) + ".zip"
79
80     # see http://pivotallabs.com/barriers-deterministic-reproducible-zip-files/
81     os.remove(os.path.join(zipdir, zipfile))
82     zipout = os.path.relpath(os.path.join(zipdir, zipfile), addon_dir)
83     subprocess.call(["zip", "-X", "-r", "--quiet", zipout, "."], cwd=addon_dir)
84
85     with open(os.path.join(zipdir, zipfile), 'rb') as fin:
86         addon.md5 = hashlib.md5(fin.read()).hexdigest()
87
88     addon.url = base_url + zipfile
89
90     addon.write(fout)
91
92
93 def generate_index(fout, directory, base_url, zipdir):
94     fout.write(";; automatically generated by build-addon-index.py\n")
95     fout.write("(supertux-addons\n")
96     for addon_dir in os.listdir(directory):
97         addon_dir = os.path.join(directory, addon_dir)
98         if os.path.isdir(addon_dir):
99             print addon_dir
100             nfos = glob.glob(os.path.join(addon_dir, "*.nfo"))
101             if len(nfos) == 0:
102                 raise Exception(".nfo file missing from %s" % addon_dir)
103             elif len(nfos) > 1:
104                 raise Exception("to many .nfo files in %s" % addon_dir)
105             else:
106                 try:
107                     process_addon(fout, addon_dir, nfos[0], base_url, zipdir)
108                 except Exception, e:
109                     sys.stderr.write("%s: ignoring addon because: %s\n" % (addon_dir, e))
110     fout.write(")\n\n;; EOF ;;\n")
111
112
113 if __name__ == "__main__":
114     parser = argparse.ArgumentParser(description='Addon Index/Zip Generator')
115     parser.add_argument('DIRECTORY',  type=str, nargs=1,
116                         help="directory containing the mods")
117     parser.add_argument('-o', '--output', metavar='FILE', type=str, required=False,
118                         help="output file")
119     parser.add_argument('-z', '--zipdir', metavar="DIR", type=str, required=True,
120                         help="generate zip files")
121     parser.add_argument('-u', '--url', metavar='FILE', type=str,
122                         default="http://addons.supertux.googlecode.com/git/repository/",
123                         help="base url")
124     args = parser.parse_args()
125
126     if args.output is None:
127         fout = sys.stdout
128         generate_index(fout, args.DIRECTORY[0], args.url, args.zipdir)
129     else:
130         with open(args.output, "w") as fout:
131             generate_index(fout, args.DIRECTORY[0], args.url, args.zipdir)
132
133 # EOF #