tools.py 3.2 KB

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