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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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
  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. # Parse command line arguments <version> <framework-repo-id> <archetype-repo-id> <plugin-repo-id>
  23. def parseArgs():
  24. # Command line arguments for this script
  25. parser = argparse.ArgumentParser(description="Automated staging validation")
  26. parser.add_argument("version", type=str, help="Vaadin version to use")
  27. parser.add_argument("framework", type=int, help="Framework repo id (comvaadin-XXXX)", nargs='?')
  28. parser.add_argument("archetype", type=int, help="Archetype repo id (comvaadin-XXXX)", nargs='?')
  29. parser.add_argument("plugin", type=int, help="Maven Plugin repo id (comvaadin-XXXX)", nargs='?')
  30. # If no args, give help
  31. if len(sys.argv) == 1:
  32. args = parser.parse_args(["-h"])
  33. else:
  34. args = parser.parse_args()
  35. return args
  36. # Function for determining the path for maven executable
  37. def getMavenCommand():
  38. # This method uses .split("\n")[0] which basically chooses the first result where/which returns.
  39. # Fixes the case with multiple maven installations available on PATH
  40. if platform.system() == "Windows":
  41. try:
  42. return subprocess.check_output(["where", "mvn.cmd"], universal_newlines=True).split("\n")[0]
  43. except:
  44. try:
  45. return subprocess.check_output(["where", "mvn.bat"], universal_newlines=True).split("\n")[0]
  46. except:
  47. print("Unable to locate mvn with where. Is the maven executable in your PATH?")
  48. else:
  49. try:
  50. return subprocess.check_output(["which", "mvn"], universal_newlines=True).split("\n")[0]
  51. except:
  52. print("Unable to locate maven executable with which. Is the maven executable in your PATH?")
  53. return None
  54. mavenCmd = getMavenCommand()
  55. # Get command line arguments. Parses arguments if needed.
  56. def getArgs():
  57. global args
  58. if args is None:
  59. args = parseArgs()
  60. return args
  61. # Maven Package and Validation
  62. def mavenValidate(artifactId, mvnCmd = mavenCmd, logFile = sys.stdout, repoIds = None):
  63. if repoIds is None:
  64. repoIds = getArgs()
  65. print("Do maven clean package validate")
  66. cmd = [mvnCmd]
  67. if hasattr(repoIds, "version") and repoIds.version is not None:
  68. cmd.append("-Dvaadin.version=%s" % (repoIds.version))
  69. cmd.extend(["clean", "package", "validate"])
  70. print("executing: %s" % (" ".join(cmd)))
  71. subprocess.check_call(cmd, cwd=join(getcwd(), 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. warFiles = glob(join(getcwd(), artifactId, "target", "*.war"))
  77. warFiles.extend(glob(join(getcwd(), artifactId, "*", "target", "*.war")))
  78. for warFile in warFiles:
  79. if len(warFiles) == 1:
  80. deployName = "%s.war" % (name)
  81. else:
  82. deployName = "%s-%d.war" % (name, warFiles.index(warFile))
  83. print("Copying .war file %s as %s to result folder" % (basename(warFile), deployName))
  84. copy(warFile, join(resultDir, "%s" % (deployName)))
  85. # Recursive pom.xml update script
  86. def updateRepositories(path, repoIds = None, repoUrl = repo):
  87. # If versions are not supplied, parse arguments
  88. if repoIds is None:
  89. repoIds = getArgs()
  90. # Read pom.xml
  91. pomXml = join(path, "pom.xml")
  92. if isfile(pomXml):
  93. # pom.xml namespace workaround
  94. root = ElementTree.parse(pomXml).getroot()
  95. nameSpace = root.tag[1:root.tag.index('}')]
  96. ElementTree.register_namespace('', nameSpace)
  97. # Read the pom.xml correctly
  98. tree = ElementTree.parse(pomXml)
  99. # NameSpace needed for finding the repositories node
  100. repoNode = tree.getroot().find("{%s}repositories" % (nameSpace))
  101. else:
  102. return
  103. if repoNode is not None:
  104. print("Add staging repositories to " + pomXml)
  105. if hasattr(repoIds, "framework") and repoIds.framework is not None:
  106. # Add framework staging repository
  107. addRepo(repoNode, "repository", "vaadin-%s-staging" % (repoIds.version), repoUrl % (repoIds.framework))
  108. # Find the correct pluginRepositories node
  109. pluginRepo = tree.getroot().find("{%s}pluginRepositories" % (nameSpace))
  110. if pluginRepo is None:
  111. # Add pluginRepositories node if needed
  112. pluginRepo = ElementTree.SubElement(tree.getroot(), "pluginRepositories")
  113. if hasattr(repoIds, "plugin") and repoIds.plugin is not None:
  114. # Add plugin staging repository
  115. addRepo(pluginRepo, "pluginRepository", "vaadin-%s-plugin-staging" % (repoIds.version), repoUrl % (repoIds.plugin))
  116. # Overwrite the modified pom.xml
  117. tree.write(pomXml, encoding='UTF-8')
  118. # Recursive pom.xml search.
  119. for i in listdir(path):
  120. file = join(path, i)
  121. if isdir(file):
  122. updateRepositories(join(path, i), repoIds, repoUrl)
  123. # Add a repository of repoType to given repoNode with id and URL
  124. def addRepo(repoNode, repoType, id, url):
  125. newRepo = ElementTree.SubElement(repoNode, repoType)
  126. idElem = ElementTree.SubElement(newRepo, "id")
  127. idElem.text = id
  128. urlElem = ElementTree.SubElement(newRepo, "url")
  129. urlElem.text = url
  130. # Get a logfile for given artifact
  131. def getLogFile(artifact, resultDir = resultPath):
  132. return open(join(resultDir, "%s.log" % (artifact)), 'w')