setvar.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Recipe creation tool - set variable plugin
  2. #
  3. # Copyright (C) 2015 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. import sys
  18. import os
  19. import argparse
  20. import glob
  21. import fnmatch
  22. import re
  23. import logging
  24. import scriptutils
  25. logger = logging.getLogger('recipetool')
  26. tinfoil = None
  27. plugins = None
  28. def tinfoil_init(instance):
  29. global tinfoil
  30. tinfoil = instance
  31. def setvar(args):
  32. import oe.recipeutils
  33. if args.delete:
  34. if args.value:
  35. logger.error('-D/--delete and specifying a value are mutually exclusive')
  36. return 1
  37. value = None
  38. else:
  39. if args.value is None:
  40. logger.error('You must specify a value if not using -D/--delete')
  41. return 1
  42. value = args.value
  43. varvalues = {args.varname: value}
  44. if args.recipe_only:
  45. patches = [oe.recipeutils.patch_recipe_file(args.recipefile, varvalues, patch=args.patch)]
  46. else:
  47. rd = oe.recipeutils.parse_recipe(tinfoil.cooker, args.recipefile, None)
  48. if not rd:
  49. return 1
  50. patches = oe.recipeutils.patch_recipe(rd, args.recipefile, varvalues, patch=args.patch)
  51. if args.patch:
  52. for patch in patches:
  53. for line in patch:
  54. sys.stdout.write(line)
  55. return 0
  56. def 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)