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, getcwd, mkdir
  7. from shutil import copy, rmtree
  8. from glob import glob
  9. class VersionObject(object):
  10. pass
  11. # Staging repo base url
  12. repo = "http://oss.sonatype.org/content/repositories/comvaadin-%d"
  13. # Directory where the resulting war files are stored
  14. # TODO: deploy results
  15. resultPath = "result"
  16. if not exists(resultPath):
  17. mkdir(resultPath)
  18. elif not isdir(resultPath):
  19. print("Result path is not a directory.")
  20. sys.exit(1)
  21. args = None
  22. # Default argument parser
  23. parser = argparse.ArgumentParser(description="Automated staging validation")
  24. parser.add_argument("version", type=str, help="Vaadin version to use")
  25. parser.add_argument("--maven", help="Additional maven command line parameters", default=None)
  26. parser.add_argument("--artifactPath", help="Path to local folder with Vaadin artifacts", default=None)
  27. # Parse command line arguments <version>
  28. def parseArgs():
  29. # If no args, give help
  30. if len(sys.argv) == 1:
  31. args = parser.parse_args(["-h"])
  32. else:
  33. args = parser.parse_args()
  34. return args
  35. # Function for determining the path for maven executable
  36. def getMavenCommand():
  37. # This method uses .split("\n")[0] which basically chooses the first result where/which returns.
  38. # Fixes the case with multiple maven installations available on PATH
  39. if platform.system() == "Windows":
  40. try:
  41. return subprocess.check_output(["where", "mvn.cmd"], universal_newlines=True).split("\n")[0]
  42. except:
  43. try:
  44. return subprocess.check_output(["where", "mvn.bat"], universal_newlines=True).split("\n")[0]
  45. except:
  46. print("Unable to locate mvn with where. Is the maven executable in your PATH?")
  47. else:
  48. try:
  49. return subprocess.check_output(["which", "mvn"], universal_newlines=True).split("\n")[0]
  50. except:
  51. print("Unable to locate maven executable with which. Is the maven executable in your PATH?")
  52. return None
  53. mavenCmd = getMavenCommand()
  54. # Get command line arguments. Parses arguments if needed.
  55. def getArgs():
  56. global args
  57. if args is None:
  58. args = parseArgs()
  59. return args
  60. # Maven Package and Validation
  61. def mavenValidate(artifactId, mvnCmd = mavenCmd, logFile = sys.stdout, repoIds = None):
  62. if repoIds is None:
  63. repoIds = getArgs()
  64. print("Do maven clean package validate")
  65. cmd = [mvnCmd]
  66. if hasattr(repoIds, "version") and repoIds.version is not None:
  67. cmd.append("-Dvaadin.version=%s" % (repoIds.version))
  68. if hasattr(repoIds, "maven") and repoIds.maven is not None:
  69. cmd.extend(repoIds.maven.split(" "))
  70. cmd.extend(["clean", "package", "validate"])
  71. print("executing: %s" % (" ".join(cmd)))
  72. subprocess.check_call(cmd, cwd=join(getcwd(), artifactId), stdout=logFile)
  73. # Collect .war files to given folder with given naming
  74. def copyWarFiles(artifactId, resultDir = resultPath, name = None):
  75. if name is None:
  76. name = artifactId
  77. copiedWars = []
  78. warFiles = glob(join(getcwd(), artifactId, "target", "*.war"))
  79. warFiles.extend(glob(join(getcwd(), artifactId, "*", "target", "*.war")))
  80. for warFile in warFiles:
  81. if len(warFiles) == 1:
  82. deployName = "%s.war" % (name)
  83. else:
  84. deployName = "%s-%d.war" % (name, warFiles.index(warFile))
  85. print("Copying .war file %s as %s to result folder" % (basename(warFile), deployName))
  86. copy(warFile, join(resultDir, deployName))
  87. copiedWars.append(join(resultDir, deployName))
  88. return copiedWars
  89. # Recursive pom.xml update script
  90. def updateRepositories(path, repoIds = None, repoUrl = repo):
  91. # If versions are not supplied, parse arguments
  92. if repoIds is None:
  93. repoIds = getArgs()
  94. # Read pom.xml
  95. pomXml = join(path, "pom.xml")
  96. if isfile(pomXml):
  97. # pom.xml namespace workaround
  98. root = ElementTree.parse(pomXml).getroot()
  99. nameSpace = root.tag[1:root.tag.index('}')]
  100. ElementTree.register_namespace('', nameSpace)
  101. # Read the pom.xml correctly
  102. tree = ElementTree.parse(pomXml)
  103. # NameSpace needed for finding the repositories node
  104. repoNode = tree.getroot().find("{%s}repositories" % (nameSpace))
  105. else:
  106. return
  107. if repoNode is not None:
  108. print("Add staging repositories to " + pomXml)
  109. if hasattr(repoIds, "framework") and repoIds.framework is not None:
  110. # Add framework staging repository
  111. addRepo(repoNode, "repository", "vaadin-%s-staging" % (repoIds.version), repoUrl % (repoIds.framework))
  112. # Find the correct pluginRepositories node
  113. pluginRepo = tree.getroot().find("{%s}pluginRepositories" % (nameSpace))
  114. if pluginRepo is None:
  115. # Add pluginRepositories node if needed
  116. pluginRepo = ElementTree.SubElement(tree.getroot(), "pluginRepositories")
  117. if hasattr(repoIds, "plugin") and repoIds.plugin is not None:
  118. # Add plugin staging repository
  119. addRepo(pluginRepo, "pluginRepository", "vaadin-%s-plugin-staging" % (repoIds.version), repoUrl % (repoIds.plugin))
  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), repoIds, repoUrl)
  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(getcwd(), 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)