Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.buildout
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
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Thomas Leymonerie
slapos.buildout
Commits
9b1213ff
Commit
9b1213ff
authored
May 17, 2011
by
Lucas Carvalho
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify the code to starting to integrate with the new design of networkcache http server.
parent
fb3af4fc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
28 deletions
+38
-28
src/zc/buildout/networkcache.py
src/zc/buildout/networkcache.py
+38
-28
No files found.
src/zc/buildout/networkcache.py
View file @
9b1213ff
...
...
@@ -13,39 +13,36 @@
##############################################################################
import
hashlib
import
os
import
posixpath
import
hashlib
from
libnetworkcache
import
NetworkcacheClient
import
re
from
download
import
check_md5sum
from
slapos.tool.libnetworkcache
import
NetworkcacheClient
def
_get_hash_from_file
(
path
,
hash_object
):
"""
Calculate the hash from file.
"""
f
=
open
(
path
,
'rb'
)
try
:
chunk
=
f
.
read
(
2
**
16
)
while
chunk
:
hash_object
.
update
(
chunk
)
chunk
=
f
.
read
(
2
**
16
)
return
hash_object
.
hexdigest
()
finally
:
f
.
close
()
_md5_re
=
re
.
compile
(
r'md5=([a-f0-9]+)'
)
def
check_sha256sum
(
path
,
sha256sum
):
"""Tell whether the SHA256sum checksum of the file at path matches
"""
return
sha256sum
==
_get_hash_from_file
(
path
,
hashlib
.
sha256
())
def
_get_md5_from_url
(
url
):
match
=
_md5_re
.
search
(
url
)
if
match
:
return
match
.
group
(
1
)
return
None
def
_update_network_cached_log
(
key
,
url
):
def
_update_network_cached_log
(
path
,
url
):
"""Update the networkcached.log file.
Adding the last key and URL which has been retrieved from networkcached server.
"""
f
=
open
(
path
,
'r'
)
try
:
key
=
hashlib
.
sha512
(
f
.
read
()).
hexdigest
()
finally
:
f
.
close
()
# Keep the log
map_path
=
os
.
path
.
join
(
os
.
environ
.
get
(
'PWD'
),
'.networkcached.log'
)
mode
=
'r+'
...
...
@@ -66,7 +63,7 @@ def _update_network_cached_log(key, url):
f
.
close
()
def
download_network_cached
(
network_cache
,
path
,
url
,
logger
):
def
download_network_cached
(
network_cache
,
path
,
url
,
logger
,
md5sum
=
None
):
"""Download from a network cache provider
If something fail (providor be offline, or hash_string fail), we ignore
...
...
@@ -77,19 +74,33 @@ def download_network_cached(network_cache, path, url, logger):
if
network_cache
in
(
None
,
''
,):
# Not able to use network cache
return
False
if
md5sum
is
None
:
print
"URL "
,
url
md5sum
=
_get_md5_from_url
(
url
)
# XXX (lucas): This will be removed.
key
=
hashlib
.
sha512
(
url
).
hexdigest
()
key
=
hashlib
.
sha256
(
url
).
hexdigest
()
network_cached_url
=
os
.
path
.
join
(
network_cache
,
key
)
logger
.
info
(
'Downloading from network cache %s'
%
network_cached_url
)
try
:
nc
=
NetworkcacheClient
(
network_cache
)
result
,
path
=
nc
.
get
(
key
,
path
)
result
=
nc
.
get
(
key
)
if
result
.
status
!=
200
:
logger
.
info
(
'Fail to download from network cache: File Not Found.'
)
return
False
if
not
check_sha256sum
(
path
,
result
.
getheader
(
'x-sha256sum'
)):
logger
.
info
(
'SHA256 checksum mismatch downloading %r'
%
\
file_content
=
result
.
read
()
# save the file
f
=
open
(
path
,
'w+b'
)
try
:
f
.
write
(
file_content
)
finally
:
f
.
close
()
if
not
check_md5sum
(
path
,
md5sum
):
logger
.
info
(
'MD5 checksum mismatch downloading %r'
%
\
network_cached_url
)
return
False
except
IOError
,
e
:
...
...
@@ -107,12 +118,11 @@ def upload_network_cached(network_cache, external_url, path, logger):
'Upload cache ignored, network-cache was not provided'
)
return
False
key
=
hashlib
.
sha256
(
external_url
).
hexdigest
()
try
:
nc
=
NetworkcacheClient
(
network_cache
)
result
=
nc
.
put
(
key
,
path
)
result
=
nc
.
put
(
path
)
if
result
.
status
==
200
:
_update_network_cached_log
(
key
,
external_url
)
_update_network_cached_log
(
path
,
external_url
)
except
(
IOError
,
EOFError
),
e
:
logger
.
info
(
'Fail to upload cache on %s. %s'
%
\
...
...
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