Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/sage_bootstrap/uncompress/cmdline.py
4055 views
1
"""
2
Commandline handling for sage-uncompress-spkg
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2016 Volker Braun <[email protected]>
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 2 of the License, or
11
# (at your option) any later version.
12
# http://www.gnu.org/licenses/
13
#*****************************************************************************
14
15
16
from __future__ import print_function
17
18
import os
19
import sys
20
import argparse
21
22
from sage_bootstrap.uncompress.action import (
23
open_archive, unpack_archive
24
)
25
26
27
28
def make_parser():
29
parser = argparse.ArgumentParser()
30
parser.add_argument('-d', dest='dir', metavar='DIR',
31
help='directory to extract archive contents into')
32
parser.add_argument('pkg', nargs=1, metavar='PKG',
33
help='the archive to extract')
34
parser.add_argument('file', nargs='?', metavar='FILE',
35
help='(deprecated) print the contents of the given '
36
'archive member to stdout')
37
return parser
38
39
40
def run():
41
parser = make_parser()
42
args = parser.parse_args(sys.argv[1:])
43
44
filename = args.pkg[0]
45
dirname = args.dir
46
47
try:
48
archive = open_archive(filename)
49
except ValueError:
50
print('Error: Unknown file type: {}'.format(filename),
51
file=sys.stderr)
52
return 1
53
54
if args.file:
55
contents = archive.extractbytes(args.file)
56
if contents:
57
print(contents, end='')
58
return 0
59
else:
60
return 1
61
62
if dirname and os.path.exists(dirname):
63
print('Error: Directory {} already exists'.format(dirname),
64
file=sys.stderr)
65
return 1
66
67
unpack_archive(archive, dirname)
68
return 0
69
70
71
if __name__ == '__main__':
72
sys.exit(run())
73
74