blob: 7ddb777dc7299b2d1b75eab1018686cdb133ab0b [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005import logging
6import os
7import sys
8import shutil
9
10import bb.utils
11
12from bblayers.common import LayerPlugin
13
14logger = logging.getLogger('bitbake-layers')
15
16def plugin_init(plugins):
17 return CreatePlugin()
18
19def read_template(template, template_dir='templates'):
20 lines = str()
21 with open(os.path.join(os.path.dirname(__file__), template_dir, template)) as fd:
22 lines = ''.join(fd.readlines())
23 return lines
24
25class CreatePlugin(LayerPlugin):
26 def do_create_layer(self, args):
27 """Create a basic layer"""
28 layerdir = os.path.abspath(args.layerdir)
29 if os.path.exists(layerdir):
30 sys.stderr.write("Specified layer directory exists\n")
31 return 1
32
33 # create dirs
34 conf = os.path.join(layerdir, 'conf')
35 bb.utils.mkdirhier(conf)
36
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080037 layername = os.path.basename(os.path.normpath(args.layerdir))
Andrew Geissler95ac1b82021-03-31 14:34:31 -050038 layerid = args.layerid if args.layerid is not None else layername
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080039
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 # Create the README from templates/README
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080041 readme_template = read_template('README').format(layername=layername)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050042 readme = os.path.join(layerdir, 'README')
43 with open(readme, 'w') as fd:
44 fd.write(readme_template)
45
46 # Copy the MIT license from meta
47 copying = 'COPYING.MIT'
48 dn = os.path.dirname
49 license_src = os.path.join(dn(dn(dn(__file__))), copying)
50 license_dst = os.path.join(layerdir, copying)
51 shutil.copy(license_src, license_dst)
52
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080053 # Get the compat value for core layer.
54 compat = self.tinfoil.config_data.getVar('LAYERSERIES_COMPAT_core') or ""
55
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056 # Create the layer.conf from templates/layer.conf
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080057 layerconf_template = read_template('layer.conf').format(
Andrew Geissler95ac1b82021-03-31 14:34:31 -050058 layerid=layerid, priority=args.priority, compat=compat)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059 layerconf = os.path.join(conf, 'layer.conf')
60 with open(layerconf, 'w') as fd:
61 fd.write(layerconf_template)
62
63 # Create the example from templates/example.bb
64 example_template = read_template('example.bb')
65 example = os.path.join(layerdir, 'recipes-' + args.examplerecipe, args.examplerecipe)
66 bb.utils.mkdirhier(example)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070067 with open(os.path.join(example, args.examplerecipe + '_%s.bb') % args.version, 'w') as fd:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050068 fd.write(example_template)
69
70 logger.plain('Add your new layer with \'bitbake-layers add-layer %s\'' % args.layerdir)
71
72 def register_commands(self, sp):
73 parser_create_layer = self.add_command(sp, 'create-layer', self.do_create_layer, parserecipes=False)
74 parser_create_layer.add_argument('layerdir', help='Layer directory to create')
Andrew Geissler95ac1b82021-03-31 14:34:31 -050075 parser_create_layer.add_argument('--layerid', '-i', help='Layer id to use if different from layername')
76 parser_create_layer.add_argument('--priority', '-p', default=6, help='Priority of recipes in layer')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 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 -070078 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 -050079