# Copyright (c) Richard Wall # See LICENSE for details. """ Functions and Classes for automating the release of Jarmon """ import logging import os import shutil import sys import httplib import urllib from datetime import datetime from optparse import OptionParser from subprocess import check_call, PIPE from zipfile import ZipFile, ZIP_DEFLATED import pkg_resources class BuildError(Exception): """ A base Exception for errors in the build system """ pass class BuildCommand(object): def __init__(self): self.log = logging.getLogger( '%s.%s' % (__name__, self.__class__.__name__)) self.workingbranch_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), '..')) # setup working dir self.build_dir = os.path.join(self.workingbranch_dir, 'build') if not os.path.isdir(self.build_dir): self.log.debug('Creating build dir: %s' % (self.build_dir,)) os.mkdir(self.build_dir) else: self.log.debug('Using build dir: %s' % (self.build_dir,)) class BuildApidocsCommand(BuildCommand): """ Generate apidocs for jarmon """ command_name = 'apidocs' def main(self, argv): """ The main entry point for the build-apidocs command @param argv: The list of arguments passed to the build-apidocs command """ parser = OptionParser( usage='build [options] %s VERSION [DIRECTORY]' % (self.command_name,)) parser.disable_interspersed_args() options, args = parser.parse_args(argv) if len(args) != 1 and len(args) != 2: parser.error('Wrong number of arguments. This command expects a ' 'version number, and optionally a directory.') buildversion = args[0] workingbranch_dir = self.workingbranch_dir build_dir = self.build_dir apidocs_dir = os.path.join(args[1] if len(args) == 2 else build_dir, 'docs', 'apidocs') # Remove any existing apidocs so that we can track removed files shutil.rmtree(apidocs_dir, True) # Use the yuidoc script that we just extracted to generate new docs self.log.debug('Running JSDoc') check_call(( 'jsdoc', '-c', os.path.join(workingbranch_dir, 'jarmonbuild', 'jsdoc.json'), '-r', os.path.join(workingbranch_dir, 'README'), '-d', apidocs_dir, os.path.join(workingbranch_dir, 'jarmon'), ),) class BuildReleaseCommand(BuildCommand): """ Export all source files, generate apidocs and create a zip archive for upload to Launchpad. """ command_name = 'release' def main(self, argv): parser = OptionParser( usage='build [options] %s VERSION' % (self.command_name,)) parser.disable_interspersed_args() options, args = parser.parse_args(argv) if len(args) != 1: parser.error('Wrong number of arguments. This command expects a ' 'version number only.') buildversion = args[0] workingbranch_dir = self.workingbranch_dir build_dir = self.build_dir self.log.debug('Clean the build folder') shutil.rmtree(build_dir, True) os.mkdir(build_dir) self.log.debug('Export versioned files to a build folder') check_call(('git', '--work-tree=%s' % (build_dir,), 'checkout', '-f')) self.log.debug('Generate apidocs') BuildApidocsCommand().main([buildversion, build_dir]) self.log.debug('Generate jsdeps') BuildJavascriptDependenciesCommand().main([build_dir]) self.log.debug('Generate testdata') BuildTestDataCommand().main([build_dir]) self.log.debug('Generate archive') archive_root = 'jarmon-%s' % (buildversion,) prefix_len = len(build_dir) + 1 z = ZipFile('%s.zip' % (archive_root,), 'w', ZIP_DEFLATED) try: for root, dirs, files in os.walk(build_dir): for file in files: z.write( os.path.join(root, file), os.path.join(archive_root, root[prefix_len:], file), ) finally: z.close() class BuildTestDataCommand(BuildCommand): """ Create data for use in unittests """ command_name = 'testdata' def main(self, argv): """ Create an RRD file with values 0-9 entered at 1 second intervals from 1980-01-01 00:00:00 (the first date that rrdtool allows) """ from pyrrd.rrd import DataSource, RRA, RRD start = int(datetime(1980, 1, 1, 0, 0).strftime('%s')) dss = [] rras = [] filename = os.path.join(argv[0] if len(argv) > 0 else self.build_dir, 'test.rrd') rows = 12 step = 10 dss.append( DataSource(dsName='speed', dsType='GAUGE', heartbeat=2 * step)) rras.append(RRA(cf='AVERAGE', xff=0.5, steps=1, rows=rows)) rras.append(RRA(cf='AVERAGE', xff=0.5, steps=12, rows=rows)) my_rrd = RRD(filename, ds=dss, rra=rras, start=start, step=step) my_rrd.create() for i, t in enumerate( range(start + step, start + step + (rows * step), step)): self.log.debug( 'DATA: %s %s (%s)' % (t, i, datetime.fromtimestamp(t))) my_rrd.bufferValue(t, i) # Add further data 1 second later to demonstrate that the rrd # lastupdatetime does not necessarily fall on a step boundary t += 1 i += 1 self.log.debug('DATA: %s %s (%s)' % (t, i, datetime.fromtimestamp(t))) my_rrd.bufferValue(t, i) my_rrd.update() class BuildJavascriptDependenciesCommand(BuildCommand): """ Export all source files, generate apidocs and create a zip archive for upload to Launchpad. """ command_name = 'jsdeps' def main(self, argv): self.log.debug('Compiling javascript dependencies') depjs_path = os.path.join( argv[0] if len(argv) > 0 else self.workingbranch_dir, 'docs/examples/assets/js/dependencies.js') # Get the closure params from the original file params = [ ('code_url', 'http://code.jquery.com/jquery-1.6.3.js'), ('code_url', 'https://raw.githubusercontent.com/flot/flot/v0.7.0/excanvas.js'), ('code_url', 'https://raw.githubusercontent.com/flot/flot/v0.7.0/jquery.flot.js'), ('code_url', 'https://raw.githubusercontent.com/flot/flot/v0.7.0/jquery.flot.stack.js'), ('code_url', 'https://raw.githubusercontent.com/flot/flot/v0.7.0/jquery.flot.selection.js'), ('code_url', 'https://lukeshu.com/git/2git/javascriptrrd/plain/src/lib/rrdFile.js?id=v1.1.1'), ('code_url', 'https://lukeshu.com/git/2git/javascriptrrd/plain/src/lib/binaryXHR.js?id=v1.1.1'), ('code_url', 'https://raw.githubusercontent.com/jquerytools/jquerytools/8ac4636a01d3860f1c4726ba722190a531bf1068/src/tabs/tabs.js'), ('code_url', 'https://raw.githubusercontent.com/jquerytools/jquerytools/8ac4636a01d3860f1c4726ba722190a531bf1068/src/toolbox/toolbox.history.js'), ('compilation_level', 'SIMPLE_OPTIMIZATIONS'), ('formatting', 'print_input_delimiter'), ('output_format', 'text'), ('output_info', 'compiled_code'), ] # Always use the following value for the Content-type header. headers = { "Content-type": "application/x-www-form-urlencoded" } conn = httplib.HTTPConnection('closure-compiler.appspot.com') conn.request('POST', '/compile', urllib.urlencode(params), headers) response = conn.getresponse() with open(depjs_path, 'w') as f: f.write( '// Compiled with closure-compiler on %s\n' % (datetime.now())) for param in params: f.write('// @%s %s\n' % param) while not response.isclosed(): f.write(response.read(1024 * 10)) conn.close # The available subcommands SUBCOMMAND_HANDLERS = [ BuildApidocsCommand, BuildReleaseCommand, BuildTestDataCommand, BuildJavascriptDependenciesCommand, ] def main(argv=sys.argv[1:]): """ The root build command which dispatches to various subcommands for eg building apidocs and release zip files. """ build_commands = dict( (handler.command_name, handler) for handler in SUBCOMMAND_HANDLERS) parser = OptionParser( usage='build [options] SUBCOMMAND [options]', description='Available subcommands are: %r' % (build_commands.keys(),)) parser.add_option( '-d', '--debug', action='store_true', default=False, dest='debug', help='Print verbose debug log to stderr') parser.disable_interspersed_args() options, args = parser.parse_args(argv) if len(args) < 1: parser.error('Please specify a sub command. ' 'Available commands: %r' % (build_commands.keys())) # First argument is the name of a subcommand command_name = args.pop(0) command_factory = build_commands.get(command_name) if not command_factory: parser.error('Unrecognised subcommand: %r' % (command_name,)) # Setup logging log = logging.getLogger(__name__) log.setLevel(logging.INFO) # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.INFO) # create formatter formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') # add formatter to ch ch.setFormatter(formatter) log.addHandler(ch) if options.debug: log.setLevel(logging.DEBUG) ch.setLevel(logging.DEBUG) command_factory().main(argv=args)