diff options
author | Lars Wirzenius <liw@liw.fi> | 2015-11-14 22:29:58 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2015-11-14 22:29:58 +0200 |
commit | 4c18166984231eb0419323487cee6f796f636ce6 (patch) | |
tree | 83068e651bca8bd2e5486922709899cbc91db490 | |
parent | 0de89dc7508baacfc0f3ca2c997975ab89eb2aec (diff) | |
download | ick-4c18166984231eb0419323487cee6f796f636ce6.tar.gz |
Refactor to put shared code in base class
-rw-r--r-- | icklib/__init__.py | 1 | ||||
-rw-r--r-- | icklib/buildinfo.py | 33 | ||||
-rw-r--r-- | icklib/buildinfo_tests.py | 7 | ||||
-rw-r--r-- | icklib/info.py | 45 | ||||
-rw-r--r-- | icklib/projectinfo.py | 27 | ||||
-rw-r--r-- | icklib/statedir.py | 5 | ||||
-rw-r--r-- | without-tests | 1 |
7 files changed, 63 insertions, 56 deletions
diff --git a/icklib/__init__.py b/icklib/__init__.py index c772f68..e88d38a 100644 --- a/icklib/__init__.py +++ b/icklib/__init__.py @@ -23,6 +23,7 @@ from .run_state import RunState from .pipeline import BuildPipeline from .project import create_projects_from_ick from .git import GitClone +from .info import InformationStore from .buildinfo import BuildInformation from .projectinfo import ProjectInformation from .statedir import StateDirectory, create_statedir_from_ick diff --git a/icklib/buildinfo.py b/icklib/buildinfo.py index b3b7f2a..0fabbee 100644 --- a/icklib/buildinfo.py +++ b/icklib/buildinfo.py @@ -18,20 +18,16 @@ import os -import yaml +import icklib -class BuildInformation(object): +class BuildInformation(icklib.InformationStore): - _attrs = ['build_number', 'commits', 'pipeline', 'status', 'duration'] + attrs = ['build_number', 'commits', 'pipeline', 'status', 'duration'] - def __init__(self): - self._dirname = None - for attr in self._attrs: - setattr(self, attr, None) - - def set_dirname(self, dirname): - self._dirname = dirname + @property + def _dirname(self): + return os.path.dirname(self._filename) def get_artifacts_directory(self): return os.path.join(self._dirname, 'artifacts') @@ -44,20 +40,3 @@ class BuildInformation(object): def get_build_log_filename(self): # pragma: no cover return os.path.join(self._dirname, 'build.log') - - @property - def _filename(self): - return os.path.join(self._dirname, 'build.yaml') - - def save(self): - obj = {} - for attr in self._attrs: - obj[attr] = getattr(self, attr) - with open(self._filename, 'w') as f: - yaml.safe_dump(obj, stream=f, default_flow_style=False, indent=4) - - def load(self): - with open(self._filename) as f: - obj = yaml.safe_load(f) - for attr in self._attrs: - setattr(self, attr, obj.get(attr)) diff --git a/icklib/buildinfo_tests.py b/icklib/buildinfo_tests.py index a9298ee..aaf1487 100644 --- a/icklib/buildinfo_tests.py +++ b/icklib/buildinfo_tests.py @@ -28,20 +28,21 @@ class BuildInformationTests(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() + self.filename = os.path.join(self.tempdir, 'build.yaml') def tearDown(self): shutil.rmtree(self.tempdir) def test_creates_artifacts_directory(self): build_info = icklib.BuildInformation() - build_info.set_dirname(self.tempdir) + build_info.set_filename(self.filename) artifacts_dir = build_info.create_artifacts_directory() self.assertEqual(os.path.dirname(artifacts_dir), self.tempdir) self.assertTrue(os.path.isdir(artifacts_dir)) def test_saves_then_loads(self): build_info = icklib.BuildInformation() - build_info.set_dirname(self.tempdir) + build_info.set_filename(self.filename) build_info.build_number = 42 build_info.commits = [ { @@ -53,7 +54,7 @@ class BuildInformationTests(unittest.TestCase): build_info.save() loaded = icklib.BuildInformation() - loaded.set_dirname(self.tempdir) + loaded.set_filename(self.filename) loaded.load() self.assertEqual(build_info.build_number, loaded.build_number) diff --git a/icklib/info.py b/icklib/info.py new file mode 100644 index 0000000..c7c63cf --- /dev/null +++ b/icklib/info.py @@ -0,0 +1,45 @@ +# Copyright 2015 Lars Wirzenius +# +# 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 3 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 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 <http://www.gnu.org/licenses/>. +# +# =*= License: GPL-3+ =*= + + +import os + +import yaml + + +class InformationStore(object): + + def __init__(self): + self._filename = None + for attr in self.attrs: + setattr(self, attr, None) + + def set_filename(self, filename): + self._filename = filename + + def save(self): + obj = {} + for attr in self.attrs: + obj[attr] = getattr(self, attr) + with open(self._filename, 'w') as f: + yaml.safe_dump(obj, stream=f, default_flow_style=False, indent=4) + + def load(self): + with open(self._filename) as f: + obj = yaml.safe_load(f) + for attr in self.attrs: + setattr(self, attr, obj.get(attr)) diff --git a/icklib/projectinfo.py b/icklib/projectinfo.py index 72014a2..c05cd03 100644 --- a/icklib/projectinfo.py +++ b/icklib/projectinfo.py @@ -18,30 +18,9 @@ import os -import yaml +import icklib -class ProjectInformation(object): +class ProjectInformation(icklib.InformationStore): - _attrs = ['seen_tags', 'next_build_number', 'built_commits'] - - def __init__(self): - self._filename = None - for attr in self._attrs: - setattr(self, attr, None) - - def set_filename(self, filename): - self._filename = filename - - def save(self): - obj = {} - for attr in self._attrs: - obj[attr] = getattr(self, attr) - with open(self._filename, 'w') as f: - yaml.safe_dump(obj, stream=f, default_flow_style=False, indent=4) - - def load(self): - with open(self._filename) as f: - obj = yaml.safe_load(f) - for attr in self._attrs: - setattr(self, attr, obj.get(attr)) + attrs = ['seen_tags', 'next_build_number', 'built_commits'] diff --git a/icklib/statedir.py b/icklib/statedir.py index 21e9c3d..01879ba 100644 --- a/icklib/statedir.py +++ b/icklib/statedir.py @@ -45,7 +45,7 @@ class StateDirectory(object): self.dirname, project_name, 'builds', '*', 'build.yaml') for info_filename in glob.glob(pattern): build_info = icklib.BuildInformation() - build_info.set_dirname(os.path.dirname(info_filename)) + build_info.set_filename(info_filename) build_info.load() result.append(build_info) return sorted(result, key=lambda bi: bi.build_number) @@ -53,11 +53,12 @@ class StateDirectory(object): def create_new_build(self, project_name, new_build_number): build_dir = self._get_dirname_for_build_info( project_name, new_build_number) + build_filename = os.path.join(build_dir, 'build.yaml') os.makedirs(build_dir) build_info = icklib.BuildInformation() build_info.build_number = new_build_number - build_info.set_dirname(build_dir) + build_info.set_filename(build_filename) build_info.save() return build_info diff --git a/without-tests b/without-tests index 37a4f05..1adfdbc 100644 --- a/without-tests +++ b/without-tests @@ -1,3 +1,4 @@ icklib/__init__.py icklib/run_state.py icklib/project.py +icklib/info.py |