This repository has been archived by the owner on May 21, 2023. It is now read-only.
/
sar.py
108 lines (90 loc) · 3.22 KB
/
sar.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import os
import optparse
import locale
import reader
from common import ArchiverException
from utils import console
COMPRESSION_METHODS = (
'huffman',
'rle',
)
def error(message):
console.writeline(u'%s: error: %s' % (os.path.basename(__file__), message))
return 1
def check_access(path, mode):
strings = {
os.R_OK: 'reading from',
os.W_OK: 'writing to',
}
if not os.access(path, mode):
raise ArchiverException(
u'Not enough rights for %s %s' % (strings[mode], path))
def check_existence(path):
if not os.path.exists(path):
raise ArchiverException(u'File not found: %s' % path)
def main():
oparser = optparse.OptionParser(usage='%prog -f <archive> [options] [files...]')
oparser.disable_interspersed_args()
optgroup = optparse.OptionGroup(oparser, 'Operations')
optgroup.add_option('-c', '--create', action='store_true', default=False,
help='create archive')
optgroup.add_option('-x', '--extract', action='store_true', default=False,
help='extract files from archive')
oparser.add_option_group(optgroup)
oparser.add_option('-m', '--method', default='rle',
help='compression method [{0}] (%default by default)'.format(
'|'.join(COMPRESSION_METHODS)))
oparser.add_option('-f', '--file', dest='archive', metavar='FILE',
help='archive file name')
(options, args) = oparser.parse_args()
if not (options.create or options.extract):
oparser.error('Missing operation')
if options.create and options.extract:
oparser.error('Only one operation must be specified')
if not options.archive:
oparser.error('Missing archive file name')
if options.extract:
check_existence(options.archive)
check_access(options.archive, os.R_OK)
if options.method not in COMPRESSION_METHODS:
oparser.error(u'Unknown compression method: %s' % options.method)
compressor = __import__('algorithms.%s' % options.method, fromlist=['algorithms'])
work_dir = os.path.dirname(options.archive) or os.getcwd()
check_access(work_dir, os.R_OK)
check_access(work_dir, os.W_OK)
if options.extract:
process = compressor.uncompress
# temporary
src = options.archive
dst = options.archive + '.ex'
else:
process = compressor.compress
if not args:
oparser.error('Wrong number of arguments')
else:
for arg in args:
check_existence(arg)
check_access(arg, os.R_OK)
# temporary
src = args[0]
dst = options.archive
with open(src, 'rb') as fsrc:
with open(dst, 'wb') as fdst:
freader = reader.BufferedReader(fsrc, reader.calc_buffer_size(src))
pbar = console.ProgressBar(maxval=os.path.getsize(src))
pbar.start()
process(freader, fdst, pbar)
pbar.finish()
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
console.writeline('Interrupted by user')
except ArchiverException, ex:
error(ex.args[0])
sys.exit(1)