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
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
Ophélie Gagnard
slapos.libnetworkcache
Commits
a8c84684
Commit
a8c84684
authored
Sep 04, 2012
by
Cédric de Saint Martin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add strategy parameter, allowing to choose best entry.
parent
dee03eda
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
13 deletions
+35
-13
slapos/networkcachehelper.py
slapos/networkcachehelper.py
+35
-13
No files found.
slapos/networkcachehelper.py
View file @
a8c84684
...
...
@@ -129,27 +129,32 @@ def helper_upload_network_cached_from_directory(dir_url, cache_url,
signature_private_key_file
,
shacache_cert_file
,
shacache_key_file
,
shadir_cert_file
,
shadir_key_file
)
finally
:
# Clean it
# Always clean it
if
os
.
path
.
exists
(
tarpath
):
os
.
remove
(
tarpath
)
return
result
def
helper_download_network_cached
(
dir_url
,
cache_url
,
signature_certificate_list
,
directory_key
,
wanted_metadata_dict
=
{},
required_key_list
=
[]):
directory_key
,
wanted_metadata_dict
=
{},
required_key_list
=
[],
strategy
=
None
):
"""
Downloads from a network cache provider.
Select from shadir directory_key entry matching (at least)
wanted_metadata_dict and with all metadata keys in required_key_list defined
and not null.
if a "strategy" function is given as parameter, use it to choose the best
entry between list of matching entries. Otherwise, choose the first.
This strategy function takes a list of entries as parameter, and should
return the best entry.
If something fails (providor be offline, or hash_string fail), we ignore
network cached index.
return (file_descriptor, metadata) if succeeded, False otherwise.
"""
# XXX It should be possible to give a function as parameter to specify
# strategy to choose best matching entry.
if
not
(
dir_url
and
cache_url
):
return
False
...
...
@@ -170,6 +175,7 @@ def helper_download_network_cached(dir_url, cache_url,
json_entry_list
=
nc
.
select_generic
(
directory_key
)
# For each entry shadir sent, chooses only the entry matching all
# wanted metadata, and having all wanted keys
matching_entry_list
=
[]
for
entry
,
_
in
json_entry_list
:
try
:
tags
=
json
.
loads
(
entry
)
...
...
@@ -188,11 +194,23 @@ def helper_download_network_cached(dir_url, cache_url,
break
if
not
match
:
continue
# Everything match. Downloads corresponding file
file_descriptor
=
nc
.
download
(
tags
.
get
(
'sha512'
))
break
# Everything match. Add it to list of matching entries
matching_entry_list
.
append
(
tags
)
except
Exception
:
pass
# If a strategy is defined, call it to determine best entry
if
strategy
:
best_entry
=
strategy
(
matching_entry_list
)
if
not
best_entry
:
logger
.
info
(
"Can't find best entry matching strategy, selecting "
"random one between acceptable ones."
)
best_entry
=
matching_entry_list
[
0
]
else
:
best_entry
=
matching_entry_list
[
0
]
# download best entry
file_descriptor
=
nc
.
download
(
best_entry
.
get
(
'sha512'
))
if
file_descriptor
is
not
None
:
return
file_descriptor
,
tags
else
:
...
...
@@ -211,14 +229,15 @@ def helper_download_network_cached(dir_url, cache_url,
def
helper_download_network_cached_to_file
(
dir_url
,
cache_url
,
signature_certificate_list
,
directory_key
,
path
,
wanted_metadata_dict
=
{}):
directory_key
,
path
,
wanted_metadata_dict
=
{},
required_key_list
=
[],
strategy
=
None
):
"""
Download a file from network cache. It is the responsibility of caller method
to check md5.
"""
result
=
helper_download_network_cached
(
dir_url
,
cache_url
,
signature_certificate_list
,
directory_key
,
wanted_metadata_dict
)
directory_key
,
wanted_metadata_dict
,
required_key_list
,
strategy
)
if
result
:
# XXX check if nc filters signature_certificate_list!
# Creates a file with content to desired path.
...
...
@@ -235,7 +254,8 @@ def helper_download_network_cached_to_file(dir_url, cache_url,
def
helper_download_network_cached_to_directory
(
dir_url
,
cache_url
,
signature_certificate_list
,
directory_key
,
path
,
wanted_metadata_dict
=
{}):
directory_key
,
path
,
wanted_metadata_dict
=
{},
required_key_list
=
[],
strategy
=
None
):
"""
Download a tar file from network cache and untar it to specified path.
"""
...
...
@@ -246,7 +266,8 @@ def helper_download_network_cached_to_directory(dir_url, cache_url,
metadata_dict
=
helper_download_network_cached_to_file
(
dir_url
,
cache_url
,
signature_certificate_list
,
directory_key
,
tarpath
,
wanted_metadata_dict
)
directory_key
,
tarpath
,
wanted_metadata_dict
,
required_key_list
,
strategy
)
if
metadata_dict
:
# Untar it to path
tar
=
tarfile
.
open
(
tarpath
)
...
...
@@ -258,5 +279,6 @@ def helper_download_network_cached_to_directory(dir_url, cache_url,
finally
:
# Always clean it
if
os
.
path
.
exists
(
tarpath
):
os
.
remove
(
tarpath
)
return
metadata_dict
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