blob: e79a195b1ff488a12690b4551cc18c640807cb76 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
18import sys
19import os
20import logging
21import glob
22
23def 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
31def logger_setup_color(logger, color='auto'):
32 from bb.msg import BBLogFormatter
33 console = logging.StreamHandler(sys.stdout)
34 formatter = BBLogFormatter("%(levelname)s: %(message)s")
35 console.setFormatter(formatter)
36 logger.handlers = [console]
37 if color == 'always' or (color=='auto' and console.stream.isatty()):
38 formatter.enable_color()
39
40
41def load_plugins(logger, plugins, pluginpath):
42 import imp
43
44 def load_plugin(name):
45 logger.debug('Loading plugin %s' % name)
46 fp, pathname, description = imp.find_module(name, [pluginpath])
47 try:
48 return imp.load_module(name, fp, pathname, description)
49 finally:
50 if fp:
51 fp.close()
52
53 logger.debug('Loading plugins from %s...' % pluginpath)
54 for fn in glob.glob(os.path.join(pluginpath, '*.py')):
55 name = os.path.splitext(os.path.basename(fn))[0]
56 if name != '__init__':
57 plugin = load_plugin(name)
58 if hasattr(plugin, 'plugin_init'):
59 plugin.plugin_init(plugins)
60 plugins.append(plugin)
61
62def git_convert_standalone_clone(repodir):
63 """If specified directory is a git repository, ensure it's a standalone clone"""
64 import bb.process
65 if os.path.exists(os.path.join(repodir, '.git')):
66 alternatesfile = os.path.join(repodir, '.git', 'objects', 'info', 'alternates')
67 if os.path.exists(alternatesfile):
68 # This will have been cloned with -s, so we need to convert it so none
69 # of the contents is shared
70 bb.process.run('git repack -a', cwd=repodir)
71 os.remove(alternatesfile)
72
73def fetch_uri(d, uri, destdir, srcrev=None):
74 """Fetch a URI to a local directory"""
75 import bb.data
76 bb.utils.mkdirhier(destdir)
77 localdata = bb.data.createCopy(d)
78 localdata.setVar('BB_STRICT_CHECKSUM', '')
79 localdata.setVar('SRCREV', srcrev)
80 ret = (None, None)
81 olddir = os.getcwd()
82 try:
83 fetcher = bb.fetch2.Fetch([uri], localdata)
84 for u in fetcher.ud:
85 ud = fetcher.ud[u]
86 ud.ignore_checksums = True
87 fetcher.download()
Patrick Williamsf1e5d692016-03-30 15:21:19 -050088 for u in fetcher.ud:
89 ud = fetcher.ud[u]
90 if ud.localpath.rstrip(os.sep) == localdata.getVar('DL_DIR', True).rstrip(os.sep):
91 raise Exception('Local path is download directory - please check that the URI "%s" is correct' % uri)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 fetcher.unpack(destdir)
93 for u in fetcher.ud:
94 ud = fetcher.ud[u]
95 if ud.method.recommends_checksum(ud):
96 md5value = bb.utils.md5_file(ud.localpath)
97 sha256value = bb.utils.sha256_file(ud.localpath)
98 ret = (md5value, sha256value)
99 finally:
100 os.chdir(olddir)
101 return ret
102