Improve meta gathering

This commit is contained in:
Dan Hawkins 2018-11-22 10:08:11 +00:00
commit d766ef7a14
3 changed files with 115 additions and 89 deletions

View file

@ -61,6 +61,14 @@ class GitInfo extends GISJsonSerialised {
this.remoteinfo = remoteinfo
}
def set(sourcepath, sourcerevision, gitrevision, remoteurl, remoteinfo) {
this.sourcepath = sourcepath
this.sourcerevision = sourcerevision
this.gitrevision = gitrevision
this.remoteurl = remoteurl
this.remoteinfo = remoteinfo
}
LinkedHashMap serialisationdata() {
def data = [
'sourcepath' : sourcepath,
@ -119,6 +127,13 @@ class BuildStampInfo extends GISJsonSerialised {
this.buildstampcommasep = buildstampcommasep
}
def set(String buildstampprefix, int revision, String buildstamp, String buildstampcommasep) {
this.buildstampprefix = buildstampprefix
this.revision = revision
this.buildstamp = buildstamp
this.buildstampcommasep = buildstampcommasep
}
LinkedHashMap serialisationdata() {
def data = [
'buildstampprefix' : buildstampprefix,

View file

@ -1,48 +1,112 @@
metafolder = load("${autobuilder.commonscripts()}/meta/folder.groovy")
VERSIONFOLDER = "${WORKSPACE}/${metafolder.METAFOLDERNAME}/version"
ddef getGitRevision(String gitrootpath, int basebuildnumber) {
dir( gitrootpath) {
def int MAXVERSIONNUMBER = 65535
def generate(meta, sourcerevision, sourcepath, basebuildnumber = "1000", versionfolder = "${VERSIONFOLDER}") {
def cs = autobuilder.commonscripts()
if (isUnix()) {
sh """
"${PYTHON}" -u "${cs}/versionnumbering/versioninfo.py" -s "${sourcerevision}" -v "${versionfolder}" -g "${sourcepath}" -b "${basebuildnumber}"
"""
def int changecount = sh(returnStdout: true, script: "${GIT_EXECUTABLE} rev-list HEAD --count")
echo "Change count: ${changecount}"
def int adjustedchangecount = (changecount + basebuildnumber) % (MAXVERSIONNUMBER + 1)
echo "Adjusted change count: ${adjustedchangecount}"
def gitrevision = sh(returnStdout: true, script: "${GIT_EXECUTABLE} rev-parse HEAD")
echo "Revision: ${gitrevision}"
def remoteinfo = sh(returnStdout: true, script: "${GIT_EXECUTABLE} remote show origin")
echo "Remote info: ${remoteinfo}"
def remoteurl = sh(returnStdout: true, script: "${GIT_EXECUTABLE} remote get-url --push origin")
echo "Remote URL: ${remoteurl}"
}
else {
bat """
"${PYTHON}" -u "${cs}\\versionnumbering\\versioninfo.py" -s "${sourcerevision}" -v "${versionfolder}" -g "${sourcepath}" -b "${basebuildnumber}"
"""
}
env.BUILD_STAMP = readFile("${VERSIONFOLDER}/buildstamp.txt").trim()
env.BUILD_STAMP_COMMA_SEP = readFile("${VERSIONFOLDER}/buildstampcommasep.txt").trim()
env.SRC_REVISION = readFile("${VERSIONFOLDER}/revision.txt").trim()
return [adjustedchangecount, gitrevision, remoteinfo, remoteurl]
}
return this;
// Handle:
// origin/master
// origin/2.0.x
def getVersionPrefix(String sourcebranch) {
echo "Determining version number prefix from source branch name: ${sourcebranch}"
sourcebranch = sourcebranch.toLowerCase()
def defaultversionprefix = "1.30000.0."
def splits = sourcebranch.split('/')
if (splits.size() < 1) {
echo "Source branch name is invalid. Defaulting to: ${defaultversionprefix}"
return defaultversionprefix
}
/*
with open(os.path.join(versionfolder, "prefix.txt"), 'w') as f:
f.write(versionprefix)
def mastertypeversionprefix = "1.20000.0."
sourcebranch = splits.last()
if (sourcebranch.toLowerCase().find("master") != null) {
echo "Source branch name has 'master' in it. Defaulting to: ${mastertypeversionprefix}"
return mastertypeversionprefix
}
with open(os.path.join(versionfolder, "revision.txt"), 'w') as f:
f.write(str(gitrevisioncount))
echo "sourcebranch is now: ${sourcebranch}"
def splits2 = sourcebranch.split('\\.')
echo "Splits2 is: ${splits2}"
if (splits2.size() < 1) {
def thirdpartyversionprefix = "1.25000.0."
echo "Source branch name has no dots in it (eg. 1.9.2). Defaulting to: ${thirdpartyversionprefix}"
return thirdpartyversionprefix
}
splits = []
for (split in splits2) {
echo "Split is: ${split}"
if (split.isInteger()) {
splits.add(split)
}
else if (split == "x") {
return mastertypeversionprefix
}
}
echo "Splits is: ${splits}"
with open(os.path.join(versionfolder, "gitrevision.txt"), 'w') as f:
f.write(gitrevision)
def versionprefix = ""
if (splits.size() < 1) {
echo "This source branch name has no numbers in its name so defaulting to: ${defaultversionprefix}"
versionprefix = defaultversionprefix
}
else {
def int splitnumbercount = 0
for (split in splits) {
echo "Split is ${split}"
if (split.isInteger()) {
versionprefix += split + "."
splitnumbercount += 1
}
else if (splitnumbercount > 2) {
break
}
}
if (splitnumbercount == 0) {
echo "No leading numbers were found in source branch name so defaulting to: ${defaultversionprefix}"
versionprefix = defaultversionprefix
}
else {
if (splitnumbercount == 1) {
versionprefix += "0.0."
}
else if (splitnumbercount == 2) {
versionprefix += "0."
}
}
}
echo "Determined version prefix to be: ${versionprefix}"
return versionprefix
}
with open(os.path.join(versionfolder, "remoteinfo.txt"), 'w') as f:
f.write(remoteinfo)
String generate(meta, String sourcerevision, String sourcepath, int basebuildnumber = 1000) {
(gitrevisioncount, gitrevision, remoteinfo, remoteurl) = getGitRevision(sourcepath, basebuildnumber)
with open(os.path.join(versionfolder, "remoteurl.txt"), 'w') as f:
f.write(remoteurl)
def versionprefix = getVersionPrefix(sourcerevision)
with open(os.path.join(versionfolder, "buildstamp.txt"), 'w') as f:
f.write(buildstamp)
def buildstamp = versionprefix + gitrevisioncount.toString()
echo "Build stamp is: ${buildstamp}"
buildstampcommasep = buildstamp.replace(".", ",")
with open(os.path.join(versionfolder, "buildstampcommasep.txt"), 'w') as f:
f.write(buildstampcommasep)
*/
meta.gitinfo.set(sourcepath, sourcerevision, gitrevision, remoteurl, remoteinfo)
meta.buildstampinfo.set(versionprefix, gitrevisioncount, buildstamp, buildstamp.replace(".", ","))
return buildstamp
}
return this;

View file

@ -23,57 +23,6 @@ __version__ = 0.1
__date__ = '2017-11-29'
__updated__ = '2017-11-29'
gitExecutable = os.getenv("GIT_EXECUTABLE", default="git")
MAXVERSIONNUMBER = 65535
def resetFolder(versionfolder):
print("Removing folders and contents of: {0}".format(versionfolder))
try:
shutil.rmtree(versionfolder)
except FileNotFoundError:
pass
print("Creating folder: {0}".format(versionfolder))
os.makedirs(versionfolder)
def getGitRevision(gitrootpath, basenumber):
cwd = os.getcwd()
path = os.path.expanduser(gitrootpath)
print("Changing current working directory to: {0}".format(path))
os.chdir(path)
print("Determining revision info using git command: {0}".format(gitExecutable))
completed = subprocess.run([gitExecutable, "rev-list", "HEAD", "--count"], stdout=subprocess.PIPE)
print("Command {0} returned: {1}".format(completed.args, completed.returncode))
changecount = completed.stdout.decode("utf-8").strip()
print("Change count: {0}".format(changecount))
adjustedchangecount = int((int(changecount) + int(basenumber)) % (MAXVERSIONNUMBER + 1))
print("Adjusted change count: {0}".format(adjustedchangecount))
print("Determining current revision info using git command: {0}".format(gitExecutable))
completed = subprocess.run([gitExecutable, "rev-parse", "HEAD"], stdout=subprocess.PIPE)
print("Command {0} returned: {1}".format(completed.args, completed.returncode))
revision = completed.stdout.decode("utf-8").strip()
print("Revision: {0}".format(revision))
print("Determining remote info using git command: {0}".format(gitExecutable))
completed = subprocess.run([gitExecutable, "remote", "show", "origin"], stdout=subprocess.PIPE)
print("Command {0} returned: {1}".format(completed.args, completed.returncode))
remoteinfo = completed.stdout.decode("utf-8")
print("Remote info: {0}".format(remoteinfo))
print("Determining remote url using git command: {0}".format(gitExecutable))
completed = subprocess.run([gitExecutable, "remote", "get-url", "--push", "origin"], stdout=subprocess.PIPE)
print("Command {0} returned: {1}".format(completed.args, completed.returncode))
remoteurl = completed.stdout.decode("utf-8")
print("Remote info: {0}".format(remoteurl))
os.chdir(cwd)
print("Restored current working directory to: {0}".format(cwd))
return (adjustedchangecount, revision, remoteinfo, remoteurl)
# Handle:
# origin/master
# origin/2.0.x
@ -207,8 +156,6 @@ USAGE
print("Source branch:", sourcebranch, "version folder:", versionfolder, "Git root:", gitrootfolder)
print("Base version number:", baseversionnumber)
resetFolder(versionfolder)
(gitrevisioncount, gitrevision, remoteinfo, remoteurl) = getGitRevision(gitrootfolder, baseversionnumber)
versionprefix = getVersionPrefix(sourcebranch)