]> dev.renevier.net Git - syj.git/blob - scripts/release.py
make release.py script simpler
[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 compress(path):
9     tmpout = tempfile.TemporaryFile()
10     subprocess.Popen(['yui-compressor', path], stdout=tmpout).communicate()
11     tmpout.seek(0)
12     with open(path, 'w') as output:
13         output.write(tmpout.read())
14
15 def genscripts():
16     tmpdir = tempfile.mkdtemp()
17
18     # copy scripts OpenLayers.js
19     for path in glob.glob('public/js/*.js'):
20         shutil.copy(path, tmpdir)
21
22     # build OpenLayers.js
23     subprocess.call(['python', 'buildUncompressed.py',
24                      pathjoin(os.getcwd(), "scripts/syj"), pathjoin(tmpdir, "OpenLayers.js")],
25                      cwd = 'public/openlayers/openlayers/build')
26
27     config = ConfigParser.ConfigParser()
28     os.makedirs(pathjoin(__BUILD__, 'public/js'))
29     config.readfp(open('application/configs/medias.ini'))
30     for key, value in config.items('production'):
31         if key.startswith('scripts.'):
32             outpath = pathjoin(__BUILD__, 'public/js/' + key[len('scripts.'):] + '.js')
33             with open(outpath, 'w') as output:
34                 for inpath in map(lambda p: pathjoin(tmpdir, p.strip() + '.js'), value.split(',')):
35                     with open(inpath) as f:
36                         output.write(f.read())
37             compress(outpath)
38     shutil.rmtree(tmpdir)
39
40 def install(source, target):
41     if not source:
42         return
43
44     if os.path.isdir(source):
45         if not target:
46             target = source
47         buildtarget = pathjoin(__BUILD__, target)
48         parentdir = os.path.normpath(pathjoin(buildtarget, '..'))
49         if not os.path.isdir(parentdir):
50             os.makedirs(parentdir)
51         shutil.copytree(source, buildtarget)
52
53     elif os.path.exists(source):
54         if not target:
55             target = os.path.dirname(source)
56         buildtarget = os.path.normpath(pathjoin(__BUILD__, target))
57         if not os.path.isdir(buildtarget):
58             os.makedirs(buildtarget)
59         shutil.copy(source, buildtarget)
60
61     else:
62         for item in glob.glob(source):
63             install(item, target)
64
65 def main():
66     if os.path.isdir(__BUILD__):
67         shutil.rmtree(__BUILD__, False)
68
69     genscripts()
70
71     for path in glob.glob('public/css/*.css'):
72         install(path, None)
73         compress(pathjoin(__BUILD__, 'public/css', os.path.basename(path)))
74
75     with open("scripts/syj.install") as f:
76         for line in f:
77             line = line.split('#')[0].strip()
78             if not line:
79                 continue;
80
81             parts = line.split(' ')
82             if len(parts) > 1:
83                 source = parts[0]
84                 target = parts[1]
85             else:
86                 source = line
87                 target = None
88
89             install(source, target)
90
91     print "creating syj.tar.gz"
92     targz = tarfile.open("build/syj.tar.gz", "w:gz")
93     for path in glob.glob(pathjoin(__BUILD__, '*')):
94         targz.add(path)
95     targz.close()
96
97 main()