<s> """<STR_LIT>""" <EOL> import codecs <EOL> from collections import defaultdict <EOL> import gzip as gziplib <EOL> import os <EOL> import os . path <EOL> import re <EOL> from shutil import copyfile <EOL> import tarfile <EOL> import unicodedata <EOL> from blueprint import git <EOL> from blueprint import util <EOL> def sh ( b , relaxed = False , server = '<STR_LIT>' , secret = None ) : <EOL> """<STR_LIT>""" <EOL> s = Script ( b . name , comment = b . DISCLAIMER ) <EOL> lut = { '<STR_LIT>' : defaultdict ( set ) , <EOL> '<STR_LIT>' : defaultdict ( lambda : defaultdict ( set ) ) , <EOL> '<STR_LIT>' : defaultdict ( set ) } <EOL> def service_file ( manager , service , pathname ) : <EOL> lut [ '<STR_LIT>' ] [ pathname ] . add ( ( manager , service ) ) <EOL> def service_package ( manager , service , package_manager , package ) : <EOL> lut [ '<STR_LIT>' ] [ package_manager ] [ package ] . add ( ( manager , service ) ) <EOL> def service_source ( manager , service , dirname ) : <EOL> lut [ '<STR_LIT>' ] [ dirname ] . add ( ( manager , service ) ) <EOL> b . walk ( service_file = service_file , <EOL> service_package = service_package , <EOL> service_source = service_source ) <EOL> commit = git . rev_parse ( b . name ) <EOL> tree = None if commit is None else git . tree ( commit ) <EOL> def source ( dirname , filename , gen_content , url ) : <EOL> """<STR_LIT>""" <EOL> if dirname in lut [ '<STR_LIT>' ] : <EOL> s . add ( '<STR_LIT>' , <EOL> args = ( dirname , ) ) <EOL> if url is not None : <EOL> s . add_list ( ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> args = ( filename , url ) , <EOL> operator = '<STR_LIT>' ) <EOL> if '<STR_LIT>' == pathname [ - <NUM_LIT:4> : ] : <EOL> s . add ( '<STR_LIT>' , args = ( filename , dirname ) ) <EOL> else : <EOL> s . add ( '<STR_LIT>' , args = ( filename , dirname ) ) <EOL> elif secret is not None : <EOL> s . add_list ( ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> args = ( server , secret , b . name , filename ) , <EOL> operator = '<STR_LIT>' ) <EOL> s . add ( '<STR_LIT>' , args = ( filename , dirname ) ) <EOL> elif gen_content is not None : <EOL> s . add ( '<STR_LIT>' , args = ( filename , dirname ) ) <EOL> s . add_source ( filename , git . blob ( tree , filename ) ) <EOL> for manager , service in lut [ '<STR_LIT>' ] [ dirname ] : <EOL> s . add_list ( ( '<STR_LIT>' <EOL> '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> args = ( dirname , manager . env_var ( service ) ) , <EOL> operator = '<STR_LIT>' ) <EOL> def file ( pathname , f ) : <EOL> """<STR_LIT>""" <EOL> if pathname in lut [ '<STR_LIT>' ] : <EOL> s . add ( '<STR_LIT>' , args = ( pathname , ) ) <EOL> s . add ( '<STR_LIT>' , args = ( os . path . dirname ( pathname ) , ) ) <EOL> if '<STR_LIT>' == f [ '<STR_LIT>' ] or '<STR_LIT>' == f [ '<STR_LIT>' ] : <EOL> s . add ( '<STR_LIT>' , args = ( f [ '<STR_LIT:content>' ] , pathname ) ) <EOL> else : <EOL> if '<STR_LIT:source>' in f : <EOL> s . add_list ( ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> args = ( pathname , f [ '<STR_LIT:source>' ] ) , <EOL> operator = '<STR_LIT>' ) <EOL> else : <EOL> if '<STR_LIT>' in f : <EOL> s . templates = True <EOL> if '<STR_LIT>' == f [ '<STR_LIT>' ] : <EOL> commands = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> commands = ( '<STR_LIT>' , ) <EOL> s . add_list ( ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> ( f . get ( '<STR_LIT:data>' , '<STR_LIT>' ) . rstrip ( ) , ) , <EOL> ( command ( * commands , <EOL> escape_stdin = True , <EOL> stdin = f [ '<STR_LIT>' ] , <EOL> stdout = pathname ) , ) , <EOL> operator = '<STR_LIT:\n>' , <EOL> wrapper = '<STR_LIT>' ) <EOL> else : <EOL> if '<STR_LIT>' == f [ '<STR_LIT>' ] : <EOL> commands = ( '<STR_LIT>' , ) <EOL> else : <EOL> commands = ( '<STR_LIT>' , ) <EOL> s . add ( * commands , stdin = f [ '<STR_LIT:content>' ] , stdout = pathname ) <EOL> if '<STR_LIT:root>' != f [ '<STR_LIT>' ] : <EOL> s . add ( '<STR_LIT>' , args = ( f [ '<STR_LIT>' ] , pathname ) ) <EOL> if '<STR_LIT:root>' != f [ '<STR_LIT>' ] : <EOL> s . add ( '<STR_LIT>' , args = ( f [ '<STR_LIT>' ] , pathname ) ) <EOL> if '<STR_LIT>' != f [ '<STR_LIT>' ] : <EOL> s . add ( '<STR_LIT>' , args = ( f [ '<STR_LIT>' ] [ - <NUM_LIT:4> : ] , pathname ) ) <EOL> for manager , service in lut [ '<STR_LIT>' ] [ pathname ] : <EOL> s . add ( '<STR_LIT>' , <EOL> args = ( pathname , manager . env_var ( service ) ) ) <EOL> def before_packages ( manager ) : <EOL> """<STR_LIT>""" <EOL> if manager not in b . packages : <EOL> return <EOL> if '<STR_LIT>' == manager : <EOL> s . add ( '<STR_LIT>' ) <EOL> s . add ( '<STR_LIT>' ) <EOL> s . add ( '<STR_LIT>' ) <EOL> s . add ( '<STR_LIT>' ) <EOL> elif '<STR_LIT>' == manager : <EOL> s . add ( '<STR_LIT>' ) <EOL> def package ( manager , package , version ) : <EOL> """<STR_LIT>""" <EOL> if manager == package : <EOL> return <EOL> if manager in lut [ '<STR_LIT>' ] and package in lut [ '<STR_LIT>' ] [ manager ] : <EOL> s . add_list ( ( manager . gate ( package , version , relaxed ) , ) , <EOL> ( command_list ( ( manager . install ( package , <EOL> version , <EOL> relaxed ) , ) , <EOL> * [ ( '<STR_LIT>' . format ( m . env_var ( service ) ) , ) <EOL> for m , service in <EOL> lut [ '<STR_LIT>' ] [ manager ] [ package ] ] , <EOL> wrapper = '<STR_LIT>' ) , ) , <EOL> operator = '<STR_LIT>' ) <EOL> else : <EOL> s . add ( manager ( package , version , relaxed ) ) <EOL> if manager not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return <EOL> match = re . match ( r'<STR_LIT>' , package ) <EOL> if match is not None and util . rubygems_update ( ) : <EOL> s . add ( '<STR_LIT>' , <EOL> args = ( match . group ( <NUM_LIT:1> ) , ) ) <EOL> s . add ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> args = ( match . group ( <NUM_LIT:1> ) , ) ) <EOL> if '<STR_LIT>' == package : <EOL> s . add_list ( ( '<STR_LIT>' , ) , <EOL> ( command_list ( ( '<STR_LIT>' , ) , <EOL> ( '<STR_LIT>' , ) , <EOL> operator = '<STR_LIT>' , <EOL> wrapper = '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) , <EOL> operator = '<STR_LIT>' ) <EOL> def service ( manager , service ) : <EOL> s . add ( manager ( service ) ) <EOL> b . walk ( source = source , <EOL> file = file , <EOL> before_packages = before_packages , <EOL> package = package , <EOL> service = service ) <EOL> return s <EOL> def command ( * commands , ** kwargs ) : <EOL> commands = list ( commands ) <EOL> if '<STR_LIT>' in kwargs : <EOL> commands [ - <NUM_LIT:1> ] += '<STR_LIT>' . format ( kwargs [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in kwargs : <EOL> stdin = ( kwargs [ '<STR_LIT>' ] . replace ( u'<STR_LIT:\\>' , u'<STR_LIT>' ) . <EOL> replace ( u'<STR_LIT:$>' , u'<STR_LIT>' ) . <EOL> replace ( u'<STR_LIT>' , u'<STR_LIT>' ) ) <EOL> if kwargs . get ( '<STR_LIT>' , False ) : <EOL> stdin = stdin . replace ( u'<STR_LIT:{>' , u'<STR_LIT>' ) . replace ( u'<STR_LIT:}>' , u'<STR_LIT>' ) <EOL> eof = '<STR_LIT>' <EOL> while eof in stdin : <EOL> eof += '<STR_LIT>' <EOL> commands [ <NUM_LIT:0> ] += '<STR_LIT>' . format ( eof ) <EOL> return '<STR_LIT>' . join ( [ '<STR_LIT>' . join ( commands ) . format ( * kwargs . get ( '<STR_LIT:args>' , ( ) ) ) , <EOL> '<STR_LIT:\n>' , <EOL> stdin , <EOL> '<STR_LIT>' if '<STR_LIT>' == stdin or '<STR_LIT:\n>' == stdin [ - <NUM_LIT:1> ] else '<STR_LIT:\n>' , <EOL> eof ] ) <EOL> return '<STR_LIT>' . join ( commands ) . format ( * kwargs . get ( '<STR_LIT:args>' , ( ) ) ) <EOL> def command_list ( * commands , ** kwargs ) : <EOL> operator = { '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT:\n>' : u'<STR_LIT:\n>' , <EOL> '<STR_LIT:;>' : u'<STR_LIT>' } [ kwargs . get ( '<STR_LIT>' , '<STR_LIT:;>' ) ] <EOL> wrapper = { '<STR_LIT>' : ( u'<STR_LIT>' , u'<STR_LIT>' ) if u'<STR_LIT:\n>' == operator else ( u'<STR_LIT:(>' , u'<STR_LIT:)>' ) , <EOL> '<STR_LIT:{}>' : ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( u'<STR_LIT>' , u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( u'<STR_LIT>' , u'<STR_LIT>' ) } [ kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> return wrapper [ <NUM_LIT:0> ] + operator . join ( [ command ( * c , ** kwargs ) for c in commands ] ) + wrapper [ - <NUM_LIT:1> ] <EOL> class Script ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , comment = None ) : <EOL> if name is None : <EOL> self . name = '<STR_LIT>' <EOL> else : <EOL> self . name = name <EOL> self . out = [ comment if comment is not None else '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> self . sources = { } <EOL> self . templates = False <EOL> def add ( self , s = '<STR_LIT>' , * args , ** kwargs ) : <EOL> self . out . append ( ( unicode ( s ) + u'<STR_LIT:\n>' ) . format ( * args ) ) <EOL> for filename , content in kwargs . get ( '<STR_LIT>' , { } ) . iteritems ( ) : <EOL> self . sources [ filename ] = content <EOL> def add ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . out . append ( command ( * args , ** kwargs ) ) <EOL> def add_list ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . out . append ( command_list ( * args , ** kwargs ) ) <EOL> def add_source ( self , filename , blob ) : <EOL> """<STR_LIT>""" <EOL> self . sources [ filename ] = blob <EOL> def dumps ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( self . out ) <EOL> def dumpf ( self , gzip = False ) : <EOL> """<STR_LIT>""" <EOL> if <NUM_LIT:0> < len ( self . sources ) or self . templates : <EOL> os . mkdir ( self . name ) <EOL> filename = os . path . join ( self . name , '<STR_LIT>' ) <EOL> f = codecs . open ( filename , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) <EOL> elif gzip : <EOL> filename = '<STR_LIT>' . format ( self . name ) <EOL> f = gziplib . open ( filename , '<STR_LIT:w>' ) <EOL> else : <EOL> filename = '<STR_LIT>' . format ( self . name ) <EOL> f = codecs . open ( filename , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) <EOL> if self . templates : <EOL> os . mkdir ( os . path . join ( self . name , '<STR_LIT>' ) ) <EOL> os . mkdir ( os . path . join ( self . name , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> os . mkdir ( os . path . join ( self . name , '<STR_LIT>' ) ) <EOL> os . mkdir ( os . path . join ( self . name , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> copyfile ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) , <EOL> os . path . join ( self . name , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for src , dest in [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( os . path . join ( os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) ] : <EOL> try : <EOL> for filename2 in os . listdir ( src ) : <EOL> if filename2 . endswith ( '<STR_LIT>' ) : <EOL> copyfile ( os . path . join ( src , filename2 ) , <EOL> os . path . join ( self . name , <EOL> dest , <EOL> '<STR_LIT>' , <EOL> filename2 ) ) <EOL> except OSError : <EOL> pass <EOL> for out in self . out : <EOL> if isinstance ( out , unicode ) : <EOL> out = unicodedata . normalize ( '<STR_LIT>' , out ) . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:ignore>' ) <EOL> f . write ( '<STR_LIT>' . format ( out ) ) <EOL> f . close ( ) <EOL> for filename2 , blob in sorted ( self . sources . iteritems ( ) ) : <EOL> git . cat_file ( blob , os . path . join ( self . name , filename2 ) ) <EOL> if gzip and ( <NUM_LIT:0> < len ( self . sources ) or self . templates ) : <EOL> filename = '<STR_LIT>' . format ( self . name ) <EOL> tarball = tarfile . open ( filename , '<STR_LIT>' ) <EOL> tarball . add ( self . name ) <EOL> tarball . close ( ) <EOL> return filename <EOL> return filename </s>
