提交 c8238952 编写于 作者: R Rudá Moura 提交者: Rudá Moura

Merge pull request #119 from lmr/avocado-utils-work

Avocado utils work
...@@ -33,6 +33,7 @@ import shutil ...@@ -33,6 +33,7 @@ import shutil
import time import time
import tempfile import tempfile
from avocado.utils import path
from avocado.settings import settings from avocado.settings import settings
_BASE_DIR = os.path.join(sys.modules[__name__].__file__, "..", "..", "..") _BASE_DIR = os.path.join(sys.modules[__name__].__file__, "..", "..", "..")
...@@ -204,9 +205,7 @@ def get_job_logs_dir(args=None): ...@@ -204,9 +205,7 @@ def get_job_logs_dir(args=None):
else: else:
logdir = get_logs_dir() logdir = get_logs_dir()
debugbase = 'run-%s' % start_time debugbase = 'run-%s' % start_time
debugdir = os.path.join(logdir, debugbase) debugdir = path.init_dir(logdir, debugbase)
if not os.path.isdir(debugdir):
os.makedirs(debugdir)
latestdir = os.path.join(logdir, "latest") latestdir = os.path.join(logdir, "latest")
try: try:
os.unlink(latestdir) os.unlink(latestdir)
......
...@@ -34,6 +34,7 @@ from avocado.core import status ...@@ -34,6 +34,7 @@ from avocado.core import status
from avocado.core import exceptions from avocado.core import exceptions
from avocado.core import error_codes from avocado.core import error_codes
from avocado.utils import archive from avocado.utils import archive
from avocado.utils import path
from avocado import multiplex_config from avocado import multiplex_config
from avocado import test from avocado import test
from avocado import result from avocado import result
...@@ -149,7 +150,7 @@ class TestRunner(object): ...@@ -149,7 +150,7 @@ class TestRunner(object):
# If there's nothing inside the queue after timeout, the process # If there's nothing inside the queue after timeout, the process
# must be terminated. # must be terminated.
send_signal(p, signal.SIGUSR1) send_signal(p, signal.SIGUSR1)
test_instance = q.get(timeout=0.1) test_instance = q.get()
self.result.check_test(test_instance) self.result.check_test(test_instance)
if not status.mapping[test_instance.status]: if not status.mapping[test_instance.status]:
...@@ -191,9 +192,7 @@ class Job(object): ...@@ -191,9 +192,7 @@ class Job(object):
self.test_index = 1 self.test_index = 1
self.status = "RUNNING" self.status = "RUNNING"
self.result_proxy = result.TestResultProxy() self.result_proxy = result.TestResultProxy()
self.sysinfo_dir = os.path.join(self.logdir, 'sysinfo') self.sysinfo_dir = path.init_dir(self.logdir, 'sysinfo')
if not os.path.isdir(self.sysinfo_dir):
os.makedirs(self.sysinfo_dir)
self.sysinfo_logger = sysinfo.SysInfo(basedir=self.sysinfo_dir) self.sysinfo_logger = sysinfo.SysInfo(basedir=self.sysinfo_dir)
self.output_manager = output.OutputManager() self.output_manager = output.OutputManager()
......
...@@ -26,7 +26,6 @@ from avocado.result import TestResult ...@@ -26,7 +26,6 @@ from avocado.result import TestResult
from avocado.plugins import plugin from avocado.plugins import plugin
from avocado.utils import virt from avocado.utils import virt
from avocado.utils import archive from avocado.utils import archive
from avocado.utils.misc import unique
class Test(object): class Test(object):
...@@ -119,7 +118,7 @@ class VMTestResult(TestResult): ...@@ -119,7 +118,7 @@ class VMTestResult(TestResult):
def _copy_tests(self): def _copy_tests(self):
self.vm.remote.makedir(self.remote_test_dir) self.vm.remote.makedir(self.remote_test_dir)
uniq_urls = unique(self.urls) uniq_urls = list(set(self.urls))
for url in uniq_urls: for url in uniq_urls:
test_path = os.path.join(self.test_dir, url) test_path = os.path.join(self.test_dir, url)
self.vm.remote.send_files(test_path, self.remote_test_dir) self.vm.remote.send_files(test_path, self.remote_test_dir)
......
...@@ -100,7 +100,7 @@ class Loggable(object): ...@@ -100,7 +100,7 @@ class Loggable(object):
""" """
path = os.path.join(logdir, self.logf) path = os.path.join(logdir, self.logf)
if os.path.exists(path): if os.path.exists(path):
return utils.misc.read_one_line(path) return utils.io.read_one_line(path)
else: else:
return "" return ""
...@@ -329,10 +329,7 @@ class SysInfo(object): ...@@ -329,10 +329,7 @@ class SysInfo(object):
logging packages is a costly operation). logging packages is a costly operation).
""" """
if basedir is None: if basedir is None:
basedir = os.path.join(os.getcwd(), 'sysinfo') basedir = utils.path.init_dir(os.getcwd(), 'sysinfo')
if not os.path.isdir(basedir):
os.makedirs(basedir)
self.basedir = basedir self.basedir = basedir
self.log_packages = log_packages self.log_packages = log_packages
...@@ -465,7 +462,7 @@ class SysInfo(object): ...@@ -465,7 +462,7 @@ class SysInfo(object):
def _log_installed_packages(self, path): def _log_installed_packages(self, path):
installed_path = os.path.join(path, "installed_packages") installed_path = os.path.join(path, "installed_packages")
installed_packages = "\n".join(self._get_installed_packages()) + "\n" installed_packages = "\n".join(self._get_installed_packages()) + "\n"
utils.misc.write_file(installed_path, installed_packages) utils.io.write_file(installed_path, installed_packages)
def _log_modified_packages(self, path): def _log_modified_packages(self, path):
""" """
...@@ -475,18 +472,16 @@ class SysInfo(object): ...@@ -475,18 +472,16 @@ class SysInfo(object):
new_packages = set(self._get_installed_packages()) new_packages = set(self._get_installed_packages())
added_path = os.path.join(path, "added_packages") added_path = os.path.join(path, "added_packages")
added_packages = "\n".join(new_packages - old_packages) + "\n" added_packages = "\n".join(new_packages - old_packages) + "\n"
utils.misc.write_file(added_path, added_packages) utils.io.write_file(added_path, added_packages)
removed_path = os.path.join(self.basedir, "removed_packages") removed_path = os.path.join(self.basedir, "removed_packages")
removed_packages = "\n".join(old_packages - new_packages) + "\n" removed_packages = "\n".join(old_packages - new_packages) + "\n"
utils.misc.write_file(removed_path, removed_packages) utils.io.write_file(removed_path, removed_packages)
def start_job_hook(self): def start_job_hook(self):
""" """
Logging hook called whenever a job starts, and again after reboot. Logging hook called whenever a job starts, and again after reboot.
""" """
pre_dir = os.path.join(self.basedir, 'pre') pre_dir = utils.path.init_dir(self.basedir, 'pre')
if not os.path.isdir(pre_dir):
os.makedirs(pre_dir)
for log in self.start_job_loggables: for log in self.start_job_loggables:
log.run(pre_dir) log.run(pre_dir)
...@@ -497,9 +492,7 @@ class SysInfo(object): ...@@ -497,9 +492,7 @@ class SysInfo(object):
""" """
Logging hook called whenever a job starts, and again after reboot. Logging hook called whenever a job starts, and again after reboot.
""" """
post_dir = os.path.join(self.basedir, 'post') post_dir = utils.path.init_dir(self.basedir, 'post')
if not os.path.isdir(post_dir):
os.makedirs(post_dir)
for log in self.start_job_loggables: for log in self.start_job_loggables:
log.run(post_dir) log.run(post_dir)
...@@ -510,9 +503,7 @@ class SysInfo(object): ...@@ -510,9 +503,7 @@ class SysInfo(object):
""" """
Logging hook called before a test starts. Logging hook called before a test starts.
""" """
pre_dir = os.path.join(self.basedir, 'pre') pre_dir = utils.path.init_dir(self.basedir, 'pre')
if not os.path.isdir(pre_dir):
os.makedirs(pre_dir)
for log in self.start_test_loggables: for log in self.start_test_loggables:
log.run(pre_dir) log.run(pre_dir)
...@@ -523,9 +514,7 @@ class SysInfo(object): ...@@ -523,9 +514,7 @@ class SysInfo(object):
""" """
Logging hook called after a test finishes. Logging hook called after a test finishes.
""" """
post_dir = os.path.join(self.basedir, 'post') post_dir = utils.path.init_dir(self.basedir, 'post')
if not os.path.isdir(post_dir):
os.makedirs(post_dir)
for log in self.end_test_loggables: for log in self.end_test_loggables:
log.run(post_dir) log.run(post_dir)
...@@ -536,9 +525,7 @@ class SysInfo(object): ...@@ -536,9 +525,7 @@ class SysInfo(object):
""" """
Logging hook called before a test iteration Logging hook called before a test iteration
""" """
pre_dir = os.path.join(self.basedir, 'pre') pre_dir = utils.path.init_dir(self.basedir, 'pre')
if not os.path.isdir(pre_dir):
os.makedirs(pre_dir)
for log in self.start_iteration_loggables: for log in self.start_iteration_loggables:
log.run(pre_dir) log.run(pre_dir)
...@@ -546,10 +533,7 @@ class SysInfo(object): ...@@ -546,10 +533,7 @@ class SysInfo(object):
""" """
Logging hook called after a test iteration Logging hook called after a test iteration
""" """
post_dir = os.path.join(self.basedir, 'post') post_dir = utils.path.init_dir(self.basedir, 'post')
if not os.path.isdir(post_dir):
os.makedirs(post_dir)
post_dir = os.path.join(self.basedir, 'post')
for log in self.end_iteration_loggables: for log in self.end_iteration_loggables:
log.run(post_dir) log.run(post_dir)
......
...@@ -26,6 +26,7 @@ import unittest ...@@ -26,6 +26,7 @@ import unittest
from avocado.core import data_dir from avocado.core import data_dir
from avocado.core import exceptions from avocado.core import exceptions
from avocado.utils import path
from avocado.utils import process from avocado.utils import process
from avocado.utils.params import Params from avocado.utils.params import Params
from avocado import sysinfo from avocado import sysinfo
...@@ -116,23 +117,15 @@ class Test(unittest.TestCase): ...@@ -116,23 +117,15 @@ class Test(unittest.TestCase):
self.job = job self.job = job
self.basedir = os.path.join(data_dir.get_test_dir(), self.name) self.basedir = os.path.join(data_dir.get_test_dir(), self.name)
self.datadir = os.path.join(self.basedir, 'data') self.datadir = os.path.join(self.basedir, 'data')
self.workdir = os.path.join(data_dir.get_tmp_dir(), self.name) self.workdir = path.init_dir(data_dir.get_tmp_dir(), self.name)
if not os.path.isdir(self.workdir): self.srcdir = path.init_dir(self.workdir, 'src')
os.makedirs(self.workdir)
self.srcdir = os.path.join(self.workdir, 'src')
if not os.path.isdir(self.srcdir):
os.makedirs(self.srcdir)
if base_logdir is None: if base_logdir is None:
base_logdir = data_dir.get_job_logs_dir() base_logdir = data_dir.get_job_logs_dir()
self.tagged_name = self.get_tagged_name(base_logdir) self.tagged_name = self.get_tagged_name(base_logdir)
self.logdir = os.path.join(base_logdir, self.tagged_name) self.logdir = path.init_dir(base_logdir, self.tagged_name)
if not os.path.isdir(self.logdir):
os.makedirs(self.logdir)
self.logfile = os.path.join(self.logdir, 'debug.log') self.logfile = os.path.join(self.logdir, 'debug.log')
self.outputdir = os.path.join(self.logdir, 'data') self.outputdir = path.init_dir(self.logdir, 'data')
if not os.path.isdir(self.outputdir): self.sysinfodir = path.init_dir(self.logdir, 'sysinfo')
os.makedirs(self.outputdir)
self.sysinfodir = os.path.join(self.logdir, 'sysinfo')
self.sysinfo_logger = sysinfo.SysInfo(basedir=self.sysinfodir) self.sysinfo_logger = sysinfo.SysInfo(basedir=self.sysinfodir)
self.log = logging.getLogger("avocado.test") self.log = logging.getLogger("avocado.test")
......
...@@ -12,9 +12,14 @@ ...@@ -12,9 +12,14 @@
# Copyright: Red Hat Inc. 2013-2014 # Copyright: Red Hat Inc. 2013-2014
# Author: Lucas Meneghel Rodrigues <lmr@redhat.com> # Author: Lucas Meneghel Rodrigues <lmr@redhat.com>
import logging import archive
import random import build
import string import crypto
import download import download
import io
import memory
import network
import params
import process import process
import misc import remote
import virt
...@@ -12,18 +12,26 @@ ...@@ -12,18 +12,26 @@
# client/shared/utils.py # client/shared/utils.py
# Authors: Martin J Bligh <mbligh@google.com>, Andy Whitcroft <apw@shadowen.org> # Authors: Martin J Bligh <mbligh@google.com>, Andy Whitcroft <apw@shadowen.org>
"""
Avocado generic IO related functions.
"""
import logging import logging
import os
log = logging.getLogger('avocado.test') log = logging.getLogger('avocado.test')
def ask(question, auto=False): def ask(question, auto=False):
""" """
Raw input with a prompt. Prompt the user with a (y/n) question.
:param question: Question to be asked :param question: Question to be asked
:type question: str
:param auto: Whether to return "y" instead of asking the question :param auto: Whether to return "y" instead of asking the question
:type auto: bool
:return: User answer
:rtype: str
""" """
if auto: if auto:
log.info("%s (y/n) y" % question) log.info("%s (y/n) y" % question)
...@@ -36,92 +44,51 @@ def read_file(filename): ...@@ -36,92 +44,51 @@ def read_file(filename):
Read the entire contents of file. Read the entire contents of file.
:param filename: Path to the file. :param filename: Path to the file.
:type filename: str
:return: File contents
:rtype: str
""" """
with open(filename, 'r') as file_obj: with open(filename, 'r') as file_obj:
contents = file_obj.read() contents = file_obj.read()
return contents return contents
f = open(filename)
try:
return f.read()
finally:
f.close()
def read_one_line(filename): def read_one_line(filename):
""" """
Read the first line of filename. Read the first line of filename.
:param filename: Path to the file. :param filename: Path to the file.
:type filename: str
:return: First line contents
:rtype: str
""" """
with open(filename, 'r') as file_obj: with open(filename, 'r') as file_obj:
line = file_obj.readline().rstrip('\n') line = file_obj.readline().rstrip('\n')
return line return line
def write_one_line(filename, line):
"""
Write one line of text to filename.
:param filename: Path to the file.
:param line: Line to be written.
"""
write_file(filename, line.rstrip('\n') + '\n')
def write_file(filename, data): def write_file(filename, data):
""" """
Write data to a file. Write data to a file.
:param filename: Path to the file. :param filename: Path to the file.
:type filename: str
:param line: Line to be written. :param line: Line to be written.
:type line: str
""" """
with open(filename, 'w') as file_obj: with open(filename, 'w') as file_obj:
file_obj.write(data) file_obj.write(data)
def get_relative_path(path, reference): def write_one_line(filename, line):
"""
Given 2 absolute paths "path" and "reference", compute the path of
"path" as relative to the directory "reference".
:param path: The absolute path to convert to a relative path.
:param reference: An absolute directory path to which the relative
path will be computed.
"""
# normalize the paths (remove double slashes, etc)
assert(os.path.isabs(path))
assert(os.path.isabs(reference))
path = os.path.normpath(path)
reference = os.path.normpath(reference)
# we could use os.path.split() but it splits from the end
path_list = path.split(os.path.sep)[1:]
ref_list = reference.split(os.path.sep)[1:]
# find the longest leading common path
for i in xrange(min(len(path_list), len(ref_list))):
if path_list[i] != ref_list[i]:
# decrement i so when exiting this loop either by no match or by
# end of range we are one step behind
i -= 1
break
i += 1
# drop the common part of the paths, not interested in that anymore
del path_list[:i]
# for each uncommon component in the reference prepend a ".."
path_list[:0] = ['..'] * (len(ref_list) - i)
return os.path.join(*path_list)
def unique(lst):
""" """
Return a list of the elements in list, but without duplicates. Write one line of text to filename.
:param lst: List with values. :param filename: Path to the file.
:return: List with non duplicate elements. :type filename: str
:param line: Line to be written.
:type line: str
""" """
return list(set(lst)) write_file(filename, line.rstrip('\n') + '\n')
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2013-2014
# Author: Yiqiao Pu <ypu@redhat.com>
"""
Avocado path related functions.
"""
import os
def init_dir(*args):
"""
Wrapper around os.path.join that creates dirs based on the final path.
:param args: List of dir arguments that will be os.path.joined.
:type directory: list
:return: directory.
:rtype: str
"""
directory = os.path.join(*args)
if not os.path.isdir(directory):
os.makedirs(directory)
return directory
...@@ -26,7 +26,6 @@ import time ...@@ -26,7 +26,6 @@ import time
import threading import threading
from avocado.core import exceptions from avocado.core import exceptions
from avocado.utils import misc
log = logging.getLogger('avocado.test') log = logging.getLogger('avocado.test')
...@@ -64,7 +63,7 @@ def find_command(cmd): ...@@ -64,7 +63,7 @@ def find_command(cmd):
path_paths = os.environ['PATH'].split(":") path_paths = os.environ['PATH'].split(":")
except IndexError: except IndexError:
path_paths = [] path_paths = []
path_paths = misc.unique(common_bin_paths + path_paths) path_paths = list(set(common_bin_paths + path_paths))
for dir_path in path_paths: for dir_path in path_paths:
cmd_path = os.path.join(dir_path, cmd) cmd_path = os.path.join(dir_path, cmd)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册