-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdav.py
249 lines (212 loc) · 6.64 KB
/
webdav.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
import base64
import os
import socket
import sys
import getpass
from autohdl import build
import autohdl.lib.tinydav as tinydav
from autohdl.lib.pyparsing import makeHTMLTags, SkipTo
from autohdl.lib.tinydav.exception import HTTPUserError
import autohdl.lib.yaml as yaml
from autohdl.hdlLogger import log_call, logging
log = logging.getLogger(__name__)
def loadAuth(iPath):
username, password = '', ''
try:
with open(iPath) as f:
content = yaml.load(f)
username = base64.decodestring(content[base64.encodestring('username')])
password = base64.decodestring(content[base64.encodestring('password')])
except Exception as e:
# logging e
logging.debug(e)
return username, password
def checkAuth(host, iUsername, iPassword):
client = tinydav.HTTPClient(host)
client.setbasicauth(iUsername, iPassword)
try:
print client.head('/test')
except HTTPUserError as e:
#logging
print e
logging.debug(e)
return False
return True
def dumpAuth(iPath, iUsername, iPassword):
contentYaml = {base64.encodestring('username'): base64.encodestring(iUsername),
base64.encodestring('password'): base64.encodestring(iPassword)}
try:
with open(iPath, 'wb') as f:
yaml.dump(contentYaml, f, default_flow_style = False)
except IOError as e:
print e
def askAuth():
quit = raw_input('To cancel hit Q, Enter to continue: ')
if quit.lower() == 'q':
print 'Exit...'
sys.exit(0)
username = raw_input('user: ')
password = getpass.getpass('password: ')
return username, password
def dump_netrc(host, username, password):
path = os.environ['USERPROFILE']+'/_netrc'
addNew = False
try:
with open(path, 'r') as f:
context = f.read()
for i in [host, username, password]:
if i not in context:
addNew = True
except IOError as e:
log.debug(e)
addNew = True
if addNew:
data = 'machine {0}\nlogin {1}\npassword {2}\n'.format(host, username, password)
with open(path, 'w') as f:
f.write(data)
def authenticate(host = 'cs.scircus.ru'):
print 'Authentication',
path = sys.prefix + '/Lib/site-packages/autohdl_cfg/open_sesame'
username, password = loadAuth(path)
state = 'check'
while True:
if state == 'check':
if checkAuth(host, username, password):
state = 'dump'
else:
state = 'ask'
elif state == 'ask':
username, password = askAuth()
state = 'check'
elif state == 'dump':
dumpAuth(path, username, password)
dump_netrc(host, username, password)
return username, password
def exists(path, client):
try:
client.get(path)
except HTTPUserError:
sys.exit()
def getBuildVer(client, folder, root_build):
"""
Get build version
"""
response = client.propfind('/{}/'.format(folder), depth=1)
anchorStart, anchorEnd = makeHTMLTags('D:href')
anchor = anchorStart + SkipTo(anchorEnd).setResultsName('body') + anchorEnd
fwList = {os.path.basename(tokens.body) for tokens in anchor.searchString(response.content)}
try:
# get rid of extensions
fwList = [os.path.splitext(i)[0] for i in fwList if root_build in i]
allBuilds = []
for i in fwList:
try:
# ripping only build versions
allBuilds.append(int(i.split(root_build)[1]))
except Exception:
continue
buildNum = max(allBuilds)
except Exception as e:
buildNum = 0
return buildNum
def formBaseInfo(conf):
b = conf or build.load()
comment = raw_input('Add some comment: ')
# conf['gitMessage'] += comment
content = 'encoding: utf-8\ncomment: {}\ndevice: {}\nPROM size: {} kilobytes\nspi: {}'.format(
comment, b.get('device'), b.get('size'), b.get('spi'))
print content
conf['gitMessage'] += comment.decode(sys.stdin.encoding or 'utf-8').encode('utf-8')
return content.decode(sys.stdin.encoding or 'utf-8').encode('utf-8')
def upload_fw(file, host = 'cs.scircus.ru', path = '/test/distout/rtl', _cache = {}, config = ''):
# dsn_name/implement/file
client = connect(host)
dsn_name = config['dsnName']
root, ext = os.path.splitext(os.path.basename(file))
key = dsn_name+root
name = _cache.get(key)
# content = getContent(file)
try:
with open(file, 'rb') as f:
content = f.read()
except IOError:
print 'Cant open file ' + os.path.abspath(file)
return
if not name:
folder = path+'/'+dsn_name
print 'Uploading folder: ', folder,
print client.mkcol(folder)
buildNum = getBuildVer(client, folder, root+'_build_')+1
config['gitMessage'] = 'build_{} '.format(buildNum)
name = '{path}/{dsn}/{dsn}_{root}_build_{num}'.format(
path=path, dsn=dsn_name, root=root, num=buildNum)
dst = name + '_info'
info = formBaseInfo(config)
print 'Uploading info: ', dst,
print client.put(dst, info)
_cache.update({key:name})
dst = name + ext
print 'Uploading file: ', dst,
print client.put(dst, content)
def getContent(iFile):
try:
with open(iFile, "rb") as f:
data = f.read()
except IOError as e:
print e
sys.exit()
return data
def connect(host='cs.scircus.ru'):
try:
username, password = authenticate(host)
client = tinydav.WebDAVClient(host)
client.setbasicauth(username, password)
return client
except socket.gaierror as e:
print e
print 'Cant connect to server'
sys.exit()
@log_call
def upload(src, dst, host = 'cs.scircus.ru'):
#TODO: atomic uploading
client = connect(host)
try:
d = dst if dst[-1] != '/' else dst[:-1]
client.get(os.path.dirname(d))
except HTTPUserError as e:
print 'Wrong destination:', dst, e
sys.exit()
try:
if 'HTTP/1.1 200 OK' in client.propfind(dst+'//').content:
print 'Already exists: ', dst
sys.exit()
except HTTPUserError:
pass
if os.path.isfile(src):
content = getContent(src)
print 'Uploading file: ', dst,
print client.put(dst, content)
else:
path_was = os.getcwd()
print dst, client.mkcol(dst)
try:
os.chdir(src)
for root, dirs, files in os.walk('.'):
for d in dirs:
ad = os.path.join(root,d).replace('\\', '/')
web_dst = '{0}{1}'.format(dst,ad.replace('./', '/'))
print web_dst,
print client.mkcol(web_dst)
for f in files:
af = os.path.join(root,f).replace('\\','/')
web_dst = '{0}{1}'.format(dst,af.replace('./', '/'))
print web_dst,
content = open(af).read()
headers = None if content else {'Content-Length': 0}
print client.put(web_dst,
content,
headers = headers)
finally:
os.chdir(path_was)
if __name__ == '__main__':
print authenticate()