metrixpp.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ 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
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import time
  20. import sys
  21. import logging
  22. import os
  23. import subprocess
  24. import itertools
  25. import core.log
  26. def main():
  27. os.environ['METRIXPLUSPLUS_INSTALL_DIR'] = os.path.dirname(os.path.abspath(__file__))
  28. available_tools = []
  29. for fname in os.listdir(os.path.join(os.environ['METRIXPLUSPLUS_INSTALL_DIR'], 'tools')):
  30. tool_name = os.path.splitext(fname)[0]
  31. if tool_name == '__init__':
  32. continue
  33. if tool_name not in available_tools:
  34. available_tools.append(tool_name)
  35. exemode = None
  36. if len(sys.argv[1:]) != 0:
  37. exemode = sys.argv[1]
  38. if exemode != "-R" and exemode != "-D":
  39. exemode = '-D' # TODO implement install and release mode
  40. # inject '-D' or '-R' option
  41. exit(subprocess.call(itertools.chain([sys.executable, sys.argv[0], '-D'], sys.argv[1:])))
  42. command = ""
  43. if len(sys.argv[1:]) > 1:
  44. command = sys.argv[2]
  45. if command not in available_tools:
  46. logging.error("Unknown action: " + str(command))
  47. print "Usage: {prog} <action> --help".format(prog=__file__)
  48. print " or: {prog} <action> [options] -- [path 1] ... [path N]".format(prog=__file__)
  49. print "where: actions are:"
  50. for each in available_tools:
  51. print "\t" + each
  52. return 1
  53. tool = __import__('tools', globals(), locals(), [command], -1)
  54. module_attr = tool.__getattribute__(command)
  55. class_attr = module_attr.__getattribute__('Tool')
  56. instance = class_attr.__new__(class_attr)
  57. instance.__init__()
  58. return instance.run(sys.argv[3:])
  59. if __name__ == '__main__':
  60. ts = time.time()
  61. core.log.set_default_format()
  62. exit_code = main()
  63. time_spent = round((time.time() - ts), 2)
  64. if 'METRIXPLUSPLUS_TEST_GENERATE_GOLDS' in os.environ.keys() and \
  65. os.environ['METRIXPLUSPLUS_TEST_GENERATE_GOLDS'] == "True":
  66. time_spent = 1 # Constant value if under tests
  67. logging.warning("Exit code: " + str(exit_code) + ". Time spent: " + str(time_spent) + " seconds. Done")
  68. exit(exit_code)