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