Skip to content

Commit

Permalink
cosalib/build.py: Add temp work directory functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Milner <[email protected]>
  • Loading branch information
ashcrow authored and Ben Howard committed Jun 26, 2019
1 parent 73ea91f commit be968a3
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/cosalib/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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 """
Expand Down

0 comments on commit be968a3

Please sign in to comment.