diff --git a/buildVersion.py b/buildVersion.py index 1985ae0cfa5dd896f57fef3065240ba5de732cfc..f1cca7b625b05ce35145c5f364278389e03d74fc 100755 --- a/buildVersion.py +++ b/buildVersion.py @@ -37,14 +37,11 @@ import os import re import subprocess import sys +import tempfile import urllib -print '\nStart buildVersion' - -#----------------------------------------------------------------------------- -# constants -# +# constants APP = 'mygit_dbui_04x' CHANGELOG = 'static/plugin_dbui/CHANGELOG' DBUI_W2P = 'web2py.plugin.dbui.%s.w2p' @@ -55,109 +52,65 @@ JSLIBSRC = 'static/plugin_dbui/src' NOW = datetime.datetime.now() PACK_PLUGIN_URL = 'http://localhost:8000/%s/default/pack_plugin' % APP -def get_version(): - s = open(JSBASE, 'rb').read() - m = re.match("(.+App.version = ')([\w._-]*)(';.*)", s, re.DOTALL) - return m.group(2).replace('.', '') - -#----------------------------------------------------------------------------- -# define main applications -# +# basic commands GIT = '/usr/bin/git' - -VERSION = '2.4.6' -SRC_PATH = "$HOME/lib/yuicompressor-%s/build/yuicompressor-%s.jar" -YUICOMPRESSOR = os.path.expandvars(SRC_PATH %(VERSION, VERSION)) - +YUICOMPRESSOR = os.path.expandvars("$HOME/lib/yuicompressor-2.4.6/build/yuicompressor-2.4.6.jar") WEB2PY = os.path.expandvars('$HOME/myweb/web2py/web2py.py') -if not os.path.exists(GIT): - print '\n\tgit application is missing !' - sys.exit(1) - -if not os.path.exists(YUICOMPRESSOR): - print '\n\tYUiCompressor application is missing !' - sys.exit(1) -if not os.path.exists(WEB2PY): - print '\n\tweb2py application is missing !' - sys.exit(1) +def clean(): + """Clean the previous build. -#----------------------------------------------------------------------------- -# setup and parse script options -# -ops = optparse.OptionParser() - -ops.add_option("-c", "--clean", - action="store_true", - dest= "clean", - help= "clean build files and exit.") - -ops.add_option("-g", "--git", - action="store_true", - dest= "git", - help= "run the git steps.") - -ops.add_option("-n", "--dry-run", - action="store_false", - dest= "run", - help= "dry run, no files modification, no commit, no tag.") - -ops.add_option("-u", "--update", - action="store_true", - dest= "update", - help= "run the update version step.") - -ops.add_option("-y", "--yuicompressor", - action="store_true", - dest= "yuicompressor", - help= "run the yuicompressor step.") - -ops.add_option("-w", "--web2py", - action="store_true", - dest= "web2py", - help= "run the web2py step.") - -ops.set_defaults(clean=False, - git=False, - run=True, - update=False, - yuicompressor=False, - web2py=False) - -(opt, args) = ops.parse_args() - -# by default run all steps: update, git, YUIcompressor and web2py -if not (opt.update or opt.git or opt.yuicompressor or opt.web2py): - opt.update = True - opt.git = True - opt.yuicompressor = True - opt.web2py = True - -#----------------------------------------------------------------------------- -# clean previous build -if opt.clean: - fn = DBUI_W2P % get_version() + """ + fn = DBUI_W2P % get_version().replace('.', '') for el in (fn , JSLIBDEBUG, JSLIBMIN): if os.path.exists(el): os.remove(el) print 'file', el, 'is removed.' - - sys.exit(0) -#----------------------------------------------------------------------------- -# update step -# Push version identifier in the CHANGELOG and appbase.js -# -if opt.update: - # version identifier - if args: - version = args[0] + +def get_version(): + """Get the current version identifier. + + """ + s = open(JSBASE, 'rb').read() + m = re.match("(.+App.version = ')([\w._-]*)(';.*)", s, re.DOTALL) + return m.group(2) + + +def git(): + """Commit and tag the current release. + + """ + version = get_version() + + # check tag in git + fi = tempfile.TemporaryFile() + subprocess.call(["git", "tag"], stdout=fi) + fi.seek(0) + if version in fi.read(): + print "\n\ttag %s already exit in git" % version + sys.exit(1) - else: - version = raw_input('Enter the version identifier: ') + # Commit the new release in git an tag it + print 'git add', JSBASE, CHANGELOG, JSLIBDEBUG, JSLIBMIN + cmd = ["git", "add", JSBASE, CHANGELOG, JSLIBDEBUG, JSLIBMIN] + subprocess.call(cmd) + + print 'git commit' + cmd = ["git", "commit", "-m", "Release version %s" % version] + subprocess.call(cmd) + + print 'git tag', version + cmd = ["git", "tag", version] + subprocess.call(cmd) + +def update(version): + """Update version identifier in CHANGELOG and appbase.js + + """ print 'Modified', JSBASE s = open(JSBASE, 'rb').read() @@ -173,12 +126,9 @@ if opt.update: # update the version and write a new file s = m.group(1) + version + m.group(3) - if opt.run: - fi = open(JSBASE, 'wb') - fi.write(s) - fi.close() - else: - print s + fi = open(JSBASE, 'wb') + fi.write(s) + fi.close() # look for a pattern HEAD in the CHANGELOG # split the the string in 2 parts (pre HEAD, post HEAD) @@ -195,18 +145,32 @@ if opt.update: # update the version and edit the CHANGELOG s = '%s\n%s (%s)\n%s' % (m.group(1), version, NOW.strftime('%b %Y'), m.group(2)) - if opt.run: - fi = open(CHANGELOG, 'wb') - fi.write(s) - fi.close() - subprocess.call(["vim", CHANGELOG]) - else: - print s + fi = open(CHANGELOG, 'wb') + fi.write(s) + fi.close() + subprocess.call(["vim", CHANGELOG]) + + +def web2py(): + """Produce the binary file for the web2py plugin. + + """ + print 'Build the web2py plugin binary file' + raw_input('Check that the web2py service is running ? Type CR to continue.') + f = urllib.urlopen(PACK_PLUGIN_URL) + s = f.read() + + fn = DBUI_W2P % get_version().replace('.', '') + fi = open(fn, 'wb') + fi.write(s) + fi.close() + print 'Binary file', fn, 'is created.' -#----------------------------------------------------------------------------- -# YUIcompressor step -# -if opt.yuicompressor: + +def yuicompressor(): + """Compresssed and minified the javascript library. + + """ # debug version of the javascript library print 'Debug version of the javascript library', JSLIBDEBUG subprocess.call('cat %s/*.js > %s' % (JSLIBSRC, JSLIBDEBUG), shell=True) @@ -216,44 +180,77 @@ if opt.yuicompressor: cmd = ["java", "-jar", YUICOMPRESSOR, "-o", JSLIBMIN, JSLIBDEBUG] subprocess.call(cmd) -#----------------------------------------------------------------------------- -# git step -# -if opt.git: - # Commit the new release in git an tag it - print 'git add', JSBASE, CHANGELOG, JSLIBDEBUG, JSLIBMIN - cmd = ["git", "add", JSBASE, CHANGELOG, JSLIBDEBUG, JSLIBMIN] - if opt.run: subprocess.call(cmd) + +if __name__ == '__main__': - print 'git commit' - cmd = ["git", "commit", "-m", "Release version %s" % version] - if opt.run: subprocess.call(cmd) + # check that basic commands are there + for cmd in (GIT, YUICOMPRESSOR, WEB2PY): + if not os.path.exists(cmd): + print '\n\t%s application is missing !' % cmd + sys.exit(1) + + # define script options + ops = optparse.OptionParser() - print 'git tag', version - cmd = ["git", "tag", version] - if opt.run: subprocess.call(cmd) - -#----------------------------------------------------------------------------- -# web2py step -# -if opt.web2py: - # get the plugin version - version = get_version() - - # build the web2py plugin - print 'Build the web2py plugin binary file' - raw_input('Check that the web2py service is running ? Type CR to continue.') - f = urllib.urlopen(PACK_PLUGIN_URL) - s = f.read() + ops.add_option("-c", "--clean", + action="store_true", + dest= "clean", + help= "clean build files and exit.") - fn = DBUI_W2P % version - fi = open(fn, 'wb') - fi.write(s) - fi.close() - print 'file', fn, 'is written.' - -#----------------------------------------------------------------------------- -# exit -# -print 'Exit buidVersion\n' -sys.exit(0) \ No newline at end of file + ops.add_option("-g", "--git", + action="store_true", + dest= "git", + help= "run the git steps.") + + ops.add_option("-u", "--update", + action="store_true", + dest= "update", + help= "run the update version step.") + + ops.add_option("-y", "--yuicompressor", + action="store_true", + dest= "yuicompressor", + help= "run the yuicompressor step.") + + ops.add_option("-w", "--web2py", + action="store_true", + dest= "web2py", + help= "run the web2py step.") + + ops.set_defaults(clean=False, + git=False, + update=False, + yuicompressor=False, + web2py=False) + + (opt, args) = ops.parse_args() + + # by default run all steps: update, git, YUIcompressor and web2py + if not (opt.update or opt.git or opt.yuicompressor or opt.web2py): + opt.update = True + opt.git = True + opt.yuicompressor = True + opt.web2py = True + + # process + print '\nStart buildVersion' + + if opt.clean: + clean() + sys.exit(0) + + if opt.update: + if args: + version = args[0] + + else: + version = raw_input('Enter the version identifier: ') + + update(version) + + if opt.yuicompressor: yuicompressor() + if opt.git: git() + if opt.web2py: web2py() + + print 'Exit buidVersion\n' + sys.exit(0) \ No newline at end of file