Added script to generate index for addon repository
[supertux.git] / tools / build-addon-index.py
1 #!/usr/bin/env python
2
3 # SuperTux
4 # Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 sexpr
21 import sys
22 import os
23 import glob
24 import hashlib
25
26 def escape_str(str):
27     return "\"%s\"" % str.replace("\"", "\\\"")
28
29 class Addon:
30     def __init__(self, filename):
31         lst = sexpr.parse(filename)
32         if lst[0][0] != "supertux-addoninfo":
33             raise Exception("not a supertux-addoninfo: %s" % lst[0][0])
34         else:
35             tags = {}
36             for k, v in lst[0][1:]:
37                 if k == "id":
38                     self.id = v
39                 elif k == "version":
40                     self.version = int(v)
41                 elif k == "type":
42                     self.type = v
43                 elif k == "title":
44                     self.title = v
45                 elif k == "author":
46                     self.author = v
47                 elif k == "license":
48                     self.license = v
49                 else:
50                     raise Exception("unknown tag: %s" % k)
51
52             self.md5 = ""
53             self.url = ""
54                     
55     def write(self, fout):
56         fout.write("  (supertux-addoninfo\n")
57         fout.write("    (id %s)\n" % escape_str(self.id))
58         fout.write("    (version %d)\n" % self.version)
59         fout.write("    (type %s)\n" % escape_str(self.type))
60         fout.write("    (title %s)\n" % escape_str(self.title))
61         fout.write("    (author %s)\n" % escape_str(self.author))
62         fout.write("    (license %s)\n" % escape_str(self.license))
63         fout.write("    (http-url %s)\n" % escape_str(self.url))
64         fout.write("    (md5 %s)\n" % escape_str(self.md5))
65         fout.write("   )\n")
66   
67 def process_addon(addon_dir, nfo, md5, url):
68     # print addon_dir, nfo
69     with open(nfo) as fin:
70         addon = Addon(fin.read())
71         addon.md5 = md5
72         addon.url = url
73         addon.write(sys.stdout)
74
75 sys.stdout.write(";; automatically generated by build-addon-index.py\n")
76 sys.stdout.write("(supertux-addons\n")
77 for directory in sys.argv[1:]:
78     for addon_dir in os.listdir(directory):
79         zipfile = os.path.normpath(os.path.join(directory, "../repository/", addon_dir + ".zip"))
80         with open(zipfile, 'rb') as fin:
81             md5 = hashlib.md5(fin.read()).hexdigest()
82         url = "http://localhost:8000/repository/%s" % (addon_dir + ".zip")
83         nfos = glob.glob(os.path.join(directory, addon_dir, "*.nfo"))
84         if len(nfos) == 0:
85             raise Exception(".nfo file missing")
86         elif len(nfos) > 1:
87             raise Exception("to many .nfo files")
88         else:
89             try:
90                 process_addon(os.path.join(directory, addon_dir), nfos[0], md5, url)
91             except Exception, e:
92                 sys.stderr.write("%s: ignoring addon because: %s\n" % (addon_dir, e))
93 sys.stdout.write(")\n\n;; EOF ;;\n")
94
95 # EOF #