You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuildHelpers.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #coding=UTF-8
  2. ## Collection of helpers for Build scripts ##
  3. import sys, argparse, subprocess, platform
  4. from xml.etree import ElementTree
  5. from os.path import join, isdir, isfile, basename, exists
  6. from os import listdir, makedirs
  7. from shutil import copy, rmtree
  8. from glob import glob
  9. # Directory where the resulting war files are stored
  10. # TODO: deploy results
  11. resultPath = join("result", "demos")
  12. if not exists(resultPath):
  13. makedirs(resultPath)
  14. elif not isdir(resultPath):
  15. print("Result path is not a directory.")
  16. sys.exit(1)
  17. args = None
  18. # Default argument parser
  19. parser = argparse.ArgumentParser(description="Automated staging validation")
  20. group = parser.add_mutually_exclusive_group(required=True)
  21. group.add_argument("--version", help="Vaadin version to use")
  22. parser.add_argument("--maven", help="Additional maven command line parameters", default=None)
  23. parser.add_argument("--fwRepo", help="Framework staging repository URL", default=None)
  24. parser.add_argument("--pluginRepo", help="Maven plugin repository URL", default=None)
  25. # Parse command line arguments <version>
  26. def parseArgs():
  27. # If no args, give help
  28. if len(sys.argv) == 1:
  29. args = parser.parse_args(["-h"])
  30. else:
  31. args = parser.parse_args()
  32. return args
  33. # Function for determining the path for maven executable
  34. def getMavenCommand():
  35. # This method uses .split("\n")[0] which basically chooses the first result where/which returns.
  36. # Fixes the case with multiple maven installations available on PATH
  37. if platform.system() == "Windows":
  38. try:
  39. return subprocess.check_output(["where", "mvn.cmd"], universal_newlines=True).split("\n")[0]
  40. except:
  41. try:
  42. return subprocess.check_output(["where", "mvn.bat"], universal_newlines=True).split("\n")[0]
  43. except:
  44. print("Unable to locate mvn with where. Is the maven executable in your PATH?")
  45. else:
  46. try:
  47. return subprocess.check_output(["which", "mvn"], universal_newlines=True).split("\n")[0]
  48. except:
  49. print("Unable to locate maven executable with which. Is the maven executable in your PATH?")
  50. return None
  51. mavenCmd = getMavenCommand()
  52. # Get command line arguments. Parses arguments if needed.
  53. def getArgs():
  54. global args
  55. if args is None:
  56. args = parseArgs()
  57. return args
  58. # Maven Package and Validation
  59. def mavenValidate(artifactId, mvnCmd = mavenCmd, logFile = sys.stdout, version = None, mavenParams = None):
  60. if version is None:
  61. version = getArgs().version
  62. if mavenParams is None:
  63. mavenParams = getArgs().maven
  64. print("Do maven clean package validate")
  65. cmd = [mvnCmd]
  66. cmd.append("-Dvaadin.version=%s" % (version))
  67. if mavenParams is not None:
  68. cmd.extend(mavenParams.strip('"').split(" "))
  69. cmd.extend(["clean", "package", "validate"])
  70. print("executing: %s" % (" ".join(cmd)))
  71. subprocess.check_call(cmd, cwd=join(resultPath, artifactId), stdout=logFile)
  72. # Collect .war files to given folder with given naming
  73. def copyWarFiles(artifactId, resultDir = resultPath, name = None):
  74. if name is None:
  75. name = artifactId
  76. copiedWars = []
  77. warFiles = glob(join(resultDir, artifactId, "target", "*.war"))
  78. warFiles.extend(glob(join(resultDir, artifactId, "*", "target", "*.war")))
  79. for warFile in warFiles:
  80. if len(warFiles) == 1:
  81. deployName = "%s.war" % (name)
  82. else:
  83. deployName = "%s-%d.war" % (name, warFiles.index(warFile))
  84. print("Copying .war file %s as %s to result folder" % (basename(warFile), deployName))
  85. copy(warFile, join(resultDir, deployName))
  86. copiedWars.append(join(resultDir, deployName))
  87. return copiedWars
  88. def readPomFile(pomFile):
  89. # pom.xml namespace workaround
  90. root = ElementTree.parse(pomFile).getroot()
  91. nameSpace = root.tag[1:root.tag.index('}')]
  92. ElementTree.register_namespace('', nameSpace)
  93. # Read the pom.xml correctly
  94. return ElementTree.parse(pomFile), nameSpace
  95. # Recursive pom.xml update script
  96. def updateRepositories(path, repoUrl = None, version = None, postfix = "staging"):
  97. # If versions are not supplied, parse arguments
  98. if version is None:
  99. version = getArgs().version
  100. # Read pom.xml
  101. pomXml = join(path, "pom.xml")
  102. if isfile(pomXml):
  103. # Read the pom.xml correctly
  104. tree, nameSpace = readPomFile(pomXml)
  105. # NameSpace needed for finding the repositories node
  106. repoNode = tree.getroot().find("{%s}repositories" % (nameSpace))
  107. else:
  108. return
  109. if repoNode is not None:
  110. print("Add staging repositories to " + pomXml)
  111. # Add framework staging repository
  112. addRepo(repoNode, "repository", "vaadin-%s-%s" % (version, postfix), repoUrl)
  113. # Find the correct pluginRepositories node
  114. pluginRepo = tree.getroot().find("{%s}pluginRepositories" % (nameSpace))
  115. if pluginRepo is None:
  116. # Add pluginRepositories node if needed
  117. pluginRepo = ElementTree.SubElement(tree.getroot(), "pluginRepositories")
  118. # Add plugin staging repository
  119. addRepo(pluginRepo, "pluginRepository", "vaadin-%s-%s" % (version, postfix), repoUrl)
  120. # Overwrite the modified pom.xml
  121. tree.write(pomXml, encoding='UTF-8')
  122. # Recursive pom.xml search.
  123. for i in listdir(path):
  124. file = join(path, i)
  125. if isdir(file):
  126. updateRepositories(join(path, i), repoUrl, version, postfix)
  127. # Add a repository of repoType to given repoNode with id and URL
  128. def addRepo(repoNode, repoType, id, url):
  129. newRepo = ElementTree.SubElement(repoNode, repoType)
  130. idElem = ElementTree.SubElement(newRepo, "id")
  131. idElem.text = id
  132. urlElem = ElementTree.SubElement(newRepo, "url")
  133. urlElem.text = url
  134. # Get a logfile for given artifact
  135. def getLogFile(artifact, resultDir = resultPath):
  136. return open(join(resultDir, "%s.log" % (artifact)), 'w')
  137. def removeDir(subdir):
  138. if '..' in subdir or '/' in subdir:
  139. # Dangerous relative paths.
  140. return
  141. rmtree(join(resultPath, subdir))
  142. def mavenInstall(pomFile, jarFile = None, mvnCmd = mavenCmd, logFile = sys.stdout):
  143. cmd = [mvnCmd, "install:install-file"]
  144. cmd.append("-Dfile=%s" % (jarFile if jarFile is not None else pomFile))
  145. cmd.append("-DpomFile=%s" % (pomFile))
  146. print("executing: %s" % (" ".join(cmd)))
  147. subprocess.check_call(cmd, stdout=logFile)