diff options
author | Milo Casagrande <milo@ubuntu.com> | 2013-07-25 12:19:10 +0200 |
---|---|---|
committer | Milo Casagrande <milo@ubuntu.com> | 2013-07-25 12:19:10 +0200 |
commit | e50c68ac302abd4a2460a1214f61763ddcde1b48 (patch) | |
tree | 465d48177a29f4ea4c27a6c6475931bd23f73729 /lava_tool | |
parent | f394326c116c2c368df910300297d1baa1279435 (diff) |
Refactored testdef init, refactored utils functions, fixed tests.
Diffstat (limited to 'lava_tool')
-rw-r--r-- | lava_tool/tests/test_utils.py | 35 | ||||
-rw-r--r-- | lava_tool/utils.py | 102 |
2 files changed, 136 insertions, 1 deletions
diff --git a/lava_tool/tests/test_utils.py b/lava_tool/tests/test_utils.py index 6a9c76a..868bb9b 100644 --- a/lava_tool/tests/test_utils.py +++ b/lava_tool/tests/test_utils.py @@ -18,12 +18,17 @@ """lava_tool.utils tests.""" +import os import subprocess +import tempfile from unittest import TestCase from mock import patch -from lava_tool.utils import has_command +from lava_tool.utils import ( + has_command, + verify_file_extension, +) class UtilTests(TestCase): @@ -39,3 +44,31 @@ class UtilTests(TestCase): # Check that a "command" exists. The call to subprocess is mocked. mocked_check_call.return_value = 0 self.assertTrue(has_command("")) + + def test_verify_file_extension_with_extension(self): + extension = ".test" + supported = [extension[1:]] + try: + temp_file = tempfile.NamedTemporaryFile(suffix=extension, + delete=False) + obtained = verify_file_extension( + temp_file.name, extension[1:], supported) + self.assertEquals(temp_file.name, obtained) + finally: + if os.path.isfile(temp_file.name): + os.unlink(temp_file.name) + + def test_verify_file_extension_without_extension(self): + extension = "json" + supported = [extension] + expected = "/tmp/a_fake.json" + obtained = verify_file_extension("/tmp/a_fake", extension, supported) + self.assertEquals(expected, obtained) + + def test_verify_file_extension_with_unsupported_extension(self): + extension = "json" + supported = [extension] + expected = "/tmp/a_fake.json" + obtained = verify_file_extension( + "/tmp/a_fake.extension", extension, supported) + self.assertEquals(expected, obtained) diff --git a/lava_tool/utils.py b/lava_tool/utils.py index 1982cfc..98882a9 100644 --- a/lava_tool/utils.py +++ b/lava_tool/utils.py @@ -19,6 +19,8 @@ import os import subprocess +from lava.tool.errors import CommandError + def has_command(command): """Checks that the given command is available. @@ -32,3 +34,103 @@ def has_command(command): except subprocess.CalledProcessError: command_available = False return command_available + + +def write_file(path, content): + """Creates a file with the specified content. + + :param path: The path of the file to write. + :param content: What to write in the file. + """ + try: + with open(path, "w") as to_write: + to_write.write(content) + except (OSError, IOError): + raise CommandError("Error writing file '{0}'".format(path)) + + +def verify_file_extension(path, default, supported): + """Verifies if a file has a supported extensions. + + If the file does not have one, it will add the default extension + provided. + + :param path: The path of a file to verify. + :param default: The default extension to use. + :param supported: A list of supported extensions to check against. + :return The path of the file. + """ + full_path, file_name = os.path.split(path) + name, extension = os.path.splitext(file_name) + if not extension: + path = ".".join([path, default]) + elif extension[1:].lower() not in supported: + path = os.path.join(full_path, ".".join([name, default])) + return path + + +def verify_path_existance(path): + """Verifies if a given path exists or not on the file system. + + Raises a CommandError in case it exists. + + :param path: The path to verify.""" + if os.path.exists(path): + raise CommandError("{0} already exists.".format(path)) + + +def retrieve_file(path, extensions): + """Searches for a file that has one of the supported extensions. + + The path of the first file that matches one of the supported provided + extensions will be returned. The files are examined in alphabetical + order. + + :param path: Where to look for the file. + :param extensions: A list of extensions the file to look for should + have. + :return The full path of the file. + """ + if os.path.isfile(path): + if check_valid_extension(path, extensions): + retrieved_path = path + else: + raise CommandError("The provided file '{0}' is not " + "valid: extension not supported.".format(path)) + else: + dir_listing = os.listdir(path) + dir_listing.sort() + + for element in dir_listing: + if element.startswith("."): + continue + + element_path = os.path.join(path, element) + if os.path.isdir(element_path): + continue + elif os.path.isfile(element_path): + if check_valid_extension(element_path, extensions): + retrieved_path = element_path + break + else: + raise CommandError("No suitable file found in '{0}'".format(path)) + + return retrieved_path + + +def check_valid_extension(path, extensions): + """Checks that a file has one of the supported extensions. + + :param path: The file to check. + :param extensions: A list of supported extensions. + """ + is_valid = False + + local_path, file_name = os.path.split(path) + name, full_extension = os.path.splittext(file_name) + + if full_extension: + extension = full_extension[1:].strip().lower() + if extension in extensions: + is_valid = True + return is_valid |