tools.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # SPDX-License-Identifier: GPL-2.0+
  2. #
  3. # Copyright (c) 2016 Google, Inc
  4. #
  5. import os
  6. import shutil
  7. import tempfile
  8. import tout
  9. outdir = None
  10. indirs = None
  11. preserve_outdir = False
  12. def PrepareOutputDir(dirname, preserve=False):
  13. """Select an output directory, ensuring it exists.
  14. This either creates a temporary directory or checks that the one supplied
  15. by the user is valid. For a temporary directory, it makes a note to
  16. remove it later if required.
  17. Args:
  18. dirname: a string, name of the output directory to use to store
  19. intermediate and output files. If is None - create a temporary
  20. directory.
  21. preserve: a Boolean. If outdir above is None and preserve is False, the
  22. created temporary directory will be destroyed on exit.
  23. Raises:
  24. OSError: If it cannot create the output directory.
  25. """
  26. global outdir, preserve_outdir
  27. preserve_outdir = dirname or preserve
  28. if dirname:
  29. outdir = dirname
  30. if not os.path.isdir(outdir):
  31. try:
  32. os.makedirs(outdir)
  33. except OSError as err:
  34. raise CmdError("Cannot make output directory '%s': '%s'" %
  35. (outdir, err.strerror))
  36. tout.Debug("Using output directory '%s'" % outdir)
  37. else:
  38. outdir = tempfile.mkdtemp(prefix='binman.')
  39. tout.Debug("Using temporary directory '%s'" % outdir)
  40. def _RemoveOutputDir():
  41. global outdir
  42. shutil.rmtree(outdir)
  43. tout.Debug("Deleted temporary directory '%s'" % outdir)
  44. outdir = None
  45. def FinaliseOutputDir():
  46. global outdir, preserve_outdir
  47. """Tidy up: delete output directory if temporary and not preserved."""
  48. if outdir and not preserve_outdir:
  49. _RemoveOutputDir()
  50. def GetOutputFilename(fname):
  51. """Return a filename within the output directory.
  52. Args:
  53. fname: Filename to use for new file
  54. Returns:
  55. The full path of the filename, within the output directory
  56. """
  57. return os.path.join(outdir, fname)
  58. def _FinaliseForTest():
  59. """Remove the output directory (for use by tests)"""
  60. global outdir
  61. if outdir:
  62. _RemoveOutputDir()
  63. def SetInputDirs(dirname):
  64. """Add a list of input directories, where input files are kept.
  65. Args:
  66. dirname: a list of paths to input directories to use for obtaining
  67. files needed by binman to place in the image.
  68. """
  69. global indir
  70. indir = dirname
  71. tout.Debug("Using input directories %s" % indir)
  72. def GetInputFilename(fname):
  73. """Return a filename for use as input.
  74. Args:
  75. fname: Filename to use for new file
  76. Returns:
  77. The full path of the filename, within the input directory
  78. """
  79. if not indir:
  80. return fname
  81. for dirname in indir:
  82. pathname = os.path.join(dirname, fname)
  83. if os.path.exists(pathname):
  84. return pathname
  85. raise ValueError("Filename '%s' not found in input path (%s)" %
  86. (fname, ','.join(indir)))
  87. def Align(pos, align):
  88. if align:
  89. mask = align - 1
  90. pos = (pos + mask) & ~mask
  91. return pos
  92. def NotPowerOfTwo(num):
  93. return num and (num & (num - 1))