blob: 2ebf151ad183b4a779710db50308a5f72a8b666d [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001import logging
2import os
3import sys
4import shutil
5
6import bb.utils
7
8from bblayers.common import LayerPlugin
9
10logger = logging.getLogger('bitbake-layers')
11
12def plugin_init(plugins):
13 return CreatePlugin()
14
15def read_template(template, template_dir='templates'):
16 lines = str()
17 with open(os.path.join(os.path.dirname(__file__), template_dir, template)) as fd:
18 lines = ''.join(fd.readlines())
19 return lines
20
21class CreatePlugin(LayerPlugin):
22 def do_create_layer(self, args):
23 """Create a basic layer"""
24 layerdir = os.path.abspath(args.layerdir)
25 if os.path.exists(layerdir):
26 sys.stderr.write("Specified layer directory exists\n")
27 return 1
28
29 # create dirs
30 conf = os.path.join(layerdir, 'conf')
31 bb.utils.mkdirhier(conf)
32
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080033 layername = os.path.basename(os.path.normpath(args.layerdir))
34
Brad Bishopd7bf8c12018-02-25 22:55:05 -050035 # Create the README from templates/README
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080036 readme_template = read_template('README').format(layername=layername)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050037 readme = os.path.join(layerdir, 'README')
38 with open(readme, 'w') as fd:
39 fd.write(readme_template)
40
41 # Copy the MIT license from meta
42 copying = 'COPYING.MIT'
43 dn = os.path.dirname
44 license_src = os.path.join(dn(dn(dn(__file__))), copying)
45 license_dst = os.path.join(layerdir, copying)
46 shutil.copy(license_src, license_dst)
47
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080048 # Get the compat value for core layer.
49 compat = self.tinfoil.config_data.getVar('LAYERSERIES_COMPAT_core') or ""
50
Brad Bishopd7bf8c12018-02-25 22:55:05 -050051 # Create the layer.conf from templates/layer.conf
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080052 layerconf_template = read_template('layer.conf').format(
53 layername=layername, priority=args.priority, compat=compat)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054 layerconf = os.path.join(conf, 'layer.conf')
55 with open(layerconf, 'w') as fd:
56 fd.write(layerconf_template)
57
58 # Create the example from templates/example.bb
59 example_template = read_template('example.bb')
60 example = os.path.join(layerdir, 'recipes-' + args.examplerecipe, args.examplerecipe)
61 bb.utils.mkdirhier(example)
Brad Bishopd5ae7d92018-06-14 09:52:03 -070062 with open(os.path.join(example, args.examplerecipe + '_%s.bb') % args.version, 'w') as fd:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050063 fd.write(example_template)
64
65 logger.plain('Add your new layer with \'bitbake-layers add-layer %s\'' % args.layerdir)
66
67 def register_commands(self, sp):
68 parser_create_layer = self.add_command(sp, 'create-layer', self.do_create_layer, parserecipes=False)
69 parser_create_layer.add_argument('layerdir', help='Layer directory to create')
70 parser_create_layer.add_argument('--priority', '-p', default=6, help='Layer directory to create')
71 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 -070072 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 -050073