simple-update-robot.py 4.3 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#!/usr/bin/python3
# process
# 1. get URL to download updated version
#    so far we know the URL in spec is not reliable
# 2. Change Version to new one
# 3. Change Source or Source0 if needed
# 4. Update %changelog
# 5. try rpmbuild -bb
# 6. fork on gitee
# 7. git clone, git add, git commit, git push
# 8. PR on gitee

from pyrpm.spec import Spec, replace_macros
import yaml
import argparse
import gitee
import sys
import subprocess
import os.path
import re
import datetime

def download_source_url(spec, o_ver, n_ver):
    source = replace_macros(spec.sources[0], spec).replace(o_ver, n_ver) 
    if re.match(r"%{.*?}", source):
        print("Extra macros in URL which failed to be expanded")
        return False
    elif source.startswith("http") or source.startswith("ftp"):
        fn = os.path.basename(source)
        subprocess.call(["curl", "-L", source, "-o", fn])
        return fn
    else:
        print("Not valid URL for Source code")
        return False

def download_upstream_url(gt, repo, o_ver, n_ver):
    upstream_yaml = gt.get_yaml(repo)
    if not upstream_yaml:
        return False

    rp_yaml = yaml.loads(upstream_yaml, Loader=yaml.Loader)
    if rp_yaml["version_control"] == "github":
        url = "https://github.com/{rp}/archive/{nv}.tar.gz".format(rp=rp_yaml["src_repo"], nv=n_ver)     
        fn = "{rp}.{nv}.tar.gz".format(rp=repo, nv=n_ver)
        subprocess.call(["curl", "-L", url, "-o", fn])
        return fn
    else:
        print("Handling {vc} is still under developing".format(vc=rp_yaml["version_control"]))
        return False

def create_spec(repo, spec_str, o_ver, n_ver, src_fn=None):
    fn = open(repo+".spec", "w")
    in_changelog = False
    for l in spec_str.splitlines():
        if l.startswith("Release:"):
56
            fn.write("Release:\t0\n")
57 58 59
            continue
        if l.startswith("Source:") or l.startswith("Source0:"):
            if src_fn:
S
Shinwell Hu 已提交
60
                fn.write("Source:	{src_fn}\n".format(src_fn=src_fn).replace(o_ver, n_ver))
61
            else:
S
Shinwell Hu 已提交
62
                fn.write(l.replace(o_ver, n_ver)+"\n")
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            continue
        if not in_changelog:
            nl = l.replace(o_ver, n_ver)
        else:
            nl = l
        fn.write(nl+"\n")

        if nl.startswith("%changelog"):
            in_changelog = True
            d = datetime.date.today()
            fn.write(d.strftime("* %a %b %d %Y SimpleUpdate Robot <tc@openeuler.org>\n"))
            fn.write("- Update to version {ver}\n".format(ver=n_ver))
            fn.write("\n")
    fn.close()

if __name__ == "__main__":
    pars = argparse.ArgumentParser()
    pars.add_argument("pkg", type=str, help="The package to be upgraded")
    pars.add_argument("-o", "--old_version", type=str, help="Current upstream version of package")
    pars.add_argument("-n", "--new_version", type=str, help="New upstream version of package will be upgrade to")
83 84 85 86
    pars.add_argument("-s", "--create_spec", help="Create spec file", action="store_true")
    pars.add_argument("-d", "--download", help="Download upstream source code", action="store_true")
    pars.add_argument("-f", "--fork", help="fork src-openeuler repo into users", action="store_true")
    pars.add_argument("-p", "--PR", help="Create upgrade PR", action="store_true")
87 88 89 90 91 92 93 94 95 96 97
    args = pars.parse_args()

    gt = gitee.Gitee()
    spec_string= gt.get_spec(args.pkg)

    spec = Spec.from_string(spec_string)
    if len(spec.patches) >= 1:
        print("I'm too naive to handle complicated package.")
        print("This package has multiple in-house patches.")
        sys.exit(1)

S
Shinwell Hu 已提交
98 99 100 101 102
    if args.fork:
        gt.fork_repo(args.pkg)
        subprocess.call(["git", "clone", "git@gitee.com:shinwell_hu/"+args.pkg])
        os.chdir(args.pkg)

103 104
    if args.download:
        source_file = download_source_url(spec, args.old_version, args.new_version)
105 106 107
        if source_file:
            print(source_file)
        else:
108 109 110 111 112 113
            source_file = download_upstream_url(gt, args.pkg, args.old_version, args.new_version)
            if source_file:
                print(source_file)
            else:
                print("Failed to download the latest source code.")
                sys.exit(1)
114

115 116
    if args.create_spec:
        create_spec(args.pkg, spec_string, args.old_version, args.new_version)
117

118 119
    if args.PR:
        gt.create_pr("shinwell_hu", args.pkg)