scriptutils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Script utility functions
  2. #
  3. # Copyright (C) 2014 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. import sys
  18. import os
  19. import logging
  20. import glob
  21. import argparse
  22. def logger_create(name):
  23. logger = logging.getLogger(name)
  24. loggerhandler = logging.StreamHandler()
  25. loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
  26. logger.addHandler(loggerhandler)
  27. logger.setLevel(logging.INFO)
  28. return logger
  29. def logger_setup_color(logger, color='auto'):
  30. from bb.msg import BBLogFormatter
  31. console = logging.StreamHandler(sys.stdout)
  32. formatter = BBLogFormatter("%(levelname)s: %(message)s")
  33. console.setFormatter(formatter)
  34. logger.handlers = [console]
  35. if color == 'always' or (color=='auto' and console.stream.isatty()):
  36. formatter.enable_color()
  37. def load_plugins(logger, plugins, pluginpath):
  38. import imp
  39. def load_plugin(name):
  40. logger.debug('Loading plugin %s' % name)
  41. fp, pathname, description = imp.find_module(name, [pluginpath])
  42. try:
  43. return imp.load_module(name, fp, pathname, description)
  44. finally:
  45. if fp:
  46. fp.close()
  47. logger.debug('Loading plugins from %s...' % pluginpath)
  48. for fn in glob.glob(os.path.join(pluginpath, '*.py')):
  49. name = os.path.splitext(os.path.basename(fn))[0]
  50. if name != '__init__':
  51. plugin = load_plugin(name)
  52. if hasattr(plugin, 'plugin_init'):
  53. plugin.plugin_init(plugins)
  54. plugins.append(plugin)
  55. def git_convert_standalone_clone(repodir):
  56. """If specified directory is a git repository, ensure it's a standalone clone"""
  57. import bb.process
  58. if os.path.exists(os.path.join(repodir, '.git')):
  59. alternatesfile = os.path.join(repodir, '.git', 'objects', 'info', 'alternates')
  60. if os.path.exists(alternatesfile):
  61. # This will have been cloned with -s, so we need to convert it so none
  62. # of the contents is shared
  63. bb.process.run('git repack -a', cwd=repodir)
  64. os.remove(alternatesfile)
  65. def fetch_uri(d, uri, destdir, srcrev=None):
  66. """Fetch a URI to a local directory"""
  67. import bb.data
  68. bb.utils.mkdirhier(destdir)
  69. localdata = bb.data.createCopy(d)
  70. localdata.setVar('BB_STRICT_CHECKSUM', '')
  71. localdata.setVar('SRCREV', srcrev)
  72. ret = (None, None)
  73. olddir = os.getcwd()
  74. try:
  75. fetcher = bb.fetch2.Fetch([uri], localdata)
  76. for u in fetcher.ud:
  77. ud = fetcher.ud[u]
  78. ud.ignore_checksums = True
  79. fetcher.download()
  80. fetcher.unpack(destdir)
  81. for u in fetcher.ud:
  82. ud = fetcher.ud[u]
  83. if ud.method.recommends_checksum(ud):
  84. md5value = bb.utils.md5_file(ud.localpath)
  85. sha256value = bb.utils.sha256_file(ud.localpath)
  86. ret = (md5value, sha256value)
  87. finally:
  88. os.chdir(olddir)
  89. return ret