@@ -153,90 +153,68 @@ def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=False,
153153 legacy = legacy )
154154 return success
155155
156- def expand_args (args , flist ):
157- """read names in flist and append to args"""
158- expanded = args [:]
159- if flist :
160- try :
161- if flist == '-' :
162- fd = sys .stdin
163- else :
164- fd = open (flist )
165- while 1 :
166- line = fd .readline ()
167- if not line :
168- break
169- expanded .append (line [:- 1 ])
170- except IOError :
171- print ("Error reading file list %s" % flist )
172- raise
173- return expanded
174156
175157def main ():
176158 """Script main program."""
177- import getopt
178- try :
179- opts , args = getopt . getopt ( sys . argv [ 1 :], 'lfqd:x:i:b' )
180- except getopt . error as msg :
181- print ( msg )
182- print ( "usage: python compileall.py [-l] [-f] [-q] [-d destdir] "
183- "[-x regexp] [-i list] [directory|file ...]" )
184- print ( "-l: don't recurse down" )
185- print ( "-f: force rebuild even if timestamps are up-to-date" )
186- print ( "-q: quiet operation" )
187- print ( "-d destdir: purported directory name for error messages" )
188- print ( " if no directory arguments, -l sys.path is assumed" )
189- print ( "-x regexp: skip files matching the regular expression regexp" )
190- print ( " the regexp is searched for in the full path of the file" )
191- print ( "-i list: expand list with its content "
192- "(file and directory names)" )
193- print ( "-b: Produce legacy byte-compile file paths" )
194- sys . exit ( 2 )
195- maxlevels = 10
196- ddir = None
197- force = False
198- quiet = False
199- rx = None
200- flist = None
201- legacy = False
202- for o , a in opts :
203- if o == '-l' : maxlevels = 0
204- if o == '-d' : ddir = a
205- if o == '-f' : force = True
206- if o == '-q' : quiet = True
207- if o == '-x' :
208- import re
209- rx = re . compile ( a )
210- if o == '-i' : flist = a
211- if o == '-b' : legacy = True
212- if ddir :
213- if len (args ) != 1 and not os . path . isdir ( args [ 0 ]) :
214- print ( "-d destdir require exactly one directory argument" )
215- sys . exit ( 2 )
216- success = 1
159+ import argparse
160+
161+ parser = argparse . ArgumentParser (
162+ description = 'Utilities to support installing Python libraries.' )
163+ parser . add_argument ( '-l' , action = 'store_const' , default = 10 , const = 0 ,
164+ dest = 'maxlevels' , help = "don't recurse down" )
165+ parser . add_argument ( '-f' , action = 'store_true' , dest = 'force' ,
166+ help = 'force rebuild even if timestamps are up to date' )
167+ parser . add_argument ( '-q' , action = 'store_true' , dest = 'quiet' ,
168+ help = ' quiet operation' )
169+ parser . add_argument ( '-b' , action = 'store_true' , dest = 'legacy' ,
170+ help = 'procude legacy byte-compiled file paths' )
171+ parser . add_argument ( '-d' , metavar = 'DESTDIR' , dest = 'ddir' , default = None ,
172+ help = ( 'purported directory name for error messages; '
173+ 'if no directory arguments, -l sys.path '
174+ 'is assumed.' ) )
175+ parser . add_argument ( '-x' , metavar = 'REGEXP' , dest = 'rx' , default = None ,
176+ help = ( 'skip files matching the regular expression. \n \t '
177+ 'The regexp is searched for in the full path'
178+ 'of the file' ))
179+ parser . add_argument ( '-i' , metavar = 'FILE' , dest = 'flist' ,
180+ help = 'expand the list with the contenent of FILE.' )
181+ parser . add_argument ( 'compile_dest' , metavar = 'FILE|DIR' , nargs = '?' )
182+ args = parser . parse_args ()
183+
184+ if ( args . ddir and args . compile_dest != 1 and
185+ not os . path . isdir ( args . compile_dest )):
186+ raise argparse . ArgumentError ( "-d destdir require exactly one "
187+ "directory argument" )
188+ if args . rx :
189+ import re
190+ args . rx = re . compile ( args . rx )
191+
192+ # if flist is provided then load it
193+ compile_dests = [ args . compile_dest ]
194+ if args . flist :
195+ with open (args . flist ) as f :
196+ files = f . read (). split ( )
197+ compile_dests . extend ( files )
198+
217199 try :
218- if args or flist :
219- try :
220- if flist :
221- args = expand_args (args , flist )
222- except IOError :
223- success = 0
224- if success :
225- for arg in args :
226- if os .path .isdir (arg ):
227- if not compile_dir (arg , maxlevels , ddir ,
228- force , rx , quiet , legacy ):
229- success = 0
230- else :
231- if not compile_file (arg , ddir , force , rx ,
232- quiet , legacy ):
233- success = 0
200+ if compile_dests :
201+ for dest in compile_dests :
202+ if os .path .isdir (dest ):
203+ if not compile_dir (dest , args .maxlevels , args .ddir ,
204+ args .force , args .rx , args .quiet ,
205+ args .legacy ):
206+ return 0
207+ else :
208+ if not compile_file (dest , args .ddir , args .force , args .rx ,
209+ args .quiet , args .legacy ):
210+ return 0
234211 else :
235- success = compile_path (legacy = legacy )
212+ return compile_path (legacy = args . legacy )
236213 except KeyboardInterrupt :
237214 print ("\n [interrupt]" )
238- success = 0
239- return success
215+ return 0
216+ return 1
217+
240218
241219if __name__ == '__main__' :
242220 exit_status = int (not main ())
0 commit comments