File: cmdline.py

package info (click to toggle)
sagemath 7.4-9
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 108,312 kB
  • ctags: 72,147
  • sloc: python: 800,328; sh: 10,775; cpp: 7,154; ansic: 2,301; objc: 1,372; makefile: 889; lisp: 1
file content (77 lines) | stat: -rw-r--r-- 2,212 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Commandline handling for sage-uncompress-spkg
"""

#*****************************************************************************
#       Copyright (C) 2016 Volker Braun <vbraun.name@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#                  http://www.gnu.org/licenses/
#*****************************************************************************


from __future__ import print_function

import os
import sys

# Note that argparse is not part of Python 2.6, so we bundle it
try:
    import argparse
except ImportError:
    from sage_bootstrap.compat import argparse

from sage_bootstrap.uncompress.action import (
    open_archive, unpack_archive
)
                                              

    
def make_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', dest='dir', metavar='DIR',
                        help='directory to extract archive contents into')
    parser.add_argument('pkg', nargs=1, metavar='PKG',
                        help='the archive to extract')
    parser.add_argument('file', nargs='?', metavar='FILE',
                        help='(deprecated) print the contents of the given '
                             'archive member to stdout')
    return parser


def run():
    parser = make_parser()
    args = parser.parse_args(sys.argv[1:])

    filename = args.pkg[0]
    dirname = args.dir

    try:
        archive = open_archive(filename)
    except ValueError:
        print('Error: Unknown file type: {}'.format(filename),
              file=sys.stderr)
        return 1
        
    if args.file:
        contents = archive.extractbytes(args.file)
        if contents:
            print(contents, end='')
            return 0
        else:
            return 1

    if dirname and os.path.exists(dirname):
        print('Error: Directory {} already exists'.format(dirname),
              file=sys.stderr)
        return 1

    unpack_archive(archive, dirname)
    return 0


if __name__ == '__main__':
    sys.exit(run())