<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import shutil <EOL> import urllib2 <EOL> import subprocess <EOL> import codecs <EOL> from optparse import OptionParser <EOL> def has_setuptools ( ) : <EOL> try : <EOL> import setuptools <EOL> except ImportError : <EOL> return False <EOL> return True <EOL> def make_new_setupfile ( setupfile ) : <EOL> """<STR_LIT>""" <EOL> setupfile = os . path . abspath ( setupfile ) <EOL> newsetupfile = os . path . join ( os . path . dirname ( setupfile ) , <EOL> '<STR_LIT>' + os . path . basename ( setupfile ) ) <EOL> startdir = os . getcwd ( ) <EOL> os . chdir ( os . path . dirname ( setupfile ) ) <EOL> try : <EOL> print "<STR_LIT>" <EOL> if not os . path . isfile ( '<STR_LIT>' ) : <EOL> print "<STR_LIT>" <EOL> resp = urllib2 . urlopen ( '<STR_LIT>' ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:wb>' ) as easyf : <EOL> shutil . copyfileobj ( resp . fp , easyf ) <EOL> print '<STR_LIT>' <EOL> print "<STR_LIT>" % setupfile <EOL> if not os . path . isfile ( setupfile ) : <EOL> raise IOError ( "<STR_LIT>" % setupfile ) <EOL> setupf = open ( setupfile , '<STR_LIT:r>' ) <EOL> setup_contents = setupf . read ( ) <EOL> setupf . close ( ) <EOL> with open ( newsetupfile , '<STR_LIT:wb>' ) as newf : <EOL> newf . write ( "<STR_LIT>" ) <EOL> newf . write ( "<STR_LIT>" ) <EOL> newf . write ( setup_contents ) <EOL> finally : <EOL> os . chdir ( startdir ) <EOL> return newsetupfile <EOL> def build_dist ( srcdir , destdir = '<STR_LIT:.>' , build_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> startdir = os . getcwd ( ) <EOL> destdir = os . path . abspath ( os . path . expanduser ( destdir ) ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> srcdir = os . path . abspath ( os . path . expanduser ( srcdir ) ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> setupname = os . path . join ( srcdir , '<STR_LIT>' ) <EOL> if not has_setuptools ( ) : <EOL> setupname = make_new_setupfile ( setupname ) <EOL> dirfiles = set ( os . listdir ( destdir ) ) <EOL> print "<STR_LIT>" % srcdir <EOL> cmd = [ sys . executable . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) , <EOL> os . path . basename ( setupname ) , <EOL> ] <EOL> cmd . extend ( build_type . split ( '<STR_LIT:U+0020>' ) ) <EOL> cmd . extend ( [ '<STR_LIT>' , destdir ] ) <EOL> os . chdir ( srcdir ) <EOL> out = codecs . open ( '<STR_LIT>' , '<STR_LIT:wb>' , <EOL> encoding = '<STR_LIT:ascii>' , errors = '<STR_LIT:replace>' ) <EOL> print '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( cmd ) <EOL> try : <EOL> p = subprocess . Popen ( '<STR_LIT:U+0020>' . join ( cmd ) , <EOL> stdout = out , stderr = subprocess . STDOUT , <EOL> shell = True ) <EOL> p . wait ( ) <EOL> finally : <EOL> out . close ( ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:r>' ) as f : <EOL> print f . read ( ) <EOL> os . chdir ( startdir ) <EOL> newfiles = set ( os . listdir ( destdir ) ) - dirfiles <EOL> if len ( newfiles ) != <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> list ( newfiles ) ) <EOL> if p . returncode != <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( srcdir , p . returncode ) ) <EOL> distfile = os . path . join ( destdir , newfiles . pop ( ) ) <EOL> print '<STR_LIT>' % distfile <EOL> return distfile <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = OptionParser ( usage = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = '<STR_LIT:string>' , <EOL> dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = '<STR_LIT:string>' , <EOL> dest = '<STR_LIT>' , default = '<STR_LIT:.>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = '<STR_LIT:string>' , <EOL> dest = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> retcode = - <NUM_LIT:1> <EOL> startdir = os . getcwd ( ) <EOL> if not options . srcdir : <EOL> print "<STR_LIT>" <EOL> parser . print_help ( ) <EOL> sys . exit ( retcode ) <EOL> srcdir = os . path . abspath ( os . path . expanduser ( options . srcdir ) ) <EOL> destdir = os . path . abspath ( os . path . expanduser ( options . destdir ) ) <EOL> if not os . path . exists ( srcdir ) : <EOL> print "<STR_LIT>" % srcdir <EOL> sys . exit ( retcode ) <EOL> try : <EOL> distfile = build_dist ( srcdir , destdir , options . buildtype ) <EOL> finally : <EOL> os . chdir ( startdir ) </s>
