]> dev.renevier.net Git - syj.git/blob - scripts/release.py
allow opening terms of use box with space keyboard
[syj.git] / scripts / release.py
1 #!/usr/bin/python
2
3 __BUILD__="build"
4
5 import shutil, os, sys, subprocess, tempfile, tarfile, glob, ConfigParser
6 pathjoin = os.path.join
7
8 def updateversion():
9     try:
10         version = None
11         import git
12         repo = git.Repo('.')
13         master = repo.commits()[0]
14         tag = (filter(lambda tag: tag.commit.id == master.id, repo.tags) or [""])[0]
15         if tag:
16             version = tag.name
17     except ImportError:
18         version = subprocess.Popen(['git', 'tag', '-l', '--contains', 'master'], stdout=subprocess.PIPE).communicate()[0][:-1]
19     if not version:
20         raise AssertionError, "master is not tagged"
21
22     fd, fname = tempfile.mkstemp()
23     f = os.fdopen(fd, 'w')
24     versionfile = 'build/application/Version.php'
25     with open('build/application/Version.php') as source:
26         for line in source:
27             f.write(line.replace('$SYJVERSION$', version))
28     f.close()
29     shutil.move(fname, versionfile)
30
31 def compress(path):
32     tmpout = tempfile.TemporaryFile()
33     subprocess.Popen(['yui-compressor', path], stdout=tmpout).communicate()
34     tmpout.seek(0)
35     with open(path, 'w') as output:
36         output.write(tmpout.read())
37
38 def genscripts():
39     tmpdir = tempfile.mkdtemp()
40
41     # copy scripts OpenLayers.js
42     for path in glob.glob('public/js/*.js'):
43         if os.path.islink(path):
44             shutil.copy(path, tmpdir)
45         else:
46             # remove "use strict"; directive
47             with open(path) as inp:
48                 dest = pathjoin(tmpdir, os.path.basename(path))
49                 with open(dest, "w") as out:
50                     for line in inp:
51                         sline = line.strip()
52                         if sline != '"use strict"' and sline != '"use strict";':
53                             out.write(line)
54
55     # build OpenLayers.js
56     subprocess.call(['python', 'buildUncompressed.py',
57                      pathjoin(os.getcwd(), "scripts/syj"), pathjoin(tmpdir, "OpenLayers.js")],
58                      cwd = 'public/openlayers/openlayers/build')
59
60     config = ConfigParser.ConfigParser()
61     os.makedirs(pathjoin(__BUILD__, 'public/js'))
62     config.readfp(open('application/configs/medias.ini'))
63     for key, value in config.items('production'):
64         if key.startswith('scripts.'):
65             outpath = pathjoin(__BUILD__, 'public/js/' + key[len('scripts.'):] + '.js')
66             with open(outpath, 'w') as output:
67                 for inpath in map(lambda p: pathjoin(tmpdir, p.strip() + '.js'), value.split(',')):
68                     with open(inpath) as f:
69                         output.write(f.read())
70             compress(outpath)
71     shutil.rmtree(tmpdir)
72
73 def install(source, target):
74     if not source:
75         return
76
77     if os.path.isdir(source):
78         if not target:
79             target = source
80         buildtarget = pathjoin(__BUILD__, target)
81         parentdir = os.path.normpath(pathjoin(buildtarget, '..'))
82         if not os.path.isdir(parentdir):
83             os.makedirs(parentdir)
84         shutil.copytree(source, buildtarget)
85
86     elif os.path.exists(source):
87         if not target:
88             target = os.path.dirname(source)
89         buildtarget = os.path.normpath(pathjoin(__BUILD__, target))
90         if not os.path.isdir(buildtarget):
91             os.makedirs(buildtarget)
92         shutil.copy(source, buildtarget)
93
94     else:
95         for item in glob.glob(source):
96             install(item, target)
97
98 def main():
99     if os.path.isdir(__BUILD__):
100         shutil.rmtree(__BUILD__, False)
101
102     genscripts()
103
104     for path in glob.glob('public/css/*.css'):
105         install(path, None)
106         compress(pathjoin(__BUILD__, 'public/css', os.path.basename(path)))
107
108     with open("scripts/syj.install") as f:
109         for line in f:
110             line = line.split('#')[0].strip()
111             if not line:
112                 continue;
113
114             parts = line.split(' ')
115             if len(parts) > 1:
116                 source = parts[0]
117                 target = parts[1]
118             else:
119                 source = line
120                 target = None
121
122             install(source, target)
123
124     updateversion()
125
126     print "creating syj.tar.gz"
127     targz = tarfile.open("build/syj.tar.gz", "w:gz")
128     for path in glob.glob(pathjoin(__BUILD__, '*')):
129         targz.add(path)
130     targz.close()
131
132 main()