create.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. import logging
  5. import os
  6. import sys
  7. import shutil
  8. import bb.utils
  9. from bblayers.common import LayerPlugin
  10. logger = logging.getLogger('bitbake-layers')
  11. def plugin_init(plugins):
  12. return CreatePlugin()
  13. def read_template(template, template_dir='templates'):
  14. lines = str()
  15. with open(os.path.join(os.path.dirname(__file__), template_dir, template)) as fd:
  16. lines = ''.join(fd.readlines())
  17. return lines
  18. class CreatePlugin(LayerPlugin):
  19. def do_create_layer(self, args):
  20. """Create a basic layer"""
  21. layerdir = os.path.abspath(args.layerdir)
  22. if os.path.exists(layerdir):
  23. sys.stderr.write("Specified layer directory exists\n")
  24. return 1
  25. # create dirs
  26. conf = os.path.join(layerdir, 'conf')
  27. bb.utils.mkdirhier(conf)
  28. layername = os.path.basename(os.path.normpath(args.layerdir))
  29. # Create the README from templates/README
  30. readme_template = read_template('README').format(layername=layername)
  31. readme = os.path.join(layerdir, 'README')
  32. with open(readme, 'w') as fd:
  33. fd.write(readme_template)
  34. # Copy the MIT license from meta
  35. copying = 'COPYING.MIT'
  36. dn = os.path.dirname
  37. license_src = os.path.join(dn(dn(dn(__file__))), copying)
  38. license_dst = os.path.join(layerdir, copying)
  39. shutil.copy(license_src, license_dst)
  40. # Get the compat value for core layer.
  41. compat = self.tinfoil.config_data.getVar('LAYERSERIES_COMPAT_core') or ""
  42. # Create the layer.conf from templates/layer.conf
  43. layerconf_template = read_template('layer.conf').format(
  44. layername=layername, priority=args.priority, compat=compat)
  45. layerconf = os.path.join(conf, 'layer.conf')
  46. with open(layerconf, 'w') as fd:
  47. fd.write(layerconf_template)
  48. # Create the example from templates/example.bb
  49. example_template = read_template('example.bb')
  50. example = os.path.join(layerdir, 'recipes-' + args.examplerecipe, args.examplerecipe)
  51. bb.utils.mkdirhier(example)
  52. with open(os.path.join(example, args.examplerecipe + '_%s.bb') % args.version, 'w') as fd:
  53. fd.write(example_template)
  54. logger.plain('Add your new layer with \'bitbake-layers add-layer %s\'' % args.layerdir)
  55. def register_commands(self, sp):
  56. parser_create_layer = self.add_command(sp, 'create-layer', self.do_create_layer, parserecipes=False)
  57. parser_create_layer.add_argument('layerdir', help='Layer directory to create')
  58. parser_create_layer.add_argument('--priority', '-p', default=6, help='Layer directory to create')
  59. parser_create_layer.add_argument('--example-recipe-name', '-e', dest='examplerecipe', default='example', help='Filename of the example recipe')
  60. parser_create_layer.add_argument('--example-recipe-version', '-v', dest='version', default='0.1', help='Version number for the example recipe')