<s> import ConfigParser <EOL> from contextlib import contextmanager <EOL> import datetime <EOL> import optparse <EOL> import os <EOL> import sys <EOL> from poachplatelib import meta <EOL> DEFAULT_CONFIG = """<STR_LIT>""" <EOL> CONFIG_LOC = os . path . expanduser ( '<STR_LIT>' ) <EOL> YEAR = datetime . date . today ( ) . strftime ( '<STR_LIT>' ) <EOL> COPYRIGHT = '<STR_LIT>' <EOL> INIT = '''<STR_LIT>''' <EOL> MANIFEST = """<STR_LIT>""" <EOL> TEST = """<STR_LIT>""" <EOL> SCRIPT = """<STR_LIT>""" <EOL> META = """<STR_LIT>""" <EOL> SETUP = """<STR_LIT>""" <EOL> MAKEFILE = """<STR_LIT>""" <EOL> @ contextmanager <EOL> def push_pop_dir ( dirname ) : <EOL> cur_dir = os . getcwd ( ) <EOL> os . chdir ( dirname ) <EOL> yield <EOL> os . chdir ( cur_dir ) <EOL> class _Unset ( object ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def cascade_value ( opt = None , opt_name = None , <EOL> env_name = None , <EOL> cfg = None , cfg_section = None , cfg_name = None , <EOL> default = None ) : <EOL> """<STR_LIT>""" <EOL> value = _Unset ( ) <EOL> if opt and opt_name : <EOL> try : <EOL> value = opt . __getattr__ ( opt_name ) <EOL> except AttributeError , e : <EOL> pass <EOL> if not isinstance ( value , _Unset ) : <EOL> return value <EOL> if env_name : <EOL> try : <EOL> value = os . environ [ env_name ] <EOL> except KeyError , e : <EOL> pass <EOL> if not isinstance ( value , _Unset ) : <EOL> return value <EOL> if cfg and cfg_section and cfg_name : <EOL> try : <EOL> value = cfg . get ( cfg_section , cfg_name ) <EOL> except ConfigParser . NoOptionError , e : <EOL> pass <EOL> if not isinstance ( value , _Unset ) : <EOL> return value <EOL> return default <EOL> def create_config ( ) : <EOL> if not os . path . exists ( CONFIG_LOC ) : <EOL> create_file ( fout ( CONFIG_LOC ) , DEFAULT_CONFIG ) <EOL> def create_file ( fout , content = None ) : <EOL> if not content is None : <EOL> fout . write ( content ) <EOL> def fout ( name ) : <EOL> out = open ( name , '<STR_LIT:w>' ) <EOL> return out <EOL> class Package ( object ) : <EOL> def __init__ ( self , name , libname = None , scriptname = None , opt = None , cfg_loc = None , <EOL> bin_dir = '<STR_LIT>' , test_dir = '<STR_LIT:test>' ) : <EOL> self . name = name <EOL> pep8name = self . name . lower ( ) <EOL> self . libname = libname or pep8name + '<STR_LIT>' <EOL> for filename in [ pep8name , self . libname ] : <EOL> if not filename [ <NUM_LIT:0> ] . isalpha ( ) : <EOL> raise NameError , '<STR_LIT>' % filename <EOL> self . bin_dir = bin_dir <EOL> self . scriptname = scriptname or pep8name <EOL> self . scriptpath = """<STR_LIT>""" % { '<STR_LIT>' : self . bin_dir , '<STR_LIT:file>' : self . scriptname } if self . scriptname else '<STR_LIT>' <EOL> self . test_dir = test_dir <EOL> cfg = None <EOL> if cfg_loc and os . path . exists ( cfg_loc ) : <EOL> cfg = ConfigParser . ConfigParser ( ) <EOL> cfg . read ( cfg_loc ) <EOL> self . author = cascade_value ( opt = opt , opt_name = '<STR_LIT>' , <EOL> cfg = cfg , cfg_section = '<STR_LIT>' , cfg_name = '<STR_LIT>' , <EOL> default = '<STR_LIT>' ) <EOL> self . version = cascade_value ( opt = opt , opt_name = '<STR_LIT:version>' , <EOL> cfg = cfg , cfg_section = '<STR_LIT>' , cfg_name = '<STR_LIT:version>' , <EOL> default = '<STR_LIT>' ) <EOL> self . email = cascade_value ( opt = opt , opt_name = '<STR_LIT:email>' , <EOL> cfg = cfg , cfg_section = '<STR_LIT>' , cfg_name = '<STR_LIT:email>' , <EOL> default = '<STR_LIT>' ) <EOL> def generate ( self ) : <EOL> self . _copyright = COPYRIGHT % { '<STR_LIT>' : self . author , '<STR_LIT>' : YEAR } <EOL> self . _init = INIT % { '<STR_LIT>' : self . _copyright } <EOL> self . _meta = META % { '<STR_LIT:version>' : self . version , <EOL> '<STR_LIT>' : self . author , <EOL> '<STR_LIT:email>' : self . email , <EOL> '<STR_LIT>' : self . _copyright } <EOL> self . _setup = SETUP % { '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT>' : self . scriptpath , <EOL> '<STR_LIT>' : self . libname , <EOL> '<STR_LIT>' : self . _copyright } <EOL> self . _requirements = '<STR_LIT>' <EOL> self . _makefile = MAKEFILE <EOL> self . _script = SCRIPT % { '<STR_LIT>' : self . libname , <EOL> '<STR_LIT>' : self . _copyright } <EOL> self . _test = TEST % { '<STR_LIT>' : self . libname , <EOL> '<STR_LIT>' : self . libname . capitalize ( ) , <EOL> '<STR_LIT>' : self . _copyright } <EOL> def write ( self ) : <EOL> self . generate ( ) <EOL> if os . path . exists ( self . name ) : <EOL> raise NameError , '<STR_LIT>' % self . name <EOL> os . makedirs ( self . name ) <EOL> with push_pop_dir ( self . name ) : <EOL> os . makedirs ( self . libname ) <EOL> with open ( os . path . join ( self . libname , '<STR_LIT>' ) , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( self . _init ) <EOL> with open ( os . path . join ( self . libname , '<STR_LIT>' ) , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( self . _meta ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( self . _setup ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( self . _requirements ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( MAKEFILE ) <EOL> for fname in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> with open ( fname , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( '<STR_LIT>' ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( MANIFEST ) <EOL> if self . scriptname : <EOL> os . makedirs ( self . bin_dir ) <EOL> with open ( os . path . join ( self . bin_dir , self . scriptname ) , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( self . _script ) <EOL> os . makedirs ( self . test_dir ) <EOL> with open ( os . path . join ( self . test_dir , '<STR_LIT>' % self . libname ) , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( self . _test ) <EOL> def main ( prog_args ) : <EOL> usage = """<STR_LIT>""" <EOL> parser = optparse . OptionParser ( usage = usage , version = meta . __version__ ) <EOL> opt , args = parser . parse_args ( prog_args ) <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> p = Package ( sys . argv [ <NUM_LIT:1> ] , opt = opt , cfg_loc = CONFIG_LOC ) <EOL> p . write ( ) <EOL> else : <EOL> parser . print_help ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( sys . argv ) or <NUM_LIT:0> ) </s>
