1 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (c) 2016 Google, Inc
14 preserve_outdir = False
16 def PrepareOutputDir(dirname, preserve=False):
17 """Select an output directory, ensuring it exists.
19 This either creates a temporary directory or checks that the one supplied
20 by the user is valid. For a temporary directory, it makes a note to
21 remove it later if required.
24 dirname: a string, name of the output directory to use to store
25 intermediate and output files. If is None - create a temporary
27 preserve: a Boolean. If outdir above is None and preserve is False, the
28 created temporary directory will be destroyed on exit.
31 OSError: If it cannot create the output directory.
33 global outdir, preserve_outdir
35 preserve_outdir = dirname or preserve
38 if not os.path.isdir(outdir):
41 except OSError as err:
42 raise CmdError("Cannot make output directory '%s': '%s'" %
43 (outdir, err.strerror))
44 tout.Debug("Using output directory '%s'" % outdir)
46 outdir = tempfile.mkdtemp(prefix='binman.')
47 tout.Debug("Using temporary directory '%s'" % outdir)
49 def _RemoveOutputDir():
53 tout.Debug("Deleted temporary directory '%s'" % outdir)
56 def FinaliseOutputDir():
57 global outdir, preserve_outdir
59 """Tidy up: delete output directory if temporary and not preserved."""
60 if outdir and not preserve_outdir:
63 def GetOutputFilename(fname):
64 """Return a filename within the output directory.
67 fname: Filename to use for new file
70 The full path of the filename, within the output directory
72 return os.path.join(outdir, fname)
74 def _FinaliseForTest():
75 """Remove the output directory (for use by tests)"""
81 def SetInputDirs(dirname):
82 """Add a list of input directories, where input files are kept.
85 dirname: a list of paths to input directories to use for obtaining
86 files needed by binman to place in the image.
91 tout.Debug("Using input directories %s" % indir)
93 def GetInputFilename(fname):
94 """Return a filename for use as input.
97 fname: Filename to use for new file
100 The full path of the filename, within the input directory
104 for dirname in indir:
105 pathname = os.path.join(dirname, fname)
106 if os.path.exists(pathname):
109 raise ValueError("Filename '%s' not found in input path (%s)" %
110 (fname, ','.join(indir)))
112 def Align(pos, align):
115 pos = (pos + mask) & ~mask
118 def NotPowerOfTwo(num):
119 return num and (num & (num - 1))