runant.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/python
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """
  18. runant.py
  19. This script is a translation of the runant.pl
  20. It runs ant with/out arguments, it should be quite portable (thanks to
  21. the python os library)
  22. This script has been tested with Python2.0/Win2K
  23. Assumptions:
  24. - the "java" executable/script is on the command path
  25. """
  26. import os, os.path, string, sys
  27. # Change it to 1 to get extra debug information
  28. debug = 0
  29. #######################################################################
  30. # If ANT_HOME is not set default to script's parent directory
  31. if os.environ.has_key('ANT_HOME'):
  32. ANT_HOME = os.environ['ANT_HOME']
  33. else:
  34. ANT_HOME = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
  35. # set ANT_LIB location
  36. ANT_LIB = os.path.join(ANT_HOME, 'lib')
  37. # set JAVACMD (check variables JAVACMD and JAVA_HOME)
  38. JAVACMD = None
  39. if not os.environ.has_key('JAVACMD'):
  40. if os.environ.has_key('JAVA_HOME'):
  41. if not os.path.exists(os.environ['JAVA_HOME']):
  42. print "Warning: JAVA_HOME is not defined correctly."
  43. else:
  44. JAVA_HOME = os.environ['JAVA_HOME']
  45. while JAVA_HOME[0] == JAVA_HOME[-1] == "\"":
  46. JAVA_HOME = JAVA_HOME[1:-1]
  47. JAVACMD = os.path.join(JAVA_HOME, 'bin', 'java')
  48. else:
  49. print "Warning: JAVA_HOME not set."
  50. else:
  51. JAVACMD = os.environ['JAVACMD']
  52. if not JAVACMD:
  53. JAVACMD = 'java'
  54. launcher_jar = os.path.join(ANT_LIB, 'ant-launcher.jar')
  55. if not os.path.exists(launcher_jar):
  56. print 'Warning: Unable to locate ant-launcher.jar. Expected to find it in %s' % \
  57. ANT_LIB
  58. # Build up standard classpath (LOCALCLASSPATH)
  59. LOCALCLASSPATH = launcher_jar
  60. if os.environ.has_key('LOCALCLASSPATH'):
  61. LOCALCLASSPATH += os.pathsep + os.environ['LOCALCLASSPATH']
  62. ANT_OPTS = ""
  63. if os.environ.has_key('ANT_OPTS'):
  64. ANT_OPTS = os.environ['ANT_OPTS']
  65. OPTS = ""
  66. if os.environ.has_key('JIKESPATH'):
  67. OPTS = '-Djikes.class.path=\"%s\"' % os.environ['JIKESPATH']
  68. ANT_ARGS = ""
  69. if os.environ.has_key('ANT_ARGS'):
  70. ANT_ARGS = os.environ['ANT_ARGS']
  71. CLASSPATH = ""
  72. if os.environ.has_key('CLASSPATH'):
  73. CLASSPATH = "-lib " + os.environ['CLASSPATH']
  74. while JAVACMD[0] == JAVACMD[-1] == "\"":
  75. JAVACMD = JAVACMD[1:-1]
  76. # Builds the commandline
  77. cmdline = ('"%s" %s -classpath %s -Dant.home=%s %s ' + \
  78. 'org.apache.tools.ant.launch.Launcher %s %s %s') \
  79. % (JAVACMD, ANT_OPTS, LOCALCLASSPATH, ANT_HOME, OPTS, ANT_ARGS, \
  80. CLASSPATH, string.join(sys.argv[1:], ' '))
  81. if debug:
  82. print '\n%s\n\n' % (cmdline)
  83. sys.stdout.flush()
  84. # Run the biniou!
  85. os.system(cmdline)