Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.libnetworkcache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
slapos.libnetworkcache
Commits
d3aec190
Commit
d3aec190
authored
Jan 26, 2012
by
Yingjie Xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generic upload function, client should take care of the content.
parent
acf6c303
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
slapos/libnetworkcache.py
slapos/libnetworkcache.py
+62
-0
No files found.
slapos/libnetworkcache.py
View file @
d3aec190
...
...
@@ -191,6 +191,68 @@ class NetworkcacheClient(object):
(
self
.
shacache_host
,
result
.
status
,
data
))
return
True
def
upload_generic
(
self
,
file_descriptor
,
key
=
None
,
**
kw
):
''' Upload the file to the server.
If key is None, it must only upload to SHACACHE.
Otherwise, it must create a new entry on SHADIR.
'''
sha512sum
=
hashlib
.
sha512
()
file_descriptor
.
seek
(
0
)
while
True
:
d
=
file_descriptor
.
read
(
sha512sum
.
block_size
)
if
not
d
:
break
sha512sum
.
update
(
d
)
sha512sum
=
sha512sum
.
hexdigest
()
file_descriptor
.
seek
(
0
)
if
self
.
shacache_scheme
==
'https'
:
shacache_connection
=
httplib
.
HTTPSConnection
(
self
.
shacache_host
,
self
.
shacache_port
,
key_file
=
self
.
shacache_key_file
,
cert_file
=
self
.
shacache_cert_file
)
else
:
shacache_connection
=
httplib
.
HTTPConnection
(
self
.
shacache_host
,
self
.
shacache_port
)
try
:
shacache_connection
.
request
(
'POST'
,
self
.
shacache_path
,
file_descriptor
,
self
.
shacache_header_dict
)
result
=
shacache_connection
.
getresponse
()
data
=
result
.
read
()
finally
:
shacache_connection
.
close
()
if
result
.
status
!=
201
or
data
!=
sha512sum
:
raise
UploadError
(
'Failed to upload the file to SHACACHE Server.'
\
'URL: %s. Response code: %s. Response data: %s'
%
\
(
self
.
shacache_host
,
result
.
status
,
data
))
if
key
is
not
None
:
sha_entry
=
json
.
dumps
(
kw
)
try
:
signature
=
self
.
_getSignatureString
(
sha_entry
)
except
Exception
:
raise
UploadError
(
'Impossible to sign content, error:
\
n
%s'
%
traceback
.
format_exc
())
data
=
[
sha_entry
,
signature
]
if
self
.
shadir_scheme
==
'https'
:
shadir_connection
=
httplib
.
HTTPSConnection
(
self
.
shadir_host
,
self
.
shadir_port
,
key_file
=
self
.
shadir_key_file
,
cert_file
=
self
.
shadir_cert_file
)
else
:
shadir_connection
=
httplib
.
HTTPConnection
(
self
.
shadir_host
,
self
.
shadir_port
)
try
:
shadir_connection
.
request
(
'PUT'
,
'/'
.
join
([
self
.
shadir_path
,
key
]),
json
.
dumps
(
data
),
self
.
shadir_header_dict
)
result
=
shadir_connection
.
getresponse
()
data
=
result
.
read
()
finally
:
shadir_connection
.
close
()
if
result
.
status
!=
201
:
raise
UploadError
(
'Failed to upload data to SHADIR Server.'
\
'URL: %s. Response code: %s. Response data: %s'
%
\
(
self
.
shadir_host
,
result
.
status
,
data
))
return
True
def
download
(
self
,
sha512sum
):
''' Download the file.
It uses http GET request method.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment