<s> import errno <EOL> import hexagonit . recipe . download <EOL> import imp <EOL> import logging <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import zc . buildout <EOL> class Recipe ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , buildout , name , options ) : <EOL> self . options = options <EOL> self . buildout = buildout <EOL> self . name = name <EOL> options [ '<STR_LIT:location>' ] = os . path . join ( <EOL> buildout [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . name ) <EOL> options [ '<STR_LIT>' ] = options . get ( '<STR_LIT>' , options [ '<STR_LIT:location>' ] ) <EOL> options [ '<STR_LIT:url>' ] = options . get ( '<STR_LIT:url>' , '<STR_LIT>' ) . strip ( ) <EOL> options [ '<STR_LIT:path>' ] = options . get ( '<STR_LIT:path>' , '<STR_LIT>' ) . strip ( ) <EOL> if options [ '<STR_LIT:url>' ] and options [ '<STR_LIT:path>' ] : <EOL> raise zc . buildout . UserError ( '<STR_LIT>' ) <EOL> if not ( options [ '<STR_LIT:url>' ] or options [ '<STR_LIT:path>' ] ) : <EOL> raise zc . buildout . UserError ( '<STR_LIT>' ) <EOL> if options [ '<STR_LIT:url>' ] : <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % options [ '<STR_LIT:location>' ] <EOL> else : <EOL> options [ '<STR_LIT>' ] = options [ '<STR_LIT:path>' ] <EOL> self . environ = { } <EOL> self . original_environment = os . environ . copy ( ) <EOL> environment_section = self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> if environment_section and environment_section in buildout : <EOL> self . environ . update ( buildout [ environment_section ] ) <EOL> for variable in self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . splitlines ( ) : <EOL> if variable . strip ( ) : <EOL> try : <EOL> key , value = variable . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> self . environ [ key . strip ( ) ] = value <EOL> except ValueError : <EOL> raise zc . buildout . UserError ( '<STR_LIT>' , variable ) <EOL> for key in self . environ : <EOL> self . environ [ key ] = self . environ [ key ] % os . environ <EOL> def augmented_environment ( self ) : <EOL> """<STR_LIT>""" <EOL> env = os . environ . copy ( ) <EOL> env . update ( self . environ ) <EOL> return env <EOL> def update ( self ) : <EOL> pass <EOL> def call_script ( self , script ) : <EOL> """<STR_LIT>""" <EOL> filename , callable = script . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> filename = os . path . abspath ( filename ) <EOL> module = imp . load_source ( '<STR_LIT>' , filename ) <EOL> script = getattr ( module , callable . strip ( ) ) <EOL> try : <EOL> script ( self . options , self . buildout , self . augmented_environment ( ) ) <EOL> except TypeError : <EOL> script ( self . options , self . buildout ) <EOL> def run ( self , cmd ) : <EOL> """<STR_LIT>""" <EOL> log = logging . getLogger ( self . name ) <EOL> try : <EOL> retcode = subprocess . call ( cmd , shell = True , env = self . augmented_environment ( ) ) <EOL> if retcode < <NUM_LIT:0> : <EOL> log . error ( '<STR_LIT>' % ( - retcode , cmd ) ) <EOL> raise zc . buildout . UserError ( '<STR_LIT>' ) <EOL> elif retcode > <NUM_LIT:0> : <EOL> log . error ( '<STR_LIT>' % ( retcode , cmd ) ) <EOL> raise zc . buildout . UserError ( '<STR_LIT>' ) <EOL> except OSError as e : <EOL> log . error ( '<STR_LIT>' % ( e , cmd ) ) <EOL> raise zc . buildout . UserError ( '<STR_LIT>' ) <EOL> def install ( self ) : <EOL> log = logging . getLogger ( self . name ) <EOL> parts = [ ] <EOL> make_cmd = self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> make_options = '<STR_LIT:U+0020>' . join ( self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) ) <EOL> make_targets = '<STR_LIT:U+0020>' . join ( self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) ) <EOL> configure_options = self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) <EOL> configure_cmd = self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> if not configure_cmd : <EOL> configure_cmd = '<STR_LIT>' <EOL> if '<STR_LIT>' not in '<STR_LIT:U+0020>' . join ( configure_options ) : <EOL> configure_options . insert ( <NUM_LIT:0> , '<STR_LIT>' % self . options [ '<STR_LIT>' ] ) <EOL> patch_cmd = self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> patch_options = '<STR_LIT:U+0020>' . join ( self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) ) <EOL> patches = self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( ) <EOL> if self . environ : <EOL> for key in sorted ( self . environ . keys ( ) ) : <EOL> log . info ( '<STR_LIT>' , key , self . environ [ key ] ) <EOL> if self . options [ '<STR_LIT:url>' ] : <EOL> compile_dir = self . options [ '<STR_LIT>' ] <EOL> if os . path . exists ( compile_dir ) : <EOL> log . warning ( '<STR_LIT>' % compile_dir ) <EOL> shutil . rmtree ( compile_dir ) <EOL> os . mkdir ( compile_dir ) <EOL> try : <EOL> opt = self . options . copy ( ) <EOL> opt [ '<STR_LIT>' ] = compile_dir <EOL> hexagonit . recipe . download . Recipe ( <EOL> self . buildout , self . name , opt ) . install ( ) <EOL> except : <EOL> shutil . rmtree ( compile_dir ) <EOL> raise <EOL> else : <EOL> log . info ( '<STR_LIT>' % self . options [ '<STR_LIT:path>' ] ) <EOL> compile_dir = self . options [ '<STR_LIT:path>' ] <EOL> current_dir = os . getcwd ( ) <EOL> try : <EOL> os . mkdir ( self . options [ '<STR_LIT:location>' ] ) <EOL> except OSError as e : <EOL> if e . errno == errno . EEXIST : <EOL> pass <EOL> os . chdir ( compile_dir ) <EOL> try : <EOL> try : <EOL> contents = os . listdir ( compile_dir ) <EOL> if len ( contents ) == <NUM_LIT:1> and os . path . isdir ( contents [ <NUM_LIT:0> ] ) : <EOL> os . chdir ( contents [ <NUM_LIT:0> ] ) <EOL> if patches : <EOL> log . info ( '<STR_LIT>' ) <EOL> for patch in patches : <EOL> self . run ( '<STR_LIT>' % ( patch_cmd , patch_options , patch ) ) <EOL> if '<STR_LIT>' in self . options and len ( self . options [ '<STR_LIT>' ] . strip ( ) ) > <NUM_LIT:0> : <EOL> log . info ( '<STR_LIT>' ) <EOL> self . call_script ( self . options [ '<STR_LIT>' ] ) <EOL> self . run ( '<STR_LIT>' % ( configure_cmd , '<STR_LIT:U+0020>' . join ( configure_options ) ) ) <EOL> if '<STR_LIT>' in self . options and len ( self . options [ '<STR_LIT>' ] . strip ( ) ) > <NUM_LIT:0> : <EOL> log . info ( '<STR_LIT>' ) <EOL> self . call_script ( self . options [ '<STR_LIT>' ] ) <EOL> self . run ( '<STR_LIT>' % ( make_cmd , make_options ) ) <EOL> self . run ( '<STR_LIT>' % ( make_cmd , make_options , make_targets ) ) <EOL> if '<STR_LIT>' in self . options and len ( self . options [ '<STR_LIT>' ] . strip ( ) ) > <NUM_LIT:0> : <EOL> log . info ( '<STR_LIT>' ) <EOL> self . call_script ( self . options [ '<STR_LIT>' ] ) <EOL> except : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' % os . getcwd ( ) ) <EOL> raise <EOL> finally : <EOL> os . chdir ( current_dir ) <EOL> if self . options [ '<STR_LIT:url>' ] : <EOL> if self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) in ( '<STR_LIT:true>' , '<STR_LIT:yes>' , '<STR_LIT:1>' , '<STR_LIT>' ) : <EOL> parts . append ( self . options [ '<STR_LIT>' ] ) <EOL> else : <EOL> shutil . rmtree ( compile_dir ) <EOL> del self . options [ '<STR_LIT>' ] <EOL> parts . append ( self . options [ '<STR_LIT:location>' ] ) <EOL> return parts </s>
