newappend.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Recipe creation tool - newappend plugin
  2. #
  3. # This sub-command creates a bbappend for the specified target and prints the
  4. # path to the bbappend.
  5. #
  6. # Example: recipetool newappend meta-mylayer busybox
  7. #
  8. # Copyright (C) 2015 Christopher Larson <kergoth@gmail.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License version 2 as
  12. # published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. import argparse
  23. import errno
  24. import logging
  25. import os
  26. import re
  27. import sys
  28. logger = logging.getLogger('recipetool')
  29. tinfoil = None
  30. def tinfoil_init(instance):
  31. global tinfoil
  32. tinfoil = instance
  33. def _provide_to_pn(cooker, provide):
  34. """Get the name of the preferred recipe for the specified provide."""
  35. import bb.providers
  36. filenames = cooker.recipecache.providers[provide]
  37. eligible, foundUnique = bb.providers.filterProviders(filenames, provide, cooker.expanded_data, cooker.recipecache)
  38. filename = eligible[0]
  39. pn = cooker.recipecache.pkg_fn[filename]
  40. return pn
  41. def _get_recipe_file(cooker, pn):
  42. import oe.recipeutils
  43. recipefile = oe.recipeutils.pn_to_recipe(cooker, pn)
  44. if not recipefile:
  45. skipreasons = oe.recipeutils.get_unavailable_reasons(cooker, pn)
  46. if skipreasons:
  47. logger.error('\n'.join(skipreasons))
  48. else:
  49. logger.error("Unable to find any recipe file matching %s" % pn)
  50. return recipefile
  51. def layer(layerpath):
  52. if not os.path.exists(os.path.join(layerpath, 'conf', 'layer.conf')):
  53. raise argparse.ArgumentTypeError('{0!r} must be a path to a valid layer'.format(layerpath))
  54. return layerpath
  55. def newappend(args):
  56. import oe.recipeutils
  57. pn = _provide_to_pn(tinfoil.cooker, args.target)
  58. recipe_path = _get_recipe_file(tinfoil.cooker, pn)
  59. rd = tinfoil.config_data.createCopy()
  60. rd.setVar('FILE', recipe_path)
  61. append_path, path_ok = oe.recipeutils.get_bbappend_path(rd, args.destlayer, args.wildcard_version)
  62. if not append_path:
  63. logger.error('Unable to determine layer directory containing %s', recipe_path)
  64. return 1
  65. if not path_ok:
  66. logger.warn('Unable to determine correct subdirectory path for bbappend file - check that what %s adds to BBFILES also matches .bbappend files. Using %s for now, but until you fix this the bbappend will not be applied.', os.path.join(destlayerdir, 'conf', 'layer.conf'), os.path.dirname(appendpath))
  67. layerdirs = [os.path.abspath(layerdir) for layerdir in rd.getVar('BBLAYERS', True).split()]
  68. if not os.path.abspath(args.destlayer) in layerdirs:
  69. logger.warn('Specified layer is not currently enabled in bblayers.conf, you will need to add it before this bbappend will be active')
  70. if not os.path.exists(append_path):
  71. bb.utils.mkdirhier(os.path.dirname(append_path))
  72. try:
  73. open(append_path, 'a')
  74. except (OSError, IOError) as exc:
  75. logger.critical(str(exc))
  76. return 1
  77. print(append_path)
  78. def register_command(subparsers):
  79. parser = subparsers.add_parser('newappend',
  80. help='Create a bbappend for the specified target in the specified layer')
  81. parser.add_argument('-w', '--wildcard-version', help='Use wildcard to make the bbappend apply to any recipe version', action='store_true')
  82. parser.add_argument('destlayer', help='Base directory of the destination layer to write the bbappend to', type=layer)
  83. parser.add_argument('target', help='Target recipe/provide to append')
  84. parser.set_defaults(func=newappend, parserecipes=True)