Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # |
| 4 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 5 | import logging |
| 6 | import os |
| 7 | import sys |
| 8 | import shutil |
| 9 | |
| 10 | import bb.utils |
| 11 | |
| 12 | from bblayers.common import LayerPlugin |
| 13 | |
| 14 | logger = logging.getLogger('bitbake-layers') |
| 15 | |
| 16 | def plugin_init(plugins): |
| 17 | return CreatePlugin() |
| 18 | |
| 19 | def 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 | |
| 25 | class 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 37 | layername = os.path.basename(os.path.normpath(args.layerdir)) |
| 38 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 39 | # Create the README from templates/README |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 40 | readme_template = read_template('README').format(layername=layername) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 41 | readme = os.path.join(layerdir, 'README') |
| 42 | with open(readme, 'w') as fd: |
| 43 | fd.write(readme_template) |
| 44 | |
| 45 | # Copy the MIT license from meta |
| 46 | copying = 'COPYING.MIT' |
| 47 | dn = os.path.dirname |
| 48 | license_src = os.path.join(dn(dn(dn(__file__))), copying) |
| 49 | license_dst = os.path.join(layerdir, copying) |
| 50 | shutil.copy(license_src, license_dst) |
| 51 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 52 | # Get the compat value for core layer. |
| 53 | compat = self.tinfoil.config_data.getVar('LAYERSERIES_COMPAT_core') or "" |
| 54 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 55 | # Create the layer.conf from templates/layer.conf |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 56 | layerconf_template = read_template('layer.conf').format( |
| 57 | layername=layername, priority=args.priority, compat=compat) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 58 | layerconf = os.path.join(conf, 'layer.conf') |
| 59 | with open(layerconf, 'w') as fd: |
| 60 | fd.write(layerconf_template) |
| 61 | |
| 62 | # Create the example from templates/example.bb |
| 63 | example_template = read_template('example.bb') |
| 64 | example = os.path.join(layerdir, 'recipes-' + args.examplerecipe, args.examplerecipe) |
| 65 | bb.utils.mkdirhier(example) |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 66 | with open(os.path.join(example, args.examplerecipe + '_%s.bb') % args.version, 'w') as fd: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 67 | fd.write(example_template) |
| 68 | |
| 69 | logger.plain('Add your new layer with \'bitbake-layers add-layer %s\'' % args.layerdir) |
| 70 | |
| 71 | def register_commands(self, sp): |
| 72 | parser_create_layer = self.add_command(sp, 'create-layer', self.do_create_layer, parserecipes=False) |
| 73 | parser_create_layer.add_argument('layerdir', help='Layer directory to create') |
| 74 | parser_create_layer.add_argument('--priority', '-p', default=6, help='Layer directory to create') |
| 75 | parser_create_layer.add_argument('--example-recipe-name', '-e', dest='examplerecipe', default='example', help='Filename of the example recipe') |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 76 | parser_create_layer.add_argument('--example-recipe-version', '-v', dest='version', default='0.1', help='Version number for the example recipe') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 77 | |