Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | import logging |
| 2 | import os |
| 3 | import sys |
| 4 | import shutil |
| 5 | |
| 6 | import bb.utils |
| 7 | |
| 8 | from bblayers.common import LayerPlugin |
| 9 | |
| 10 | logger = logging.getLogger('bitbake-layers') |
| 11 | |
| 12 | def plugin_init(plugins): |
| 13 | return CreatePlugin() |
| 14 | |
| 15 | def 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 | |
| 21 | class 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 33 | layername = os.path.basename(os.path.normpath(args.layerdir)) |
| 34 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 35 | # Create the README from templates/README |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 36 | readme_template = read_template('README').format(layername=layername) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 37 | 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 48 | # Get the compat value for core layer. |
| 49 | compat = self.tinfoil.config_data.getVar('LAYERSERIES_COMPAT_core') or "" |
| 50 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 51 | # Create the layer.conf from templates/layer.conf |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 52 | layerconf_template = read_template('layer.conf').format( |
| 53 | layername=layername, priority=args.priority, compat=compat) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | 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 Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 62 | 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] | 63 | 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 Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 72 | 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] | 73 | |