1*4882a593Smuzhiyun# 2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 3*4882a593Smuzhiyun# 4*4882a593Smuzhiyun 5*4882a593Smuzhiyunimport logging 6*4882a593Smuzhiyunimport os 7*4882a593Smuzhiyunimport sys 8*4882a593Smuzhiyunimport shutil 9*4882a593Smuzhiyun 10*4882a593Smuzhiyunimport bb.utils 11*4882a593Smuzhiyun 12*4882a593Smuzhiyunfrom bblayers.common import LayerPlugin 13*4882a593Smuzhiyun 14*4882a593Smuzhiyunlogger = logging.getLogger('bitbake-layers') 15*4882a593Smuzhiyun 16*4882a593Smuzhiyundef plugin_init(plugins): 17*4882a593Smuzhiyun return CreatePlugin() 18*4882a593Smuzhiyun 19*4882a593Smuzhiyundef read_template(template, template_dir='templates'): 20*4882a593Smuzhiyun lines = str() 21*4882a593Smuzhiyun with open(os.path.join(os.path.dirname(__file__), template_dir, template)) as fd: 22*4882a593Smuzhiyun lines = ''.join(fd.readlines()) 23*4882a593Smuzhiyun return lines 24*4882a593Smuzhiyun 25*4882a593Smuzhiyunclass CreatePlugin(LayerPlugin): 26*4882a593Smuzhiyun def do_create_layer(self, args): 27*4882a593Smuzhiyun """Create a basic layer""" 28*4882a593Smuzhiyun layerdir = os.path.abspath(args.layerdir) 29*4882a593Smuzhiyun if os.path.exists(layerdir): 30*4882a593Smuzhiyun sys.stderr.write("Specified layer directory exists\n") 31*4882a593Smuzhiyun return 1 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun # create dirs 34*4882a593Smuzhiyun conf = os.path.join(layerdir, 'conf') 35*4882a593Smuzhiyun bb.utils.mkdirhier(conf) 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun layername = os.path.basename(os.path.normpath(args.layerdir)) 38*4882a593Smuzhiyun layerid = args.layerid if args.layerid is not None else layername 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun # Create the README from templates/README 41*4882a593Smuzhiyun readme_template = read_template('README').format(layername=layername) 42*4882a593Smuzhiyun readme = os.path.join(layerdir, 'README') 43*4882a593Smuzhiyun with open(readme, 'w') as fd: 44*4882a593Smuzhiyun fd.write(readme_template) 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun # Copy the MIT license from meta 47*4882a593Smuzhiyun copying = 'COPYING.MIT' 48*4882a593Smuzhiyun dn = os.path.dirname 49*4882a593Smuzhiyun license_src = os.path.join(dn(dn(dn(__file__))), copying) 50*4882a593Smuzhiyun license_dst = os.path.join(layerdir, copying) 51*4882a593Smuzhiyun shutil.copy(license_src, license_dst) 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun # Get the compat value for core layer. 54*4882a593Smuzhiyun compat = self.tinfoil.config_data.getVar('LAYERSERIES_COMPAT_core') or "" 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun # Create the layer.conf from templates/layer.conf 57*4882a593Smuzhiyun layerconf_template = read_template('layer.conf').format( 58*4882a593Smuzhiyun layerid=layerid, priority=args.priority, compat=compat) 59*4882a593Smuzhiyun layerconf = os.path.join(conf, 'layer.conf') 60*4882a593Smuzhiyun with open(layerconf, 'w') as fd: 61*4882a593Smuzhiyun fd.write(layerconf_template) 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun # Create the example from templates/example.bb 64*4882a593Smuzhiyun example_template = read_template('example.bb') 65*4882a593Smuzhiyun example = os.path.join(layerdir, 'recipes-' + args.examplerecipe, args.examplerecipe) 66*4882a593Smuzhiyun bb.utils.mkdirhier(example) 67*4882a593Smuzhiyun with open(os.path.join(example, args.examplerecipe + '_%s.bb') % args.version, 'w') as fd: 68*4882a593Smuzhiyun fd.write(example_template) 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun logger.plain('Add your new layer with \'bitbake-layers add-layer %s\'' % args.layerdir) 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun def register_commands(self, sp): 73*4882a593Smuzhiyun parser_create_layer = self.add_command(sp, 'create-layer', self.do_create_layer, parserecipes=False) 74*4882a593Smuzhiyun parser_create_layer.add_argument('layerdir', help='Layer directory to create') 75*4882a593Smuzhiyun parser_create_layer.add_argument('--layerid', '-i', help='Layer id to use if different from layername') 76*4882a593Smuzhiyun parser_create_layer.add_argument('--priority', '-p', default=6, help='Priority of recipes in layer') 77*4882a593Smuzhiyun parser_create_layer.add_argument('--example-recipe-name', '-e', dest='examplerecipe', default='example', help='Filename of the example recipe') 78*4882a593Smuzhiyun parser_create_layer.add_argument('--example-recipe-version', '-v', dest='version', default='0.1', help='Version number for the example recipe') 79*4882a593Smuzhiyun 80