<s> """<STR_LIT>""" <EOL> import cStringIO <EOL> import logging <EOL> import os <EOL> import shutil <EOL> import struct <EOL> import subprocess <EOL> import zipfile <EOL> try : <EOL> from PyInstaller import __main__ as PyInstallerMain <EOL> except ImportError : <EOL> pass <EOL> from grr . lib import config_lib <EOL> from grr . lib import rdfvalue <EOL> from grr . lib import utils <EOL> from grr . lib . rdfvalues import client as rdf_client <EOL> class BuilderBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context = None ) : <EOL> self . context = context or config_lib . CONFIG . context [ : ] <EOL> self . context = [ "<STR_LIT>" ] + self . context <EOL> def GenerateDirectory ( self , input_dir = None , output_dir = None , <EOL> replacements = None ) : <EOL> input_dir = utils . NormalizePath ( input_dir ) <EOL> output_dir = utils . NormalizePath ( output_dir ) <EOL> replacements = replacements or [ ] <EOL> for ( root , _ , files ) in os . walk ( input_dir ) : <EOL> for filename in files : <EOL> in_file = utils . JoinPath ( root , filename ) <EOL> out_file = in_file . replace ( input_dir , output_dir ) <EOL> for ( s , replacement ) in replacements : <EOL> out_file = out_file . replace ( s , replacement ) <EOL> utils . EnsureDirExists ( os . path . dirname ( out_file ) ) <EOL> self . GenerateFile ( in_file , out_file ) <EOL> def GenerateFile ( self , input_filename = None , output_filename = None ) : <EOL> """<STR_LIT>""" <EOL> if input_filename is None : <EOL> input_filename = output_filename + "<STR_LIT>" <EOL> if output_filename [ - <NUM_LIT:3> : ] == "<STR_LIT>" : <EOL> output_filename = output_filename [ : - <NUM_LIT:3> ] <EOL> data = open ( input_filename , "<STR_LIT:rb>" ) . read ( ) <EOL> logging . debug ( "<STR_LIT>" , output_filename , input_filename ) <EOL> with open ( output_filename , "<STR_LIT:wb>" ) as fd : <EOL> fd . write ( config_lib . CONFIG . InterpolateValue ( data , context = self . context ) ) <EOL> class ClientBuilder ( BuilderBase ) : <EOL> """<STR_LIT>""" <EOL> def MakeBuildDirectory ( self ) : <EOL> """<STR_LIT>""" <EOL> self . build_dir = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> self . work_path = config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = self . context ) <EOL> self . CleanDirectory ( self . build_dir ) <EOL> self . CleanDirectory ( self . work_path ) <EOL> def CleanDirectory ( self , directory ) : <EOL> logging . info ( "<STR_LIT>" , directory ) <EOL> try : <EOL> shutil . rmtree ( directory ) <EOL> except OSError : <EOL> pass <EOL> utils . EnsureDirExists ( directory ) <EOL> def BuildWithPyInstaller ( self ) : <EOL> """<STR_LIT>""" <EOL> self . CleanDirectory ( config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . spec_file = os . path . join ( self . build_dir , "<STR_LIT>" ) <EOL> with open ( self . spec_file , "<STR_LIT:wb>" ) as fd : <EOL> fd . write ( config_lib . CONFIG . Get ( "<STR_LIT>" , context = self . context ) ) <EOL> with open ( os . path . join ( self . build_dir , "<STR_LIT>" ) , "<STR_LIT:wb>" ) as fd : <EOL> fd . write ( config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) ) <EOL> with open ( os . path . join ( self . build_dir , "<STR_LIT>" ) , "<STR_LIT:wb>" ) as fd : <EOL> fd . write ( config_lib . CONFIG . Get ( "<STR_LIT>" , context = self . context ) ) <EOL> self . output_dir = os . path . join ( <EOL> config_lib . CONFIG . Get ( "<STR_LIT>" , context = self . context ) , <EOL> "<STR_LIT>" ) <EOL> args = [ "<STR_LIT>" , str ( config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) ) , <EOL> "<STR_LIT>" , str ( config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) ) , <EOL> str ( self . spec_file ) ] <EOL> logging . info ( "<STR_LIT>" , args ) <EOL> PyInstallerMain . run ( pyi_args = [ utils . SmartStr ( x ) for x in args ] ) <EOL> for path in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> dir_path = os . path . join ( self . output_dir , path ) <EOL> try : <EOL> shutil . rmtree ( dir_path ) <EOL> os . mkdir ( dir_path ) <EOL> with open ( os . path . join ( dir_path , path ) , "<STR_LIT:wb>" ) : <EOL> pass <EOL> except OSError : <EOL> pass <EOL> with open ( os . path . join ( self . output_dir , "<STR_LIT>" ) , "<STR_LIT:w>" ) as fd : <EOL> fd . write ( "<STR_LIT>" % <EOL> rdf_client . Uname . FromCurrentSystem ( ) . signature ( ) ) <EOL> fd . write ( "<STR_LIT>" % <EOL> str ( rdfvalue . RDFDatetime ( ) . Now ( ) ) ) <EOL> def CopyMissingModules ( self ) : <EOL> """<STR_LIT>""" <EOL> def MakeExecutableTemplate ( self , output_file = None ) : <EOL> """<STR_LIT>""" <EOL> self . template_file = output_file or config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = self . context ) <EOL> utils . EnsureDirExists ( os . path . dirname ( self . template_file ) ) <EOL> def MakeZip ( self , input_dir , output_file ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( "<STR_LIT>" , output_file ) <EOL> basename , _ = os . path . splitext ( output_file ) <EOL> shutil . make_archive ( basename , "<STR_LIT>" , <EOL> base_dir = "<STR_LIT:.>" , <EOL> root_dir = input_dir , <EOL> verbose = True ) <EOL> ClientBuilder . classes = globals ( ) <EOL> class ClientDeployer ( BuilderBase ) : <EOL> """<STR_LIT>""" <EOL> CONFIG_SECTIONS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> SKIP_OPTION_LIST = [ "<STR_LIT>" ] <EOL> def __init__ ( self , context = None , signer = None ) : <EOL> super ( ClientDeployer , self ) . __init__ ( context = context ) <EOL> self . signer = signer <EOL> def MergeBuildConfig ( self , fd = None , data = None ) : <EOL> data = config_lib . YamlParser ( fd = fd , data = data ) . RawData ( ) <EOL> config_lib . CONFIG . MergeData ( data ) <EOL> def GetClientConfig ( self , context , validate = True ) : <EOL> """<STR_LIT>""" <EOL> with utils . TempDirectory ( ) as tmp_dir : <EOL> filename = os . path . join ( <EOL> tmp_dir , config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = context ) ) <EOL> new_config = config_lib . CONFIG . MakeNewConfig ( ) <EOL> new_config . Initialize ( reset = True , data = "<STR_LIT>" ) <EOL> new_config . SetWriteBack ( filename ) <EOL> for descriptor in sorted ( config_lib . CONFIG . type_infos , <EOL> key = lambda x : x . name ) : <EOL> if descriptor . name in self . SKIP_OPTION_LIST : <EOL> continue <EOL> if descriptor . section in self . CONFIG_SECTIONS : <EOL> value = config_lib . CONFIG . GetRaw ( <EOL> descriptor . name , context = context , <EOL> default = None ) <EOL> if value is not None : <EOL> logging . debug ( "<STR_LIT>" , <EOL> descriptor . name ) <EOL> new_config . SetRaw ( descriptor . name , value ) <EOL> new_config . Set ( "<STR_LIT>" , <EOL> str ( rdfvalue . RDFDatetime ( ) . Now ( ) ) ) <EOL> plugins = [ ] <EOL> for plugin in config_lib . CONFIG [ "<STR_LIT>" ] : <EOL> plugins . append ( os . path . basename ( plugin ) ) <EOL> new_config . SetRaw ( "<STR_LIT>" , plugins ) <EOL> new_config . Write ( ) <EOL> if validate : <EOL> self . ValidateEndConfig ( new_config ) <EOL> return open ( filename , "<STR_LIT:rb>" ) . read ( ) <EOL> def ValidateEndConfig ( self , config , errors_fatal = True ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> location = config . Get ( "<STR_LIT>" , context = self . context ) <EOL> for url in location : <EOL> if not url . startswith ( "<STR_LIT:http>" ) : <EOL> errors . append ( "<STR_LIT>" % url ) <EOL> key_data = config . GetRaw ( "<STR_LIT>" , <EOL> default = None , context = self . context ) <EOL> if key_data is None : <EOL> errors . append ( "<STR_LIT>" ) <EOL> elif not key_data . startswith ( "<STR_LIT>" ) : <EOL> errors . append ( <EOL> "<STR_LIT>" % key_data ) <EOL> certificate = config . GetRaw ( "<STR_LIT>" , default = None , <EOL> context = self . context ) <EOL> if ( certificate is None or <EOL> not certificate . startswith ( "<STR_LIT>" ) ) : <EOL> errors . append ( "<STR_LIT>" ) <EOL> for bad_opt in [ "<STR_LIT>" ] : <EOL> if config . Get ( bad_opt , context = self . context , default = "<STR_LIT>" ) : <EOL> errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" % bad_opt ) <EOL> if not config . Get ( "<STR_LIT>" ) : <EOL> errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if errors_fatal and errors : <EOL> for error in errors : <EOL> logging . error ( "<STR_LIT>" , error ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> else : <EOL> return errors <EOL> def MakeDeployableBinary ( self , template_path , output_path = None ) : <EOL> """<STR_LIT>""" <EOL> def RepackInstaller ( self , payload_data , output_path = None ) : <EOL> """<STR_LIT>""" <EOL> class WindowsClientDeployer ( ClientDeployer ) : <EOL> """<STR_LIT>""" <EOL> def ValidateEndConfig ( self , config , errors_fatal = True ) : <EOL> """<STR_LIT>""" <EOL> errors = super ( WindowsClientDeployer , self ) . ValidateEndConfig ( <EOL> config , errors_fatal = errors_fatal ) <EOL> for path in config . GetRaw ( "<STR_LIT>" ) : <EOL> if path . startswith ( "<STR_LIT:/>" ) : <EOL> errors . append ( <EOL> "<STR_LIT>" % <EOL> path ) <EOL> if config . GetRaw ( "<STR_LIT>" ) . startswith ( "<STR_LIT:/>" ) : <EOL> errors . append ( "<STR_LIT>" % <EOL> config [ "<STR_LIT>" ] ) <EOL> if "<STR_LIT>" in config . GetRaw ( "<STR_LIT>" ) : <EOL> errors . append ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not config [ "<STR_LIT>" ] . endswith ( "<STR_LIT>" ) : <EOL> errors . append ( "<STR_LIT>" % <EOL> config [ "<STR_LIT>" ] ) <EOL> if not config [ "<STR_LIT>" ] . endswith ( "<STR_LIT>" ) : <EOL> errors . append ( "<STR_LIT>" ) <EOL> if errors_fatal and errors : <EOL> for error in errors : <EOL> logging . error ( "<STR_LIT>" , error ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> else : <EOL> return errors <EOL> def InterpolateVariableInBinary ( self , stream , parameter ) : <EOL> """<STR_LIT>""" <EOL> pattern = "<STR_LIT>" % parameter <EOL> pattern = pattern . encode ( "<STR_LIT>" ) <EOL> replacement = config_lib . CONFIG . Get ( parameter , context = self . context ) <EOL> replacement = utils . SmartStr ( replacement + "<STR_LIT:\x00>" ) . encode ( "<STR_LIT>" ) <EOL> replacement = replacement [ : len ( pattern ) ] <EOL> replacement += "<STR_LIT:\x00>" * ( len ( pattern ) - len ( replacement ) ) <EOL> start = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> offset = stream . getvalue ( ) . find ( pattern , start ) <EOL> if offset < <NUM_LIT:0> : <EOL> return <EOL> stream . seek ( offset ) <EOL> stream . write ( replacement ) <EOL> def Sign ( self , inbuf ) : <EOL> if self . signer : <EOL> return self . signer . SignBuffer ( inbuf ) <EOL> return inbuf <EOL> def MakeDeployableBinary ( self , template_path , output_path = None ) : <EOL> """<STR_LIT>""" <EOL> context = self . context + [ "<STR_LIT>" ] <EOL> zip_data = cStringIO . StringIO ( ) <EOL> output_zip = zipfile . ZipFile ( <EOL> zip_data , mode = "<STR_LIT:w>" , compression = zipfile . ZIP_DEFLATED ) <EOL> z_template = zipfile . ZipFile ( open ( template_path , "<STR_LIT:rb>" ) ) <EOL> completed_files = [ ] <EOL> client_bin_name = config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = context ) <EOL> extension = "<STR_LIT>" <EOL> if ( "<STR_LIT>" == <EOL> config_lib . CONFIG . Get ( "<STR_LIT>" , context = context ) ) : <EOL> extension = "<STR_LIT>" <EOL> bin_name = z_template . getinfo ( "<STR_LIT>" + extension ) <EOL> bin_dat = cStringIO . StringIO ( ) <EOL> bin_dat . write ( z_template . read ( bin_name ) ) <EOL> SetPeSubsystem ( bin_dat , console = config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = context ) ) <EOL> for parameter in [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] : <EOL> self . InterpolateVariableInBinary ( bin_dat , parameter ) <EOL> output_zip . writestr ( client_bin_name , self . Sign ( bin_dat . getvalue ( ) ) ) <EOL> CopyFileInZip ( z_template , "<STR_LIT>" % bin_name . filename , output_zip , <EOL> "<STR_LIT>" % client_bin_name ) <EOL> completed_files . append ( bin_name . filename ) <EOL> completed_files . append ( "<STR_LIT>" % bin_name . filename ) <EOL> for name in z_template . namelist ( ) : <EOL> if os . path . basename ( name ) == "<STR_LIT>" : <EOL> data = z_template . read ( name ) <EOL> self . MergeBuildConfig ( data = data ) <EOL> completed_files . append ( name ) <EOL> break <EOL> service_template = z_template . getinfo ( "<STR_LIT>" ) <EOL> service_bin_dat = cStringIO . StringIO ( ) <EOL> service_bin_dat . write ( z_template . read ( service_template ) ) <EOL> SetPeSubsystem ( service_bin_dat , <EOL> console = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = context ) ) <EOL> service_bin_name = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = context ) <EOL> output_zip . writestr ( service_bin_name , self . Sign ( service_bin_dat . getvalue ( ) ) ) <EOL> completed_files . append ( service_template . filename ) <EOL> for template_file in z_template . namelist ( ) : <EOL> if template_file not in completed_files : <EOL> CopyFileInZip ( z_template , template_file , output_zip ) <EOL> for plugin in config_lib . CONFIG . Get ( "<STR_LIT>" , context = context ) : <EOL> logging . debug ( "<STR_LIT>" , plugin ) <EOL> output_zip . writestr ( os . path . basename ( plugin ) , <EOL> open ( plugin , "<STR_LIT:rb>" ) . read ( ) , zipfile . ZIP_STORED ) <EOL> output_zip . close ( ) <EOL> return self . RepackInstaller ( zip_data . getvalue ( ) , output_path ) <EOL> def RepackInstaller ( self , payload_data , output_path = None ) : <EOL> """<STR_LIT>""" <EOL> if output_path is None : <EOL> output_path = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> context = self . context + [ "<STR_LIT>" ] <EOL> src_zip = zipfile . ZipFile ( cStringIO . StringIO ( payload_data ) , mode = "<STR_LIT:r>" ) <EOL> zip_data = cStringIO . StringIO ( ) <EOL> output_zip = zipfile . ZipFile ( <EOL> zip_data , mode = "<STR_LIT:w>" , compression = zipfile . ZIP_DEFLATED ) <EOL> config_file_name = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = context ) <EOL> for template_file in src_zip . namelist ( ) : <EOL> if template_file != config_file_name : <EOL> CopyFileInZip ( src_zip , template_file , output_zip ) <EOL> client_config_content = self . GetClientConfig ( context ) <EOL> output_zip . writestr ( config_file_name , <EOL> client_config_content , compress_type = zipfile . ZIP_STORED ) <EOL> output_zip . comment = "<STR_LIT>" % config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = context ) <EOL> output_zip . close ( ) <EOL> utils . EnsureDirExists ( os . path . dirname ( output_path ) ) <EOL> with open ( output_path , "<STR_LIT:wb>" ) as fd : <EOL> stub_data = cStringIO . StringIO ( ) <EOL> unzipsfx_stub = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = context ) <EOL> stub_raw = open ( unzipsfx_stub , "<STR_LIT:rb>" ) . read ( ) <EOL> if "<STR_LIT>" not in stub_raw : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> stub_data . write ( stub_raw ) <EOL> SetPeSubsystem ( <EOL> stub_data , <EOL> console = config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = context ) ) <EOL> end_of_file = zip_data . tell ( ) + stub_data . tell ( ) <EOL> offset_to_rsrc = stub_data . getvalue ( ) . find ( "<STR_LIT>" ) <EOL> stub_data . seek ( offset_to_rsrc + <NUM_LIT:20> ) <EOL> start_of_rsrc_section = struct . unpack ( "<STR_LIT>" , stub_data . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] <EOL> stub_data . seek ( offset_to_rsrc + <NUM_LIT:16> ) <EOL> stub_data . write ( struct . pack ( "<STR_LIT>" , end_of_file - start_of_rsrc_section ) ) <EOL> out_data = cStringIO . StringIO ( ) <EOL> out_data . write ( stub_data . getvalue ( ) ) <EOL> out_data . write ( zip_data . getvalue ( ) ) <EOL> fd . write ( self . Sign ( out_data . getvalue ( ) ) ) <EOL> logging . info ( "<STR_LIT>" , output_path ) <EOL> return output_path <EOL> class DarwinClientDeployer ( ClientDeployer ) : <EOL> """<STR_LIT>""" <EOL> def MakeDeployableBinary ( self , template_path , output_path = None ) : <EOL> """<STR_LIT>""" <EOL> if output_path is None : <EOL> output_path = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> context = self . context + [ "<STR_LIT>" ] <EOL> utils . EnsureDirExists ( os . path . dirname ( output_path ) ) <EOL> config_lib . CONFIG . Set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> client_config_data = self . GetClientConfig ( context ) <EOL> shutil . copyfile ( template_path , output_path ) <EOL> zip_file = zipfile . ZipFile ( output_path , mode = "<STR_LIT:a>" ) <EOL> zip_info = zipfile . ZipInfo ( filename = "<STR_LIT>" ) <EOL> zip_file . writestr ( zip_info , client_config_data ) <EOL> zip_file . close ( ) <EOL> return output_path <EOL> class LinuxClientDeployer ( ClientDeployer ) : <EOL> """<STR_LIT>""" <EOL> def GenerateDPKGFiles ( self , template_path ) : <EOL> """<STR_LIT>""" <EOL> template_binary_dir = os . path . join ( <EOL> template_path , "<STR_LIT>" ) <EOL> package_name = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> target_binary_dir = os . path . join ( template_path , "<STR_LIT>" % ( <EOL> package_name , config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) ) ) <EOL> if package_name == "<STR_LIT>" : <EOL> shutil . move ( template_binary_dir , "<STR_LIT>" % template_binary_dir ) <EOL> template_binary_dir = "<STR_LIT>" % template_binary_dir <EOL> utils . EnsureDirExists ( os . path . dirname ( target_binary_dir ) ) <EOL> shutil . move ( template_binary_dir , target_binary_dir ) <EOL> shutil . move ( <EOL> os . path . join ( target_binary_dir , "<STR_LIT>" ) , <EOL> os . path . join ( <EOL> target_binary_dir , <EOL> config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) ) ) <EOL> deb_in_dir = os . path . join ( template_path , "<STR_LIT>" ) <EOL> self . GenerateDirectory ( <EOL> deb_in_dir , os . path . join ( template_path , "<STR_LIT>" ) , <EOL> [ ( "<STR_LIT>" , package_name ) ] ) <EOL> utils . EnsureDirExists ( os . path . join ( <EOL> template_path , "<STR_LIT>" % package_name ) ) <EOL> self . GenerateFile ( <EOL> os . path . join ( template_path , "<STR_LIT>" ) , <EOL> os . path . join ( template_path , "<STR_LIT>" % package_name ) ) <EOL> self . GenerateFile ( <EOL> os . path . join ( template_path , "<STR_LIT>" ) , <EOL> os . path . join ( template_path , "<STR_LIT>" % package_name ) ) <EOL> self . GenerateFile ( <EOL> os . path . join ( template_path , <EOL> "<STR_LIT>" ) , <EOL> os . path . join ( template_path , "<STR_LIT>" % package_name ) ) <EOL> shutil . rmtree ( deb_in_dir ) <EOL> shutil . rmtree ( os . path . join ( template_path , "<STR_LIT>" ) ) <EOL> shutil . rmtree ( os . path . join ( template_path , "<STR_LIT>" ) ) <EOL> shutil . rmtree ( os . path . join ( template_path , "<STR_LIT>" ) ) <EOL> def MakeDeployableBinary ( self , template_path , output_path = None ) : <EOL> """<STR_LIT>""" <EOL> if output_path is None : <EOL> output_path = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> buildpackage_binary = "<STR_LIT>" <EOL> if not os . path . exists ( buildpackage_binary ) : <EOL> logging . error ( "<STR_LIT>" ) <EOL> return <EOL> with utils . TempDirectory ( ) as tmp_dir : <EOL> template_dir = os . path . join ( tmp_dir , "<STR_LIT>" ) <EOL> utils . EnsureDirExists ( template_dir ) <EOL> zf = zipfile . ZipFile ( template_path ) <EOL> for name in zf . namelist ( ) : <EOL> if os . path . basename ( name ) == "<STR_LIT>" : <EOL> self . MergeBuildConfig ( data = zf . read ( name ) ) <EOL> continue <EOL> dirname = os . path . dirname ( name ) <EOL> utils . EnsureDirExists ( os . path . join ( template_dir , dirname ) ) <EOL> with open ( os . path . join ( template_dir , name ) , "<STR_LIT:wb>" ) as fd : <EOL> fd . write ( zf . read ( name ) ) <EOL> self . GenerateDPKGFiles ( tmp_dir ) <EOL> client_context = [ "<STR_LIT>" ] + self . context <EOL> client_config_content = self . GetClientConfig ( client_context ) <EOL> target_dir = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) . lstrip ( "<STR_LIT:/>" ) <EOL> agent_dir = os . path . join ( <EOL> template_dir , "<STR_LIT>" , <EOL> config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) , <EOL> target_dir ) <EOL> with open ( os . path . join ( agent_dir , config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = self . context ) ) , "<STR_LIT:wb>" ) as fd : <EOL> fd . write ( client_config_content ) <EOL> os . chmod ( os . path . join ( agent_dir , config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = self . context ) ) , <NUM_LIT:0> <NUM_LIT> ) <EOL> arch = config_lib . CONFIG . Get ( "<STR_LIT>" , context = self . context ) <EOL> try : <EOL> old_working_dir = os . getcwd ( ) <EOL> except OSError : <EOL> old_working_dir = os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> try : <EOL> os . chdir ( template_dir ) <EOL> command = [ buildpackage_binary , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" % arch ] <EOL> try : <EOL> subprocess . check_output ( command , stderr = subprocess . STDOUT ) <EOL> except subprocess . CalledProcessError as e : <EOL> if "<STR_LIT>" not in e . output : <EOL> logging . error ( "<STR_LIT>" , command ) <EOL> logging . error ( e . output ) <EOL> raise <EOL> filename_base = config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = self . context ) <EOL> output_base = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> utils . EnsureDirExists ( os . path . dirname ( output_path ) ) <EOL> for extension in [ "<STR_LIT>" , config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = self . context ) ] : <EOL> input_name = "<STR_LIT>" % ( filename_base , extension ) <EOL> output_name = "<STR_LIT>" % ( output_base , extension ) <EOL> shutil . move ( os . path . join ( tmp_dir , input_name ) , <EOL> os . path . join ( os . path . dirname ( output_path ) , output_name ) ) <EOL> logging . info ( "<STR_LIT>" , output_path ) <EOL> return output_path <EOL> finally : <EOL> try : <EOL> os . chdir ( old_working_dir ) <EOL> except OSError : <EOL> pass <EOL> class CentosClientDeployer ( LinuxClientDeployer ) : <EOL> """<STR_LIT>""" <EOL> def Sign ( self , rpm_filename ) : <EOL> if self . signer : <EOL> return self . signer . AddSignatureToRPM ( rpm_filename ) <EOL> def MakeDeployableBinary ( self , template_path , output_path = None ) : <EOL> """<STR_LIT>""" <EOL> if output_path is None : <EOL> output_path = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> rpmbuild_binary = "<STR_LIT>" <EOL> if not os . path . exists ( rpmbuild_binary ) : <EOL> logging . error ( "<STR_LIT>" ) <EOL> return <EOL> with utils . TempDirectory ( ) as tmp_dir : <EOL> template_dir = os . path . join ( tmp_dir , "<STR_LIT>" ) <EOL> utils . EnsureDirExists ( template_dir ) <EOL> zf = zipfile . ZipFile ( template_path ) <EOL> for name in zf . namelist ( ) : <EOL> if os . path . basename ( name ) == "<STR_LIT>" : <EOL> self . MergeBuildConfig ( data = zf . read ( name ) ) <EOL> continue <EOL> dirname = os . path . dirname ( name ) <EOL> utils . EnsureDirExists ( os . path . join ( template_dir , dirname ) ) <EOL> with open ( os . path . join ( template_dir , name ) , "<STR_LIT:wb>" ) as fd : <EOL> fd . write ( zf . read ( name ) ) <EOL> rpm_root_dir = os . path . join ( tmp_dir , "<STR_LIT>" ) <EOL> rpm_build_dir = os . path . join ( rpm_root_dir , "<STR_LIT>" ) <EOL> utils . EnsureDirExists ( rpm_build_dir ) <EOL> rpm_buildroot_dir = os . path . join ( rpm_root_dir , "<STR_LIT>" ) <EOL> utils . EnsureDirExists ( rpm_buildroot_dir ) <EOL> rpm_rpms_dir = os . path . join ( rpm_root_dir , "<STR_LIT>" ) <EOL> utils . EnsureDirExists ( rpm_rpms_dir ) <EOL> rpm_specs_dir = os . path . join ( rpm_root_dir , "<STR_LIT>" ) <EOL> utils . EnsureDirExists ( rpm_specs_dir ) <EOL> template_binary_dir = os . path . join ( <EOL> tmp_dir , "<STR_LIT>" ) <EOL> target_binary_dir = "<STR_LIT>" % ( <EOL> rpm_build_dir , config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) ) <EOL> utils . EnsureDirExists ( os . path . dirname ( target_binary_dir ) ) <EOL> try : <EOL> shutil . rmtree ( target_binary_dir ) <EOL> except OSError : <EOL> pass <EOL> shutil . move ( template_binary_dir , target_binary_dir ) <EOL> client_name = config_lib . CONFIG . Get ( "<STR_LIT>" , context = self . context ) <EOL> client_binary_name = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> if client_binary_name != "<STR_LIT>" : <EOL> shutil . move ( os . path . join ( target_binary_dir , "<STR_LIT>" ) , <EOL> os . path . join ( target_binary_dir , client_binary_name ) ) <EOL> spec_filename = os . path . join ( rpm_specs_dir , "<STR_LIT>" % <EOL> client_name ) <EOL> self . GenerateFile ( os . path . join ( <EOL> tmp_dir , "<STR_LIT>" ) , spec_filename ) <EOL> initd_target_filename = os . path . join ( <EOL> rpm_build_dir , "<STR_LIT>" , client_name ) <EOL> utils . EnsureDirExists ( os . path . dirname ( initd_target_filename ) ) <EOL> self . GenerateFile ( <EOL> os . path . join ( tmp_dir , "<STR_LIT>" ) , <EOL> initd_target_filename ) <EOL> client_context = [ "<STR_LIT>" ] + self . context <EOL> client_config_content = self . GetClientConfig ( client_context ) <EOL> with open ( os . path . join ( target_binary_dir , config_lib . CONFIG . Get ( <EOL> "<STR_LIT>" , context = self . context ) ) , "<STR_LIT:wb>" ) as fd : <EOL> fd . write ( client_config_content ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> prelink = "<STR_LIT>" <EOL> if os . access ( prelink , os . X_OK ) : <EOL> libs = os . path . join ( target_binary_dir , "<STR_LIT>" ) <EOL> subprocess . call ( "<STR_LIT>" % ( prelink , libs ) , shell = True ) <EOL> else : <EOL> logging . info ( "<STR_LIT>" , prelink ) <EOL> os . chmod ( os . path . join ( target_binary_dir , client_binary_name ) , <NUM_LIT:0> <NUM_LIT> ) <EOL> client_arch = config_lib . CONFIG . Get ( "<STR_LIT>" , context = self . context ) <EOL> if client_arch == "<STR_LIT>" : <EOL> client_arch = "<STR_LIT>" <EOL> command = [ <EOL> rpmbuild_binary , <EOL> "<STR_LIT>" , "<STR_LIT>" + rpm_root_dir , <EOL> "<STR_LIT>" , client_arch , <EOL> "<STR_LIT>" , rpm_buildroot_dir , <EOL> "<STR_LIT>" , spec_filename ] <EOL> try : <EOL> subprocess . check_output ( command , stderr = subprocess . STDOUT ) <EOL> except subprocess . CalledProcessError as e : <EOL> logging . error ( "<STR_LIT>" , command ) <EOL> logging . error ( e . output ) <EOL> raise <EOL> client_version = config_lib . CONFIG . Get ( "<STR_LIT>" , <EOL> context = self . context ) <EOL> rpm_filename = os . path . join ( <EOL> rpm_rpms_dir , client_arch , <EOL> "<STR_LIT>" % ( client_name , client_version , client_arch ) ) <EOL> utils . EnsureDirExists ( os . path . dirname ( output_path ) ) <EOL> shutil . move ( rpm_filename , output_path ) <EOL> logging . info ( "<STR_LIT>" , output_path ) <EOL> self . Sign ( output_path ) <EOL> return output_path <EOL> def CopyFileInZip ( from_zip , from_name , to_zip , to_name = None , signer = None ) : <EOL> """<STR_LIT>""" <EOL> data = from_zip . read ( from_name ) <EOL> if to_name is None : <EOL> to_name = from_name <EOL> if signer : <EOL> data = signer . SignBuffer ( data ) <EOL> to_zip . writestr ( to_name , data ) <EOL> def SetPeSubsystem ( fd , console = True ) : <EOL> """<STR_LIT>""" <EOL> current_pos = fd . tell ( ) <EOL> fd . seek ( <NUM_LIT> ) <EOL> header_offset = struct . unpack ( "<STR_LIT>" , fd . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] <EOL> subsystem_offset = header_offset + <NUM_LIT> <EOL> fd . seek ( subsystem_offset ) <EOL> if console : <EOL> fd . write ( "<STR_LIT>" ) <EOL> else : <EOL> fd . write ( "<STR_LIT>" ) <EOL> fd . seek ( current_pos ) </s>
