Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Python: align style with PEP-008
[simgrid.git] / tools / internal / generate-multi-jar.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2013-2014. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 """
10 Tool to generate the simgrid.jar
11 """
12 import urllib2
13 import zipfile
14 import tempfile
15 import os
16 import sys
17 import re
18
19 dists = ['https://ci.inria.fr/simgrid/job/SimGrid-Multi/build_mode=Debug,label=%s/lastSuccessfulBuild/artifact/build/SimGrid-3.11/simgrid.jar' % dist
20          for dist in ["small-debian-32", "small-debian-64", "small-freebsd-64-clang"]]
21 dists.append(
22     'https://ci.inria.fr/simgrid/job/Simgrid-Mult-Win7/build_mode=Debug,label=windows-64/lastSuccessfulBuild/artifact/build/simgrid.jar')
23
24
25 class SimJar(object):
26
27     def __init__(self, filename='simgrid.jar'):
28         self.zipfile = zipfile.ZipFile(filename, 'w')
29         self.done = set()
30         self.git_version = None
31
32     def addJar(self, filename):
33         with zipfile.ZipFile(filename) as zf:
34             platform = None
35             arch = None
36             git_version = None
37             for z in zf.infolist():
38                 path = filter(None, z.filename.split('/'))
39                 if len(path) == 3 and path[0] == 'NATIVE':
40                     platform, arch = path[1:3]
41                 elif z.filename == 'META-INF/MANIFEST.MF':
42                     zf.read('META-INF/MANIFEST.MF')
43                     git_version = re.findall(
44                         r"Implementation-Version: \"(.*?)\"", zf.read('META-INF/MANIFEST.MF'))
45
46             assert platform is not None and git_version is not None, "Jar file not valid (%s, %s)" % (
47                 platform, git_version)
48             print "Adding: %s %s" % (platform, arch)
49             if self.git_version is None:
50                 self.git_version = git_version
51             elif self.git_version != git_version:
52                 print "WARNING: Assembling jar of various commits (%s vs %s)" % (self.git_version, git_version)
53
54             for info in zf.infolist():
55                 if info.filename not in self.done:
56                     self.done.add(info.filename)
57                     self.zipfile.writestr(info, zf.read(info.filename))
58
59     def addByUrl(self, url):
60         data = urllib2.urlopen(url)
61         f = tempfile.NamedTemporaryFile(delete=False)
62         f.write(data.read())
63         f.close()
64         self.addJar(f.name)
65         os.unlink(f.name)
66
67     def close(self):
68         self.zipfile.close()
69
70 if __name__ == "__main__":
71     jar = SimJar()
72     for dist in dists:
73         jar.addByUrl(dist)
74     for a in sys.argv[1:]:
75         jar.addJar(a)
76     jar.close()