#!/usr/bin/python
-TARGET="build"
+__BUILD__="build"
import shutil, os, sys, subprocess, tempfile, tarfile, glob, ConfigParser
pathjoin = os.path.join
-def createdir():
- if os.path.isdir(TARGET):
- shutil.rmtree(TARGET, False)
- os.makedirs(TARGET)
+def updateversion():
+ try:
+ import git
+ repo = git.Repo('.')
+ master = repo.commits()[0]
+ tag = (filter(lambda tag: tag.commit.id == master.id, repo.tags) or [""])[0]
+ if tag:
+ version = tag.name
+ except ImportError:
+ version = subprocess.Popen(['git', 'tag', '-l', '--contains', 'master'], stdout=subprocess.PIPE).communicate()[0][:-1]
+ if not version:
+ raise AssertionError, "master is not tagged"
+
+ fd, fname = tempfile.mkstemp()
+ f = os.fdopen(fd, 'w')
+ versionfile = 'build/application/Version.php'
+ with open('build/application/Version.php') as source:
+ for line in source:
+ f.write(line.replace('$SYJVERSION$', version))
+ f.close()
+ shutil.move(fname, versionfile)
def compress(path):
- print ("compressing " + path)
tmpout = tempfile.TemporaryFile()
subprocess.Popen(['yui-compressor', path], stdout=tmpout).communicate()
tmpout.seek(0)
shutil.copy(path, tmpdir)
# build OpenLayers.js
- subprocess.call(['python', 'buildUncompressed.py', 'syj'],
+ subprocess.call(['python', 'buildUncompressed.py',
+ pathjoin(os.getcwd(), "scripts/syj"), pathjoin(tmpdir, "OpenLayers.js")],
cwd = 'public/openlayers/openlayers/build')
- shutil.copy('public/openlayers/openlayers/build/OpenLayers.js', tmpdir)
config = ConfigParser.ConfigParser()
- os.makedirs(pathjoin(TARGET, 'public/js'))
+ os.makedirs(pathjoin(__BUILD__, 'public/js'))
config.readfp(open('application/configs/medias.ini'))
for key, value in config.items('production'):
if key.startswith('scripts.'):
- outpath = pathjoin(TARGET, 'public/js/' + key[len('scripts.'):] + '.js')
+ outpath = pathjoin(__BUILD__, 'public/js/' + key[len('scripts.'):] + '.js')
with open(outpath, 'w') as output:
for inpath in map(lambda p: pathjoin(tmpdir, p.strip() + '.js'), value.split(',')):
with open(inpath) as f:
compress(outpath)
shutil.rmtree(tmpdir)
-def genstyles():
- directory = pathjoin(TARGET, 'public/css')
- os.makedirs(directory)
+def install(source, target):
+ if not source:
+ return
+
+ if os.path.isdir(source):
+ if not target:
+ target = source
+ buildtarget = pathjoin(__BUILD__, target)
+ parentdir = os.path.normpath(pathjoin(buildtarget, '..'))
+ if not os.path.isdir(parentdir):
+ os.makedirs(parentdir)
+ shutil.copytree(source, buildtarget)
+
+ elif os.path.exists(source):
+ if not target:
+ target = os.path.dirname(source)
+ buildtarget = os.path.normpath(pathjoin(__BUILD__, target))
+ if not os.path.isdir(buildtarget):
+ os.makedirs(buildtarget)
+ shutil.copy(source, buildtarget)
+
+ else:
+ for item in glob.glob(source):
+ install(item, target)
+
+def main():
+ if os.path.isdir(__BUILD__):
+ shutil.rmtree(__BUILD__, False)
+
+ genscripts()
+
for path in glob.glob('public/css/*.css'):
- shutil.copy(path, directory)
- compress(pathjoin(TARGET, 'public/css', os.path.basename(path)))
-
-def genicons():
- directory = pathjoin(TARGET, 'public/icons')
- os.makedirs(directory)
- for path in glob.glob('public/icons/*'):
- shutil.copy(path, directory)
-
-def genolmisc():
- directory = pathjoin(TARGET, 'public/img')
- os.makedirs(directory)
- for path in glob.glob('public/js/img/*'):
- shutil.copy(path, directory)
-
-def tarbuild():
- print "creating syj.tar.gz"
- targz = tarfile.open("build/syj.tar.gz", "w:gz")
- for path in ["application", "library", "public"]:
- targz.add(pathjoin(TARGET, path))
- targz.close()
+ install(path, None)
+ compress(pathjoin(__BUILD__, 'public/css', os.path.basename(path)))
+ with open("scripts/syj.install") as f:
+ for line in f:
+ line = line.split('#')[0].strip()
+ if not line:
+ continue;
-def genlibrary():
- directory = pathjoin(TARGET, 'library')
- os.makedirs(directory)
- for path in glob.glob('library/*.php'):
- shutil.copy(path, directory)
+ parts = line.split(' ')
+ if len(parts) > 1:
+ source = parts[0]
+ target = parts[1]
+ else:
+ source = line
+ target = None
- directory = pathjoin(TARGET, 'library/Zend')
- os.makedirs(directory)
- for path in glob.glob('library/Zend/*'): # will not take .git
- if (os.path.isdir(path)):
- shutil.copytree(path, pathjoin(directory, os.path.basename(path)))
- else:
- shutil.copy(path, directory)
+ install(source, target)
-def genmedias():
- genscripts()
- genstyles()
- genicons()
- genolmisc()
- genlibrary()
- shutil.copytree('application', pathjoin(TARGET, 'application'))
- shutil.copy('public/index.php', pathjoin(TARGET, 'public'))
- tarbuild()
-
-createdir()
-genmedias()
+ updateversion()
+
+ print "creating syj.tar.gz"
+ targz = tarfile.open("build/syj.tar.gz", "w:gz")
+ for path in glob.glob(pathjoin(__BUILD__, '*')):
+ targz.add(path)
+ targz.close()
+
+main()