-
Notifications
You must be signed in to change notification settings - Fork 44
/
inout.py
executable file
·188 lines (167 loc) · 5.33 KB
/
inout.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
"""
An interactive script to
1) compress a directory and store the archive in another folder, and
2) uncompress an archive to a given folder
Supported archiving / compression methods:
* tar
* tgz
* zip
Contributors:
* Gábor Szőcs <https://github.com/szocs08>, zip support
"""
import os
import readline
import sys
from pathlib import Path
COMPRESS, UNCOMPRESS = (1, 2)
TAR, TGZ, ZIP, TAR_BZ2 = (3, 4, 5, 6)
MODE = None # will be set later
def die(msg):
"""
Print an error message and terminate.
"""
print(msg, file=sys.stderr)
sys.exit(1)
def my_input(msg):
"""
Take input from the user and handle Ctrl + c.
"""
try:
return input(msg)
except (KeyboardInterrupt, EOFError):
print()
die("# abort")
def set_mode():
"""
Handle symbolic links.
If you put a symbolic link on this file called "be", then compression mode is selected.
If you put a symbolic link on this file called "ki", then uncompression mode is selected.
(be / ki means in / out in Hungarian, i.e. compress / uncompress)
"""
global MODE
p = Path(sys.argv[0])
if p.name == 'be':
MODE = COMPRESS
if p.name == 'ki':
MODE = UNCOMPRESS
if MODE is None:
inp = my_input("> compress or uncompress (c / u)? ")
if inp == 'c':
MODE = COMPRESS
elif inp == 'u':
MODE = UNCOMPRESS
else:
die("# error: unknown option")
def compress():
"""
Compress a folder interactively.
"""
print("# goal: compress a folder")
while True:
dname = my_input("> which folder to compress (ls: directory list)? ")
if dname == 'ls':
os.system("ls -ald */") # show directories only
print()
else:
break
dname = Path(dname)
if not dname.is_dir():
die("error: it's not a directory")
to_dir = Path(my_input("> where (in which directory) to store the archive file? "))
if not to_dir.is_dir():
to_dir.mkdir()
if not to_dir.is_dir():
die("# error: couldn't create the directory")
print("# directory created")
#
accepted = ["tar", "tgz", "zip"]
while True:
ext = my_input("> what compression to use ({})? ".format(", ".join(accepted)))
if ext not in accepted:
print("# unknown format")
else:
break
#
tar_options = "cvf"
if ext == "tgz":
tar_options = "cvzf"
if ext in ["tar", "tgz"]:
fname = str(dname) + ".tgz"
cmd = 'tar {options} "{to_dir}/{fname}" "{dname}"'.format(options=tar_options,
to_dir=str(to_dir),
fname=fname,
dname=dname)
print("# " + cmd)
os.system(cmd)
zip_options = "-r"
if ext == "zip":
fname = str(dname) + ".zip"
cmd = 'zip {options} "{to_dir}/{fname}" "{dname}"'.format(options=zip_options,
to_dir=str(to_dir),
fname=fname,
dname=dname)
print("# " + cmd)
os.system(cmd)
def uncompress():
"""
Uncompress an archive file interactively.
"""
print("# goal: uncompress an archive file")
while True:
fname = my_input("> which file to uncompress (ls: directory list)? ")
if fname == 'ls':
os.system('ls -al | grep -v "^d"') # show files only
print()
else:
break
fname = Path(fname)
if not fname.is_file():
die("error: it's not a file")
#
ftype = None # file type: tar, tgz, zip
s = str(fname)
if s.endswith(".tar"):
ftype = TAR
elif s.endswith((".tar.gz", ".tgz")):
ftype = TGZ
elif s.endswith(".zip"):
ftype = ZIP
elif s.endswith(".tar.bz2"):
ftype = TAR_BZ2
else:
die("# error: unknown file extension")
#
to_dir = Path(my_input("> where to uncompress the archive file (give a directory name)? "))
if not to_dir.is_dir():
to_dir.mkdir()
if not to_dir.is_dir():
die("# error: couldn't create the directory")
print("# directory created")
#
tar_options = "xvf"
if ftype == TGZ:
tar_options = "xvzf"
if ftype == TAR_BZ2:
tar_options = "xvjf"
if ftype in [TAR, TGZ, TAR_BZ2]:
cmd = 'tar {options} "{fname}" -C "{to_dir}"'.format(options=tar_options,
to_dir=str(to_dir),
fname=fname)
print("# " + cmd)
os.system(cmd)
if ftype == ZIP:
cmd = 'unzip "{fname}" -d "{to_dir}"'.format(options=tar_options,
to_dir=str(to_dir),
fname=fname)
print("# " + cmd)
os.system(cmd)
def main():
set_mode()
if MODE == COMPRESS:
compress()
else:
uncompress()
##############################################################################
if __name__ == "__main__":
main()