/bindings/bindingtester/util.py

https://github.com/apple/foundationdb
Python | 76 lines | 39 code | 14 blank | 23 comment | 13 complexity | 69ffa6f3461521bf8fcbf2de2bb30979 MD5 | raw file
  1. #
  2. # util.py
  3. #
  4. # This source file is part of the FoundationDB open source project
  5. #
  6. # Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. import logging
  21. import signal
  22. import os
  23. import glob
  24. import fdb
  25. def initialize_logger_level(logging_level):
  26. logger = get_logger()
  27. assert logging_level in ['DEBUG', 'INFO', 'WARNING', 'ERROR']
  28. if logging_level == 'DEBUG':
  29. logger.setLevel(logging.DEBUG)
  30. elif logging_level == 'INFO':
  31. logger.setLevel(logging.INFO)
  32. elif logging_level == 'WARNING':
  33. logger.setLevel(logging.WARNING)
  34. elif logging_level == 'ERROR':
  35. logger.setLevel(logging.ERROR)
  36. def get_logger():
  37. return logging.getLogger('foundationdb.bindingtester')
  38. # Attempts to get the name associated with a process termination signal
  39. def signal_number_to_name(signal_num):
  40. name = []
  41. for key in signal.__dict__.keys():
  42. if key.startswith('SIG') and getattr(signal, key) == signal_num:
  43. name.append(key)
  44. if len(name) == 1:
  45. return name[0]
  46. else:
  47. return str(signal_num)
  48. def import_subclasses(filename, module_path):
  49. for f in glob.glob(os.path.join(os.path.dirname(filename), '*.py')):
  50. fn = os.path.basename(f)
  51. if fn == '__init__.py':
  52. continue
  53. __import__('%s.%s' % (module_path, os.path.splitext(fn)[0]))
  54. # Attempts to unpack a subspace
  55. # This throws an exception if the subspace cannot be unpacked as a tuple
  56. # As a result, the binding tester cannot use subspaces that have non-tuple raw prefixes
  57. def subspace_to_tuple(subspace):
  58. try:
  59. return fdb.tuple.unpack(subspace.key())
  60. except Exception as e:
  61. get_logger().debug(e)
  62. raise Exception('The binding tester does not support subspaces with non-tuple raw prefixes')