]> dev.renevier.net Git - syj.git/blob - scripts/release.py
a567d8539bc2d455cedc74d7f4ee63812113992a
[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         shutil.copy(path, tmpdir)
44
45     # build OpenLayers.js
46     subprocess.call(['python', 'buildUncompressed.py',
47                      pathjoin(os.getcwd(), "scripts/syj"), pathjoin(tmpdir, "OpenLayers.js")],
48                      cwd = 'public/openlayers/openlayers/build')
49
50     config = ConfigParser.ConfigParser()
51     os.makedirs(pathjoin(__BUILD__, 'public/js'))
52     config.readfp(open('application/configs/medias.ini'))
53     for key, value in config.items('production'):
54         if key.startswith('scripts.'):
55             outpath = pathjoin(__BUILD__, 'public/js/' + key[len('scripts.'):] + '.js')
56             with open(outpath, 'w') as output:
57                 for inpath in map(lambda p: pathjoin(tmpdir, p.strip() + '.js'), value.split(',')):
58                     with open(inpath) as f:
59                         output.write(f.read())
60             compress(outpath)
61     shutil.rmtree(tmpdir)
62
63 def install(source, target):
64     if not source:
65         return
66
67     if os.path.isdir(source):
68         if not target:
69             target = source
70         buildtarget = pathjoin(__BUILD__, target)
71         parentdir = os.path.normpath(pathjoin(buildtarget, '..'))
72         if not os.path.isdir(parentdir):
73             os.makedirs(parentdir)
74         shutil.copytree(source, buildtarget)
75
76     elif os.path.exists(source):
77         if not target:
78             target = os.path.dirname(source)
79         buildtarget = os.path.normpath(pathjoin(__BUILD__, target))
80         if not os.path.isdir(buildtarget):
81             os.makedirs(buildtarget)
82         shutil.copy(source, buildtarget)
83
84     else:
85         for item in glob.glob(source):
86             install(item, target)
87
88 def main():
89     if os.path.isdir(__BUILD__):
90         shutil.rmtree(__BUILD__, False)
91
92     genscripts()
93
94     for path in glob.glob('public/css/*.css'):
95         install(path, None)
96         compress(pathjoin(__BUILD__, 'public/css', os.path.basename(path)))
97
98     with open("scripts/syj.install") as f:
99         for line in f:
100             line = line.split('#')[0].strip()
101             if not line:
102                 continue;
103
104             parts = line.split(' ')
105             if len(parts) > 1:
106                 source = parts[0]
107                 target = parts[1]
108             else:
109                 source = line
110                 target = None
111
112             install(source, target)
113
114     updateversion()
115
116     print "creating syj.tar.gz"
117     targz = tarfile.open("build/syj.tar.gz", "w:gz")
118     for path in glob.glob(pathjoin(__BUILD__, '*')):
119         targz.add(path)
120     targz.close()
121
122 main()