<s> from __future__ import with_statement <EOL> import os <EOL> from shutil import copytree <EOL> from os . path import dirname , exists , join <EOL> import re <EOL> from subprocess import check_call , Popen , PIPE <EOL> from datetime import datetime <EOL> from distutils . core import Command <EOL> from citools . build import ReplaceTemplateFiles , RenameTemplateFiles <EOL> from citools . debian . control import ControlFile , Dependency <EOL> from citools . git import fetch_repository <EOL> from citools . version import get_git_describe , compute_version , compute_meta_version , get_git_head_hash , retrieve_current_branch <EOL> __all__ = ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> ) <EOL> def return_true ( * args , ** kwargs ) : <EOL> return True <EOL> class BuildDebianPackage ( Command ) : <EOL> """<STR_LIT>""" <EOL> description = "<STR_LIT>" <EOL> user_options = [ <EOL> ] <EOL> def initialize_options ( self ) : <EOL> pass <EOL> def finalize_options ( self ) : <EOL> pass <EOL> def run ( self ) : <EOL> check_call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def get_new_dependencies ( dir , accepted_tag_pattern = None , branch = "<STR_LIT>" ) : <EOL> version = compute_version ( get_git_describe ( repository_directory = dir , fix_environment = True , accepted_tag_pattern = accepted_tag_pattern ) ) <EOL> control = os . path . join ( dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> version = "<STR_LIT:.>" . join ( map ( str , version ) ) <EOL> from citools . build import replace_template_files <EOL> replace_template_files ( root_directory = dir , variables = { <EOL> '<STR_LIT>' : branch , <EOL> '<STR_LIT:version>' : version , <EOL> } ) <EOL> cfile = ControlFile ( filename = control ) <EOL> packages = cfile . get_packages ( ) <EOL> for p in packages : <EOL> p . version = version <EOL> return packages <EOL> def fetch_new_dependencies ( repository , workdir = None ) : <EOL> if repository . has_key ( '<STR_LIT>' ) : <EOL> branch = repository [ '<STR_LIT>' ] <EOL> else : <EOL> if workdir : <EOL> branch = retrieve_current_branch ( repository_directory = workdir , fix_environment = True ) <EOL> else : <EOL> branch = retrieve_current_branch ( ) <EOL> repo = fetch_repository ( <EOL> repository = repository [ '<STR_LIT:url>' ] , branch = branch <EOL> ) <EOL> project_pattern = "<STR_LIT>" % repository [ '<STR_LIT>' ] <EOL> deps = get_new_dependencies ( repo , accepted_tag_pattern = project_pattern , branch = branch ) <EOL> return deps <EOL> def replace_versioned_packages ( control_path , version , workdir = None ) : <EOL> workdir = workdir or os . curdir <EOL> cfile = ControlFile ( filename = control_path ) <EOL> cfile . replace_versioned_packages ( version ) <EOL> cfile . dump ( control_path ) <EOL> def replace_versioned_debian_files ( debian_path , original_version , new_version , control_file ) : <EOL> versioned_deps = control_file . get_versioned_dependencies ( ) <EOL> for path , dirs , files in os . walk ( debian_path ) : <EOL> for file in files : <EOL> for dep in versioned_deps : <EOL> s = "<STR_LIT>" % ( dep . name , original_version ) <EOL> if file . startswith ( s ) : <EOL> f = open ( os . path . join ( path , file ) ) <EOL> content = f . read ( ) <EOL> f . close ( ) <EOL> new_name = "<STR_LIT>" % ( dep . name , new_version , file [ len ( s ) : ] ) <EOL> new_content = re . sub ( original_version , new_version , content ) <EOL> f = open ( os . path . join ( path , new_name ) , '<STR_LIT:w>' ) <EOL> f . write ( new_content ) <EOL> f . close ( ) <EOL> os . remove ( os . path . join ( path , file ) ) <EOL> def update_dependency_versions ( repositories , control_path , workdir = None , accepted_tag_pattern = None ) : <EOL> """<STR_LIT>""" <EOL> workdir = workdir or os . curdir <EOL> cfile = ControlFile ( filename = control_path ) <EOL> deps_from_repositories = [ ] <EOL> cfile_meta_version = '<STR_LIT>' <EOL> for repository in repositories : <EOL> deps = fetch_new_dependencies ( repository , workdir ) <EOL> deps_from_repositories . extend ( deps ) <EOL> meta_version = compute_meta_version ( repositories , workdir = workdir , accepted_tag_pattern = accepted_tag_pattern ) <EOL> meta_version_string = "<STR_LIT:.>" . join ( map ( str , meta_version ) ) <EOL> deps = get_new_dependencies ( workdir , accepted_tag_pattern = accepted_tag_pattern ) <EOL> for dep in deps : <EOL> dep . version = meta_version_string <EOL> deps_from_repositories . extend ( deps ) <EOL> cfile . replace_dependencies ( deps_from_repositories ) <EOL> replace_versioned_debian_files ( debian_path = dirname ( control_path ) , original_version = cfile_meta_version , new_version = meta_version_string , control_file = cfile ) <EOL> cfile . replace_versioned_packages ( meta_version_string , old_version = cfile_meta_version ) <EOL> cfile . dump ( control_path ) <EOL> class UpdateDependencyVersions ( Command ) : <EOL> description = "<STR_LIT>" <EOL> user_options = [ <EOL> ] <EOL> def initialize_options ( self ) : <EOL> pass <EOL> def finalize_options ( self ) : <EOL> pass <EOL> def run ( self ) : <EOL> try : <EOL> format = "<STR_LIT>" % self . distribution . metadata . get_name ( ) <EOL> update_dependency_versions ( self . distribution . dependencies_git_repositories , os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) , accepted_tag_pattern = format ) <EOL> except : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> raise <EOL> def update_debianization ( version ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( version , ( tuple , list ) ) : <EOL> version = '<STR_LIT:.>' . join ( map ( str , version ) ) <EOL> changelog = '<STR_LIT>' <EOL> hash = get_git_head_hash ( ) <EOL> message = "<STR_LIT>" % { <EOL> '<STR_LIT:version>' : version , <EOL> '<STR_LIT>' : hash <EOL> } <EOL> proc = Popen ( [ '<STR_LIT>' , '<STR_LIT>' , changelog , '<STR_LIT>' , version , '<STR_LIT>' % message ] , stdout = PIPE ) <EOL> return_code = proc . wait ( ) <EOL> if return_code == <NUM_LIT:0> : <EOL> return proc . stdout . read ( ) . strip ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % return_code ) <EOL> def get_packages_names ( ) : <EOL> control = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not os . path . exists ( control ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> packages = [ ] <EOL> version_pattern = re . compile ( "<STR_LIT>" ) <EOL> for line in open ( control , '<STR_LIT:r>' ) : <EOL> match = re . match ( version_pattern , line ) <EOL> if match : <EOL> packages . append ( match . groupdict ( ) [ '<STR_LIT:name>' ] ) <EOL> return packages <EOL> def get_package_path ( package_name , module_name , current_version = None ) : <EOL> """<STR_LIT>""" <EOL> if not current_version : <EOL> format = "<STR_LIT>" % module_name <EOL> current_version = '<STR_LIT:.>' . join ( map ( str , compute_version ( get_git_describe ( accepted_tag_pattern = format ) ) ) ) <EOL> package_name = u"<STR_LIT>" % { <EOL> '<STR_LIT:name>' : package_name , <EOL> '<STR_LIT:version>' : current_version , <EOL> '<STR_LIT>' : '<STR_LIT:all>' <EOL> } <EOL> return os . path . normpath ( os . path . join ( os . curdir , os . pardir , package_name ) ) <EOL> class UpdateDebianVersion ( Command ) : <EOL> description = "<STR_LIT>" <EOL> user_options = [ <EOL> ( '<STR_LIT>' , None , "<STR_LIT>" ) , <EOL> ] <EOL> def initialize_options ( self ) : <EOL> self . build_number = None <EOL> def finalize_options ( self ) : <EOL> pass <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> version = self . distribution . get_version ( ) <EOL> if self . build_number : <EOL> version = '<STR_LIT>' % ( version , self . build_number ) <EOL> try : <EOL> update_debianization ( version ) <EOL> except Exception : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> raise <EOL> class CreateDebianPackage ( Command ) : <EOL> description = "<STR_LIT>" <EOL> user_options = [ <EOL> ( '<STR_LIT>' , None , "<STR_LIT>" ) , <EOL> ] <EOL> def initialize_options ( self ) : <EOL> self . build_number = None <EOL> def finalize_options ( self ) : <EOL> pass <EOL> def run ( self ) : <EOL> for cmd_name in self . get_sub_commands ( ) : <EOL> sub_cmd = self . reinitialize_command ( cmd_name ) <EOL> sub_cmd . build_number = self . build_number <EOL> self . run_command ( cmd_name ) <EOL> sub_commands = [ <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ] <EOL> class CreateDebianMetaPackage ( Command ) : <EOL> description = "<STR_LIT>" <EOL> user_options = [ <EOL> ( '<STR_LIT>' , None , "<STR_LIT>" ) , <EOL> ] <EOL> def initialize_options ( self ) : <EOL> self . build_number = None <EOL> def finalize_options ( self ) : <EOL> pass <EOL> def run ( self ) : <EOL> for cmd_name in self . get_sub_commands ( ) : <EOL> sub_cmd = self . reinitialize_command ( cmd_name ) <EOL> sub_cmd . build_number = self . build_number <EOL> self . run_command ( cmd_name ) <EOL> sub_commands = [ <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ] <EOL> def parse_setuppy_dependency ( package ) : <EOL> package = '<STR_LIT>' + package . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> if '<STR_LIT:=>' in package : <EOL> i = package . index ( '<STR_LIT:=>' ) <EOL> offset = <NUM_LIT:0> <EOL> if package [ package . rindex ( '<STR_LIT:=>' ) - <NUM_LIT:1> ] in ( '<STR_LIT:<>' , '<STR_LIT:>>' , '<STR_LIT:=>' ) : <EOL> offset = <NUM_LIT:1> <EOL> name , sign , version = package [ : i - offset ] , package [ i - offset : i + <NUM_LIT:1> ] , package [ i + <NUM_LIT:1> : ] <EOL> return Dependency ( name , version , sign ) <EOL> return Dependency ( package ) <EOL> def get_tzdiff ( local , remote ) : <EOL> '''<STR_LIT>''' <EOL> delta_minute = ( local . hour - remote . hour ) * <NUM_LIT> + ( local . minute - remote . minute ) <EOL> sign = delta_minute < <NUM_LIT:0> and '<STR_LIT:->' or '<STR_LIT:+>' <EOL> hour = abs ( delta_minute / <NUM_LIT> ) <EOL> minute = delta_minute % <NUM_LIT> <EOL> return '<STR_LIT>' % ( sign , hour , minute ) <EOL> def create_debianization ( distribution ) : <EOL> if exists ( '<STR_LIT>' ) : <EOL> raise NotImplementedError ( ) <EOL> name = distribution . get_name ( ) <EOL> name = '<STR_LIT>' % name . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) . lower ( ) <EOL> maintainer = distribution . get_maintainer ( ) <EOL> maintainer_email = distribution . get_maintainer_email ( ) <EOL> if maintainer == '<STR_LIT>' : <EOL> maintainer = '<STR_LIT>' <EOL> if maintainer_email == '<STR_LIT>' : <EOL> maintainer_email = '<STR_LIT>' <EOL> maintainer = '<STR_LIT>' % ( maintainer , maintainer_email ) <EOL> version = distribution . get_version ( ) <EOL> if not version : <EOL> version = '<STR_LIT>' <EOL> now = datetime . now ( ) <EOL> utcnow = datetime . utcnow ( ) <EOL> tzdiff = get_tzdiff ( now , utcnow ) <EOL> nowstring = '<STR_LIT>' % ( now . strftime ( '<STR_LIT>' ) , tzdiff ) <EOL> description = distribution . get_description ( ) <EOL> description = description . strip ( ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> architecture = '<STR_LIT:all>' <EOL> if distribution . has_ext_modules ( ) : <EOL> architecture = '<STR_LIT>' <EOL> copytree ( join ( dirname ( __file__ ) , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> for root , dirs , files in os . walk ( '<STR_LIT>' ) : <EOL> for f in files : <EOL> file = join ( root , f ) <EOL> with open ( file ) as fin : <EOL> content = fin . read ( ) <EOL> for key , value in ( <EOL> ( '<STR_LIT>' , name ) , <EOL> ( '<STR_LIT>' , maintainer ) , <EOL> ( '<STR_LIT>' , version ) , <EOL> ( '<STR_LIT>' , nowstring ) , <EOL> ) : <EOL> content = content . replace ( key , value ) <EOL> with open ( file , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( content ) <EOL> cf = ControlFile ( filename = '<STR_LIT>' ) <EOL> src = cf . source <EOL> p = cf . packages [ <NUM_LIT:0> ] <EOL> src [ '<STR_LIT>' ] = p [ '<STR_LIT>' ] = name <EOL> src [ '<STR_LIT>' ] = maintainer <EOL> p [ '<STR_LIT>' ] = description <EOL> p [ '<STR_LIT>' ] = architecture <EOL> install_requires = distribution . install_requires <EOL> if install_requires : <EOL> for package in install_requires : <EOL> p [ '<STR_LIT>' ] . append ( parse_setuppy_dependency ( package ) ) <EOL> cf . dump ( '<STR_LIT>' ) <EOL> class CreateDebianization ( Command ) : <EOL> description = "<STR_LIT>" <EOL> user_options = [ <EOL> ] <EOL> def initialize_options ( self ) : <EOL> pass <EOL> def finalize_options ( self ) : <EOL> pass <EOL> def run ( self ) : <EOL> create_debianization ( self . distribution ) </s>
