Path: blob/develop/build/sage_bootstrap/uncompress/cmdline.py
4055 views
"""1Commandline handling for sage-uncompress-spkg2"""34#*****************************************************************************5# Copyright (C) 2016 Volker Braun <[email protected]>6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 2 of the License, or10# (at your option) any later version.11# http://www.gnu.org/licenses/12#*****************************************************************************131415from __future__ import print_function1617import os18import sys19import argparse2021from sage_bootstrap.uncompress.action import (22open_archive, unpack_archive23)24252627def make_parser():28parser = argparse.ArgumentParser()29parser.add_argument('-d', dest='dir', metavar='DIR',30help='directory to extract archive contents into')31parser.add_argument('pkg', nargs=1, metavar='PKG',32help='the archive to extract')33parser.add_argument('file', nargs='?', metavar='FILE',34help='(deprecated) print the contents of the given '35'archive member to stdout')36return parser373839def run():40parser = make_parser()41args = parser.parse_args(sys.argv[1:])4243filename = args.pkg[0]44dirname = args.dir4546try:47archive = open_archive(filename)48except ValueError:49print('Error: Unknown file type: {}'.format(filename),50file=sys.stderr)51return 15253if args.file:54contents = archive.extractbytes(args.file)55if contents:56print(contents, end='')57return 058else:59return 16061if dirname and os.path.exists(dirname):62print('Error: Directory {} already exists'.format(dirname),63file=sys.stderr)64return 16566unpack_archive(archive, dirname)67return 0686970if __name__ == '__main__':71sys.exit(run())727374