Mercurial > repos > brenninc > preconfigured_directory_reader
comparison directory_copier.py @ 1:2470f3968557 draft default tip
Uploaded
| author | brenninc | 
|---|---|
| date | Tue, 21 Jun 2016 04:42:16 -0400 | 
| parents | 97a9636974bc | 
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 0:97a9636974bc | 1:2470f3968557 | 
|---|---|
| 1 import gzip | 1 import gzip | 
| 2 import optparse # using optparse as hydra still python 2.6 | 2 import optparse # using optparse as hydra still python 2.6 | 
| 3 import os.path | 3 import os.path | 
| 4 import re | |
| 4 import shutil | 5 import shutil | 
| 5 import sys | 6 import sys | 
| 6 | 7 | 
| 7 def report_error(*args): | 8 def report_error(*args): | 
| 8 sys.stderr.write(' '.join(map(str,args)) + '\n') | 9 sys.stderr.write(' '.join(map(str,args)) + '\n') | 
| 12 | 13 | 
| 13 def check_pattern_get_new_name(a_file, ending, options): | 14 def check_pattern_get_new_name(a_file, ending, options): | 
| 14 if options.start: | 15 if options.start: | 
| 15 if not(a_file.startswith(options.start)): | 16 if not(a_file.startswith(options.start)): | 
| 16 return None | 17 return None | 
| 18 name = a_file[:-len(ending)] | |
| 19 if name.endswith("."): | |
| 20 name = name[:-1] | |
| 17 if options.last: | 21 if options.last: | 
| 18 if ending[0] == ".": | 22 if not(name.endswith(last)): | 
| 19 last = options.last + ending | 23 return None | 
| 20 else: | 24 if options.regex: | 
| 21 if options.last[-1] == ".": | 25 pattern = re.compile(options.regex) | 
| 22 last = options.last + ending | 26 if pattern.search(name) is None: | 
| 23 else: | |
| 24 last = options.last + "." + ending | |
| 25 if not(a_file.endswith(last)): | |
| 26 return None | 27 return None | 
| 27 if options.new_ending: | 28 if options.new_ending: | 
| 28 name = a_file[:-len(ending)] | |
| 29 if options.new_ending[0] ==".": | 29 if options.new_ending[0] ==".": | 
| 30 if name[-1] == ".": | 30 return name + options.new_ending | 
| 31 name = name[:-1] | 31 else: | 
| 32 return name + options.new_ending | 32 return name + "." + options.new_ending | 
| 33 if options.decompress: | 33 if options.decompress: | 
| 34 if a_file.endswith(".gz"): | 34 if a_file.endswith(".gz"): | 
| 35 return a_file[:-3] | 35 return a_file[:-3] | 
| 36 return a_file | 36 return a_file | 
| 37 | 37 | 
| 44 | 44 | 
| 45 | 45 | 
| 46 def link(a_file, new_name, path): | 46 def link(a_file, new_name, path): | 
| 47 file_path = os.path.join(os.path.realpath(path), a_file) | 47 file_path = os.path.join(os.path.realpath(path), a_file) | 
| 48 sym_path = os.path.join(os.path.realpath("output"), new_name) | 48 sym_path = os.path.join(os.path.realpath("output"), new_name) | 
| 49 #if not(os.path.exists(sym_path)): | |
| 50 os.link(file_path, sym_path) | 49 os.link(file_path, sym_path) | 
| 51 | 50 | 
| 52 | 51 | 
| 53 def decompress(a_file, new_name, path): | 52 def decompress(a_file, new_name, path): | 
| 54 file_path = os.path.join(os.path.realpath(path), a_file) | 53 file_path = os.path.join(os.path.realpath(path), a_file) | 
| 56 with gzip.open(file_path, 'rb') as f_in, open(target_path, 'wb') as f_out: | 55 with gzip.open(file_path, 'rb') as f_in, open(target_path, 'wb') as f_out: | 
| 57 shutil.copyfileobj(f_in, f_out) | 56 shutil.copyfileobj(f_in, f_out) | 
| 58 | 57 | 
| 59 | 58 | 
| 60 def copy_and_link(path, options): | 59 def copy_and_link(path, options): | 
| 61 os.mkdir("output") | 60 if options.decompress or options.link: | 
| 61 os.mkdir("output") | |
| 62 with open(options.list, 'w') as list_file: | 62 with open(options.list, 'w') as list_file: | 
| 63 files = os.listdir(path) | 63 files = os.listdir(path) | 
| 64 files.sort() | 64 files.sort() | 
| 65 for a_file in files: | 65 for a_file in files: | 
| 66 new_name = check_and_get_new_name(a_file, options) | 66 new_name = check_and_get_new_name(a_file, options) | 
| 84 help="Ending that can be listed and if requested linked or decompressed. ") | 84 help="Ending that can be listed and if requested linked or decompressed. ") | 
| 85 parser.add_option("--start", action="store", type="string", | 85 parser.add_option("--start", action="store", type="string", | 
| 86 help="String that must be at the start of the file name ") | 86 help="String that must be at the start of the file name ") | 
| 87 parser.add_option("--last", action="store", type="string", | 87 parser.add_option("--last", action="store", type="string", | 
| 88 help="String that must be the last bit of the file name before the endings") | 88 help="String that must be the last bit of the file name before the endings") | 
| 89 parser.add_option("--regex", action="store", type="string", | |
| 90 help="Regex for file names not including the endings") | |
| 89 parser.add_option("--new_ending", action="store", type="string", | 91 parser.add_option("--new_ending", action="store", type="string", | 
| 90 help="New ending to replace any previous ending in list and if required links or decompressions. Note: If not set decompression will auto remove the compressioned part of the ending") | 92 help="New ending to replace any previous ending in list and if required links or decompressions. Note: If not set decompression will auto remove the compressioned part of the ending") | 
| 91 #parser.add_option("--regex", action="store", type="string", | |
| 92 # help="Regex pattern the file name (less . ending) must match before the endings") | |
| 93 parser.add_option("--list", action="store", type="string", | 93 parser.add_option("--list", action="store", type="string", | 
| 94 help="Path to where all files should be listed. ") | 94 help="Path to where all files should be listed. ") | 
| 95 parser.add_option("--link", action="store_true", default=False, | 95 parser.add_option("--link", action="store_true", default=False, | 
| 96 help="If set will cause links to be added in output directory. ") | 96 help="If set will cause links to be added in output directory. ") | 
| 97 parser.add_option("--decompress", action="store_true", default=False, | 97 parser.add_option("--decompress", action="store_true", default=False, | 
