<s> from __future__ import print_function <EOL> import os <EOL> from argparse import RawDescriptionHelpFormatter <EOL> from gearbox . command import Command <EOL> from gearbox . template import GearBoxTemplate <EOL> from gearbox . utils . plugins import find_egg_info_dir <EOL> class ScaffoldCommand ( Command ) : <EOL> def get_description ( self ) : <EOL> return '''<STR_LIT>''' <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( ScaffoldCommand , self ) . get_parser ( prog_name ) <EOL> parser . formatter_class = RawDescriptionHelpFormatter <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> nargs = '<STR_LIT:+>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT:target>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT:path>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return parser <EOL> def _lookup ( self , template , where ) : <EOL> template_filename = None <EOL> for root , __ , files in os . walk ( where ) : <EOL> for f in files : <EOL> fname , fext = os . path . splitext ( f ) <EOL> if fext == '<STR_LIT>' and os . path . splitext ( fname ) [ <NUM_LIT:0> ] == template : <EOL> template_filename = os . path . join ( root , f ) <EOL> break <EOL> return template_filename <EOL> def _find_toplevel_packages ( self ) : <EOL> egg_info_dir = find_egg_info_dir ( os . getcwd ( ) ) <EOL> if egg_info_dir is None : <EOL> print ( '<STR_LIT>' ) <EOL> return [ ] <EOL> modules = os . path . join ( egg_info_dir , '<STR_LIT>' ) <EOL> try : <EOL> modules = open ( modules ) . readlines ( ) <EOL> except : <EOL> print ( '<STR_LIT>' ) <EOL> return [ ] <EOL> modules = map ( lambda x : x . strip ( ) . replace ( os . sep , '<STR_LIT:.>' ) , modules ) <EOL> modules = filter ( lambda x : not x . startswith ( '<STR_LIT:test>' ) , modules ) <EOL> return list ( modules ) <EOL> def take_action ( self , opts ) : <EOL> for template in opts . scaffold_name : <EOL> template_filename = None <EOL> template_relpath = None <EOL> if template . endswith ( '<STR_LIT>' ) : <EOL> template_filename = template <EOL> template_relpath = '<STR_LIT:.>' <EOL> else : <EOL> template_filename = self . _lookup ( template , '<STR_LIT:.>' ) <EOL> if template_filename : <EOL> template_relpath = os . path . dirname ( template_filename ) <EOL> if template_filename is None and opts . lookup : <EOL> template_filename = self . _lookup ( template , opts . lookup ) <EOL> if template_filename : <EOL> template_relpath = os . path . relpath ( os . path . dirname ( template_filename ) , opts . lookup ) <EOL> if not template_filename or not os . path . exists ( template_filename ) : <EOL> print ( '<STR_LIT>' % ( template ) ) <EOL> continue <EOL> print ( '<STR_LIT>' % ( template_filename , opts . target ) ) <EOL> template_with_ext , __ = os . path . splitext ( template_filename ) <EOL> __ , output_ext = os . path . splitext ( template_with_ext ) <EOL> output_dir = opts . path <EOL> if not output_dir : <EOL> output_dir = template_relpath <EOL> if opts . subdir : <EOL> output_dir = os . path . join ( output_dir , opts . subdir ) <EOL> if not os . path . exists ( output_dir ) : <EOL> os . makedirs ( output_dir ) <EOL> if opts . subdir : <EOL> package_init = os . path . join ( output_dir , '<STR_LIT>' ) <EOL> if not os . path . exists ( package_init ) : <EOL> with open ( package_init , '<STR_LIT:w>' ) as pif : <EOL> pif . write ( '<STR_LIT>' ) <EOL> output_path = os . path . join ( output_dir , opts . target ) + output_ext <EOL> print ( '<STR_LIT>' % output_path ) <EOL> with open ( template_filename , '<STR_LIT:r>' ) as tf : <EOL> try : <EOL> subdir_as_package = opts . subdir . replace ( os . sep , '<STR_LIT:.>' ) if opts . subdir else '<STR_LIT>' <EOL> text = GearBoxTemplate ( ) . template_renderer ( tf . read ( ) , { <EOL> '<STR_LIT:target>' : opts . target , <EOL> '<STR_LIT>' : opts . subdir , <EOL> '<STR_LIT>' : subdir_as_package , <EOL> '<STR_LIT>' : '<STR_LIT:.>' + subdir_as_package , <EOL> '<STR_LIT>' : self . _find_toplevel_packages ( ) <EOL> } ) <EOL> except NameError as e : <EOL> print ( '<STR_LIT>' % e ) <EOL> continue <EOL> with open ( output_path , '<STR_LIT:w>' ) as of : <EOL> of . write ( text ) </s>
