From be968a3a8178a6bd27f64ca9e7d23e6b44485ac1 Mon Sep 17 00:00:00 2001 From: Steve Milner Date: Tue, 25 Jun 2019 15:08:44 -0400 Subject: [PATCH] cosalib/build.py: Add temp work directory functionality Signed-off-by: Steve Milner --- src/cosalib/build.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/cosalib/build.py b/src/cosalib/build.py index eea5d0a4ca..baf0966430 100644 --- a/src/cosalib/build.py +++ b/src/cosalib/build.py @@ -7,6 +7,7 @@ import os.path import platform import tempfile +import shutil # COSA_INPATH is the _in container_ path for the image build source COSA_INPATH = "/cosa" @@ -63,7 +64,7 @@ class _Build: - _build_artifacts(*args, **kwargs) """ - def __init__(self, build_dir, build="latest"): + def __init__(self, build_dir, build="latest", workdir=None): """ init loads the builds.json which lists the builds, loads the relevant meta-data from JSON and finally, locates the build artifacts. @@ -72,10 +73,14 @@ def __init__(self, build_dir, build="latest"): :type build_dir: str :param build: build id or "latest" to parse :type build: str + :param workdir: Temporary directory to ensure exists and is clean + :type workdir: None or str :raises: BuildError If the build meta-data fails to parse, then a generic exception is raised. + + If workdir is None then no temporary work directory is created. """ log.info('Evaluating builds.json') builds = load_json('%s/builds.json' % build_dir) @@ -95,6 +100,8 @@ def __init__(self, build_dir, build="latest"): "meta": None } self._found_files = {} + self._workdir = workdir + self._create_work_dir() # Check to make sure that the build and it's meta-data can be parsed. emsg = "was not read in properly or is not defined" @@ -111,6 +118,32 @@ def __init__(self, build_dir, build="latest"): self.summary, self.build_name.upper(), self.arch, self.build_id) + def _create_work_dir(self): + """ + Ensures the temp work directory is created and clean. + """ + # Setup the work directory + if self._workdir is not None: + if os.path.isdir(self._workdir): + shutil.rmtree(self._workdir) + os.mkdir(self._workdir) + log.info( + 'Created temporary work directory at {}'.format(self.workdir)) + + def clean(self): + """ + Removes the temporary work directory. + """ + if self._workdir is not None: + shutil.rmtree(self._workdir) + log.info( + 'Removed temporary work directory at {}'.format(self.workdir)) + + @property + def workdir(self): + """ get the temporary work directory """ + return self._workdir + @property def arch(self): """ get the build arch """