2 # Copyright (c) 2016 Google, Inc
4 # SPDX-License-Identifier: GPL-2.0+
15 preserve_outdir = False
17 def PrepareOutputDir(dirname, preserve=False):
18 """Select an output directory, ensuring it exists.
20 This either creates a temporary directory or checks that the one supplied
21 by the user is valid. For a temporary directory, it makes a note to
22 remove it later if required.
25 dirname: a string, name of the output directory to use to store
26 intermediate and output files. If is None - create a temporary
28 preserve: a Boolean. If outdir above is None and preserve is False, the
29 created temporary directory will be destroyed on exit.
32 OSError: If it cannot create the output directory.
34 global outdir, preserve_outdir
36 preserve_outdir = dirname or preserve
39 if not os.path.isdir(outdir):
42 except OSError as err:
43 raise CmdError("Cannot make output directory '%s': '%s'" %
44 (outdir, err.strerror))
45 tout.Debug("Using output directory '%s'" % outdir)
47 outdir = tempfile.mkdtemp(prefix='binman.')
48 tout.Debug("Using temporary directory '%s'" % outdir)
50 def _RemoveOutputDir():
54 tout.Debug("Deleted temporary directory '%s'" % outdir)
57 def FinaliseOutputDir():
58 global outdir, preserve_outdir
60 """Tidy up: delete output directory if temporary and not preserved."""
61 if outdir and not preserve_outdir:
64 def GetOutputFilename(fname):
65 """Return a filename within the output directory.
68 fname: Filename to use for new file
71 The full path of the filename, within the output directory
73 return os.path.join(outdir, fname)
75 def _FinaliseForTest():
76 """Remove the output directory (for use by tests)"""
82 def SetInputDirs(dirname):
83 """Add a list of input directories, where input files are kept.
86 dirname: a list of paths to input directories to use for obtaining
87 files needed by binman to place in the image.
92 tout.Debug("Using input directories %s" % indir)
94 def GetInputFilename(fname):
95 """Return a filename for use as input.
98 fname: Filename to use for new file
101 The full path of the filename, within the input directory
105 for dirname in indir:
106 pathname = os.path.join(dirname, fname)
107 if os.path.exists(pathname):
110 raise ValueError("Filename '%s' not found in input path (%s)" %
111 (fname, ','.join(indir)))
113 def Align(pos, align):
116 pos = (pos + mask) & ~mask
119 def NotPowerOfTwo(num):
120 return num and (num & (num - 1))