methodpool.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. #
  4. #
  5. # Copyright (C) 2006 Holger Hans Peter Freyther
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2 as
  9. # published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. """
  20. What is a method pool?
  21. BitBake has a global method scope where .bb, .inc and .bbclass
  22. files can install methods. These methods are parsed from strings.
  23. To avoid recompiling and executing these string we introduce
  24. a method pool to do this task.
  25. This pool will be used to compile and execute the functions. It
  26. will be smart enough to
  27. """
  28. from bb.utils import better_compile, better_exec
  29. from bb import error
  30. # A dict of modules we have handled
  31. # it is the number of .bbclasses + x in size
  32. _parsed_methods = { }
  33. _parsed_fns = { }
  34. def insert_method(modulename, code, fn):
  35. """
  36. Add code of a module should be added. The methods
  37. will be simply added, no checking will be done
  38. """
  39. comp = better_compile(code, "<bb>", fn )
  40. better_exec(comp, __builtins__, code, fn)
  41. # now some instrumentation
  42. code = comp.co_names
  43. for name in code:
  44. if name in ['None', 'False']:
  45. continue
  46. elif name in _parsed_fns and not _parsed_fns[name] == modulename:
  47. error( "Error Method already seen: %s in' %s' now in '%s'" % (name, _parsed_fns[name], modulename))
  48. else:
  49. _parsed_fns[name] = modulename
  50. def check_insert_method(modulename, code, fn):
  51. """
  52. Add the code if it wasnt added before. The module
  53. name will be used for that
  54. Variables:
  55. @modulename a short name e.g. base.bbclass
  56. @code The actual python code
  57. @fn The filename from the outer file
  58. """
  59. if not modulename in _parsed_methods:
  60. return insert_method(modulename, code, fn)
  61. _parsed_methods[modulename] = 1
  62. def parsed_module(modulename):
  63. """
  64. Inform me file xyz was parsed
  65. """
  66. return modulename in _parsed_methods
  67. def get_parsed_dict():
  68. """
  69. shortcut
  70. """
  71. return _parsed_methods