Skip to content

Commit

Permalink
Add option to specify compression level.
Browse files Browse the repository at this point in the history
Refs #54
  • Loading branch information
Kentzo committed Oct 31, 2018
1 parent f765a0b commit d2b9e14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Archive repository with all its submodules.

::

git-archive-all [-v] [--prefix PREFIX] [--no-exclude] [--force-submodules] [--extra] [--dry-run] OUTPUT_FILE
git-archive-all [-v] [--prefix PREFIX] [--no-exclude] [--force-submodules] [--extra EXTRA1 ...] [--dry-run] [-0 | ... | -9] OUTPUT_FILE

Options:

Expand Down
32 changes: 26 additions & 6 deletions git_archive_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(self, prefix='', exclude=True, force_sub=False, extra=None, main_re
self.force_sub = force_sub
self.main_repo_abspath = main_repo_abspath

def create(self, output_path, dry_run=False, output_format=None):
def create(self, output_path, dry_run=False, output_format=None, compresslevel=None):
"""
Create the archive at output_file_path.
Expand All @@ -147,7 +147,13 @@ def create(self, output_path, dry_run=False, output_format=None):
if output_format in self.ZIPFILE_FORMATS:
from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED

archive = ZipFile(path.abspath(output_path), 'w')
if sys.version_info > (3, 7):
archive = ZipFile(path.abspath(output_path), 'w', compresslevel=compresslevel)
else:
if compresslevel is not None:
raise ValueError("Compression level for zip archives requires Python 3.7+")

archive = ZipFile(path.abspath(output_path), 'w')

def add_file(file_path, arcname):
if not path.islink(file_path):
Expand All @@ -161,7 +167,14 @@ def add_file(file_path, arcname):
import tarfile

mode = self.TARFILE_FORMATS[output_format]
archive = tarfile.open(path.abspath(output_path), mode)

try:
archive = tarfile.open(path.abspath(output_path), mode, compresslevel=compresslevel)
except TypeError:
if compresslevel is not None:
raise ValueError("{0} cannot be compressed".format(output_format))

archive = tarfile.open(path.abspath(output_path), mode)

def add_file(file_path, arcname):
archive.add(file_path, arcname)
Expand Down Expand Up @@ -312,11 +325,11 @@ def run_git_shell(cmd, cwd=None):


def main():
from optparse import OptionParser
from optparse import OptionParser, SUPPRESS_HELP

parser = OptionParser(
usage="usage: %prog [-v] [--prefix PREFIX] [--no-exclude] [--force-submodules]"
" [--extra EXTRA1 [EXTRA2]] [--dry-run] OUTPUT_FILE",
" [--extra EXTRA1 ...] [--dry-run] [-0 | ... | -9] OUTPUT_FILE",
version="%prog {0}".format(__version__)
)

Expand Down Expand Up @@ -355,6 +368,13 @@ def main():
dest='dry_run',
help="don't actually archive anything, just show what would be done")

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()

if len(args) != 1:
Expand Down Expand Up @@ -386,7 +406,7 @@ def main():
options.exclude,
options.force_sub,
options.extra)
archiver.create(output_file_path, options.dry_run)
archiver.create(output_file_path, options.dry_run, compresslevel=options.compresslevel)
except Exception as e:
parser.exit(2, "{0}\n".format(e))

Expand Down

0 comments on commit d2b9e14

Please sign in to comment.