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 6.1KB

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