blob: 517554c587d76989169c67ec1a2e8bd0713f8b1e [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
5#
6
Brad Bishopd7bf8c12018-02-25 22:55:05 -05007import logging
8import os
9import sys
10import shutil
11
12import bb.utils
13
14from bblayers.common import LayerPlugin
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060015from bblayers.action import ActionPlugin
Brad Bishopd7bf8c12018-02-25 22:55:05 -050016
17logger = logging.getLogger('bitbake-layers')
18
19def plugin_init(plugins):
20 return CreatePlugin()
21
22def read_template(template, template_dir='templates'):
23 lines = str()
24 with open(os.path.join(os.path.dirname(__file__), template_dir, template)) as fd:
25 lines = ''.join(fd.readlines())
26 return lines
27
28class CreatePlugin(LayerPlugin):
29 def do_create_layer(self, args):
30 """Create a basic layer"""
31 layerdir = os.path.abspath(args.layerdir)
32 if os.path.exists(layerdir):
33 sys.stderr.write("Specified layer directory exists\n")
34 return 1
35
36 # create dirs
37 conf = os.path.join(layerdir, 'conf')
38 bb.utils.mkdirhier(conf)
39
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080040 layername = os.path.basename(os.path.normpath(args.layerdir))
Andrew Geissler95ac1b82021-03-31 14:34:31 -050041 layerid = args.layerid if args.layerid is not None else layername
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080042
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043 # Create the README from templates/README
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080044 readme_template = read_template('README').format(layername=layername)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050045 readme = os.path.join(layerdir, 'README')
46 with open(readme, 'w') as fd:
47 fd.write(readme_template)
48
49 # Copy the MIT license from meta
50 copying = 'COPYING.MIT'
51 dn = os.path.dirname
52 license_src = os.path.join(dn(dn(dn(__file__))), copying)
53 license_dst = os.path.join(layerdir, copying)
54 shutil.copy(license_src, license_dst)
55
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080056 # Get the compat value for core layer.
Andrew Geissler517393d2023-01-13 08:55:19 -060057 compat = self.tinfoil.config_data.getVar('LAYERSERIES_CORENAMES') or ""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080058
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059 # Create the layer.conf from templates/layer.conf
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080060 layerconf_template = read_template('layer.conf').format(
Andrew Geissler95ac1b82021-03-31 14:34:31 -050061 layerid=layerid, priority=args.priority, compat=compat)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062 layerconf = os.path.join(conf, 'layer.conf')
63 with open(layerconf, 'w') as fd:
64 fd.write(layerconf_template)
65
66 # Create the example from templates/example.bb
67 example_template = read_template('example.bb')
68 example = os.path.join(layerdir, 'recipes-' + args.examplerecipe, args.examplerecipe)
69 bb.utils.mkdirhier(example)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070070 with open(os.path.join(example, args.examplerecipe + '_%s.bb') % args.version, 'w') as fd:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071 fd.write(example_template)
72
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060073 if args.add_layer:
74 # Add the layer to bblayers.conf
75 args.layerdir = [layerdir]
76 ActionPlugin.do_add_layer(self, args)
77 logger.plain('Layer added %s' % args.layerdir)
78
79 else:
80 logger.plain('Add your new layer with \'bitbake-layers add-layer %s\'' % args.layerdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050081
82 def register_commands(self, sp):
83 parser_create_layer = self.add_command(sp, 'create-layer', self.do_create_layer, parserecipes=False)
84 parser_create_layer.add_argument('layerdir', help='Layer directory to create')
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060085 parser_create_layer.add_argument('--add-layer', '-a', action='store_true', help='Add the layer to bblayers.conf after creation')
Andrew Geissler95ac1b82021-03-31 14:34:31 -050086 parser_create_layer.add_argument('--layerid', '-i', help='Layer id to use if different from layername')
87 parser_create_layer.add_argument('--priority', '-p', default=6, help='Priority of recipes in layer')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 parser_create_layer.add_argument('--example-recipe-name', '-e', dest='examplerecipe', default='example', help='Filename of the example recipe')
Brad Bishopd5ae7d92018-06-14 09:52:03 -070089 parser_create_layer.add_argument('--example-recipe-version', '-v', dest='version', default='0.1', help='Version number for the example recipe')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090