Added script to generate index for addon repository
[supertux.git] / tools / build-addon-index.py
diff --git a/tools/build-addon-index.py b/tools/build-addon-index.py
new file mode 100755 (executable)
index 0000000..0159ab9
--- /dev/null
@@ -0,0 +1,95 @@
+#!/usr/bin/env python
+
+# SuperTux
+# Copyright (C) 2006 Matthias Braun <matze@braunis.de>
+#
+# 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 sexpr
+import sys
+import os
+import glob
+import hashlib
+
+def escape_str(str):
+    return "\"%s\"" % str.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:
+            tags = {}
+            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("    (http-url %s)\n" % escape_str(self.url))
+        fout.write("    (md5 %s)\n" % escape_str(self.md5))
+        fout.write("   )\n")
+  
+def process_addon(addon_dir, nfo, md5, url):
+    # print addon_dir, nfo
+    with open(nfo) as fin:
+        addon = Addon(fin.read())
+        addon.md5 = md5
+        addon.url = url
+        addon.write(sys.stdout)
+
+sys.stdout.write(";; automatically generated by build-addon-index.py\n")
+sys.stdout.write("(supertux-addons\n")
+for directory in sys.argv[1:]:
+    for addon_dir in os.listdir(directory):
+        zipfile = os.path.normpath(os.path.join(directory, "../repository/", addon_dir + ".zip"))
+        with open(zipfile, 'rb') as fin:
+            md5 = hashlib.md5(fin.read()).hexdigest()
+        url = "http://localhost:8000/repository/%s" % (addon_dir + ".zip")
+        nfos = glob.glob(os.path.join(directory, addon_dir, "*.nfo"))
+        if len(nfos) == 0:
+            raise Exception(".nfo file missing")
+        elif len(nfos) > 1:
+            raise Exception("to many .nfo files")
+        else:
+            try:
+                process_addon(os.path.join(directory, addon_dir), nfos[0], md5, url)
+            except Exception, e:
+                sys.stderr.write("%s: ignoring addon because: %s\n" % (addon_dir, e))
+sys.stdout.write(")\n\n;; EOF ;;\n")
+
+# EOF #