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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. group.add_argument("--artifactPath", help="Path to local folder with Vaadin artifacts")
  23. parser.add_argument("--maven", help="Additional maven command line parameters", default=None)
  24. # Parse command line arguments <version>
  25. def parseArgs():
  26. # If no args, give help
  27. if len(sys.argv) == 1:
  28. args = parser.parse_args(["-h"])
  29. else:
  30. args = parser.parse_args()
  31. return args
  32. # Function for determining the path for maven executable
  33. def getMavenCommand():
  34. # This method uses .split("\n")[0] which basically chooses the first result where/which returns.
  35. # Fixes the case with multiple maven installations available on PATH
  36. if platform.system() == "Windows":
  37. try:
  38. return subprocess.check_output(["where", "mvn.cmd"], universal_newlines=True).split("\n")[0]
  39. except:
  40. try:
  41. return subprocess.check_output(["where", "mvn.bat"], universal_newlines=True).split("\n")[0]
  42. except:
  43. print("Unable to locate mvn with where. Is the maven executable in your PATH?")
  44. else:
  45. try:
  46. return subprocess.check_output(["which", "mvn"], universal_newlines=True).split("\n")[0]
  47. except:
  48. print("Unable to locate maven executable with which. Is the maven executable in your PATH?")
  49. return None
  50. mavenCmd = getMavenCommand()
  51. # Get command line arguments. Parses arguments if needed.
  52. def getArgs():
  53. global args
  54. if args is None:
  55. args = parseArgs()
  56. return args
  57. # Maven Package and Validation
  58. def mavenValidate(artifactId, mvnCmd = mavenCmd, logFile = sys.stdout, version = None, mavenParams = None):
  59. if version is None:
  60. version = getArgs().version
  61. if mavenParams is None:
  62. mavenParams = getArgs().maven
  63. print("Do maven clean package validate")
  64. cmd = [mvnCmd]
  65. cmd.append("-Dvaadin.version=%s" % (version))
  66. if mavenParams is not None:
  67. cmd.extend(mavenParams.strip('"').split(" "))
  68. cmd.extend(["clean", "package", "validate"])
  69. print("executing: %s" % (" ".join(cmd)))
  70. subprocess.check_call(cmd, cwd=join(resultPath, artifactId), stdout=logFile)
  71. # Collect .war files to given folder with given naming
  72. def copyWarFiles(artifactId, resultDir = resultPath, name = None):
  73. if name is None:
  74. name = artifactId
  75. copiedWars = []
  76. warFiles = glob(join(resultDir, artifactId, "target", "*.war"))
  77. warFiles.extend(glob(join(resultDir, 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, deployName))
  85. copiedWars.append(join(resultDir, deployName))
  86. return copiedWars
  87. def readPomFile(pomFile):
  88. # pom.xml namespace workaround
  89. root = ElementTree.parse(pomFile).getroot()
  90. nameSpace = root.tag[1:root.tag.index('}')]
  91. ElementTree.register_namespace('', nameSpace)
  92. # Read the pom.xml correctly
  93. return ElementTree.parse(pomFile), nameSpace
  94. # Recursive pom.xml update script
  95. def updateRepositories(path, repoUrl = None, version = None):
  96. # If versions are not supplied, parse arguments
  97. if version is None:
  98. version = getArgs().version
  99. # Read pom.xml
  100. pomXml = join(path, "pom.xml")
  101. if isfile(pomXml):
  102. # Read the pom.xml correctly
  103. tree, nameSpace = readPomFile(pomXml)
  104. # NameSpace needed for finding the repositories node
  105. repoNode = tree.getroot().find("{%s}repositories" % (nameSpace))
  106. else:
  107. return
  108. if repoNode is not None:
  109. print("Add staging repositories to " + pomXml)
  110. # Add framework staging repository
  111. addRepo(repoNode, "repository", "vaadin-%s-staging" % (version), repoUrl)
  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. # Add plugin staging repository
  118. addRepo(pluginRepo, "pluginRepository", "vaadin-%s-plugin-staging" % (version), repoUrl)
  119. # Overwrite the modified pom.xml
  120. tree.write(pomXml, encoding='UTF-8')
  121. # Recursive pom.xml search.
  122. for i in listdir(path):
  123. file = join(path, i)
  124. if isdir(file):
  125. updateRepositories(join(path, i), repoUrl, version)
  126. # Add a repository of repoType to given repoNode with id and URL
  127. def addRepo(repoNode, repoType, id, url):
  128. newRepo = ElementTree.SubElement(repoNode, repoType)
  129. idElem = ElementTree.SubElement(newRepo, "id")
  130. idElem.text = id
  131. urlElem = ElementTree.SubElement(newRepo, "url")
  132. urlElem.text = url
  133. # Get a logfile for given artifact
  134. def getLogFile(artifact, resultDir = resultPath):
  135. return open(join(resultDir, "%s.log" % (artifact)), 'w')
  136. def removeDir(subdir):
  137. if '..' in subdir or '/' in subdir:
  138. # Dangerous relative paths.
  139. return
  140. rmtree(join(resultPath, subdir))
  141. def mavenInstall(pomFile, jarFile = None, mvnCmd = mavenCmd, logFile = sys.stdout):
  142. cmd = [mvnCmd, "install:install-file"]
  143. cmd.append("-Dfile=%s" % (jarFile if jarFile is not None else pomFile))
  144. cmd.append("-DpomFile=%s" % (pomFile))
  145. print("executing: %s" % (" ".join(cmd)))
  146. subprocess.check_call(cmd, stdout=logFile)