scriptutils.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import subprocess
  23. def logger_create(name):
  24. logger = logging.getLogger(name)
  25. loggerhandler = logging.StreamHandler()
  26. loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
  27. logger.addHandler(loggerhandler)
  28. logger.setLevel(logging.INFO)
  29. return logger
  30. def logger_setup_color(logger, color='auto'):
  31. from bb.msg import BBLogFormatter
  32. console = logging.StreamHandler(sys.stdout)
  33. formatter = BBLogFormatter("%(levelname)s: %(message)s")
  34. console.setFormatter(formatter)
  35. logger.handlers = [console]
  36. if color == 'always' or (color=='auto' and console.stream.isatty()):
  37. formatter.enable_color()
  38. def load_plugins(logger, plugins, pluginpath):
  39. import imp
  40. def load_plugin(name):
  41. logger.debug('Loading plugin %s' % name)
  42. fp, pathname, description = imp.find_module(name, [pluginpath])
  43. try:
  44. return imp.load_module(name, fp, pathname, description)
  45. finally:
  46. if fp:
  47. fp.close()
  48. logger.debug('Loading plugins from %s...' % pluginpath)
  49. for fn in glob.glob(os.path.join(pluginpath, '*.py')):
  50. name = os.path.splitext(os.path.basename(fn))[0]
  51. if name != '__init__':
  52. plugin = load_plugin(name)
  53. if hasattr(plugin, 'plugin_init'):
  54. plugin.plugin_init(plugins)
  55. plugins.append(plugin)
  56. def git_convert_standalone_clone(repodir):
  57. """If specified directory is a git repository, ensure it's a standalone clone"""
  58. import bb.process
  59. if os.path.exists(os.path.join(repodir, '.git')):
  60. alternatesfile = os.path.join(repodir, '.git', 'objects', 'info', 'alternates')
  61. if os.path.exists(alternatesfile):
  62. # This will have been cloned with -s, so we need to convert it so none
  63. # of the contents is shared
  64. bb.process.run('git repack -a', cwd=repodir)
  65. os.remove(alternatesfile)
  66. def fetch_uri(d, uri, destdir, srcrev=None):
  67. """Fetch a URI to a local directory"""
  68. import bb.data
  69. bb.utils.mkdirhier(destdir)
  70. localdata = bb.data.createCopy(d)
  71. localdata.setVar('BB_STRICT_CHECKSUM', '')
  72. localdata.setVar('SRCREV', srcrev)
  73. ret = (None, None)
  74. olddir = os.getcwd()
  75. try:
  76. fetcher = bb.fetch2.Fetch([uri], localdata)
  77. for u in fetcher.ud:
  78. ud = fetcher.ud[u]
  79. ud.ignore_checksums = True
  80. fetcher.download()
  81. for u in fetcher.ud:
  82. ud = fetcher.ud[u]
  83. if ud.localpath.rstrip(os.sep) == localdata.getVar('DL_DIR', True).rstrip(os.sep):
  84. raise Exception('Local path is download directory - please check that the URI "%s" is correct' % uri)
  85. fetcher.unpack(destdir)
  86. for u in fetcher.ud:
  87. ud = fetcher.ud[u]
  88. if ud.method.recommends_checksum(ud):
  89. md5value = bb.utils.md5_file(ud.localpath)
  90. sha256value = bb.utils.sha256_file(ud.localpath)
  91. ret = (md5value, sha256value)
  92. finally:
  93. os.chdir(olddir)
  94. return ret
  95. def run_editor(fn):
  96. if isinstance(fn, basestring):
  97. params = '"%s"' % fn
  98. else:
  99. params = ''
  100. for fnitem in fn:
  101. params += ' "%s"' % fnitem
  102. editor = os.getenv('VISUAL', os.getenv('EDITOR', 'vi'))
  103. try:
  104. return subprocess.check_call('%s %s' % (editor, params), shell=True)
  105. except OSError as exc:
  106. logger.error("Execution of editor '%s' failed: %s", editor, exc)
  107. return 1