-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.py
128 lines (92 loc) · 3.36 KB
/
util.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
import sys
import requests
from time import time
from functools import wraps
from urlparse import urlparse
from os import unlink, makedirs
from os.path import isdir, exists
from optparse import OptionParser
from azure.storage import BlobService
VERSION = 'v1.0.0'
USAGE = 'usage: python %prog -u url -k account_key -p path -f filename\n' \
'*(Required field)'
def print_warning():
"""TODO: Docstring for print_warning.
:returns: TODO
"""
print 'Extension and Filename are mutually exclusive.'
return 1
def get_options():
"""TODO: Docstring for get_options.
:returns: TODO
"""
parser = OptionParser(usage=USAGE, version=VERSION)
parser.add_option('-u', '--url', action='store', type='string',
help='Url of the vhd *', dest='url', default='')
parser.add_option('-k', '--key', action='store', type='string',
help='Account Key', dest='account_key', default='')
parser.add_option('-f', '--file', action='store', type='string',
help='File name', dest='filename', default='')
parser.add_option('-p', '--path', action='store', type='string',
help='Searching path *', dest='path', default='/')
parser.add_option('-e', '--extension', action='store', type='string',
help='Extension', dest='extension', default='')
parser.add_option('-t', '--type', action='store', type='int',
help='EXT2/3/4; 2,3,4', dest='type', default='4')
parser.add_option('--ls', action='store_true',
help='List the dir', dest='ls', default=False)
(options, args) = parser.parse_args()
len(sys.argv) == 1 and exit(parser.print_help())
options.extension and options.filename and exit(print_warning())
tmp = urlparse(options.url)
options.account_name = tmp.netloc.split('.')[0]
options.container = tmp.path.split('/')[1]
options.vhd = tmp.path.split('/')[2]
options.host_base = tmp.netloc[tmp.netloc.find('.'):]
if options.account_key:
options.blob_service = BlobService(options.account_name,
options.account_key,
host_base=options.host_base)
options.blob_service._httpclient.request_session = requests.Session()
else:
options.blob_service = None
options.path_list = split_path(options.path)
return (options, args)
def log_time(fn):
"""TODO: Docstring for log_time.
:fn: TODO
:returns: TODO
"""
@wraps(fn)
def wrapper(*args, **kwargs):
start_time = time()
result = fn(*args, **kwargs)
print '%s -> Time used : %d\n' % (fn.__name__, time() - start_time)
return result
return wrapper
def embed_params(**kwargs):
"""TODO: Docstring for embed_params.
:**kwargs: TODO
:returns: TODO
"""
def decorator(fn):
@wraps(fn)
def wrapper(*arg):
return fn(*arg, **kwargs)
return wrapper
return decorator
def split_path(path):
"""TODO: Docstring for split_path.
:path: TODO
:returns: TODO
"""
item = [x for x in path.split('/') if x != '']
return item
def init_dir(path):
"""TODO: Docstring for init_dir.
:path: TODO
:returns: TODO
"""
if not isdir(path):
exists(path) and unlink(path)
makedirs(path)