4 # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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.
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.
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/>.
31 return "\"%s\"" % str.replace("\"", "\\\"")
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])
41 for k, v in lst[0][1:]:
55 raise Exception("unknown tag: %s" % k)
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))
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())
78 zipfile = addon.id + "_v" + str(addon.version) + ".zip"
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)
85 with open(os.path.join(zipdir, zipfile), 'rb') as fin:
86 addon.md5 = hashlib.md5(fin.read()).hexdigest()
88 addon.url = base_url + zipfile
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):
100 nfos = glob.glob(os.path.join(addon_dir, "*.nfo"))
102 raise Exception(".nfo file missing from %s" % addon_dir)
104 raise Exception("to many .nfo files in %s" % addon_dir)
107 process_addon(fout, addon_dir, nfos[0], base_url, zipdir)
109 sys.stderr.write("%s: ignoring addon because: %s\n" % (addon_dir, e))
110 fout.write(")\n\n;; EOF ;;\n")
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,
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/",
124 args = parser.parse_args()
126 if args.output is None:
128 generate_index(fout, args.DIRECTORY[0], args.url, args.zipdir)
130 with open(args.output, "w") as fout:
131 generate_index(fout, args.DIRECTORY[0], args.url, args.zipdir)