version-bump 7.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import re
import sys
import hashlib
import datetime
import optparse

def get_and_save_user_info():
    name = "PACKAGER_NAME"
    email = "PACKAGER_EMAIL"

    conf_file = os.path.expanduser("~/.packagerinfo")
    if os.path.exists(conf_file):
        # Read from it
        name, email = open(conf_file, "r").read().strip().split(",")

    else:
        print("Please enter your full name as seen in pspec files")
        name = input("> ")
        print("Please enter your e-mail as seen in pspec files")
        email = input("> ")
        print("Saving packager info into {}".format(conf_file))

        open(conf_file, "w").write("{},{}".format(name, email))

    return name, email

31
def bump_spec(spec, version, packager_name, packager_email,
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
          critical, security, bumptype, comment):
    lines = open(spec).readlines()
    name_line = -1
    archive_line = -1
    dep_line = -1
    update_line = -1
    dep_lines = []

    for n, line in enumerate(lines):
        if "<Source>" in line:
            name_line = n + 1

        elif "<Archive" in line:
            archive_line = n

        elif "<Dependency" in line:
            dep_lines.append(n)

        elif "<History>" in line:
            update_line = n + 1

    m = re.match(r" *<Name>(.+)</Name>", lines[name_line])
    name = m.groups()[0]
    lang = name.split("-", 1)[-1]

    m = re.match(r' *<Archive type=".+" sha1sum="(.+)">(.+)</Archive>', lines[archive_line])
    if m is None:
        m = re.match(r' *<Archive sha1sum="(.+)" type=".+">(.+)</Archive>', lines[archive_line])

    sha1sum, uri = m.groups()
    archive_name = os.path.basename(uri)

    m = re.match(r' *<Update release="(.*?)"', lines[update_line])
    release = int(m.groups()[0])

    m = re.match(r" *<Version>(.+)</Version>", lines[update_line + 2])
    old_version = m.groups()[0]

70 71 72 73 74
    if not version:
        version = old_version
        new_sha1sum = sha1sum
        new_archive_name = archive_name

75
    if old_version == version:
76
        comment = "Package is rebuilded and released up."
77

78 79 80
    else:
        lines[archive_line] = lines[archive_line].replace(old_version, version)
        new_archive_name = archive_name.replace(old_version, version)
81

82 83
        if not os.path.exists("/var/cache/inary/archives/{}".format(new_archive_name)):
            print("Archive not found downloading via inary...")
84

85 86 87 88 89 90
            tmp_spec_file = spec_file.replace("pspec", "tmp_pspec")
            open(tmp_spec_file, "w").writelines(lines)
            os.system("sudo inary build {} --fetch".format(tmp_spec_file))
            os.unlink(tmp_spec_file)
        else:
            print("Using existing archive from cache...")
91

92 93
        new_sha1sum = hashlib.sha1(open("/var/cache/inary/archives/{}".format(new_archive_name), 'rb').read()).hexdigest()
        lines[archive_line] = lines[archive_line].replace(sha1sum, new_sha1sum)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122


    for dep_line in dep_lines:
        m = re.match(r' *<Dependency versionFrom="(.*)">.*', lines[dep_line])
        if m:
            dep_version = m.groups()[0]
            if dep_version == old_version:
                lines[dep_line] = lines[dep_line].replace(old_version, version)

    if not comment=="":
        pass
    else:
        comment="Version bump to {}".format(version)

    if bumptype:
        updatetype = " type=\"{}\"".format(bumptype)
    else:
        updatetype=""

    crit_or_sec = ""

    if security:
        for pkg in security:
            crit_or_sec += "\n            <Type package=\"{}\">security</Type>".format(pkg)
    elif critical:
        for pkg in critical:
            crit_or_sec += "\n            <Type package=\"{}\">critical</Type>".format(pkg)


123
    update_lines = ['        <Update release="{}">{}\n'.format(release + 1, bumptype or ""),
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                    '            <Date>{}</Date>\n'.format(datetime.date.isoformat(datetime.date.today())),
                    '            <Version>{}</Version>\n'.format(version),
                    '            <Comment>{}</Comment>{}\n'.format(comment, crit_or_sec),
                    '            <Name>{}</Name>\n'.format(packager_name),
                    '            <Email>{}</Email>\n'.format(packager_email),
                    '        </Update>\n']


    lines[update_line:update_line] = update_lines

    open(spec_file, "w").writelines(lines)
    print("{} updated".format(spec_file))
    print()
    print("Old SHA1SUM: {}".format(sha1sum))
    print("New SHA1SUM: {}".format(new_sha1sum))
    print("Old Archive: {}".format(archive_name))
    print("New Archive: {}".format(new_archive_name))
    print("Comment: {}".format(comment))
    print("Version: {}".format(version))
    print("Date:    {}".format(datetime.date.isoformat(datetime.date.today())))


if __name__ == "__main__":
    usage = "Usage: %prog <Release comment> [Bumped version] [options] [Pspec list]"
    parser = optparse.OptionParser(usage)
    parser.add_option("--comment",
                      dest="comment",
                      default="",
                      help="Update specfile to x version from old one.")
    parser.add_option("--to-version",
                      dest="to_version",
                      default=None,
                      help="Update specfile to x version from old one.")
    parser.add_option("-n", "--no-increment",
                      dest="no_increment",
                      default=False,
                      help="Do not increment release, just update sha1sums.",
                      action="store_true")
    parser.add_option("-c", "--critical",
                      dest="critical",
                      default=False,
                      help="Mark update as critical and add Type=critical for those packages",
                      action="store_true")
    parser.add_option("-s", "--security",
                      dest="security",
                      default=False,
                      help="Mark update as security and add Type=security for those packages",
                      action="store_true")
    parser.add_option("--all-critical",
                      dest="bumptype",
                      action="store_const",
                      const="critical",
                      help="Mark the update critical for all packages [old way]")
    parser.add_option("--all-security",
                      dest="bumptype",
                      action="store_const",
                      const="security",
                      help="Mark the update security for all packages [old way]")

    (options,args) = parser.parse_args()


    if len(sys.argv) < 2:
        print(usage)
        sys.exit(1)

    files=[]
    for i in sys.argv:
        if i.endswith("pspec.xml"):
            files.append(i)
    if not files:
        print(usage)
        sys.exit(1)

    version = options.to_version
    comment = options.comment
    packager_name, packager_email = get_and_save_user_info()

    for spec_file in files:
        bump_spec(spec_file, version, packager_name, packager_email,
                                      options.critical, options.security,
                                      options.bumptype, comment)