<s> import os <EOL> import sys <EOL> import errno <EOL> import requests <EOL> import shutil <EOL> import subprocess <EOL> import template <EOL> import urllib <EOL> import zipfile <EOL> import yaml <EOL> import re <EOL> LIB_PATH = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> REPO_PATH = os . path . realpath ( os . path . join ( LIB_PATH , '<STR_LIT:..>' ) ) <EOL> DOCKER_BOSHRELEASE_VERSION = '<STR_LIT>' <EOL> def build ( config , verbose = False ) : <EOL> validate_config ( config ) <EOL> context = config . copy ( ) <EOL> add_defaults ( context ) <EOL> upgrade_config ( context ) <EOL> context [ '<STR_LIT>' ] = verbose <EOL> with cd ( '<STR_LIT>' , clobber = True ) : <EOL> create_bosh_release ( context ) <EOL> validate_memory_quota ( context ) <EOL> with cd ( '<STR_LIT>' , clobber = True ) : <EOL> create_tile ( context ) <EOL> def validate_config ( config ) : <EOL> try : <EOL> validname = re . compile ( '<STR_LIT>' ) <EOL> if validname . match ( config [ '<STR_LIT:name>' ] ) is None : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except KeyError as e : <EOL> print >> sys . stderr , '<STR_LIT>' , e <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> for package in config . get ( '<STR_LIT>' , [ ] ) : <EOL> try : <EOL> if validname . match ( package [ '<STR_LIT:name>' ] ) is None : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except KeyError as e : <EOL> print >> sys . stderr , '<STR_LIT>' , e <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return config <EOL> def upgrade_config ( config ) : <EOL> for package in config . get ( '<STR_LIT>' , [ ] ) : <EOL> auto_services = package . get ( '<STR_LIT>' , None ) <EOL> if auto_services is not None : <EOL> if isinstance ( auto_services , basestring ) : <EOL> package [ '<STR_LIT>' ] = [ { '<STR_LIT:name>' : s } for s in auto_services . split ( ) ] <EOL> for package in config . get ( '<STR_LIT>' , [ ] ) : <EOL> if package . get ( '<STR_LIT:type>' ) == '<STR_LIT>' : <EOL> manifest = package . get ( '<STR_LIT>' ) <EOL> if manifest is not None and isinstance ( manifest , basestring ) : <EOL> package [ '<STR_LIT>' ] = yaml . safe_load ( manifest ) <EOL> def add_defaults ( context ) : <EOL> context [ '<STR_LIT>' ] = context . get ( '<STR_LIT>' , { } ) <EOL> context [ '<STR_LIT>' ] = context . get ( '<STR_LIT>' , [ ] ) <EOL> context [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> context [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> for form in context . get ( '<STR_LIT>' , [ ] ) : <EOL> properties = form . get ( '<STR_LIT>' , [ ] ) <EOL> for property in properties : <EOL> property [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> context [ '<STR_LIT>' ] += properties <EOL> for property in context [ '<STR_LIT>' ] : <EOL> property [ '<STR_LIT:name>' ] = property [ '<STR_LIT:name>' ] . lower ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> def update_memory ( context , manifest ) : <EOL> memory = manifest . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> unit = memory . lstrip ( '<STR_LIT>' ) . lstrip ( '<STR_LIT:U+0020>' ) . lower ( ) <EOL> if unit not in [ '<STR_LIT:g>' , '<STR_LIT>' , '<STR_LIT:m>' , '<STR_LIT>' ] : <EOL> print >> sys . stderr , '<STR_LIT>' , unit , '<STR_LIT>' , memory <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> memory = int ( memory [ : - len ( unit ) ] ) <EOL> if unit in [ '<STR_LIT:g>' , '<STR_LIT>' ] : <EOL> memory *= <NUM_LIT> <EOL> context [ '<STR_LIT>' ] += memory <EOL> if memory > context [ '<STR_LIT>' ] : <EOL> context [ '<STR_LIT>' ] = memory <EOL> def validate_memory_quota ( context ) : <EOL> required = context [ '<STR_LIT>' ] + context [ '<STR_LIT>' ] <EOL> specified = context . get ( '<STR_LIT>' , None ) <EOL> if specified is None : <EOL> context [ '<STR_LIT>' ] = context [ '<STR_LIT>' ] * <NUM_LIT:2> <EOL> elif specified < required : <EOL> print >> sys . stderr , '<STR_LIT>' , specified , '<STR_LIT>' <EOL> print >> sys . stderr , '<STR_LIT>' , context [ '<STR_LIT>' ] , '<STR_LIT>' <EOL> print >> sys . stderr , '<STR_LIT>' , context [ '<STR_LIT>' ] , '<STR_LIT>' <EOL> print >> sys . stderr , '<STR_LIT>' , required , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> package_types = [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } , <EOL> ] <EOL> def create_bosh_release ( context ) : <EOL> target = os . getcwd ( ) <EOL> bosh ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> template . render ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> template . render ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> template . render ( '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> packages = context . get ( '<STR_LIT>' , [ ] ) <EOL> requires_cf_cli = False <EOL> requires_docker_bosh = False <EOL> for package in packages : <EOL> typename = package . get ( '<STR_LIT:type>' , None ) <EOL> if typename is None : <EOL> print >> sys . stderr , '<STR_LIT>' , package [ '<STR_LIT:name>' ] , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> typedef = ( [ t for t in package_types if t [ '<STR_LIT>' ] == typename ] + [ None ] ) [ <NUM_LIT:0> ] <EOL> if typedef is None : <EOL> print >> sys . stderr , '<STR_LIT>' , package [ '<STR_LIT:name>' ] , '<STR_LIT>' , typename <EOL> print >> sys . stderr , '<STR_LIT>' , '<STR_LIT:U+002CU+0020>' . join ( [ t [ '<STR_LIT>' ] for t in package_types ] ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> for flag in typedef [ '<STR_LIT>' ] : <EOL> package [ flag ] = True <EOL> add_blob_package ( context , package ) <EOL> for job in typedef . get ( '<STR_LIT>' , [ ] ) : <EOL> add_bosh_job ( <EOL> context , <EOL> package , <EOL> job . lstrip ( '<STR_LIT>' ) , <EOL> post_deploy = job . startswith ( '<STR_LIT:+>' ) , <EOL> pre_delete = job . startswith ( '<STR_LIT:->' ) <EOL> ) <EOL> requires_cf_cli |= package . get ( '<STR_LIT>' , False ) <EOL> requires_docker_bosh |= package . get ( '<STR_LIT>' , False ) <EOL> if requires_cf_cli : <EOL> add_cf_cli ( context ) <EOL> add_bosh_job ( context , None , '<STR_LIT>' , post_deploy = True ) <EOL> add_bosh_job ( context , None , '<STR_LIT>' , pre_delete = True ) <EOL> add_common_utils ( context ) <EOL> bosh ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> output = bosh ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , context [ '<STR_LIT:version>' ] ) <EOL> context [ '<STR_LIT>' ] = bosh_extract ( output , [ <EOL> { '<STR_LIT:label>' : '<STR_LIT:name>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:label>' : '<STR_LIT:version>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:label>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:label>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] ) <EOL> context [ '<STR_LIT>' ] = requires_docker_bosh <EOL> context [ '<STR_LIT>' ] = requires_cf_cli <EOL> print <EOL> def add_bosh_job ( context , package , job_type , post_deploy = False , pre_delete = False ) : <EOL> errand = False <EOL> if post_deploy or pre_delete : <EOL> errand = True <EOL> job_name = job_type <EOL> if package is not None : <EOL> job_name += '<STR_LIT:->' + package [ '<STR_LIT:name>' ] <EOL> bosh ( '<STR_LIT>' , '<STR_LIT>' , job_name ) <EOL> job_context = { <EOL> '<STR_LIT>' : job_name , <EOL> '<STR_LIT>' : job_type , <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT>' : package , <EOL> '<STR_LIT>' : errand , <EOL> } <EOL> template . render ( <EOL> os . path . join ( '<STR_LIT>' , job_name , '<STR_LIT>' ) , <EOL> os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> job_context <EOL> ) <EOL> template . render ( <EOL> os . path . join ( '<STR_LIT>' , job_name , '<STR_LIT>' , job_name + '<STR_LIT>' ) , <EOL> os . path . join ( '<STR_LIT>' , job_type + '<STR_LIT>' ) , <EOL> job_context <EOL> ) <EOL> template . render ( <EOL> os . path . join ( '<STR_LIT>' , job_name , '<STR_LIT>' ) , <EOL> os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> job_context <EOL> ) <EOL> context [ '<STR_LIT>' ] = context . get ( '<STR_LIT>' , [ ] ) + [ { <EOL> '<STR_LIT:name>' : job_name , <EOL> '<STR_LIT:type>' : job_type , <EOL> '<STR_LIT>' : package , <EOL> } ] <EOL> if post_deploy : <EOL> context [ '<STR_LIT>' ] = context . get ( '<STR_LIT>' , [ ] ) + [ { '<STR_LIT:name>' : job_name } ] <EOL> if pre_delete : <EOL> context [ '<STR_LIT>' ] = context . get ( '<STR_LIT>' , [ ] ) + [ { '<STR_LIT:name>' : job_name } ] <EOL> def add_src_package ( context , package , alternate_template = None ) : <EOL> add_package ( '<STR_LIT:src>' , context , package , alternate_template ) <EOL> def add_blob_package ( context , package , alternate_template = None ) : <EOL> add_package ( '<STR_LIT>' , context , package , alternate_template ) <EOL> def add_package ( dir , context , package , alternate_template = None ) : <EOL> name = package [ '<STR_LIT:name>' ] . lower ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> package [ '<STR_LIT:name>' ] = name <EOL> bosh ( '<STR_LIT>' , '<STR_LIT>' , name ) <EOL> target_dir = os . path . realpath ( os . path . join ( dir , name ) ) <EOL> package_dir = os . path . realpath ( os . path . join ( '<STR_LIT>' , name ) ) <EOL> mkdir_p ( target_dir ) <EOL> template_dir = '<STR_LIT>' <EOL> if alternate_template is not None : <EOL> template_dir = os . path . join ( template_dir , alternate_template ) <EOL> package_context = { <EOL> '<STR_LIT>' : context , <EOL> '<STR_LIT>' : package , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> with cd ( '<STR_LIT:..>' ) : <EOL> files = package . get ( '<STR_LIT>' , [ ] ) <EOL> path = package . get ( '<STR_LIT:path>' , None ) <EOL> if path is not None : <EOL> files += [ { '<STR_LIT:path>' : path } ] <EOL> package [ '<STR_LIT:path>' ] = os . path . basename ( path ) <EOL> manifest = package . get ( '<STR_LIT>' , None ) <EOL> manifest_path = None <EOL> if type ( manifest ) is dict : <EOL> manifest_path = manifest . get ( '<STR_LIT:path>' , None ) <EOL> if manifest_path is not None : <EOL> files += [ { '<STR_LIT:path>' : manifest_path } ] <EOL> package [ '<STR_LIT>' ] [ '<STR_LIT:path>' ] = os . path . basename ( manifest_path ) <EOL> for file in files : <EOL> filename = file . get ( '<STR_LIT:name>' , os . path . basename ( file [ '<STR_LIT:path>' ] ) ) <EOL> file [ '<STR_LIT:name>' ] = filename <EOL> urllib . urlretrieve ( file [ '<STR_LIT:path>' ] , os . path . join ( target_dir , filename ) ) <EOL> package_context [ '<STR_LIT>' ] += [ filename ] <EOL> for docker_image in package . get ( '<STR_LIT>' , [ ] ) : <EOL> filename = docker_image . lower ( ) . replace ( '<STR_LIT:/>' , '<STR_LIT:->' ) . replace ( '<STR_LIT::>' , '<STR_LIT:->' ) + '<STR_LIT>' <EOL> download_docker_image ( docker_image , os . path . join ( target_dir , filename ) , cache = context . get ( '<STR_LIT>' , None ) ) <EOL> package_context [ '<STR_LIT>' ] += [ filename ] <EOL> if package . get ( '<STR_LIT>' , False ) : <EOL> manifest = package . get ( '<STR_LIT>' , { '<STR_LIT:name>' : name } ) <EOL> if manifest . get ( '<STR_LIT>' , False ) : <EOL> print >> sys . stderr , '<STR_LIT>' , name + '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> manifest_file = os . path . join ( target_dir , '<STR_LIT>' ) <EOL> with open ( manifest_file , '<STR_LIT:wb>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( yaml . safe_dump ( manifest , default_flow_style = False ) ) <EOL> package_context [ '<STR_LIT>' ] += [ '<STR_LIT>' ] <EOL> update_memory ( context , manifest ) <EOL> template . render ( <EOL> os . path . join ( package_dir , '<STR_LIT>' ) , <EOL> os . path . join ( template_dir , '<STR_LIT>' ) , <EOL> package_context <EOL> ) <EOL> template . render ( <EOL> os . path . join ( package_dir , '<STR_LIT>' ) , <EOL> os . path . join ( template_dir , '<STR_LIT>' ) , <EOL> package_context <EOL> ) <EOL> def add_cf_cli ( context ) : <EOL> add_blob_package ( context , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:path>' : '<STR_LIT>' <EOL> } ] <EOL> } , <EOL> alternate_template = '<STR_LIT>' <EOL> ) <EOL> context [ '<STR_LIT>' ] = context . get ( '<STR_LIT>' , [ ] ) + [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> def add_common_utils ( context ) : <EOL> add_src_package ( context , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ ] <EOL> } , <EOL> alternate_template = '<STR_LIT>' <EOL> ) <EOL> def create_tile ( context ) : <EOL> release = context [ '<STR_LIT>' ] <EOL> release [ '<STR_LIT:file>' ] = os . path . basename ( release [ '<STR_LIT>' ] ) <EOL> with cd ( '<STR_LIT>' ) : <EOL> print '<STR_LIT>' , release [ '<STR_LIT:name>' ] <EOL> shutil . copy ( release [ '<STR_LIT>' ] , release [ '<STR_LIT:file>' ] ) <EOL> if context . get ( '<STR_LIT>' , False ) : <EOL> print '<STR_LIT>' <EOL> docker_release = download_docker_release ( ) <EOL> context [ '<STR_LIT>' ] = docker_release <EOL> print '<STR_LIT>' <EOL> template . render ( '<STR_LIT>' + release [ '<STR_LIT:name>' ] + '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> print '<STR_LIT>' <EOL> template . render ( '<STR_LIT>' + release [ '<STR_LIT:name>' ] + '<STR_LIT>' , '<STR_LIT>' , context ) <EOL> print '<STR_LIT>' <EOL> pivotal_file = release [ '<STR_LIT:name>' ] + '<STR_LIT:->' + release [ '<STR_LIT:version>' ] + '<STR_LIT>' <EOL> with zipfile . ZipFile ( pivotal_file , '<STR_LIT:w>' ) as f : <EOL> f . write ( os . path . join ( '<STR_LIT>' , release [ '<STR_LIT:file>' ] ) ) <EOL> if context . get ( '<STR_LIT>' , False ) : <EOL> docker_release = context [ '<STR_LIT>' ] <EOL> f . write ( os . path . join ( '<STR_LIT>' , docker_release [ '<STR_LIT:file>' ] ) ) <EOL> f . write ( os . path . join ( '<STR_LIT>' , release [ '<STR_LIT:name>' ] + '<STR_LIT>' ) ) <EOL> f . write ( os . path . join ( '<STR_LIT>' , release [ '<STR_LIT:name>' ] + '<STR_LIT>' ) ) <EOL> print <EOL> print '<STR_LIT>' , pivotal_file <EOL> def download_docker_release ( ) : <EOL> release_name = '<STR_LIT>' <EOL> release_version = DOCKER_BOSHRELEASE_VERSION <EOL> release_file = release_name + '<STR_LIT>' + release_version + '<STR_LIT>' <EOL> release_tarball = release_file <EOL> if not os . path . isfile ( release_tarball ) : <EOL> url = '<STR_LIT>' + release_version <EOL> download ( url , release_tarball ) <EOL> return { <EOL> '<STR_LIT>' : release_tarball , <EOL> '<STR_LIT:name>' : release_name , <EOL> '<STR_LIT:version>' : release_version , <EOL> '<STR_LIT:file>' : release_file , <EOL> } <EOL> def download_docker_image ( docker_image , target_file , cache = None ) : <EOL> try : <EOL> from docker . client import Client <EOL> from docker . utils import kwargs_from_env <EOL> kwargs = kwargs_from_env ( ) <EOL> kwargs [ '<STR_LIT>' ] . assert_hostname = False <EOL> docker_cli = Client ( ** kwargs ) <EOL> image = docker_cli . get_image ( docker_image ) <EOL> image_tar = open ( target_file , '<STR_LIT:w>' ) <EOL> image_tar . write ( image . data ) <EOL> image_tar . close ( ) <EOL> except Exception as e : <EOL> if cache is not None : <EOL> cached_file = os . path . join ( cache , docker_image . lower ( ) . replace ( '<STR_LIT:/>' , '<STR_LIT:->' ) . replace ( '<STR_LIT::>' , '<STR_LIT:->' ) + '<STR_LIT>' ) <EOL> if os . path . isfile ( cached_file ) : <EOL> print '<STR_LIT>' , docker_image <EOL> urllib . urlretrieve ( cached_file , target_file ) <EOL> return <EOL> print '<STR_LIT>' , docker_image <EOL> raise <EOL> def bosh_extract ( output , properties ) : <EOL> result = { } <EOL> for l in output . split ( '<STR_LIT:\n>' ) : <EOL> for p in properties : <EOL> if l . startswith ( p [ '<STR_LIT>' ] ) : <EOL> result [ p [ '<STR_LIT:label>' ] ] = l . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] . strip ( ) <EOL> return result <EOL> def bosh ( * argv ) : <EOL> argv = list ( argv ) <EOL> print '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( argv ) <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] + argv <EOL> try : <EOL> return subprocess . check_output ( command , stderr = subprocess . STDOUT ) <EOL> except subprocess . CalledProcessError as e : <EOL> if argv [ <NUM_LIT:0> ] == '<STR_LIT>' and argv [ <NUM_LIT:1> ] == '<STR_LIT>' and '<STR_LIT>' in e . output : <EOL> return e . output <EOL> if argv [ <NUM_LIT:0> ] == '<STR_LIT>' and '<STR_LIT>' in e . output : <EOL> return e . output <EOL> print e . output <EOL> sys . exit ( e . returncode ) <EOL> def bash ( * argv ) : <EOL> argv = list ( argv ) <EOL> try : <EOL> return subprocess . check_output ( argv , stderr = subprocess . STDOUT ) <EOL> except subprocess . CalledProcessError as e : <EOL> print '<STR_LIT:U+0020>' . join ( argv ) , '<STR_LIT>' <EOL> print e . output <EOL> sys . exit ( e . returncode ) <EOL> def is_semver ( version ) : <EOL> semver = version . split ( '<STR_LIT:.>' ) <EOL> if len ( semver ) != <NUM_LIT:3> : <EOL> return False <EOL> try : <EOL> int ( semver [ <NUM_LIT:0> ] ) <EOL> int ( semver [ <NUM_LIT:1> ] ) <EOL> int ( semver [ <NUM_LIT:2> ] ) <EOL> return True <EOL> except : <EOL> return False <EOL> def update_version ( history , version ) : <EOL> if version is None : <EOL> version = '<STR_LIT>' <EOL> prior_version = history . get ( '<STR_LIT:version>' , None ) <EOL> if prior_version is not None : <EOL> history [ '<STR_LIT>' ] = history . get ( '<STR_LIT>' , [ ] ) <EOL> history [ '<STR_LIT>' ] += [ prior_version ] <EOL> if not is_semver ( version ) : <EOL> semver = history . get ( '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> if not is_semver ( semver ) : <EOL> print >> sys . stderr , '<STR_LIT>' , semver <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> semver = semver . split ( '<STR_LIT:.>' ) <EOL> if version == '<STR_LIT>' : <EOL> semver [ <NUM_LIT:2> ] = str ( int ( semver [ <NUM_LIT:2> ] ) + <NUM_LIT:1> ) <EOL> elif version == '<STR_LIT>' : <EOL> semver [ <NUM_LIT:1> ] = str ( int ( semver [ <NUM_LIT:1> ] ) + <NUM_LIT:1> ) <EOL> semver [ <NUM_LIT:2> ] = '<STR_LIT:0>' <EOL> elif version == '<STR_LIT>' : <EOL> semver [ <NUM_LIT:0> ] = str ( int ( semver [ <NUM_LIT:0> ] ) + <NUM_LIT:1> ) <EOL> semver [ <NUM_LIT:1> ] = '<STR_LIT:0>' <EOL> semver [ <NUM_LIT:2> ] = '<STR_LIT:0>' <EOL> else : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> version = '<STR_LIT:.>' . join ( semver ) <EOL> history [ '<STR_LIT:version>' ] = version <EOL> return version <EOL> class cd : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , newPath , clobber = False ) : <EOL> self . clobber = clobber <EOL> self . newPath = os . path . expanduser ( newPath ) <EOL> def __enter__ ( self ) : <EOL> self . savedPath = os . getcwd ( ) <EOL> if self . clobber and os . path . isdir ( self . newPath ) : <EOL> shutil . rmtree ( self . newPath ) <EOL> mkdir_p ( self . newPath ) <EOL> os . chdir ( self . newPath ) <EOL> def __exit__ ( self , etype , value , traceback ) : <EOL> os . chdir ( self . savedPath ) <EOL> def mkdir_p ( dir ) : <EOL> try : <EOL> os . makedirs ( dir ) <EOL> except os . error , e : <EOL> if e . errno != errno . EEXIST : <EOL> raise <EOL> def download ( url , filename ) : <EOL> response = requests . get ( url , stream = True ) <EOL> with open ( filename , '<STR_LIT:wb>' ) as file : <EOL> for chunk in response . iter_content ( chunk_size = <NUM_LIT> ) : <EOL> if chunk : <EOL> file . write ( chunk ) </s>
