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