1# Recipe creation tool - set variable plugin
2#
3# Copyright (C) 2015 Intel Corporation
4#
5# SPDX-License-Identifier: GPL-2.0-only
6#
7
8import sys
9import os
10import argparse
11import glob
12import fnmatch
13import re
14import logging
15import scriptutils
16
17logger = logging.getLogger('recipetool')
18
19tinfoil = None
20plugins = None
21
22def tinfoil_init(instance):
23    global tinfoil
24    tinfoil = instance
25
26def setvar(args):
27    import oe.recipeutils
28
29    if args.delete:
30        if args.value:
31            logger.error('-D/--delete and specifying a value are mutually exclusive')
32            return 1
33        value = None
34    else:
35        if args.value is None:
36            logger.error('You must specify a value if not using -D/--delete')
37            return 1
38        value = args.value
39    varvalues = {args.varname: value}
40
41    if args.recipe_only:
42        patches = [oe.recipeutils.patch_recipe_file(args.recipefile, varvalues, patch=args.patch)]
43    else:
44        rd = tinfoil.parse_recipe_file(args.recipefile, False)
45        if not rd:
46            return 1
47        patches = oe.recipeutils.patch_recipe(rd, args.recipefile, varvalues, patch=args.patch)
48    if args.patch:
49        for patch in patches:
50            for line in patch:
51                sys.stdout.write(line)
52    tinfoil.modified_files()
53    return 0
54
55
56def register_commands(subparsers):
57    parser_setvar = subparsers.add_parser('setvar',
58                                          help='Set a variable within a recipe',
59                                          description='Adds/updates the value a variable is set to in a recipe')
60    parser_setvar.add_argument('recipefile', help='Recipe file to update')
61    parser_setvar.add_argument('varname', help='Variable name to set')
62    parser_setvar.add_argument('value', nargs='?', help='New value to set the variable to')
63    parser_setvar.add_argument('--recipe-only', '-r', help='Do not set variable in any include file if present', action='store_true')
64    parser_setvar.add_argument('--patch', '-p', help='Create a patch to make the change instead of modifying the recipe', action='store_true')
65    parser_setvar.add_argument('--delete', '-D', help='Delete the specified value instead of setting it', action='store_true')
66    parser_setvar.set_defaults(func=setvar)
67