blob: c8f3f1b370ef8c98a9e4f2178c67690332ad70f8 [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
15
16logger = logging.getLogger('bitbake-layers')
17
18def plugin_init(plugins):
19 return CreatePlugin()
20
21def read_template(template, template_dir='templates'):
22 lines = str()
23 with open(os.path.join(os.path.dirname(__file__), template_dir, template)) as fd:
24 lines = ''.join(fd.readlines())
25 return lines
26
27class CreatePlugin(LayerPlugin):
28 def do_create_layer(self, args):
29 """Create a basic layer"""
30 layerdir = os.path.abspath(args.layerdir)
31 if os.path.exists(layerdir):
32 sys.stderr.write("Specified layer directory exists\n")
33 return 1
34
35 # create dirs
36 conf = os.path.join(layerdir, 'conf')
37 bb.utils.mkdirhier(conf)
38
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080039 layername = os.path.basename(os.path.normpath(args.layerdir))
Andrew Geissler95ac1b82021-03-31 14:34:31 -050040 layerid = args.layerid if args.layerid is not None else layername
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080041
Brad Bishopd7bf8c12018-02-25 22:55:05 -050042 # Create the README from templates/README
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080043 readme_template = read_template('README').format(layername=layername)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050044 readme = os.path.join(layerdir, 'README')
45 with open(readme, 'w') as fd:
46 fd.write(readme_template)
47
48 # Copy the MIT license from meta
49 copying = 'COPYING.MIT'
50 dn = os.path.dirname
51 license_src = os.path.join(dn(dn(dn(__file__))), copying)
52 license_dst = os.path.join(layerdir, copying)
53 shutil.copy(license_src, license_dst)
54
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080055 # Get the compat value for core layer.
Andrew Geissler517393d2023-01-13 08:55:19 -060056 compat = self.tinfoil.config_data.getVar('LAYERSERIES_CORENAMES') or ""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080057
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058 # Create the layer.conf from templates/layer.conf
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080059 layerconf_template = read_template('layer.conf').format(
Andrew Geissler95ac1b82021-03-31 14:34:31 -050060 layerid=layerid, priority=args.priority, compat=compat)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061 layerconf = os.path.join(conf, 'layer.conf')
62 with open(layerconf, 'w') as fd:
63 fd.write(layerconf_template)
64
65 # Create the example from templates/example.bb
66 example_template = read_template('example.bb')
67 example = os.path.join(layerdir, 'recipes-' + args.examplerecipe, args.examplerecipe)
68 bb.utils.mkdirhier(example)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070069 with open(os.path.join(example, args.examplerecipe + '_%s.bb') % args.version, 'w') as fd:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050070 fd.write(example_template)
71
72 logger.plain('Add your new layer with \'bitbake-layers add-layer %s\'' % args.layerdir)
73
74 def register_commands(self, sp):
75 parser_create_layer = self.add_command(sp, 'create-layer', self.do_create_layer, parserecipes=False)
76 parser_create_layer.add_argument('layerdir', help='Layer directory to create')
Andrew Geissler95ac1b82021-03-31 14:34:31 -050077 parser_create_layer.add_argument('--layerid', '-i', help='Layer id to use if different from layername')
78 parser_create_layer.add_argument('--priority', '-p', default=6, help='Priority of recipes in layer')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050079 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 -070080 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 -050081