From ba546ea1ebc43ab51bcf156338393cec2b5fbe02 Mon Sep 17 00:00:00 2001 From: Lucas Meneghel Rodrigues Date: Mon, 28 Jul 2014 12:10:18 -0300 Subject: [PATCH] avocado.utils.path: Add PathInspector class We're planning to move away from the rule 1 test == 1 directory In order to do that, we need to be able to tell the difference between a dropin test (this can be written in any language as long as it is an executable file) and an avocado test (a python file that contains a test class derived from the base avocado test file). Introduce in the path library a path inspector, that has code to figure this out. Signed-off-by: Lucas Meneghel Rodrigues --- avocado/utils/path.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/avocado/utils/path.py b/avocado/utils/path.py index d175265a..a0f0d8ba 100644 --- a/avocado/utils/path.py +++ b/avocado/utils/path.py @@ -17,6 +17,10 @@ Avocado path related functions. """ import os +import stat + +PY_EXTENSIONS = ['.py'] +SHEBANG = '#!' def init_dir(*args): @@ -32,3 +36,42 @@ def init_dir(*args): if not os.path.isdir(directory): os.makedirs(directory) return directory + + +class PathInspector(object): + + def __init__(self, path): + self.path = path + + def get_first_line(self): + first_line = "" + if os.path.isfile(self.path): + checked_file = open(self.path, "r") + first_line = checked_file.readline() + checked_file.close() + return first_line + + def has_exec_permission(self): + mode = os.stat(self.path)[stat.ST_MODE] + return mode & stat.S_IXUSR + + def is_empty(self): + size = os.stat(self.path)[stat.ST_SIZE] + return size == 0 + + def is_script(self, language=None): + first_line = self.get_first_line() + if first_line: + if first_line.startswith(SHEBANG): + if language is None: + return True + elif language in first_line: + return True + return False + + def is_python(self): + for extension in PY_EXTENSIONS: + if self.path.endswith(extension): + return True + + return self.is_script(language='python') -- GitLab