bin/extract.py
changeset 17 a5dfa7884e30
parent 8 02a1dd166f2b
child 19 47ae52d1c868
equal deleted inserted replaced
14:75f2092df3f7 17:a5dfa7884e30
    31 VERBOSE = False
    31 VERBOSE = False
    32 INPUT_DIR = os.getcwd()
    32 INPUT_DIR = os.getcwd()
    33 OUTPUT_DIR = os.getcwd()
    33 OUTPUT_DIR = os.getcwd()
    34 INCLUDE = None
    34 INCLUDE = None
    35 EXCLUDE = None
    35 EXCLUDE = None
       
    36 CLEAN = False
    36 
    37 
    37 # ============================================================================
    38 # ============================================================================
    38 # OptionParser
    39 # OptionParser
    39 # ============================================================================
    40 # ============================================================================
    40 class OptionParser(optparse.OptionParser):
    41 class OptionParser(optparse.OptionParser):
    50                         help="specify the output <dir> (default %s)" % OUTPUT_DIR)
    51                         help="specify the output <dir> (default %s)" % OUTPUT_DIR)
    51         self.add_option("--include", dest="include", action="append", metavar="pattern",
    52         self.add_option("--include", dest="include", action="append", metavar="pattern",
    52                         help="specify the include <pattern> (default %s)" % INCLUDE)
    53                         help="specify the include <pattern> (default %s)" % INCLUDE)
    53         self.add_option("--exclude", dest="exclude", action="append", metavar="pattern",
    54         self.add_option("--exclude", dest="exclude", action="append", metavar="pattern",
    54                         help="specify the exclude <pattern> (default %s)" % EXCLUDE)
    55                         help="specify the exclude <pattern> (default %s)" % EXCLUDE)
       
    56         self.add_option("--clean", dest="clean", action="store_true",
       
    57                         help="clean intermediate files in the output directory")
    55 
    58 
    56 # ============================================================================
    59 # ============================================================================
    57 # Utils
    60 # Utils
    58 # ============================================================================
    61 # ============================================================================
    59 if not hasattr(os.path, "relpath"):
    62 if not hasattr(os.path, "relpath"):
   103 
   106 
   104 # ============================================================================
   107 # ============================================================================
   105 # main()
   108 # main()
   106 # ============================================================================
   109 # ============================================================================
   107 def main():
   110 def main():
   108     global VERBOSE, INPUT_DIR, OUTPUT_DIR, INCLUDE, EXCLUDE
   111     global VERBOSE, INPUT_DIR, OUTPUT_DIR, INCLUDE, EXCLUDE, CLEAN
   109 
   112 
   110     parser = OptionParser()
   113     parser = OptionParser()
   111     (options, args) = parser.parse_args()
   114     (options, args) = parser.parse_args()
   112 
   115 
   113     if options.verbose != None:
   116     if options.verbose != None:
   118         OUTPUT_DIR = options.output
   121         OUTPUT_DIR = options.output
   119     if options.include != None:
   122     if options.include != None:
   120         INCLUDE = options.include
   123         INCLUDE = options.include
   121     if options.exclude != None:
   124     if options.exclude != None:
   122         EXCLUDE = options.exclude
   125         EXCLUDE = options.exclude
       
   126     if options.clean != None:
       
   127         CLEAN = options.clean
   123 
   128 
   124     extracted = 0
   129     extracted = 0
   125     copied = 0
   130     copied = 0
   126     omitted = 0
   131     omitted = 0
   127     workpath = None
   132     workpath = None
   128     newline = False
   133     newline = False
       
   134     if CLEAN:
       
   135         shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
   129     sys.stdout.write("Processing: ")
   136     sys.stdout.write("Processing: ")
   130     for root, dirs, files in os.walk(INPUT_DIR):
   137     for root, dirs, files in os.walk(INPUT_DIR):
   131         for file in files:
   138         for file in files:
   132             filepath = os.path.join(root, file)
   139             filepath = os.path.join(root, file)
   133             extension = os.path.splitext(file)[1]
   140             extension = os.path.splitext(file)[1]