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: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 781625ac
......@@ -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')
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册