| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import argparse |
| 6 |
import datetime |
| 7 |
import json |
| 8 |
import os.path as op |
| 9 |
import os |
| 10 |
import subprocess |
| 11 |
import sys |
| 12 |
from xml.dom import minidom |
| 13 |
from xml.etree.ElementTree import Element, SubElement, tostring |
| 14 |
|
| 15 |
__version__ = "0.3.0" |
| 16 |
|
| 17 |
|
| 18 |
class Constants: |
| 19 |
|
| 20 |
NAME = "SMRT Link Software Update" |
| 21 |
AUTHOR = "build" |
| 22 |
|
| 23 |
|
| 24 |
def get_parser(): |
| 25 |
desc = "Update the manifest.xml" |
| 26 |
p = argparse.ArgumentParser(description=desc, |
| 27 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 28 |
p.add_argument("bundle_id", help="ID of the software bundle") |
| 29 |
p.add_argument("version", |
| 30 |
help="Version string or path to filename that contains it") |
| 31 |
p.add_argument("-o", dest="output_manifest_xml", default="manifest.xml", |
| 32 |
help="Path to the version.txt file. Must have a single line with Major.Minor.Tiny format") |
| 33 |
p.add_argument( |
| 34 |
"-j", |
| 35 |
dest="pacbio_manifest_json", |
| 36 |
default="pacbio-manifest.json", |
| 37 |
help="Output path output manifest JSON used by SMRT Link") |
| 38 |
p.add_argument( |
| 39 |
'--author', |
| 40 |
dest="author", |
| 41 |
default=Constants.AUTHOR, |
| 42 |
help="Bundle creation Author") |
| 43 |
p.add_argument("--name", dest="name", default=Constants.NAME, |
| 44 |
help="Name of software bundle") |
| 45 |
p.add_argument("--desc", dest="description", default="No Description", |
| 46 |
help="Description to appear in the manifest") |
| 47 |
return p |
| 48 |
|
| 49 |
|
| 50 |
def git_short_sha(): |
| 51 |
args = "git rev-parse --short HEAD".split() |
| 52 |
return subprocess.check_output(args, encoding="utf-8").strip() |
| 53 |
|
| 54 |
|
| 55 |
def git_branch(): |
| 56 |
args = "git rev-parse --abbrev-ref HEAD".split() |
| 57 |
return subprocess.check_output(args, encoding="utf-8").strip() |
| 58 |
|
| 59 |
|
| 60 |
def get_bamboo_buildnumber(default=0): |
| 61 |
return int(os.environ.get('bamboo_globalBuildNumber', default)) |
| 62 |
|
| 63 |
|
| 64 |
def to_semver(major, minor, patch, git_sha, |
| 65 |
build_number=None, prerelease_tag=None): |
| 66 |
"""Convert to semver format""" |
| 67 |
base = ".".join(str(i) for i in (major, minor, patch)) |
| 68 |
prerelease = "" if prerelease_tag is None else "-{}".format(prerelease_tag) |
| 69 |
|
| 70 |
number = "" if build_number is None else str(build_number) + "." |
| 71 |
metadata = "+{b}{s}".format(b=number, s=git_sha[:7]) |
| 72 |
return "{b}{p}{m}".format(b=base, p=prerelease, m=metadata) |
| 73 |
|
| 74 |
|
| 75 |
def to_undocumented_pacbio_version_format(major, minor, tiny, other): |
| 76 |
return ".".join([str(i) for i in (major, minor, tiny, other)]) |
| 77 |
|
| 78 |
|
| 79 |
def get_version(major, minor, tiny): |
| 80 |
build_number = get_bamboo_buildnumber() |
| 81 |
git_sha = git_short_sha() |
| 82 |
|
| 83 |
return to_semver(major, minor, tiny, git_sha, build_number=None) |
| 84 |
|
| 85 |
|
| 86 |
def read_version_txt(version): |
| 87 |
if op.isfile(version): |
| 88 |
with open(version, 'r') as f: |
| 89 |
x = f.readline() |
| 90 |
else: |
| 91 |
x = version |
| 92 |
major, minor, patch = [int(i) for i in x.split(".")[:3]] |
| 93 |
return major, minor, patch |
| 94 |
|
| 95 |
|
| 96 |
def to_pacbio_manifest_d(bundle_id, version, name, desc): |
| 97 |
return dict(id=bundle_id, |
| 98 |
name=name, |
| 99 |
version=version, |
| 100 |
description=desc, |
| 101 |
dependencies=[]) |
| 102 |
|
| 103 |
|
| 104 |
def prettify(elem): |
| 105 |
return minidom.parseString( |
| 106 |
tostring(elem, 'utf-8')).toprettyxml(indent=" ") |
| 107 |
|
| 108 |
|
| 109 |
def write_manifest_xml(bundle_id, version, name, |
| 110 |
description, author, manifest_xml): |
| 111 |
|
| 112 |
root = Element("Manifest") |
| 113 |
|
| 114 |
def sub(n, value_): |
| 115 |
e = SubElement(root, n) |
| 116 |
e.text = value_ |
| 117 |
return e |
| 118 |
|
| 119 |
sub("Package", bundle_id) |
| 120 |
sub("Name", name) |
| 121 |
sub("Version", version) |
| 122 |
sub("Created", datetime.datetime.utcnow().isoformat()) |
| 123 |
sub("Author", author) |
| 124 |
sub("Description", description) |
| 125 |
|
| 126 |
with open(manifest_xml, 'w') as f: |
| 127 |
f.write(prettify(root)) |
| 128 |
|
| 129 |
return root |
| 130 |
|
| 131 |
|
| 132 |
def write_pacbio_manifest_json(bundle_id, version, name, desc, output_json): |
| 133 |
with open(output_json, 'w') as f: |
| 134 |
f.write( |
| 135 |
json.dumps( |
| 136 |
to_pacbio_manifest_d( |
| 137 |
bundle_id, |
| 138 |
version, |
| 139 |
name, |
| 140 |
desc), |
| 141 |
indent=True)) |
| 142 |
|
| 143 |
|
| 144 |
def runner(bundle_id, version, output_manifest_xml, |
| 145 |
pacbio_manifest_json, author, name, desc): |
| 146 |
|
| 147 |
major, minor, patch = read_version_txt(version) |
| 148 |
sem_ver = get_version(major, minor, patch) |
| 149 |
branch = git_branch() |
| 150 |
other = get_bamboo_buildnumber() |
| 151 |
if not (branch.startswith("master") or branch.startswith("release")): |
| 152 |
other = "SNAPSHOT" + str(other) |
| 153 |
version_str = to_undocumented_pacbio_version_format( |
| 154 |
major, minor, patch, other) |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
author = "User {} created {} bundle {}".format( |
| 159 |
Constants.AUTHOR, bundle_id, sem_ver) |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
write_manifest_xml( |
| 164 |
bundle_id, |
| 165 |
version_str, |
| 166 |
name, |
| 167 |
desc, |
| 168 |
author, |
| 169 |
output_manifest_xml) |
| 170 |
write_pacbio_manifest_json( |
| 171 |
bundle_id, |
| 172 |
version_str, |
| 173 |
name, |
| 174 |
desc, |
| 175 |
pacbio_manifest_json) |
| 176 |
|
| 177 |
return 0 |
| 178 |
|
| 179 |
|
| 180 |
def main(argv_): |
| 181 |
p = get_parser() |
| 182 |
args = p.parse_args(argv_) |
| 183 |
return runner(args.bundle_id, |
| 184 |
args.version, |
| 185 |
args.output_manifest_xml, |
| 186 |
args.pacbio_manifest_json, |
| 187 |
args.author, |
| 188 |
args.name, |
| 189 |
args.description) |
| 190 |
|
| 191 |
|
| 192 |
if __name__ == '__main__': |
| 193 |
sys.exit(main(sys.argv[1:])) |