]> dev.renevier.net Git - syj.git/blob - scripts/release.py
allow release if yui-compressor is called yuicompressor
[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     try:
34         subprocess.Popen(['yui-compressor', path], stdout=tmpout).communicate()
35     except OSError:
36         subprocess.Popen(['yuicompressor', path], stdout=tmpout).communicate()
37     tmpout.seek(0)
38     with open(path, 'w') as output:
39         output.write(tmpout.read())
40
41 def genscripts():
42     tmpdir = tempfile.mkdtemp()
43
44     # copy scripts OpenLayers.js
45     for path in glob.glob('public/js/*.js'):
46         if os.path.islink(path):
47             shutil.copy(path, tmpdir)
48         else:
49             # remove "use strict"; directive
50             with open(path) as inp:
51                 dest = pathjoin(tmpdir, os.path.basename(path))
52                 with open(dest, "w") as out:
53                     for line in inp:
54                         sline = line.strip()
55                         if sline != '"use strict"' and sline != '"use strict";':
56                             out.write(line)
57
58     # build OpenLayers.js
59     subprocess.call(['python', 'buildUncompressed.py',
60                      pathjoin(os.getcwd(), "scripts/syj"), pathjoin(tmpdir, "OpenLayers.js")],
61                      cwd = 'public/openlayers/openlayers/build')
62
63     config = ConfigParser.ConfigParser()
64     os.makedirs(pathjoin(__BUILD__, 'public/js'))
65     config.readfp(open('application/configs/medias.ini'))
66     for key, value in config.items('production'):
67         if key.startswith('scripts.'):
68             outpath = pathjoin(__BUILD__, 'public/js/' + key[len('scripts.'):] + '.js')
69             with open(outpath, 'w') as output:
70                 for inpath in map(lambda p: pathjoin(tmpdir, p.strip() + '.js'), value.split(',')):
71                     with open(inpath) as f:
72                         output.write(f.read())
73             compress(outpath)
74     shutil.rmtree(tmpdir)
75
76 def install(source, target):
77     if not source:
78         return
79
80     if os.path.isdir(source):
81         if not target:
82             target = source
83         buildtarget = pathjoin(__BUILD__, target)
84         parentdir = os.path.normpath(pathjoin(buildtarget, '..'))
85         if not os.path.isdir(parentdir):
86             os.makedirs(parentdir)
87         shutil.copytree(source, buildtarget)
88
89     elif os.path.exists(source):
90         if not target:
91             target = os.path.dirname(source)
92         buildtarget = os.path.normpath(pathjoin(__BUILD__, target))
93         if not os.path.isdir(buildtarget):
94             os.makedirs(buildtarget)
95         shutil.copy(source, buildtarget)
96
97     else:
98         for item in glob.glob(source):
99             install(item, target)
100
101 def main():
102     if os.path.isdir(__BUILD__):
103         shutil.rmtree(__BUILD__, False)
104
105     genscripts()
106
107     for path in glob.glob('public/css/*.css'):
108         install(path, None)
109         compress(pathjoin(__BUILD__, 'public/css', os.path.basename(path)))
110
111     with open("scripts/syj.install") as f:
112         for line in f:
113             line = line.split('#')[0].strip()
114             if not line:
115                 continue;
116
117             parts = line.split(' ')
118             if len(parts) > 1:
119                 source = parts[0]
120                 target = parts[1]
121             else:
122                 source = line
123                 target = None
124
125             install(source, target)
126
127     updateversion()
128
129     print "creating syj.tar.gz"
130     targz = tarfile.open("build/syj.tar.gz", "w:gz")
131     for path in glob.glob(pathjoin(__BUILD__, '*')):
132         targz.add(path)
133     targz.close()
134
135 main()