12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- def main(argv=None):
- if argv is None:
- argv = sys.argv
- from optparse import OptionParser, SUPPRESS_HELP
- parser = OptionParser(
- usage="usage: %prog [-v] [-C BASE_REPO] [--prefix PREFIX] [--no-export-ignore]"
- " [--force-submodules] [--include EXTRA1 ...] [--dry-run] [-0 | ... | -9] OUTPUT_FILE",
- version="%prog {0}".format(__version__)
- )
- parser.add_option('--prefix',
- type='string',
- dest='prefix',
- default=None,
- help="""prepend PREFIX to each filename in the archive;
- defaults to OUTPUT_FILE name""")
- parser.add_option('-C',
- type='string',
- dest='base_repo',
- default=None,
- help="""use BASE_REPO as the main git repository to archive;
- defaults to the current directory when empty""")
- parser.add_option('-v', '--verbose',
- action='store_true',
- dest='verbose',
- help='enable verbose mode')
- for i in range(10):
- parser.add_option('-{0}'.format(i),
- action='store_const',
- const=i,
- dest='compresslevel',
- help=SUPPRESS_HELP)
- options, args = parser.parse_args(argv[1:])
- if len(args) != 1:
- parser.error("You must specify exactly one output file")
- output_file_path = args[0]
- if path.isdir(output_file_path):
- parser.error("You cannot use directory as output")
- # avoid tarbomb
- if options.prefix is not None:
- options.prefix = path.join(options.prefix, '')
- else:
- output_name = path.basename(output_file_path)
- output_name = re.sub(
- '(\\.zip|\\.tar|\\.tbz2|\\.tgz|\\.txz|\\.bz2|\\.gz|\\.xz|\\.tar\\.bz2|\\.tar\\.gz|\\.tar\\.xz)$',
- '',
- output_name
- ) or "Archive"
- options.prefix = path.join(output_name, '')
- try:
- handler = logging.StreamHandler(sys.stdout)
- handler.setFormatter(logging.Formatter('%(message)s'))
- GitArchiver.LOG.addHandler(handler)
- GitArchiver.LOG.setLevel(logging.DEBUG if options.verbose else logging.INFO)
- archiver = GitArchiver(options.prefix,
- options.exclude,
- options.force_sub,
- options.extra,
- path.abspath(options.base_repo) if options.base_repo is not None else None
- )
- archiver.create(output_file_path, options.dry_run, compresslevel=options.compresslevel)
- except Exception as e:
- parser.exit(2, "{0}\n".format(e))
- return 0
|