|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import urllib.request |
| 4 | +import subprocess |
| 5 | + |
| 6 | +import koji |
| 7 | + |
| 8 | + |
| 9 | +def get_buildrequire_pkgs_from_build(build_id, session, config): |
| 10 | + """ |
| 11 | + Function which queries koji for pkgs whom belong to a given build tag |
| 12 | + of a koji build and paires rpms with their respective package. |
| 13 | +
|
| 14 | + :param str build_id: build id of a build in koji. |
| 15 | + :param koji.ClientSession session: koji connection session object |
| 16 | + :return: list of pairings of package and rpms. |
| 17 | + :rtype: list |
| 18 | + """ |
| 19 | + print("Retriewing build metadata from: ", config.koji_host) |
| 20 | + build = session.getBuild(build_id) |
| 21 | + if not build: |
| 22 | + raise Exception("Build with id '{id}' has not been found.".format(id=build_id)) |
| 23 | + |
| 24 | + print("Build with the ID", build_id, "found.") |
| 25 | + tags = session.listTags(build["build_id"]) |
| 26 | + |
| 27 | + build_tag = [t["name"] for t in tags if t["name"].endswith("-build")] |
| 28 | + if not build_tag: |
| 29 | + raise Exception("Build with id '{id}' is not tagged in a 'build' tag.".format(id=build_id)) |
| 30 | + |
| 31 | + tag_data = session.listTaggedRPMS(build_tag[0], latest=True, inherit=True) |
| 32 | + |
| 33 | + print("Found the build tag '", build_tag[0], "' associated with the build.") |
| 34 | + tagged_rpms = tag_data[0] |
| 35 | + tagged_pkgs = tag_data[1] |
| 36 | + pkgs = [] |
| 37 | + archs = [config.arch, "noarch"] |
| 38 | + print("Gathering packages and rpms tagged in '", build_tag[0],"'.") |
| 39 | + for pkg in tagged_pkgs: |
| 40 | + pkg_md = { |
| 41 | + "package": pkg, |
| 42 | + "rpms": [], |
| 43 | + } |
| 44 | + |
| 45 | + for rpm in tagged_rpms: |
| 46 | + if pkg["build_id"] == rpm["build_id"] and rpm["arch"] in archs: |
| 47 | + pkg_md["rpms"].append(rpm) |
| 48 | + tagged_rpms.remove(rpm) |
| 49 | + |
| 50 | + if pkg_md["rpms"]: |
| 51 | + pkgs.append(pkg_md) |
| 52 | + print("Gathering done.") |
| 53 | + return pkgs |
| 54 | + |
| 55 | + |
| 56 | +def add_rpm_urls(pkgs, config): |
| 57 | + """ |
| 58 | + For each rpm from a package creates an download url and adds it to the package. |
| 59 | +
|
| 60 | + :param list pkgs: list of packages |
| 61 | + :return pkgs: list of packages and their rpms |
| 62 | + :rtype: list |
| 63 | + :return rpm_num: number of rpms |
| 64 | + :rtype: int |
| 65 | + """ |
| 66 | + rpm_num = 0 |
| 67 | + for pkg in pkgs: |
| 68 | + build_path = koji.pathinfo.build(pkg["package"]).replace(koji.pathinfo.topdir, "") |
| 69 | + pkg["rpm_urls"] = [] |
| 70 | + for rpm in pkg["rpms"]: |
| 71 | + rpm_num += 1 |
| 72 | + rpm_filename = "-".join([rpm["name"], rpm["version"], |
| 73 | + rpm["release"]]) + "." + rpm["arch"] + ".rpm" |
| 74 | + rpm_url = config.koji_storage_host + build_path + "/" + rpm["arch"] + "/" + rpm_filename |
| 75 | + pkg["rpm_urls"].append(rpm_url) |
| 76 | + |
| 77 | + |
| 78 | + return pkgs, rpm_num |
| 79 | + |
| 80 | + |
| 81 | +def download_file(url, target_pkg_dir, filename): |
| 82 | + """ |
| 83 | + Wrapper function for downloading a file |
| 84 | +
|
| 85 | + :param str url: url to a file |
| 86 | + :param str target_pkg_dir: the dir where the file should be downloaded |
| 87 | + :param str filename: the name of the downloaded file |
| 88 | + """ |
| 89 | + abs_file_path = "/".join([target_pkg_dir, filename]) |
| 90 | + try: |
| 91 | + urllib.request.urlretrieve(url, abs_file_path) |
| 92 | + except Exception as ex: |
| 93 | + raise Exception("HTTP error for url: {url}\nError message: {msg}\nHTTP code: {code}".format( |
| 94 | + url=ex.url, msg=ex.msg, code=ex.code)) |
| 95 | + |
| 96 | + |
| 97 | +def rpm_bulk_download(pkgs, rpm_num, working_dir): |
| 98 | + """ |
| 99 | + Downloads all the rpms from which belong to a package. |
| 100 | +
|
| 101 | + :param list pkgs: list of pkgs with their rpms and urls to those rpms |
| 102 | + :param int rpm_num: number of all the rpms included in pkgs |
| 103 | + :param str working_dir: the dir where the rpms will be downloaded |
| 104 | + """ |
| 105 | + print("Starting bulk download of rpms...") |
| 106 | + rpm_dwnlded = 0 |
| 107 | + |
| 108 | + for pkg in pkgs: |
| 109 | + for url in pkg["rpm_urls"]: |
| 110 | + # we print the status of the download |
| 111 | + status = "[{rpms}/{left}]".format(rpms=rpm_num, left=rpm_dwnlded) |
| 112 | + print(status, end="\r", flush=True) |
| 113 | + # we store the rpm in a similar location as it is on the storage server |
| 114 | + url_parts = url.split("/") |
| 115 | + filename = url_parts[-1] |
| 116 | + arch = url_parts[-2] |
| 117 | + pkg_name = "-".join([url_parts[-5], url_parts[-4], url_parts[-3]]) |
| 118 | + target_pkg_dir = "/".join([working_dir, pkg_name, arch]) |
| 119 | + # we create the package dir if it is not created |
| 120 | + if not os.path.exists(target_pkg_dir): |
| 121 | + os.makedirs(target_pkg_dir) |
| 122 | + else: |
| 123 | + # if we downloaded the file already we skip |
| 124 | + file_path = target_pkg_dir + "/" + filename |
| 125 | + if os.path.exists(file_path): |
| 126 | + rpm_dwnlded += 1 |
| 127 | + continue |
| 128 | + download_file(url, target_pkg_dir, filename) |
| 129 | + rpm_dwnlded += 1 |
| 130 | + |
| 131 | + # update the status last time to mark all of the rpms downloaded |
| 132 | + status = "[{rpms}/{left}]".format(rpms=rpm_num, left=rpm_dwnlded) |
| 133 | + print(status) |
| 134 | + print("Download successful.") |
| 135 | + |
| 136 | + |
| 137 | +def create_repo(working_dir): |
| 138 | + print("Calling createrepo_c...") |
| 139 | + args = ["createrepo_c", working_dir] |
| 140 | + subprocess.Popen(args, cwd=working_dir).communicate() |
| 141 | + print("Repo created.") |
| 142 | + |
0 commit comments