commit 16eaa1bef30c919b4460839d5bf945169665357e Author: Jan Tluka jtluka@redhat.com Date: Mon Jul 21 16:38:38 2014 +0200
RecipePath: Add exists and resolve methods
These two new methods, exists() and resolve() are needed for proper handling of python tasks if a recipe is specified as URL.
The exists() method returns True if a file or URL exists, False otherwise.
The resolve() method returns the filename containing the data. In case of ordinary file nothing special needs to be done. In case of URL, the file is downloaded and saved in temporary file.
Signed-off-by: Jan Tluka jtluka@redhat.com Signed-off-by: Jiri Pirko jiri@resnulli.us
lnst/Common/RecipePath.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 40 insertions(+), 0 deletions(-) --- diff --git a/lnst/Common/RecipePath.py b/lnst/Common/RecipePath.py index 84c6acc..b976238 100644 --- a/lnst/Common/RecipePath.py +++ b/lnst/Common/RecipePath.py @@ -13,6 +13,7 @@ jtluka@redhat.com (Jan Tluka) import os from urlparse import urljoin from urllib2 import urlopen +from tempfile import NamedTemporaryFile
def get_recipepath_class(root, path): if root == None: @@ -44,6 +45,12 @@ class RecipePath: def to_str(self): return self._recipepath_class.to_str()
+ def exists(self): + return self._recipepath_class.exists() + + def resolve(self): + return self._recipepath_class.resolve() + class RecipePathGeneric: def __init__(self, root, path): self._root = root @@ -59,6 +66,12 @@ class RecipePathGeneric: def to_str(self): pass
+ def exists(self): + pass + + def resolve(self): + pass + class FileRecipePath(RecipePathGeneric): def _load_file(self): f = open(self.abs_path(),'r') @@ -85,7 +98,14 @@ class FileRecipePath(RecipePathGeneric): def get_root(self): return os.path.dirname(self.abs_path())
+ def exists(self): + return os.path.isfile(self.abs_path()) + + def resolve(self): + return self.abs_path() + class HttpRecipePath(RecipePathGeneric): + _file = None def _get_url(self): url = self.abs_path()
@@ -113,3 +133,23 @@ class HttpRecipePath(RecipePathGeneric): def get_root(self): url = self.abs_path() return url.rpartition('/')[0] + + def exists(self): + url = self.abs_path() + try: + f = urlopen(url) + except HTTPError: + return False + + f.close() + return True + + def resolve(self): + if self._file: + return self._file.name + + self._file = NamedTemporaryFile(suffix='.py',delete=True) + self._file.write(self.to_str()) + self._file.flush() + + return self._file.name
lnst-developers@lists.fedorahosted.org