-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtarmanager_test.py
110 lines (89 loc) · 3.66 KB
/
tarmanager_test.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-
import os
import sys
import getopt
from ..misc.tarmanager import TarManager, TarManagerError
def usage():
print(r"\n{} -c[x] -s xxx -d xxx -f xxx\n".format(os.path.basename(sys.argv[0])))
print(r"\t-h--help show this help menu")
print(r"\t-v--verbose output verbose message")
print(r"\t-c--create create a package file")
print(r"\t-x--extract extract a package file")
print(r"\t-s--src specify will package file path or will extract file path")
print(r"\t-d--dest specify package file name or will extract file path")
print(r"\t-x--format specify package file format: {}".format(TarManager.get_support_format()))
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "hcxvf:s:d:", ["help", "create", "extract", "verbose",
"format=", "src=", "dest="])
formats = ""
verbose = False
src_path = ""
dest_path = ""
package_operate = False
unpackage_operate = False
for option, argument in opts:
# Create a package
if option in ("-c", "--create"):
package_operate = True
# Extract a package
elif option in ("-x", "--extract"):
unpackage_operate = True
# Verbose message output
elif option in ("-v", "--verbose"):
verbose = True
# Show help message
elif option in ("-h", "--help"):
usage()
sys.exit()
# Format setting
elif option in ("-f", "--format") and len(argument):
if argument not in TarManager.get_support_format():
print("Unknown format:{}".format(argument))
usage()
sys.exit()
formats = argument
# Get src file, for package operate, src is will package directory,
# for unpackage operate, src is will unpackaged file
elif option in ("-s", "--src") and len(argument):
src_path = argument
elif option in ("-d", "--dest") and len(argument):
dest_path = argument
# Operate check
if not package_operate and not unpackage_operate:
print("You must specified a operate package(-c/--create) or unpackage(-x/--extract)")
usage()
sys.exit()
if package_operate and unpackage_operate:
print("Conflicting options: -c[--create], -x[--extract], they can't specified at same time")
usage()
sys.exit(-1)
# Format check
if len(formats) == 0:
print("You must specified a format (-f/--format=):{}".format(TarManager.get_support_format()))
usage()
sys.exit()
# Check src and dest
if len(src_path) == 0:
print("You must specified a source path, -s/--src")
usage()
sys.exit()
if len(dest_path) == 0:
print("You must specified a dest path, -d/--dest")
usage()
sys.exit()
# Operate
if package_operate:
try:
TarManager.pack(src_path, dest_path, formats, verbose=verbose)
except TarManagerError as error:
print("Pack error:{}".format(error))
if unpackage_operate:
try:
TarManager.unpack(src_path, fmt=formats)
except TarManagerError as error:
print("Unpack error:{}".format(error))
except getopt.GetoptError as e:
print("Error:{}".format(e))
usage()
sys.exit()