metrixpp.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. this_file = os.path.basename(__file__)
  29. available_tools = []
  30. excluded_tools = ['utils']
  31. internal_tools = ['debug', 'test']
  32. for fname in os.listdir(os.path.join(os.environ['METRIXPLUSPLUS_INSTALL_DIR'], 'tools')):
  33. tool_name = os.path.splitext(fname)[0]
  34. if tool_name == '__init__':
  35. continue
  36. if tool_name not in available_tools:
  37. if tool_name not in excluded_tools + internal_tools:
  38. available_tools.append(tool_name)
  39. exemode = None
  40. if len(sys.argv[1:]) != 0:
  41. exemode = sys.argv[1]
  42. if exemode != "-R" and exemode != "-D":
  43. exemode = '-D' # TODO implement install and release mode
  44. # inject '-D' or '-R' option
  45. exit(subprocess.call(itertools.chain([sys.executable, sys.argv[0], '-D'], sys.argv[1:])))
  46. command = ""
  47. if len(sys.argv[1:]) > 1:
  48. command = sys.argv[2]
  49. if command == '--help' or command == '-h' or command == '--h':
  50. print "Usage: python {prog} <action> --help".format(prog=this_file)
  51. print " or: python {prog} <action> [options] -- [path 1] ... [path N]".format(prog=this_file)
  52. print "where: actions are:"
  53. for each in sorted(available_tools):
  54. print "\t" + each
  55. if exemode == '-D':
  56. for each in sorted(internal_tools):
  57. print "\t" + each + "\t[internal]"
  58. exit(0)
  59. if command not in available_tools + internal_tools:
  60. print >> sys.stderr, "Usage: python {prog} --help\n".format(prog=this_file)
  61. print >> sys.stderr, "{prog}: error: no such action: {action}".format(prog=this_file, action=command)
  62. exit(1)
  63. tool = __import__('tools', globals(), locals(), [command], -1)
  64. module_attr = tool.__getattribute__(command)
  65. class_attr = module_attr.__getattribute__('Tool')
  66. instance = class_attr.__new__(class_attr)
  67. instance.__init__()
  68. return instance.run(sys.argv[3:])
  69. if __name__ == '__main__':
  70. ts = time.time()
  71. core.log.set_default_format()
  72. exit_code = main()
  73. time_spent = round((time.time() - ts), 2)
  74. if 'METRIXPLUSPLUS_TEST_GENERATE_GOLDS' in os.environ.keys() and \
  75. os.environ['METRIXPLUSPLUS_TEST_GENERATE_GOLDS'] == "True":
  76. time_spent = 1 # Constant value if under tests
  77. logging.warning("Done (" + str(time_spent) +" seconds). Exit code: " + str(exit_code))
  78. exit(exit_code)