/
upc.py
executable file
·762 lines (621 loc) · 22.3 KB
/
upc.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#!/usr/bin/env python -u
# -*- coding: utf-8 -*-
"""A simple command-line tool for managing upyun files.
It acts just like GNU tools, and also support command completion
which can be triggered by double enter 'Tab'.
For local files operation, you have lls, lcd and others which acts like
lftp.
"""
import os
import sys
import cmd
import stat
import getopt
import fnmatch
import functools
from datetime import datetime
from upyun import UpYun, UpYunClientException, UpYunServiceException
# fix auto completion on macintosh
import readline
if 'libedit' in readline.__doc__:
import rlcompleter
readline.parse_and_bind("bind ^I rl_complete")
try:
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser
__author__ = 'Kang Li<i@likang.me>'
__version__ = '0.0.5'
conf_path = os.path.expanduser('~/.upcrc')
def neat(**neat_args):
"""Command function wrapper, capture error and show them neatly"""
def neat_wrapper(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
try:
return method(self, *args, **kwargs)
except UpYunServiceException as ex:
if not neat_args.get('silence', False):
self.output('ServiceError(%s): %s' % (ex.status, ex.msg))
except UpYunClientException as ex:
if not neat_args.get('silence', False):
self.output('ClientError: %s' % ex.msg)
except Exception as ex:
if not neat_args.get('silence', False):
msg = getattr(ex, 'msg', '')
message = getattr(ex, 'message', '')
self.output('UnexpectedError: %s' % msg or message or ex)
if 'default' in neat_args:
return neat_args['default']
return wrapper
return neat_wrapper
class Terminal(cmd.Cmd):
"""A simple command-line tool for managing upyun files.
It acts just like GNU tools, and also support command completion
which can be triggered by double enter 'Tab'.
For local files operation, you have lls, lcd and others which acts like
lftp.
"""
def __init__(self, options, completekey='tab', stdin=None, stdout=None):
self.options = options
self.pwd = '/'
self.prompt = 'upyun > '
self.up = None
self.vocab = ['ls', 'pwd', 'cd', 'rm', 'put', 'get',
'mkdir', 'rmdir', 'use', 'open', 'usage',
'quit', 'help']
cmd.Cmd.__init__(self, completekey, stdin, stdout)
@neat()
def do_usage(self, line):
"""Show the usage of current bucket
usage [-h]
-h Show human-readable output
"""
usage_size = self.up.usage().strip()
if '-h' in line:
self.output(human_size(usage_size))
else:
self.output(usage_size + ' bytes')
@neat()
def do_ls(self, line):
"""List directory contents
usage [-hsStTnN]
-h Show human-readable output
-s Sort by file size
-S Sort by file size desc
-t Sort by created time
-T Sort by created time desc
-n Sort by file name
-N Sort by file name desc
"""
opts, dirs = getopt.getopt(line.split(), 'hsStTnN')
opts = [o[0] for o in opts]
dirs = dirs or ['']
path = os.path.join(self.pwd, dirs[0])
files = self.up.getlist(path)
# do the sort
if '-s' in opts or '-S' in opts:
files.sort(key=lambda v: int(v['size']), reverse='-S' in opts)
if '-t' in opts or '-T' in opts:
files.sort(key=lambda v: v['time'], reverse='-T' in opts)
if '-n' in opts or '-N' in opts:
files.sort(key=lambda v: v['name'], reverse='-N' in opts)
show_size = human_size if '-h' in opts else int
for f in files:
self.output('%s\t%s\t%s\t%s' % (
{'F': 'd', 'N': '-'}[f['type']],
datetime.fromtimestamp(int(f['time'])),
show_size(f['size']) if f['type'] == 'N' else '-',
f['name'])
)
def complete_ls(self, *args):
return file_complete(args, self.pwd,
self.remote_list_dir_func, type_filter='dir')
@neat()
def do_put(self, line):
"""Upload local files/directories to current work directory.
put [-r] file ...
-r Recursively upload entire directories.
"""
opts, fds = getopt.getopt(line.split(), 'r')
opts = [o[0] for o in opts]
if not fds:
self.do_help('put')
return
files = []
dirs = []
# support shell wildcards
for f in os.listdir(os.getcwd()):
for fd in fds:
fd = fd.rstrip(os.sep)
if fnmatch.fnmatch(f, fd):
path = os.path.join(os.getcwd(), f)
if os.path.isdir(path):
dirs.append(f)
else:
files.append(f)
files = list(set(files))
dirs = list(set(dirs))
# find files recursively
if '-r' in opts:
for d in dirs:
for dirpath, dirnames, filenames in os.walk(d):
files.extend([os.path.join(dirpath, f) for f in filenames])
for f in files:
with open(os.path.join(os.getcwd(), f), 'rb') as fd:
to_path = os.path.join(self.pwd, f)
self.up.put(to_path, fd, checksum=False)
self.output('%s\t OK' % f)
def complete_put(self, *args):
return file_complete(args, os.getcwd(), local_list_dir_func)
@neat()
def do_get(self, line, first_call=True):
"""Download files entries
get [-r] file ...
-r Download folder.
"""
opts, fds = getopt.getopt(line.split(), 'r')
opts = [o[0] for o in opts]
if not fds:
self.do_help('get')
return
to_be_dld = []
# support shell wildcards
cwd_files = self.up.getlist(self.pwd)
if first_call:
for cf in cwd_files:
for fd in fds:
fd = fd.rstrip(os.sep)
if fnmatch.fnmatch(cf['name'], fd):
to_be_dld.append(cf['name'])
else:
to_be_dld = fds
# rm file, recursively if needed
for fd in to_be_dld:
path = os.path.join(self.pwd, fd)
info = self.up.getinfo(path)
if info['file-type'] == 'folder':
if '-r' in opts:
fs = self.up.getlist(path)
for f in fs:
new_path = os.path.join(path, f['name'])
self.do_get('-r %s' % new_path, False)
else:
relative_path = path[len(self.pwd):]
try:
os.makedirs(os.path.join(os.getcwd(),
os.path.dirname(relative_path)))
except OSError:
pass
with open(os.path.join(os.getcwd(), relative_path), 'wb') as f:
self.up.get(path, f)
def complete_file(self, *args):
return file_complete(args, self.pwd, self.remote_list_dir_func)
@neat()
def do_mkdir(self, line):
"""Make directories
mkdir [-p] directory_name ...
-p Create intermediate directories as required.
"""
opts, dirs = getopt.getopt(line.split(), 'p')
opts = [o[0] for o in opts]
if not dirs:
self.do_help('mkdir')
return
for d in dirs:
headers = {'Folder': 'true'}
if '-p' in opts:
headers['Mkdir'] = 'true'
# Okay, I don't like this too, but ... :(
self.up._UpYun__do_http_request('POST', os.path.join(self.pwd, d),
headers=headers)
@neat()
def do_rm(self, line, first_call=True):
"""Remove directory entries
rm [-r] file ...
-r Remove the file hierarchy rooted in each file argument.
"""
opts, fds = getopt.getopt(line.split(), 'r')
opts = [o[0] for o in opts]
if not fds:
self.do_help('rm')
return
to_be_rmd = []
# support shell wildcards
cwd_files = self.up.getlist(self.pwd)
if first_call:
for cf in cwd_files:
for fd in fds:
fd = fd.rstrip(os.sep)
if fnmatch.fnmatch(cf['name'], fd):
to_be_rmd.append(cf['name'])
else:
to_be_rmd = fds
# rm file, recursively if needed
for fd in to_be_rmd:
path = os.path.join(self.pwd, fd)
info = self.up.getinfo(path)
if info['file-type'] == 'folder':
if '-r' in opts:
fs = self.up.getlist(path)
for f in fs:
new_path = os.path.join(path, f['name'])
self.do_rm('-r %s' % new_path, False)
self.up.delete(path)
def complete_rm(self, *args):
return file_complete(args, self.pwd, self.remote_list_dir_func)
def do_rmdir(self, d):
"""Remove directories, same with rm"""
self.do_rm(d)
complete_rmdir = complete_rm
@neat()
def do_cd(self, line):
"""Change working directory
cd [directory]
"""
dir_name = (line.strip().split()+[''])[0]
if not dir_name:
self.pwd = '/'
return
path = os.path.abspath(os.path.join(self.pwd, dir_name))
if path != '/':
path = path.rstrip(os.sep)
self.up.getinfo(path)
self.pwd = path
complete_cd = complete_ls
def do_pwd(self, line):
"""Print working directory name"""
self.output(self.pwd)
@neat()
def do_file(self, line):
"""Show file/directory info
file file ...
-h Show human-readable output
"""
opts, files = getopt.getopt(line.strip().split(), 'h')
if not files:
self.do_help('file')
return
opts = [o[0] for o in opts]
show_size = human_size if '-h' in opts else int
for f in files:
info = self.up.getinfo(os.path.join(self.pwd, f))
if info['file-type'] == 'folder':
size = '-'
else:
size = show_size(info['file-size'])
self.output('%s\t%s\t%s\t%s' % (
{'folder': 'd', 'file': '-'}[info['file-type']],
datetime.fromtimestamp(int(info['file-date'], )),
size,
f))
complete_file = complete_rm
@neat()
def do_lls(self, line):
"""List local directory contents
usage [-hsStT]
-h Show human-readable output
-s Sort by file size
-S Sort by file size desc
-t Sort by created time
-T Sort by created time desc
-n Sort by file name
-N Sort by file name desc
"""
opts, dirs = getopt.getopt(line.strip().split(), 'hsStTnN')
opts = [o[0] for o in opts]
dirs = dirs or ['']
path = os.path.join(os.getcwd(), dirs[0])
files = []
for f in os.listdir(path):
fi = os.stat(os.path.join(path, f))
files.append({
'size': fi.st_size,
'time': fi.st_mtime,
'name': f,
'type': 'F' if stat.S_ISDIR(fi.st_mode) else 'N',
})
# do the sort
if '-s' in opts or '-S' in opts:
files.sort(key=lambda v: int(v['size']), reverse='-S' in opts)
if '-t' in opts or '-T' in opts:
files.sort(key=lambda v: v['time'], reverse='-T' in opts)
if '-n' in opts or '-N' in opts:
files.sort(key=lambda v: v['name'], reverse='-N' in opts)
show_size = human_size if '-h' in opts else int
for f in files:
self.output('%s\t%s\t%s\t%s' % (
{'F': 'd', 'N': '-'}[f['type']],
datetime.fromtimestamp(int(f['time'])),
show_size(f['size']) if f['type'] == 'N' else '-',
f['name'])
)
def complete_lls(self, *args):
return file_complete(args, os.getcwd(),
local_list_dir_func, type_filter='dir')
def do_lpwd(self, line):
"""Print local working directory name"""
self.output(os.getcwd())
@neat()
def do_lcd(self, d):
"""Change local working directory"""
d = (d.strip().split() + [''])[0]
if not d:
d = '~'
path = os.path.expanduser(os.path.join(os.getcwd(), d))
if not os.path.isdir(path):
self.output('no such directory: %s' % path)
return
os.chdir(path)
def complete_lcd(self, *args):
return file_complete(args, os.getcwd(),
local_list_dir_func, type_filter='dir')
@neat()
def do_lmkdir(self, line):
opts, dirs = getopt.getopt(line.strip().split(), 'p')
opts = [o[0] for o in opts]
for d in dirs:
path = os.path.join(os.getcwd(), d)
path = os.path.abspath(os.path.expanduser(path))
if '-p' in opts:
os.makedirs(path)
else:
os.mkdir(path)
complete_lmkdir = complete_lcd
def do_use(self, line):
"""Switch bucket.
use bucket_name
"""
bucket = (line.strip().split() + [''])[0]
if not bucket:
self.do_help('use')
return
self.switch_bucket(bucket)
def complete_use(self, *args):
bucket = (args[0].strip().split()+[''])[0]
return [b for b in self.options.sections() if b.startswith(bucket)]
def do_quit(self, line):
"""
Quit/Exit the upyun client shell.
You can also use the Ctrol-D shortcut.
"""
self.output('')
sys.exit(0)
do_EOF = do_exit = do_quit
def do_welcome(self, line):
self.output(r"""
__ __ ______ __ __ __ __ __ __
/\ \/\ \ /\ == \ /\ \_\ \ /\ \/\ \ /\ "-.\ \
\ \ \_\ \ \ \ _-/ \ \____ \ \ \ \_\ \ \ \ \-. \
\ \_____\ \ \_\ \/\_____\ \ \_____\ \ \_\\"\_\
\/_____/ \/_/ \/_____/ \/_____/ \/_/ \/_/
""")
@neat(default=list(), silence=True)
def remote_list_dir_func(self, wd):
"""Get directory contents on the remote."""
files = self.up.getlist(wd)
result = []
for f in files:
result.append({
'name': f['name'],
'is_dir': f['type'] == 'F'
})
return result
@neat()
def switch_bucket(self, bucket):
"""Switch to buckets defined in conf_file, and check if it's valid."""
if self.options.has_section(bucket):
params = {k: v.strip() for k, v in self.options.items(bucket)}
params['bucket'] = bucket
up = UpYun(**params)
up.usage()
self.pwd = '/'
self.up = up
self.update_prompt()
else:
self.output("bucket '%s' doesn't exist in the config file(%s)." % (
bucket, conf_path))
def update_prompt(self):
"""Update cmd prompt"""
if self.up:
prompt = '/' if self.pwd == '/' else os.path.basename(self.pwd)
self.prompt = '(%s) %s > ' % (self.up.bucket, prompt)
def postcmd(self, stop, line):
self.update_prompt()
def output(self, stuff):
self.stdout.write(stuff + '\n')
def do_help(self, arg):
"""List available commands with "help"
or detailed help with "help cmd"."""
if arg:
try:
func = getattr(self, 'help_' + arg)
except AttributeError:
try:
doc = trim_doc(getattr(self, 'do_' + arg).__doc__)
if doc:
self.stdout.write('%s\n' % str(doc))
return
except AttributeError:
pass
self.stdout.write('%s\n' % str(self.nohelp % (arg,)))
return
func()
else:
names = self.get_names()
cmds_doc = []
cmds_undoc = []
help = {}
for name in names:
if name[:5] == 'help_':
help[name[5:]] = 1
names.sort()
# There can be duplicates if routines overridden
prevname = ''
for name in names:
if name[:3] == 'do_':
if name == prevname:
continue
prevname = name
cmd=name[3:]
if cmd in help:
cmds_doc.append(cmd)
del help[cmd]
elif getattr(self, name).__doc__:
cmds_doc.append(cmd)
else:
cmds_undoc.append(cmd)
self.stdout.write("%s\n" % str(self.doc_leader))
self.print_topics(self.doc_header, cmds_doc, 15, 80)
self.print_topics(self.misc_header, help.keys(), 15, 80)
self.print_topics(self.undoc_header, cmds_undoc, 15, 80)
def mix_unicode(s, encoding='utf-8'):
if sys.version < '3':
return s.decode(encoding)
return s
def mix_str(s, encoding='utf-8'):
if sys.version < '3':
return s.encode(encoding)
return s
def local_list_dir_func(wd):
files = os.listdir(wd)
result = []
for f in files:
result.append({
'name': mix_unicode(f),
'is_dir': os.path.isdir(os.path.join(wd, f))
})
return result
def human_size(num):
num = int(num)
if num == 0:
return '0 byte'
for x in ['bytes', 'KB', 'MB', 'GB']:
if num < 1024.0:
return '%3.2f %s' % (num, x)
num /= 1024.0
return '%3.2f %s' % (num, 'TB')
def file_complete(args, pwd, list_dir_func, type_filter=None):
"""Get list of files based on the typed path.
:param path: typed path
:param pwd: current word directory
:param list_dir_func: call back function which
return contents in certain directory
:param type_filter:
:return: list of possible contents you might want to type
"""
path = args[0]
new_mod = False
# fix python3 on macintosh
if path != args[1].split()[-1]:
new_mod = True
path = args[1].split()[-1]
path = mix_unicode(path)
pwd = mix_unicode(pwd)
input_path = (path.strip().split() + [''])[0]
dist_path = os.path.join(pwd, input_path)
abs_dist_path = os.path.abspath(dist_path)
if dist_path.endswith(os.sep):
abs_dist_path += os.sep
origin_dir_name = os.path.dirname(input_path)
dir_name = os.path.dirname(abs_dist_path)
base_name = os.path.basename(abs_dist_path)
files = []
# filter the files
for f in list_dir_func(dir_name):
if f['is_dir']:
if type_filter == 'file':
continue
files.append(f['name'] + os.sep)
else:
if type_filter == 'dir':
continue
files.append(f['name'])
completes = [f for f in files if f.startswith(base_name)]
# find the longest common substring from the very start
if len(completes) > 1:
common = base_name
max_common_length = min([len(c) for c in completes])
for i in range(len(base_name), max_common_length):
if len(set([c[i] for c in completes])) == 1:
common += completes[0][i]
else:
break
if common != base_name:
if not new_mod:
return [mix_str(common)]
else:
return [mix_str(os.path.join(origin_dir_name, common))]
if new_mod:
return [mix_str(c) for c in completes]
else:
return [mix_str(os.path.join(origin_dir_name, c)) for c in completes]
def trim_doc(docstring):
"""Remove indention of python docstring properly
for more detail, see http://legacy.python.org/dev/peps/pep-0257/
"""
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
def load_options():
if not os.path.exists(conf_path):
print("Sorry but I can't find the config file. Please fill the "
"following template and save it to %s" % conf_path)
print("""
; Sample upc config file
; The below sample bucket section shows all possible config values,
; create one or more 'real' bucket sections to be able to control them under
; upc.
;[bucket_name]
;username=foo
;password=bar
;endpoint=
;timeout=
;chunksize=
;[more_buckets] ; your bucket name
;..""")
sys.exit(2)
options = ConfigParser()
with open(conf_path, 'r') as f:
options.readfp(f)
return options
def main():
options = load_options()
up_cmd = Terminal(options)
if len(sys.argv) == 1:
print ('usage: upc bucket-name')
print ('usage: upc bucket-name command [option ...]')
sys.exit(2)
up_cmd.switch_bucket(sys.argv[1])
if up_cmd.up is None:
sys.exit(2)
if len(sys.argv) >= 3:
up_cmd.onecmd(' '.join(sys.argv[2:]))
else:
try:
up_cmd.cmdqueue.append('welcome')
up_cmd.cmdloop()
except KeyboardInterrupt:
up_cmd.output('')
if __name__ == '__main__':
main()