Archive / / archive-old.py
2009-05-13 05:17:54 UTC
previous next
#!/usr/bin/python3 -tt from optparse import OptionParser, OptionGroup, OptionValueError cfg_archive_dir = "/home/shane/scratch/archivepy-test" cfg_output_dir = "/home/shane/scratch/archivepy-text/extracted" #def extract_action_callback(option, opt_str, value, parser): # global show_help # show_help = False # # if (value < 1990 or value > 2020): # raise OptionValueError("'{0}' is not a valid year".format(value)) # # print('Extracting {0}...'.format(value)) def parse_options(): parser = OptionParser(version="%prog 1.0") actions = [] def extract_callback(option, opt_str, value, parser): if (value < 1990 or value > 2020): # TODO check for year in archive raise OptionValueError("'{0}' is not a valid year".format(value)) else: actions.append(("extract", value)) def import_callback(option, opt_str, value, parser): if (value < 1990 or value > 2020): # TODO check for year in archive raise OptionValueError("'{0}' is not a valid year".format(value)) else: actions.append(("import", value)) parser.set_defaults(archive_dir=cfg_archive_dir, output_dir=cfg_output_dir, check_data=False, delete_output=False) action_group = OptionGroup(parser, "Actions", "One or more of these actions are required") action_group.add_option("-e", "--extract", type="int", action="callback", callback=extract_callback, metavar="YEAR", help="Extract archive for YEAR") action_group.add_option("-i", "--import", type="int", action="callback", callback=import_callback, metavar="YEAR", help="Import new or modified data " "from the output directory for YEAR to created a new " "archive file (any preexisting archive file will be " "backed up beforehand)") action_group.add_option("-c", "--check", dest="check_data", action="store_true", help="Check archive files against " "checksums to ensure they have not be corrupted, then " "check any extracted content to ensure it matches the " "data in the archive files") action_group.add_option("-d", "--delete-output", dest="delete_output", action="store_true", help="Delete extracted content of " "archive files (implies -c)") parser.add_option_group(action_group) config_group = OptionGroup(parser, "Options") config_group.add_option("--archive-dir", dest="archive_dir", metavar="PATH", help="Specify the directory with the " "stored archive files. (Currently set to {0})".format( cfg_archive_dir)) config_group.add_option("--output-dir", dest="output_dir", metavar="PATH", help="Specify the directory which " "contains the extracted content of the archive files. " "(Currently set to {0})".format(cfg_output_dir)) parser.add_option_group(config_group) (options, args) = parser.parse_args() if (options.delete_output == True): if (actions != []): parser.error("cannot extract or import data while deleting output") else: return "delete-output" if (options.check if (actions == [] and options.check_archive == False and options.check_output == False and ): parser.print_help() return False else: return (actions, options if __name__ == "__main__": main()