devtool 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #!/usr/bin/env python3
  2. # OpenEmbedded Development tool
  3. #
  4. # Copyright (C) 2014-2015 Intel Corporation
  5. #
  6. # SPDX-License-Identifier: GPL-2.0-only
  7. #
  8. import dataclasses
  9. import sys
  10. import os
  11. import argparse
  12. import glob
  13. import re
  14. import configparser
  15. import logging
  16. # This can be removed once our minimum is Python 3.9: https://docs.python.org/3/whatsnew/3.9.html#type-hinting-generics-in-standard-collections
  17. from typing import List
  18. scripts_path = os.path.dirname(os.path.realpath(__file__))
  19. lib_path = scripts_path + '/lib'
  20. sys.path = sys.path + [lib_path]
  21. from devtool import DevtoolError, setup_tinfoil
  22. import scriptutils
  23. import argparse_oe
  24. logger = scriptutils.logger_create('devtool')
  25. class ConfigHandler:
  26. basepath = None
  27. config_file = ''
  28. config_obj = None
  29. init_path = ''
  30. workspace_path = ''
  31. def __init__(self, basepath, filename):
  32. self.basepath = basepath
  33. self.config_file = filename
  34. self.config_obj = configparser.ConfigParser()
  35. def get(self, section, option, default=None):
  36. try:
  37. ret = self.config_obj.get(section, option)
  38. except (configparser.NoOptionError, configparser.NoSectionError):
  39. if default is not None:
  40. ret = default
  41. else:
  42. raise
  43. return ret
  44. def read(self):
  45. if os.path.exists(self.config_file):
  46. self.config_obj.read(self.config_file)
  47. if self.config_obj.has_option('General', 'init_path'):
  48. pth = self.get('General', 'init_path')
  49. self.init_path = os.path.join(self.basepath, pth)
  50. if not os.path.exists(self.init_path):
  51. logger.error('init_path %s specified in config file cannot be found' % pth)
  52. return False
  53. else:
  54. self.config_obj.add_section('General')
  55. self.workspace_path = self.get('General', 'workspace_path', os.path.join(self.basepath, 'workspace'))
  56. return True
  57. def write(self):
  58. logger.debug('writing to config file %s' % self.config_file)
  59. self.config_obj.set('General', 'workspace_path', self.workspace_path)
  60. with open(self.config_file, 'w') as f:
  61. self.config_obj.write(f)
  62. def set(self, section, option, value):
  63. if not self.config_obj.has_section(section):
  64. self.config_obj.add_section(section)
  65. self.config_obj.set(section, option, value)
  66. @dataclasses.dataclass
  67. class Context:
  68. fixed_setup: bool
  69. config: ConfigHandler
  70. pluginpaths: List[str]
  71. def read_workspace(basepath, context):
  72. workspace = {}
  73. if not os.path.exists(os.path.join(context.config.workspace_path, 'conf', 'layer.conf')):
  74. if context.fixed_setup:
  75. logger.error("workspace layer not set up")
  76. sys.exit(1)
  77. else:
  78. logger.info('Creating workspace layer in %s' % context.config.workspace_path)
  79. _create_workspace(context.config.workspace_path, basepath)
  80. if not context.fixed_setup:
  81. _enable_workspace_layer(context.config.workspace_path, context.config, basepath)
  82. logger.debug('Reading workspace in %s' % context.config.workspace_path)
  83. externalsrc_re = re.compile(r'^EXTERNALSRC(:pn-([^ =]+))? *= *"([^"]*)"$')
  84. for fn in glob.glob(os.path.join(context.config.workspace_path, 'appends', '*.bbappend')):
  85. with open(fn, 'r') as f:
  86. pnvalues = {}
  87. pn = None
  88. for line in f:
  89. res = externalsrc_re.match(line.rstrip())
  90. if res:
  91. recipepn = os.path.splitext(os.path.basename(fn))[0].split('_')[0]
  92. pn = res.group(2) or recipepn
  93. # Find the recipe file within the workspace, if any
  94. bbfile = os.path.basename(fn).replace('.bbappend', '.bb').replace('%', '*')
  95. recipefile = glob.glob(os.path.join(context.config.workspace_path,
  96. 'recipes',
  97. recipepn,
  98. bbfile))
  99. if recipefile:
  100. recipefile = recipefile[0]
  101. pnvalues['srctree'] = res.group(3)
  102. pnvalues['bbappend'] = fn
  103. pnvalues['recipefile'] = recipefile
  104. elif line.startswith('# srctreebase: '):
  105. pnvalues['srctreebase'] = line.split(':', 1)[1].strip()
  106. if pnvalues:
  107. if not pn:
  108. raise DevtoolError("Found *.bbappend in %s, but could not determine EXTERNALSRC:pn-*. "
  109. "Maybe still using old syntax?" % context.config.workspace_path)
  110. if not pnvalues.get('srctreebase', None):
  111. pnvalues['srctreebase'] = pnvalues['srctree']
  112. logger.debug('Found recipe %s' % pnvalues)
  113. workspace[pn] = pnvalues
  114. return workspace
  115. def create_workspace(args, config, basepath, _workspace):
  116. if args.layerpath:
  117. workspacedir = os.path.abspath(args.layerpath)
  118. else:
  119. workspacedir = os.path.abspath(os.path.join(basepath, 'workspace'))
  120. layerseries = None
  121. if args.layerseries:
  122. layerseries = args.layerseries
  123. _create_workspace(workspacedir, basepath, layerseries)
  124. if not args.create_only:
  125. _enable_workspace_layer(workspacedir, config, basepath)
  126. def _create_workspace(workspacedir, basepath, layerseries=None):
  127. import bb.utils
  128. confdir = os.path.join(workspacedir, 'conf')
  129. if os.path.exists(os.path.join(confdir, 'layer.conf')):
  130. logger.info('Specified workspace already set up, leaving as-is')
  131. else:
  132. if not layerseries:
  133. tinfoil = setup_tinfoil(config_only=True, basepath=basepath)
  134. try:
  135. layerseries = tinfoil.config_data.getVar('LAYERSERIES_CORENAMES')
  136. finally:
  137. tinfoil.shutdown()
  138. # Add a config file
  139. bb.utils.mkdirhier(confdir)
  140. with open(os.path.join(confdir, 'layer.conf'), 'w') as f:
  141. f.write('# ### workspace layer auto-generated by devtool ###\n')
  142. f.write('BBPATH =. "$' + '{LAYERDIR}:"\n')
  143. f.write('BBFILES += "$' + '{LAYERDIR}/recipes/*/*.bb \\\n')
  144. f.write(' $' + '{LAYERDIR}/appends/*.bbappend"\n')
  145. f.write('BBFILE_COLLECTIONS += "workspacelayer"\n')
  146. f.write('BBFILE_PATTERN_workspacelayer = "^$' + '{LAYERDIR}/"\n')
  147. f.write('BBFILE_PATTERN_IGNORE_EMPTY_workspacelayer = "1"\n')
  148. f.write('BBFILE_PRIORITY_workspacelayer = "99"\n')
  149. f.write('LAYERSERIES_COMPAT_workspacelayer = "%s"\n' % layerseries)
  150. # Add a README file
  151. with open(os.path.join(workspacedir, 'README'), 'w') as f:
  152. f.write('This layer was created by the OpenEmbedded devtool utility in order to\n')
  153. f.write('contain recipes and bbappends that are currently being worked on. The idea\n')
  154. f.write('is that the contents is temporary - once you have finished working on a\n')
  155. f.write('recipe you use the appropriate method to move the files you have been\n')
  156. f.write('working on to a proper layer. In most instances you should use the\n')
  157. f.write('devtool utility to manage files within it rather than modifying files\n')
  158. f.write('directly (although recipes added with "devtool add" will often need\n')
  159. f.write('direct modification.)\n')
  160. f.write('\nIf you no longer need to use devtool or the workspace layer\'s contents\n')
  161. f.write('you can remove the path to this workspace layer from your conf/bblayers.conf\n')
  162. f.write('file (and then delete the layer, if you wish).\n')
  163. f.write('\nNote that by default, if devtool fetches and unpacks source code, it\n')
  164. f.write('will place it in a subdirectory of a "sources" subdirectory of the\n')
  165. f.write('layer. If you prefer it to be elsewhere you can specify the source\n')
  166. f.write('tree path on the command line.\n')
  167. def _enable_workspace_layer(workspacedir, config, basepath):
  168. """Ensure the workspace layer is in bblayers.conf"""
  169. import bb.utils
  170. bblayers_conf = os.path.join(basepath, 'conf', 'bblayers.conf')
  171. if not os.path.exists(bblayers_conf):
  172. logger.error('Unable to find bblayers.conf')
  173. return
  174. if os.path.abspath(workspacedir) != os.path.abspath(config.workspace_path):
  175. removedir = config.workspace_path
  176. else:
  177. removedir = None
  178. _, added = bb.utils.edit_bblayers_conf(bblayers_conf, workspacedir, removedir)
  179. if added:
  180. logger.info('Enabling workspace layer in bblayers.conf')
  181. if config.workspace_path != workspacedir:
  182. # Update our config to point to the new location
  183. config.workspace_path = workspacedir
  184. config.write()
  185. def main():
  186. if sys.getfilesystemencoding() != "utf-8":
  187. sys.exit("Please use a locale setting which supports utf-8.\nPython can't change the filesystem locale after loading so we need a utf-8 when python starts or things won't work.")
  188. # Default basepath
  189. basepath = os.path.dirname(os.path.abspath(__file__))
  190. parser = argparse_oe.ArgumentParser(description="OpenEmbedded development tool",
  191. add_help=False,
  192. epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
  193. parser.add_argument('--basepath', help='Base directory of SDK / build directory')
  194. parser.add_argument('--bbpath', help='Explicitly specify the BBPATH, rather than getting it from the metadata')
  195. parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
  196. parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
  197. parser.add_argument('--color', choices=['auto', 'always', 'never'], default='auto', help='Colorize output (where %(metavar)s is %(choices)s)', metavar='COLOR')
  198. global_args, unparsed_args = parser.parse_known_args()
  199. # Help is added here rather than via add_help=True, as we don't want it to
  200. # be handled by parse_known_args()
  201. parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
  202. help='show this help message and exit')
  203. if global_args.debug:
  204. logger.setLevel(logging.DEBUG)
  205. elif global_args.quiet:
  206. logger.setLevel(logging.ERROR)
  207. is_fixed_setup = False
  208. if global_args.basepath:
  209. # Override
  210. basepath = global_args.basepath
  211. if os.path.exists(os.path.join(basepath, '.devtoolbase')):
  212. is_fixed_setup = True
  213. else:
  214. pth = basepath
  215. while pth != '' and pth != os.sep:
  216. if os.path.exists(os.path.join(pth, '.devtoolbase')):
  217. is_fixed_setup = True
  218. basepath = pth
  219. break
  220. pth = os.path.dirname(pth)
  221. if not is_fixed_setup:
  222. basepath = os.environ.get('BUILDDIR')
  223. if not basepath:
  224. logger.error("This script can only be run after initialising the build environment (e.g. by using oe-init-build-env)")
  225. sys.exit(1)
  226. logger.debug('Using basepath %s' % basepath)
  227. config = ConfigHandler(basepath, os.path.join(basepath, 'conf', 'devtool.conf'))
  228. if not config.read():
  229. return -1
  230. bitbake_subdir = config.get('General', 'bitbake_subdir', '')
  231. if bitbake_subdir:
  232. # Normally set for use within the SDK
  233. logger.debug('Using bitbake subdir %s' % bitbake_subdir)
  234. sys.path.insert(0, os.path.join(basepath, bitbake_subdir, 'lib'))
  235. core_meta_subdir = config.get('General', 'core_meta_subdir')
  236. sys.path.insert(0, os.path.join(basepath, core_meta_subdir, 'lib'))
  237. else:
  238. # Standard location
  239. import scriptpath
  240. bitbakepath = scriptpath.add_bitbake_lib_path()
  241. if not bitbakepath:
  242. logger.error("Unable to find bitbake by searching parent directory of this script or PATH")
  243. sys.exit(1)
  244. logger.debug('Using standard bitbake path %s' % bitbakepath)
  245. scriptpath.add_oe_lib_path()
  246. scriptutils.logger_setup_color(logger, global_args.color)
  247. if global_args.bbpath is None:
  248. import bb
  249. try:
  250. tinfoil = setup_tinfoil(config_only=True, basepath=basepath)
  251. try:
  252. global_args.bbpath = tinfoil.config_data.getVar('BBPATH')
  253. finally:
  254. tinfoil.shutdown()
  255. except bb.BBHandledException:
  256. return 2
  257. # Search BBPATH first to allow layers to override plugins in scripts_path
  258. pluginpaths = [os.path.join(path, 'lib', 'devtool') for path in global_args.bbpath.split(':') + [scripts_path]]
  259. context = Context(fixed_setup=is_fixed_setup, config=config, pluginpaths=pluginpaths)
  260. plugins = []
  261. for pluginpath in pluginpaths:
  262. scriptutils.load_plugins(logger, plugins, pluginpath)
  263. subparsers = parser.add_subparsers(dest="subparser_name", title='subcommands', metavar='<subcommand>')
  264. subparsers.required = True
  265. subparsers.add_subparser_group('sdk', 'SDK maintenance', -2)
  266. subparsers.add_subparser_group('advanced', 'Advanced', -1)
  267. subparsers.add_subparser_group('starting', 'Beginning work on a recipe', 100)
  268. subparsers.add_subparser_group('info', 'Getting information')
  269. subparsers.add_subparser_group('working', 'Working on a recipe in the workspace')
  270. subparsers.add_subparser_group('testbuild', 'Testing changes on target')
  271. if not context.fixed_setup:
  272. parser_create_workspace = subparsers.add_parser('create-workspace',
  273. help='Set up workspace in an alternative location',
  274. description='Sets up a new workspace. NOTE: other devtool subcommands will create a workspace automatically as needed, so you only need to use %(prog)s if you want to specify where the workspace should be located.',
  275. group='advanced')
  276. parser_create_workspace.add_argument('layerpath', nargs='?', help='Path in which the workspace layer should be created')
  277. parser_create_workspace.add_argument('--layerseries', help='Layer series the workspace should be set to be compatible with')
  278. parser_create_workspace.add_argument('--create-only', action="store_true", help='Only create the workspace layer, do not alter configuration')
  279. parser_create_workspace.set_defaults(func=create_workspace, no_workspace=True)
  280. for plugin in plugins:
  281. if hasattr(plugin, 'register_commands'):
  282. plugin.register_commands(subparsers, context)
  283. args = parser.parse_args(unparsed_args, namespace=global_args)
  284. try:
  285. workspace = {}
  286. if not getattr(args, 'no_workspace', False):
  287. workspace = read_workspace(basepath, context)
  288. ret = args.func(args, config, basepath, workspace)
  289. except DevtoolError as err:
  290. if str(err):
  291. logger.error(str(err))
  292. ret = err.exitcode
  293. except argparse_oe.ArgumentUsageError as ae:
  294. parser.error_subcommand(ae.message, ae.subcommand)
  295. ret = 2
  296. return ret
  297. if __name__ == "__main__":
  298. try:
  299. ret = main()
  300. except Exception:
  301. ret = 1
  302. import traceback
  303. traceback.print_exc()
  304. sys.exit(ret)