import java.nio.file.Paths import java.nio.file.Path import groovy.json.JsonSlurperClassic abstract class GISJsonSerialised implements Serializable { def script GISJsonSerialised(script) { this.script = script } abstract LinkedHashMap serialisationdata() abstract void deserialiseJson(rawmeta) def saveJsonSerialised(String outputfile, int prettylevel = 4) { def outJson = script.readJSON text: groovy.json.JsonOutput.toJson(serialisationdata()) script.writeJSON(file: outputfile, json: outJson, pretty: prettylevel) } } class ProductInfo extends GISJsonSerialised { private String format private String productname private String subcomponentname private String variant private String uniqueid ProductInfo(script, String productname = '', String subcomponentname = '', String variant = '') { super(script) this.format = '0.1' this.productname = productname this.subcomponentname = subcomponentname this.variant = variant this.uniqueid = UUID.randomUUID().toString() } void set(String productname, String subcomponentname, String variant = '') { // Make sure the fields can be used in git tags def int pstatus = 0 if (script.isUnix()) { pstatus = script.sh(script: "git check-ref-format --normalize \"refs/tags/${productname}\" >/dev/null 2>/dev/null", , returnStatus: true) } else { pstatus = script.powershell(script: "git check-ref-format --normalize \"refs/tags/${productname}\" 2>&1>\$null", , returnStatus: true) } if (pstatus > 0) { script.error("The product name: ${productname} needs to be renamed as it cannot be used in a git tag name") } def int sstatus = 0 if (script.isUnix()) { sstatus = script.sh(script: "git check-ref-format --normalize \"refs/tags/${subcomponentname}\" >/dev/null 2>/dev/null", , returnStatus: true) } else { sstatus = script.powershell(script: "git check-ref-format --normalize \"refs/tags/${subcomponentname}\" 2>&1>\$null", , returnStatus: true) } if (sstatus > 0) { script.error("The subcomponent name: ${subcomponentname} needs to be renamed as it cannot be used in a git tag name") } if (variant) { def int vstatus = 0 if (script.isUnix()) { vstatus = script.sh(script: "git check-ref-format --normalize \"refs/tags/${variant}\" >/dev/null 2>/dev/null", , returnStatus: true) } else { vstatus = script.powershell(script: "git check-ref-format --normalize \"refs/tags/${variant}\" 2>&1>\$null", , returnStatus: true) } if (vstatus > 0) { script.error("The subcomponent name: ${variant} needs to be renamed as it cannot be used in a git tag name") } } this.productname = productname this.subcomponentname = subcomponentname this.variant = variant } void set(ProductInfo otherinfo) { this.productname = otherinfo.productname this.subcomponentname = otherinfo.subcomponentname this.variant = otherinfo.variant this.uniqueid = otherinfo.uniqueid } void deserialiseJson(rawmeta) { //format = rawmeta['format'] productname = rawmeta['productname'] subcomponentname = rawmeta['subcomponentname'] variant = rawmeta['variant'] ?: '' uniqueid = rawmeta['uniqueid'] ?: uniqueid } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'productname' : productname, 'subcomponentname' : subcomponentname, 'variant' : variant, 'uniqueid': uniqueid, ] return data } } class GitInfo extends GISJsonSerialised { private String format private String sourcepath private String sourcerevision private String gitrevision private String remoteurl private String remoteinfo GitInfo(script, sourcepath = '', sourcerevision = '', gitrevision = '', remoteurl = '', remoteinfo = '') { super(script) this.format = '0.1' this.sourcepath = sourcepath this.sourcerevision = sourcerevision this.gitrevision = gitrevision this.remoteurl = remoteurl this.remoteinfo = remoteinfo } GitInfo(GitInfo another) { super(another.script) this.sourcepath = another.sourcepath this.sourcerevision = another.sourcerevision this.gitrevision = another.gitrevision this.remoteurl = another.remoteurl this.remoteinfo = another.remoteinfo } void set(String sourcepath, String sourcerevision, String gitrevision, String remoteurl, String remoteinfo) { this.sourcepath = sourcepath this.sourcerevision = sourcerevision this.gitrevision = gitrevision this.remoteurl = remoteurl this.remoteinfo = remoteinfo } void set(GitInfo otherinfo) { this.sourcepath = otherinfo.sourcepath this.sourcerevision = otherinfo.sourcerevision this.gitrevision = otherinfo.gitrevision this.remoteurl = otherinfo.remoteurl this.remoteinfo = otherinfo.remoteinfo } void deserialiseJson(rawmeta) { //format = rawmeta['format'] sourcepath = rawmeta['sourcepath'] sourcerevision = rawmeta['sourcerevision'] gitrevision = rawmeta['gitrevision'] remoteurl = rawmeta['remoteurl'] remoteinfo = rawmeta['remoteinfo'] } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'sourcepath' : sourcepath, 'sourcerevision' : sourcerevision, 'gitrevision' : gitrevision, 'remoteurl' : remoteurl, 'remoteinfo' : remoteinfo, ] return data } } class JenkinsBuildParameter extends GISJsonSerialised { private String name private String valuetype private String value JenkinsBuildParameter(script, String name = '', String valuetype = '', String value = '') { super(script) this.name = name this.valuetype = valuetype this.value = value } void set(String name, String valuetype, String value) { this.name = name this.valuetype = valuetype this.value = value } void deserialiseJson(rawmeta) { name = rawmeta['name'] valuetype = rawmeta['valuetype'] value = rawmeta['value'] } LinkedHashMap serialisationdata() { def data = [ 'name' : name, 'valuetype' : valuetype, 'value' : value, ] return data } } class JenkinsBuildInfo extends GISJsonSerialised { private String format private String buildjobname private int buildnumber private String buildmachine private String builddate private ArrayList parameters private boolean isfullbuild JenkinsBuildInfo(script, String buildjobname = '', int buildnumber = 0, String buildmachine = '', String builddate = '', boolean isfullbuild = true) { super(script) this.format = '0.1' this.buildjobname = buildjobname this.buildnumber = buildnumber this.buildmachine = buildmachine this.builddate = builddate this.isfullbuild = isfullbuild parameters = new ArrayList() } void set(String buildjobname, int buildnumber, String buildmachine, String builddate, boolean isfullbuild = true) { this.buildjobname = buildjobname this.buildnumber = buildnumber this.buildmachine = buildmachine this.builddate = builddate this.isfullbuild = isfullbuild } void set(boolean isfullbuild) { this.isfullbuild = isfullbuild } boolean isFullBuild() { return isfullbuild } void addParameter(String name, value) { def p = new JenkinsBuildParameter(script, name, value.getClass().toString(), value.toString()) parameters.add(p) return p } void deserialiseJson(rawmeta) { //format = rawmeta['format'] buildjobname = rawmeta['buildjobname'] buildnumber = rawmeta['buildnumber'] buildmachine = rawmeta['buildmachine'] builddate = rawmeta['builddate'] isfullbuild = (rawmeta['isfullbuild'] == null) ? true : rawmeta['isfullbuild'] parameters = new ArrayList() if (rawmeta['isfullbuild'] != null) { for (rawparameter in rawmeta['parameters']) { def p = new JenkinsBuildParameter(script) p.deserialiseJson(rawparameter) parameters.add(p) } } } List parametersserialised() { def associated = [] for (p in parameters) { associated.add(p.serialisationdata()) } return associated } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'buildjobname' : buildjobname, 'buildnumber' : buildnumber, 'buildmachine' : buildmachine, 'builddate' : builddate, 'isfullbuild' : isfullbuild, 'parameters' : parametersserialised(), ] return data } } class BuildStampInfo extends GISJsonSerialised { private String format private String buildstampprefix private int revision private String buildstamp BuildStampInfo(script, buildstampprefix = "0.30000.0.", revision = 0, buildstamp = "0.30000.0.0") { super(script) this.format = '0.1' this.buildstampprefix = buildstampprefix this.revision = revision this.buildstamp = buildstamp } BuildStampInfo(BuildStampInfo another) { super(another.script) this.buildstampprefix = another.buildstampprefix this.revision = another.revision this.buildstamp = another.buildstamp } void set(String buildstampprefix, int revision, String buildstamp) { this.buildstampprefix = buildstampprefix this.revision = revision this.buildstamp = buildstamp } void set(BuildStampInfo other) { set(other.buildstampprefix, other.revision, other.buildstamp) } // Override the buildstamp void override(String buildstamp) { this.buildstamp = buildstamp script.echo("Build stamp overridden to: ${this.buildstamp}") def s = this.buildstamp.split('\\.') if (s.size() != 4) { script.error("Overriding build stamp: ${buildstamp} is not of the format: 1.2.3.4") } this.buildstampprefix = "${s[0]}.${s[1]}.${s[2]}" script.echo("Stamp prefix overridden to: ${this.buildstampprefix}") this.revision = s[3].toInteger() script.echo("Revision overridden to: ${this.revision}") } String buildstampcommasep() { def String buildstampcommasep = buildstamp.replace(".", ",") return buildstampcommasep } void changerevision(int newrevision) { script.echo("Build stamp currently ${buildstamp}") revision = newrevision def List buildstampsplit = buildstamp.split('\\.') buildstampsplit[-1] = newrevision.toString() buildstamp = buildstampsplit.join('.') script.echo("Build stamp now ${buildstamp}") } void set(String buildstampprefix, int revision) { buildstampprefix += buildstampprefix.endsWith('.') ? '' : '.' this.buildstampprefix = buildstampprefix this.buildstamp = buildstampprefix + "0" changerevision(revision) } void set(String buildstampprefix, String revision = '0') { set(buildstampprefix, revision.toInteger()) } void deserialiseJson(rawmeta) { //format = rawmeta['format'] buildstampprefix = rawmeta['buildstampprefix'] revision = rawmeta['revision'] buildstamp = rawmeta['buildstamp'] } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'buildstampprefix' : buildstampprefix, 'revision' : revision, 'buildstamp' : buildstamp, ] return data } } class ReleaseInfo extends GISJsonSerialised { private String format private String releaserepoURL private ArrayList releaseitems private boolean released ReleaseInfo(script, String releaserepoURL = '', boolean released = false) { super(script) this.format = '0.1' this.releaserepoURL = releaserepoURL this.releaseitems = new ArrayList() this.released = released } void set(String releaserepoURL = '') { this.releaserepoURL = releaserepoURL } void addReleaseItem(String releaseitem) { script.echo("Adding release item: ${releaseitem} to release items") releaseitems.add(releaseitem) } void markreleased() { this.released = true } List releaseitemssserialised() { def items = [] for (ri in releaseitems) { items.add(ri) } return items } void deserialiseJson(rawmeta) { //format = rawmeta['format'] releaserepoURL = rawmeta['releaserepoURL'] releaseitems = new ArrayList() for (rawreleaseitem in rawmeta['releaseitems']) { releaseitems.add(rawreleaseitem) } released = rawmeta['released'] ?: false } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'releaserepoURL' : releaserepoURL, 'releaseitems' : releaseitemssserialised(), 'released' : released ] return data } } class ConfigurationInfo extends GISJsonSerialised { private String configurationname private String configurationURL private String configurationversion private String configurationSHA ConfigurationInfo(script, configurationname = '', configurationURL = '', configurationversion = '', configurationSHA = '') { super(script) this.configurationname = configurationname this.configurationURL = configurationURL this.configurationversion = configurationversion this.configurationSHA = configurationSHA } void deserialiseJson(rawmeta) { configurationname = rawmeta['configurationname'] configurationURL = rawmeta['configurationURL'] configurationversion = rawmeta['configurationversion'] configurationSHA = rawmeta['configurationSHA'] } LinkedHashMap serialisationdata() { def data = [ 'configurationname' : configurationname, 'configurationURL' : configurationURL, 'configurationversion' : configurationversion, 'configurationSHA' : configurationSHA, ] return data } } class ChecksumPair extends GISJsonSerialised { private String filepath private String sum ChecksumPair(script, String filepath = '', String sum = '') { super(script) this.filepath = filepath this.sum = sum } void deserialiseJson(rawmeta) { filepath = rawmeta['filepath'] sum = rawmeta['sum'] } LinkedHashMap serialisationdata() { def data = [ 'filepath' : filepath, 'sum' : sum, ] return data } } class ChecksumSet extends GISJsonSerialised { private String format private String artifactpath private ArrayList checksumpairs ChecksumSet(script, artifactpath = '') { super(script) this.format = '0.1' this.artifactpath = artifactpath checksumpairs = new ArrayList() } ChecksumPair addChecksumPair(String filepath, String sum) { def cp = new ChecksumPair(script, filepath, sum) checksumpairs.add(cp) return cp } List checksumpairsserialised() { def associated = [] for (cp in checksumpairs) { associated.add(cp.serialisationdata()) } return associated } void deserialiseJson(rawmeta) { //format = rawmeta['format'] artifactpath = rawmeta['artifactpath'] checksumpairs = new ArrayList() for (rawchecksumpair in rawmeta['checksumpairs']) { def csp = new ChecksumPair(script) csp.deserialiseJson(rawchecksumpair) checksumpairs.add(csp) } } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'artifactpath' : artifactpath, 'checksumpairs' : checksumpairsserialised(), ] return data } } class ChecksumInfo extends GISJsonSerialised { private ArrayList checksumsets ChecksumInfo(script) { super(script) checksumsets = new ArrayList() } ChecksumSet addChecksumSet(String artifactpath) { def cs = new ChecksumSet(script, artifactpath) checksumsets.add(cs) return cs } List checksumsetsserialised() { def associated = [] for (cs in checksumsets) { associated.add(cs.serialisationdata()) } return associated } void deserialiseJson(rawmeta) { checksumsets = new ArrayList() for (rawchecksumset in rawmeta['checksumsets']) { def cs = new ChecksumSet(script) cs.deserialiseJson(rawchecksumset) checksumsets.add(cs) } } LinkedHashMap serialisationdata() { def data = [ 'checksumsets' : checksumsetsserialised(), ] return data } } class PublishableInfo extends GISJsonSerialised { private String format private String filepath PublishableInfo(script, String filepath = '') { super(script) this.format = '0.1' this.filepath = filepath } void deserialiseJson(rawmeta) { //format = rawmeta['format'] filepath = rawmeta['filepath'] } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'filepath' : filepath, ] return data } } class MetaInfo extends GISJsonSerialised { private static String METAFOLDERNAME = 'meta' private static String METAFILENAME = 'info.json' private static String METAARTIFACTS = "${METAFOLDERNAME}/**" private static String format = '0.2' abstract class RelatedMetaInfo extends GISJsonSerialised { String format RelatedMetaInfo(script, format) { super(script) this.format = format } abstract void deserialiseJson(rawmeta) abstract LinkedHashMap serialisationdata() abstract String getrelatedid() abstract void markreleased() } class RelatedMetaInfo_v0_1 extends RelatedMetaInfo { private MetaInfo meta private String offsetpath private String uniqueid RelatedMetaInfo_v0_1(script, String offsetpath = '', MetaInfo meta = null, String uniqueid = UUID.randomUUID().toString()) { super(script, '0.1') this.offsetpath = offsetpath this.uniqueid = uniqueid this.meta = meta } void deserialiseJson(rawmeta) { offsetpath = rawmeta['offsetpath'] uniqueid = rawmeta['uniqueid'] ?: uniqueid meta = new MetaInfo(script) meta.deserialiseJson(rawmeta['meta']) } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'offsetpath' : offsetpath, 'uniqueid' : uniqueid, 'meta' : meta.serialisationdata(), ] return data } String getrelatedid() { return uniqueid } void markreleased() { meta.releaseinfo.markreleased() } } class RelatedMetaInfo_v0_2 extends RelatedMetaInfo { private String importpath private String offsetpath private String uniqueid RelatedMetaInfo_v0_2(script, String importpath = '', String offsetpath = '', String uniqueid = UUID.randomUUID().toString()) { super(script, '0.2') this.importpath = importpath this.offsetpath = offsetpath this.uniqueid = uniqueid } void deserialiseJson(rawmeta) { //format = rawmeta['format'] offsetpath = rawmeta['offsetpath'] uniqueid = rawmeta['uniqueid'] } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'offsetpath' : offsetpath, 'uniqueid' : uniqueid ] return data } String getrelatedid() { return uniqueid } void markreleased() { // Do nothing } } private ProductInfo productinfo private GitInfo gitinfo private JenkinsBuildInfo jenkinsbuildinfo private BuildStampInfo buildstampinfo private ReleaseInfo releaseinfo private ChecksumInfo checksuminfo private ArrayList configurationinfo private ArrayList publishableinfo private LinkedHashMap custominfo private ArrayList associatedmeta MetaInfo(script) { super(script) productinfo = new ProductInfo(script) gitinfo = new GitInfo(script) jenkinsbuildinfo = new JenkinsBuildInfo(script) buildstampinfo = new BuildStampInfo(script) releaseinfo = new ReleaseInfo(script) checksuminfo = new ChecksumInfo(script) configurationinfo = new ArrayList() publishableinfo = new ArrayList() custominfo = new LinkedHashMap() associatedmeta = new ArrayList() } void markreleased() { script.echo("Marking product: ${productinfo.productname} component: ${productinfo.subcomponentname} as released") releaseinfo.markreleased() for (associated in associatedmeta) { associated.markreleased() } } List configurationinfoserialised() { def configuration = [] for (configurationitem in configurationinfo) { configuration.add(configurationitem.serialisationdata()) } return configuration } void addPublishable(String filepath) { def PublishableInfo pi = new PublishableInfo(script, filepath) script.echo("Adding ${filepath} to publishable list") publishableinfo.add(pi) } void addConfigurationInfo(String configurationname, String configurationURL, String configurationversion, String configurationSHA) { def ConfigurationInfo ci = new ConfigurationInfo(script, configurationname, configurationURL, configurationversion, configurationSHA) script.echo("Adding configuration info: name: ${configurationname} url: ${configurationURL} version: ${configurationversion} sha: ${configurationSHA}") configurationinfo.add(ci) return ci } void addCustomInfo(String customId, String customValue) { custominfo.put(customId.trim(), customValue) } String getCustomInfo(String customId) { return custominfo.getAt(customId.trim()) } def isFullBuild() { return jenkinsbuildinfo.isFullBuild() } List publishableinfoserialised() { def publishable = [] for (publishableitem in publishableinfo) { publishable.add(publishableitem.serialisationdata()) } return publishable } List associatedmetaserialised() { def associated = [] for (metaitem in associatedmeta) { associated.add(metaitem.serialisationdata()) } return associated } void deserialiseJson(rawmeta) { if (rawmeta['format'].toString() != '0.1' && rawmeta['format'].toString() != '0.2' ) { script.error("Unsupported meta format found") return } productinfo.deserialiseJson(rawmeta['productinfo']) gitinfo.deserialiseJson(rawmeta['gitinfo']) jenkinsbuildinfo.deserialiseJson(rawmeta['jenkinsbuildinfo']) buildstampinfo.deserialiseJson(rawmeta['buildstampinfo']) releaseinfo.deserialiseJson(rawmeta['releaseinfo']) checksuminfo.deserialiseJson(rawmeta['checksuminfo']) for (rawconfig in rawmeta['configuration']) { def cf = new ConfigurationInfo(script) cf.deserialiseJson(rawconfig) configurationinfo.add(cf) } for (rawpublishable in rawmeta['publishableinfo']) { def pb = new PublishableInfo(script) pb.deserialiseJson(rawpublishable) publishableinfo.add(pb) } custominfo = rawmeta['custominfo'] for (rawrelatedmeta in rawmeta['associatedmeta']) { if (!rawrelatedmeta['format'] || rawrelatedmeta['format'] == '0.1') { def rm = new RelatedMetaInfo_v0_1(script) rm.deserialiseJson(rawrelatedmeta) associatedmeta.add(rm) } else if (rawrelatedmeta['format'] == '0.2') { def rm = new RelatedMetaInfo_v0_2(script) rm.deserialiseJson(rawrelatedmeta) associatedmeta.add(rm) } } } def deserialiseJson(String filepath) { script.echo("Deserialising JSON at file: ${filepath}") def rawmeta = script.readJSON(file: filepath) deserialiseJson(rawmeta) } def deserialiseJsonSlurper(String filepath) { File jsonfile = new File(filepath) def jsonfiletext = jsonfile.getText() def rawmeta = new JsonSlurperClassic().parseText(jsonfiletext) deserialiseJson(rawmeta) } String relativize(String mypath) { String pathRelative script.echo("Seeing if ${mypath} begins with ${script.WORKSPACE}") if (mypath.startsWith(script.WORKSPACE)) { pathRelative = mypath.minus(script.WORKSPACE) script.echo("Extracted ${pathRelative} from ${script.WORKSPACE} and ${mypath}") } else { pathRelative = mypath script.echo("Path relative is unchanged: ${pathRelative}") } // Convert \ with / pathRelative = pathRelative.tokenize('\\').join('/') // Strip leading / if any pathRelative = pathRelative.replaceAll('^/+', '') script.echo("Path relative is ${pathRelative}") return pathRelative } static String metafilepath() { return METAFOLDERNAME + "/" + METAFILENAME } MetaInfo importrelated(boolean appendtoparentmeta, String destpath, String givenRepo = 'Unknown', String givenRevision = 'Unknown', String givenSHA = 'Unknown') { MetaInfo mi = new MetaInfo(script) def String adjustedpath = relativize(destpath) def importedmetafile = adjustedpath + "/" + metafilepath() if (script.fileExists(importedmetafile)) { script.echo("Deserialising meta at file: ${importedmetafile} (destpath: ${destpath})") mi.deserialiseJson(importedmetafile) RelatedMetaInfo rmi = new RelatedMetaInfo_v0_2(script, importedmetafile, adjustedpath, mi.productinfo.uniqueid) if (appendtoparentmeta) { associatedmeta.add(rmi) } } else { script.echo("Could not deserialise meta at file: ${importedmetafile} (destpath: ${destpath})- Does not exist") addConfigurationInfo('', givenRepo, givenRevision, givenSHA) } return mi } def copyrelatedmeta() { script.echo("Copying related input meta data to output meta") for (relatedmetainfo in associatedmeta) { def srcpath = "${relatedmetainfo.offsetpath}/${METAFOLDERNAME}" def destpath = "${METAFOLDERNAME}/${relatedmetainfo.offsetpath}" script.echo("Copying related meta folder: ${srcpath} to meta output folder: ${destpath} recursively") script.fileOperations([ script.folderCreateOperation(folderPath: destpath), script.folderCopyOperation(sourceFolderPath: srcpath, destinationFolderPath: destpath) ]) } } LinkedHashMap serialisationdata() { def data = [ 'format' : format, 'productinfo' : productinfo.serialisationdata(), 'gitinfo' : gitinfo.serialisationdata(), 'jenkinsbuildinfo' : jenkinsbuildinfo.serialisationdata(), 'buildstampinfo' : buildstampinfo.serialisationdata(), 'releaseinfo' : releaseinfo.serialisationdata(), 'configuration' : configurationinfoserialised(), 'checksuminfo' : checksuminfo.serialisationdata(), 'publishableinfo' : publishableinfoserialised(), 'custominfo' : custominfo, 'associatedmeta' : associatedmetaserialised(), ] return data } static String folder() { return METAFOLDERNAME } def String generateartifacts(ArrayList releasedfromhere = new ArrayList(), // These items are recorded in meta as released from here ArrayList releasedfromsubmeta = new ArrayList(), // These items are not recorded in meta as released from here String metafilepath ="${METAFOLDERNAME}/${METAFILENAME}" ) { def String artifactlist = METAARTIFACTS releaseinfo.addReleaseItem(METAARTIFACTS) for (releaseitem in releasedfromhere) { artifactlist += "," + releaseitem releaseinfo.addReleaseItem(releaseitem) } for (releaseitem in releasedfromsubmeta) { artifactlist += "," + releaseitem } saveJsonSerialised(metafilepath) return artifactlist } String componentidentifier() { return productinfo.productname + "_" + productinfo.subcomponentname } String productvariant() { if (!productinfo.variant) { return productinfo.productname } else { return productinfo.productname + "_" + productinfo.variant } } String componentversionuniqueidentifier() { return componentidentifier() + "_" + buildstampinfo.buildstamp } String globalreleasetag() { return componentversionuniqueidentifier() + "_" + jenkinsbuildinfo.buildnumber } String releasetag() { return buildstampinfo.buildstamp + "_" + jenkinsbuildinfo.buildnumber } String releasetagfordebpackage() { return buildstampinfo.buildstamp + "-" + jenkinsbuildinfo.buildnumber } class CollectedMeta { private MetaInfo metainfo private String accumulatedpath private String accumulatedmetapath CollectedMeta(MetaInfo metainfo, String accumulatedpath = '', String accumulatedmetapath = '') { this.metainfo = metainfo this.accumulatedpath = accumulatedpath this.accumulatedmetapath = accumulatedmetapath } } class IterationInfo { private ArrayList metacollection private Map metamap private String comment private int level private String gatheredpath private String gatheredmetapath private boolean limiteddepth private int maxdepth IterationInfo(ArrayList metacollection, Map metamap, String comment = '', int level = 0, String gatheredpath = '', String gatheredmetapath = '', boolean limiteddepth = false, int maxdepth = 0) { this.metacollection = metacollection this.metamap = metamap this.comment = comment this.level = level this.gatheredpath = gatheredpath this.gatheredmetapath = gatheredmetapath this.limiteddepth = limiteddepth this.maxdepth = maxdepth } String show() { return "Iteration:\nLevel: ${level}\nGathered path: ${gatheredpath}\nGathered meta path: ${gatheredmetapath}\nLimited depth: ${limiteddepth}\nMax depth: ${maxdepth}" } void add(CollectedMeta item) { metacollection.add(item) metamap.put(item.metainfo.productinfo.uniqueid, item) } } def openchildmeta(IterationInfo ii, RelatedMetaInfo relatedmetainfo) { def MetaInfo mi = new MetaInfo(script) def String nextgatheredpath = (ii.gatheredpath == '' ? relatedmetainfo.offsetpath : "${ii.gatheredpath}/${relatedmetainfo.offsetpath}") def String nextgatheredmetapath = (ii.gatheredmetapath == '' ? "${relatedmetainfo.offsetpath}/${METAFOLDERNAME}" : "${ii.gatheredmetapath}/${relatedmetainfo.offsetpath}") def importedmetafile = "${nextgatheredmetapath}/${METAFILENAME}" def boolean imported // If this has already been read in from a file if (relatedmetainfo.getrelatedid() in ii.metamap) { //script.echo("Already read in meta associated with uniqueid: ${relatedmetainfo.getrelatedid()} - Using it") mi = ii.metamap[relatedmetainfo.getrelatedid()].metainfo imported = true } else if (script.fileExists(importedmetafile)) { mi.deserialiseJson(importedmetafile) imported = true } else { script.echo("Could not deserialise meta at file: ${importedmetafile} - Does not exist") imported = false } def childmetainfo = [ 'imported': imported, 'nextgatheredpath': nextgatheredpath, 'nextgatheredmetapath': nextgatheredmetapath, 'childmeta': mi ] return childmetainfo } def walkrelated_v0_1(IterationInfo ii) { //script.echo("Walk related v0.1: ${ii.show()}") if (!ii.limiteddepth || ii.maxdepth > 0) { ii.add(new CollectedMeta(this, ii.gatheredpath, ii.gatheredmetapath)) ii.comment += "${ii.level} ".padRight(ii.level * 4) + globalreleasetag() + '\n' for (relatedmetainfo in associatedmeta) { if (!relatedmetainfo.format || relatedmetainfo.format == '0.1') { def MetaInfo othermeta = relatedmetainfo.meta def String nextgatheredpath = (ii.gatheredpath == '' ? relatedmetainfo.offsetpath : "${ii.gatheredpath}/${relatedmetainfo.offsetpath}") def String nextgatheredmetapath = (ii.gatheredmetapath == '' ? relatedmetainfo.offsetpath : "${ii.gatheredmetapath}/${relatedmetainfo.offsetpath}") def nextii = new IterationInfo(ii.metacollection, ii.metamap, ii.comment, ii.level + 1, nextgatheredpath, nextgatheredmetapath, ii.limiteddepth, ii.maxdepth - 1) def results = othermeta.walkrelated_v0_1(nextii) ii.metacollection = results.metacollection ii.metamap = results.metamap ii.comment = results.comment } else { script.error("There is non v0.1 format related meta info under v0.1 related meta info") } } } return [ 'metacollection' : ii.metacollection, 'metamap' : ii.metamap, 'comment': ii.comment ] } def walkrelated(IterationInfo ii) { //script.echo("Walk related: ${ii.show()}") if (!ii.limiteddepth || ii.maxdepth > 0) { ii.add(new CollectedMeta(this, ii.gatheredpath, ii.gatheredmetapath)) ii.comment += "${ii.level} ".padRight(ii.level * 4) + globalreleasetag() + '\n' for (relatedmetainfo in associatedmeta) { //script.echo("Related meta info: ${relatedmetainfo.offsetpath} format: ${relatedmetainfo.format}") if (relatedmetainfo.format == '0.2') { def childmetainfo = openchildmeta(ii, relatedmetainfo) if (childmetainfo.imported) { def nextii = new IterationInfo(ii.metacollection, ii.metamap, ii.comment, ii.level + 1, childmetainfo.nextgatheredpath, childmetainfo.nextgatheredmetapath, ii.limiteddepth, ii.maxdepth - 1) def results = childmetainfo.childmeta.walkrelated(nextii) ii.metacollection = results.metacollection ii.metamap = results.metamap ii.comment = results.comment } } else if (!relatedmetainfo.format || relatedmetainfo.format == '0.1') { def MetaInfo othermeta = relatedmetainfo.meta def String nextgatheredpath = (ii.gatheredpath == '' ? relatedmetainfo.offsetpath : "${ii.gatheredpath}/${relatedmetainfo.offsetpath}") def String nextgatheredmetapath = (ii.gatheredmetapath == '' ? relatedmetainfo.offsetpath : "${ii.gatheredmetapath}/${relatedmetainfo.offsetpath}") def nextii = new IterationInfo(ii.metacollection, ii.metamap, ii.comment, ii.level + 1, nextgatheredpath, nextgatheredmetapath, ii.limiteddepth, ii.maxdepth - 1) def results = othermeta.walkrelated_v0_1(nextii) ii.metacollection = results.metacollection ii.metamap = results.metamap ii.comment = results.comment } } } return [ 'metacollection' : ii.metacollection, 'metamap' : ii.metamap, 'comment': ii.comment ] } // Walks all layers of associated data only def allrelatedmeta(String metapath = '') { def ArrayList metacollection = new ArrayList() def Map metamap = new LinkedHashMap() return walkrelated(new IterationInfo(metacollection, metamap, '', 0, '', metapath)) } ArrayList releasewalker(int maxdepth, String startpath = '', String startmetapath = '') { def ArrayList metacollection = new ArrayList() def Map metamap = new LinkedHashMap() def IterationInfo ii = new IterationInfo(metacollection, metamap, 'meta', 0, startpath, startmetapath, true, maxdepth) //script.echo("Release walker iteration info at start:\n${ii.show()}") def results = walkrelated(ii) return results.metacollection } // Walks 1 layer of associated data only def getassociated(String rootpath) { return releasewalker(2, '', "${rootpath}/${METAFOLDERNAME}") // Max depth 2 (including itself) } Map putwithshortestoffsetpath(Map releasemetacollection, String uniquestring, CollectedMeta metaitem) { if (releasemetacollection.containsKey(uniquestring)) { def existingentry = releasemetacollection[uniquestring] def existingentrypathlength = existingentry.accumulatedpath.split('/').size() def newentrypathlength = metaitem.accumulatedpath.split('/').size() if (existingentrypathlength > newentrypathlength) { releasemetacollection.put(uniquestring, metaitem) } } else { releasemetacollection.put(uniquestring, metaitem) } return releasemetacollection } Map releasewalkerversionunique(int maxdepth, String startpath = '', String startmetapath = '') { def ArrayList metacollection = releasewalker(maxdepth, startpath, "${startmetapath}/${METAFOLDERNAME}") def Map versionuniquecollection = new LinkedHashMap() for (metaitem in metacollection) { def String uniquestring = metaitem.metainfo.componentversionuniqueidentifier() versionuniquecollection = putwithshortestoffsetpath(versionuniquecollection, uniquestring, metaitem) } return versionuniquecollection } Map releasewalkerunique(int maxdepth, String startpath = '', String startmetapath = '') { def ArrayList metacollection = releasewalker(maxdepth, startpath, "${startmetapath}/${METAFOLDERNAME}") def Map uniquecollection = new LinkedHashMap() for (metaitem in metacollection) { def String uniquestring = metaitem.metainfo.componentidentifier() uniquecollection = putwithshortestoffsetpath(uniquecollection, uniquestring, metaitem) } return uniquecollection } MetaInfo getNewMetaObject(script) { return new MetaInfo(script) } def findAssociatedMeta(String parentmetapath, String productname, String subcomponentname) { def results = [ 'matchedmetaitem' : new CollectedMeta(this), 'matchfound' : false ] def walkresults = allrelatedmeta("${parentmetapath}/${METAFOLDERNAME}") for (dependentmetaitem in walkresults.metacollection) { script.echo("Comparing: ${dependentmetaitem.metainfo.productinfo.productname} ${dependentmetaitem.metainfo.productinfo.subcomponentname}") if (dependentmetaitem.metainfo.productinfo.productname == productname && dependentmetaitem.metainfo.productinfo.subcomponentname == subcomponentname) { script.echo("Found matching component: ${dependentmetaitem.metainfo.globalreleasetag()}") results.matchedmetaitem = dependentmetaitem results.matchfound = true break } } return results } } MetaInfo metareset(script) { def mi = new MetaInfo(script) echo "Resetting meta folder" dir(mi.folder()) { deleteDir() } return mi } MetaInfo metafromfolder(String folder = '') { MetaInfo buildmeta = new MetaInfo(this) buildmeta.deserialiseJson("${folder}/${MetaInfo.metafilepath()}") return buildmeta } MetaInfo metafromfolderSlurper(String folder = '') { MetaInfo buildmeta = new MetaInfo(this) buildmeta.deserialiseJsonSlurper("${folder}/${MetaInfo.metafilepath()}") return buildmeta } return this;