git_archive_all_2.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. def main(argv=None):
  2. if argv is None:
  3. argv = sys.argv
  4. from optparse import OptionParser, SUPPRESS_HELP
  5. parser = OptionParser(
  6. usage="usage: %prog [-v] [-C BASE_REPO] [--prefix PREFIX] [--no-export-ignore]"
  7. " [--force-submodules] [--include EXTRA1 ...] [--dry-run] [-0 | ... | -9] OUTPUT_FILE",
  8. version="%prog {0}".format(__version__)
  9. )
  10. parser.add_option('--prefix',
  11. type='string',
  12. dest='prefix',
  13. default=None,
  14. help="""prepend PREFIX to each filename in the archive;
  15. defaults to OUTPUT_FILE name""")
  16. parser.add_option('-C',
  17. type='string',
  18. dest='base_repo',
  19. default=None,
  20. help="""use BASE_REPO as the main git repository to archive;
  21. defaults to the current directory when empty""")
  22. parser.add_option('-v', '--verbose',
  23. action='store_true',
  24. dest='verbose',
  25. help='enable verbose mode')
  26. for i in range(10):
  27. parser.add_option('-{0}'.format(i),
  28. action='store_const',
  29. const=i,
  30. dest='compresslevel',
  31. help=SUPPRESS_HELP)
  32. options, args = parser.parse_args(argv[1:])
  33. if len(args) != 1:
  34. parser.error("You must specify exactly one output file")
  35. output_file_path = args[0]
  36. if path.isdir(output_file_path):
  37. parser.error("You cannot use directory as output")
  38. # avoid tarbomb
  39. if options.prefix is not None:
  40. options.prefix = path.join(options.prefix, '')
  41. else:
  42. output_name = path.basename(output_file_path)
  43. output_name = re.sub(
  44. '(\\.zip|\\.tar|\\.tbz2|\\.tgz|\\.txz|\\.bz2|\\.gz|\\.xz|\\.tar\\.bz2|\\.tar\\.gz|\\.tar\\.xz)$',
  45. '',
  46. output_name
  47. ) or "Archive"
  48. options.prefix = path.join(output_name, '')
  49. try:
  50. handler = logging.StreamHandler(sys.stdout)
  51. handler.setFormatter(logging.Formatter('%(message)s'))
  52. GitArchiver.LOG.addHandler(handler)
  53. GitArchiver.LOG.setLevel(logging.DEBUG if options.verbose else logging.INFO)
  54. archiver = GitArchiver(options.prefix,
  55. options.exclude,
  56. options.force_sub,
  57. options.extra,
  58. path.abspath(options.base_repo) if options.base_repo is not None else None
  59. )
  60. archiver.create(output_file_path, options.dry_run, compresslevel=options.compresslevel)
  61. except Exception as e:
  62. parser.exit(2, "{0}\n".format(e))
  63. return 0