From 12cb150d6b66ee6b23c81c36ad34a35e9c9cbdd5 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Thu, 30 Jul 2015 17:18:56 +0000 Subject: Move definitions VERSION parsing code into its own definitions_version module I need to use this outside of the 'sourceresolver' module. Also, the 'sourceresolver' module is too big. Change-Id: I523bcc9555193b7369768441b72f1059e6adde5c --- morphlib/__init__.py | 1 + morphlib/definitions_version.py | 78 +++++++++++++++++++++++++++++++++++ morphlib/definitions_version_tests.py | 26 ++++++++++++ morphlib/sourceresolver.py | 50 ++-------------------- morphlib/sourceresolver_tests.py | 2 - 5 files changed, 108 insertions(+), 49 deletions(-) create mode 100644 morphlib/definitions_version.py create mode 100644 morphlib/definitions_version_tests.py diff --git a/morphlib/__init__.py b/morphlib/__init__.py index 551e04cb..81540a31 100644 --- a/morphlib/__init__.py +++ b/morphlib/__init__.py @@ -61,6 +61,7 @@ import cachedrepo import cachekeycomputer import cmdline_parse_utils import definitions_repo +import definitions_version import extensions import extractedtarball import fsutils diff --git a/morphlib/definitions_version.py b/morphlib/definitions_version.py new file mode 100644 index 00000000..44bed178 --- /dev/null +++ b/morphlib/definitions_version.py @@ -0,0 +1,78 @@ +# Copyright (C) 2015 Codethink Limited +# +# 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; version 2 of the License. +# +# 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 the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# =*= License: GPL-2 =*= + + +'''Functions for dealing with the definitions VERSION marker file.''' + + +import cliapp +import yaml + +import morphlib + + +SUPPORTED_VERSIONS = [3, 4, 5, 6] + + +class DefinitionsVersionError(cliapp.AppException): + pass + + +class UnknownVersionError(DefinitionsVersionError): # pragma: no cover + def __init__(self, version): + DefinitionsVersionError.__init__( + self, "Definitions format version %s is not supported" % version) + + +class InvalidVersionFileError(DefinitionsVersionError): # pragma: no cover + def __init__(self): + DefinitionsVersionError.__init__(self, "invalid VERSION file") + + +def parse_version_file(version_text): + '''Parse VERSION file and return the version of the format if: + + VERSION is a YAML file + and it's a dict + and has the key 'version' + and the type stored in the 'version' key is an int + + otherwise returns None + + ''' + yaml_obj = yaml.safe_load(version_text) + + return (yaml_obj['version'] if yaml_obj is not None + and isinstance(yaml_obj, dict) + and 'version' in yaml_obj + and isinstance(yaml_obj['version'], int) + + else None) + + +def check_version_file(version_text): # pragma: no cover + '''Check the VERSION information is valid and is a supported version.''' + + if version_text == None: + raise InvalidVersionFileError() + + version = morphlib.definitions_version.parse_version_file(version_text) + + if version == None: + raise InvalidVersionFileError() + + if version not in SUPPORTED_VERSIONS: + raise UnknownVersionError(version) diff --git a/morphlib/definitions_version_tests.py b/morphlib/definitions_version_tests.py new file mode 100644 index 00000000..c8521d46 --- /dev/null +++ b/morphlib/definitions_version_tests.py @@ -0,0 +1,26 @@ +# Copyright (C) 2015 Codethink Limited +# +# 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; version 2 of the License. +# +# 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 the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# =*= License: GPL-2 =*= + + +import unittest + +import morphlib + + +class DefinitionsVersionTests(unittest.TestCase): + def test_parses_version_file(self): + self.assertEqual( + morphlib.definitions_version.parse_version_file('version: 6\n'), 6) diff --git a/morphlib/sourceresolver.py b/morphlib/sourceresolver.py index 3bfe5bfa..cbab0f7f 100644 --- a/morphlib/sourceresolver.py +++ b/morphlib/sourceresolver.py @@ -28,12 +28,12 @@ import cliapp import morphlib from morphlib.util import sanitise_morphology_path + tree_cache_size = 10000 tree_cache_filename = 'trees.cache.pickle' buildsystem_cache_size = 10000 buildsystem_cache_filename = 'detected-chunk-buildsystems.cache.pickle' -supported_versions = [3, 4, 5, 6] class PickleCacheManager(object): # pragma: no cover '''Cache manager for PyLRU that reads and writes to Pickle files. @@ -111,17 +111,6 @@ class MorphologyReferenceNotFoundError(SourceResolverError): # pragma: no cover % (filename, reference_file)) -class UnknownVersionError(SourceResolverError): # pragma: no cover - def __init__(self, version): - SourceResolverError.__init__( - self, "Definitions format version %s is not supported" % version) - - -class InvalidVersionFileError(SourceResolverError): #pragma: no cover - def __init__(self): - SourceResolverError.__init__(self, "invalid VERSION file") - - # Callers may want to give the user a special error message if we hit an # InvalidRefError in the definitions.git repo. Currently a separate exception # type seems the easiest way to do that, but adding enough detail to the @@ -294,44 +283,11 @@ class SourceResolver(object): return text - @staticmethod - def _parse_version_file(version_file): # pragma : no cover - '''Parse VERSION file and return the version of the format if: - - VERSION is a YAML file - and it's a dict - and has the key 'version' - and the type stored in the 'version' key is an int - - otherwise returns None - - ''' - - yaml_obj = yaml.safe_load(version_file) - - return (yaml_obj['version'] if yaml_obj is not None - and isinstance(yaml_obj, dict) - and 'version' in yaml_obj - and isinstance(yaml_obj['version'], int) - - else None) - def _check_version_file(self, definitions_checkout_dir): # pragma: no cover - version_file = self._get_file_contents_from_definitions( + version_text = self._get_file_contents_from_definitions( definitions_checkout_dir, 'VERSION') - if version_file == None: - raise InvalidVersionFileError() - - version = self._parse_version_file(version_file) - - if version == None: - raise InvalidVersionFileError() - - if version not in supported_versions: - raise UnknownVersionError(version) - - return version + return morphlib.definitions_version.check_version_file(version_text) def _get_morphology(self, resolved_morphologies, definitions_checkout_dir, definitions_repo, definitions_absref, morph_loader, diff --git a/morphlib/sourceresolver_tests.py b/morphlib/sourceresolver_tests.py index 6fe1dc54..5985579c 100644 --- a/morphlib/sourceresolver_tests.py +++ b/morphlib/sourceresolver_tests.py @@ -353,5 +353,3 @@ class SourceResolverTests(unittest.TestCase): morphlib.morphloader.MorphologyLoader(), 'reponame', 'sha1', 'stratum-empty.morph') - def test_parses_version_file(self): - self.assertEqual(self.sr._parse_version_file('version: 6\n'), 6) -- cgit v1.2.1