From: Ondrej Lichtner <olichtne(a)redhat.com>
Renaming the module and the contained methods/classes to just Path since
it's implemented in a general way and can be used in othe parts of LNST,
not directly related to Recipes.
Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
---
lnst/Common/Path.py | 155 +++++++++++++++++++++++++++++++++++
lnst/Common/RecipePath.py | 155 -----------------------------------
lnst/Controller/NetTestController.py | 6 +-
lnst/Controller/RecipeParser.py | 4 +-
4 files changed, 160 insertions(+), 160 deletions(-)
create mode 100644 lnst/Common/Path.py
delete mode 100644 lnst/Common/RecipePath.py
diff --git a/lnst/Common/Path.py b/lnst/Common/Path.py
new file mode 100644
index 0000000..fe7f709
--- /dev/null
+++ b/lnst/Common/Path.py
@@ -0,0 +1,155 @@
+"""
+This module contains code code for paths and references.
+
+Copyright 2012 Red Hat, Inc.
+Licensed under the GNU General Public License, version 2 as
+published by the Free Software Foundation; see COPYING for details.
+"""
+
+__author__ = """
+jtluka(a)redhat.com (Jan Tluka)
+"""
+
+import os
+from urlparse import urljoin
+from urllib2 import urlopen, HTTPError
+from tempfile import NamedTemporaryFile
+
+def get_path_class(root, path):
+ if root == None:
+ if path.startswith('http'):
+ return HttpPath(root, path)
+ else:
+ if os.access(path, os.R_OK):
+ return FilePath(None, os.path.realpath(path))
+ else:
+ raise Exception("Path does not exist \"%s\"!" %
path)
+
+ if root.startswith('http'):
+ return HttpPath(root, path)
+ elif os.access(root, os.R_OK):
+ return FilePath(root, path)
+ else:
+ raise Exception("Could not recognize path type \"%s\"" %
path)
+
+class Path:
+ def __init__(self, root, path):
+ self._path_class = get_path_class(root, path)
+
+ def get_root(self):
+ return self._path_class.get_root()
+
+ def abs_path(self):
+ return self._path_class.abs_path()
+
+ def to_str(self):
+ return self._path_class.to_str()
+
+ def exists(self):
+ return self._path_class.exists()
+
+ def resolve(self):
+ return self._path_class.resolve()
+
+class PathGeneric:
+ def __init__(self, root, path):
+ self._root = root
+ self._path = path
+ self._data = None
+
+ def get_root(self):
+ pass
+
+ def abs_path(self):
+ pass
+
+ def to_str(self):
+ pass
+
+ def exists(self):
+ pass
+
+ def resolve(self):
+ pass
+
+class FilePath(PathGeneric):
+ def _load_file(self):
+ f = open(self.abs_path(),'r')
+ self._data = f.read()
+ f.close()
+
+ def to_str(self):
+ if not self._data:
+ self._load_file()
+
+ return self._data
+
+ def _append_path(self, path):
+ basedir = os.path.dirname(self._path)
+ return os.path.join(basedir, path)
+
+ def abs_path(self):
+ if self._root:
+ return os.path.normpath(os.path.join(self._root,
+ os.path.expanduser(self._path)))
+ else:
+ return os.path.normpath(os.path.expanduser(self._path))
+
+ 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 HttpPath(PathGeneric):
+ _file = None
+ def _get_url(self):
+ url = self.abs_path()
+
+ try:
+ f = urlopen(url)
+ self._data = f.read()
+ except IOError as err:
+ msg = "Unable to resolve path: %s (%s)" % (url, str(err))
+ raise Exception(msg)
+
+ f.close()
+
+ def to_str(self):
+ if not self._data:
+ self._get_url()
+
+ return self._data
+
+ def abs_path(self):
+ if self._root:
+ return urljoin(self._root + '/', self._path)
+ else:
+ return self._path
+
+ 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(delete=True)
+ self._file.write(self.to_str())
+ self._file.flush()
+
+ return self._file.name
diff --git a/lnst/Common/RecipePath.py b/lnst/Common/RecipePath.py
deleted file mode 100644
index 3199a1a..0000000
--- a/lnst/Common/RecipePath.py
+++ /dev/null
@@ -1,155 +0,0 @@
-"""
-This module contains code code for LNST recipe paths and references.
-
-Copyright 2012 Red Hat, Inc.
-Licensed under the GNU General Public License, version 2 as
-published by the Free Software Foundation; see COPYING for details.
-"""
-
-__author__ = """
-jtluka(a)redhat.com (Jan Tluka)
-"""
-
-import os
-from urlparse import urljoin
-from urllib2 import urlopen, HTTPError
-from tempfile import NamedTemporaryFile
-
-def get_recipepath_class(root, path):
- if root == None:
- if path.startswith('http'):
- return HttpRecipePath(root, path)
- else:
- if os.access(path, os.R_OK):
- return FileRecipePath(None, os.path.realpath(path))
- else:
- raise Exception("Recipe path does not exist \"%s\"!"
% path)
-
- if root.startswith('http'):
- return HttpRecipePath(root, path)
- elif os.access(root, os.R_OK):
- return FileRecipePath(root, path)
- else:
- raise Exception("Could not recognize recipe path type \"%s\""
% path)
-
-class RecipePath:
- def __init__(self, root, path):
- self._recipepath_class = get_recipepath_class(root, path)
-
- def get_root(self):
- return self._recipepath_class.get_root()
-
- def abs_path(self):
- return self._recipepath_class.abs_path()
-
- 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
- self._path = path
- self._data = None
-
- def get_root(self):
- pass
-
- def abs_path(self):
- pass
-
- 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')
- self._data = f.read()
- f.close()
-
- def to_str(self):
- if not self._data:
- self._load_file()
-
- return self._data
-
- def _append_path(self, path):
- basedir = os.path.dirname(self._path)
- return os.path.join(basedir, path)
-
- def abs_path(self):
- if self._root:
- return os.path.normpath(os.path.join(self._root,
- os.path.expanduser(self._path)))
- else:
- return os.path.normpath(os.path.expanduser(self._path))
-
- 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()
-
- try:
- f = urlopen(url)
- self._data = f.read()
- except IOError as err:
- msg = "Unable to resolve path: %s (%s)" % (url, str(err))
- raise Exception(msg)
-
- f.close()
-
- def to_str(self):
- if not self._data:
- self._get_url()
-
- return self._data
-
- def abs_path(self):
- if self._root:
- return urljoin(self._root + '/', self._path)
- else:
- return self._path
-
- 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
diff --git a/lnst/Controller/NetTestController.py b/lnst/Controller/NetTestController.py
index ec0db54..2208346 100644
--- a/lnst/Controller/NetTestController.py
+++ b/lnst/Controller/NetTestController.py
@@ -31,7 +31,7 @@ from lnst.Controller.Machine import StaticInterface
from lnst.Common.ConnectionHandler import send_data, recv_data
from lnst.Common.ConnectionHandler import ConnectionHandler
from lnst.Common.Config import lnst_config
-from lnst.Common.RecipePath import RecipePath
+from lnst.Common.Path import Path
from lnst.Common.Colours import decorate_with_preset
from lnst.Common.NetUtils import test_tcp_connection
import lnst.Controller.Task as Task
@@ -357,8 +357,8 @@ class NetTestController:
task["tools_dir"] = task_data["tools_dir"]
if "python" in task_data:
- root = RecipePath(None, self._recipe_path).get_root()
- path = RecipePath(root, task_data["python"])
+ root = Path(None, self._recipe_path).get_root()
+ path = Path(root, task_data["python"])
task["python"] = path
if not path.exists():
diff --git a/lnst/Controller/RecipeParser.py b/lnst/Controller/RecipeParser.py
index 8be52fb..318b231 100644
--- a/lnst/Controller/RecipeParser.py
+++ b/lnst/Controller/RecipeParser.py
@@ -11,7 +11,7 @@ rpazdera(a)redhat.com (Radek Pazdera)
"""
import os
-from lnst.Common.RecipePath import RecipePath
+from lnst.Common.Path import Path
from lnst.Controller.XmlParser import XmlParser
from lnst.Controller.XmlProcessing import XmlProcessingError, XmlData
from lnst.Controller.XmlProcessing import XmlCollection
@@ -21,7 +21,7 @@ class RecipeError(XmlProcessingError):
class RecipeParser(XmlParser):
def __init__(self, recipe_path):
- recipe_path = RecipePath(None, recipe_path).abs_path()
+ recipe_path = Path(None, recipe_path).abs_path()
super(RecipeParser, self).__init__("schema-recipe.rng", recipe_path)
def _process(self, lnst_recipe):
--
2.6.0