pyporter 15.5 KB
Newer Older
Z
Zhipeng Xie 已提交
1 2 3 4 5
#!/usr/bin/python3
"""
This is a packager bot for python modules from pypi.org
"""
#******************************************************************************
6
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
Z
Zhipeng Xie 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: Shinwell_Hu Myeuler
# Create: 2020-05-07
# Description: provide a tool to package python module automatically
# ******************************************************************************/

import urllib
import urllib.request
from pprint import pprint
from os import path
import json
import sys
import re
import datetime
import argparse
import subprocess
import os
31
import platform
Z
Zhipeng Xie 已提交
32 33 34 35
from pathlib import Path
# python3-wget is not default available on openEuler yet.
# import wget  

36

Z
Zhipeng Xie 已提交
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
url_template = 'https://pypi.org/pypi/{pkg_name}/json'
json_file_template = '{pkg_name}.json'
name_tag_template    = 'Name:\t\tpython-{pkg_name}' 
summary_tag_template = 'Summary:\t{pkg_sum}' 
version_tag_template = 'Version:\t{pkg_ver}' 
release_tag_template = 'Release:\t1'
license_tag_template = 'License:\t{pkg_lic}' 
home_tag_template = 'URL:\t\t{pkg_home}' 
source_tag_template = 'Source0:\t{pkg_source}'

buildreq_tag_template = 'BuildRequires:\t{req}'

build_noarch = True # Usually python modules are arch independent

# TODO List
# 1. Need a reliable way to get description of module .. Partially done
# 2. requires_dist has some dependency restirction, need to present
# 3. dependency outside python (i.e. pycurl depends on libcurl) doesn't exist in pipy


def get_license(j):
    """
    By default, the license info can be achieved from json["info"]["license"]
    In rare cases it doesn't work.
    We fall back to json["info"]["classifiers"], it looks like License :: OSI Approved :: BSD Clause
    """
    if j["info"]["license"] != "":
        return j["info"]["license"]
    for k in j["info"]["classifiers"]:
        if k.startswith("License"):
            ks = k.split("::")
            return ks[2].strip()
    return ""


def get_source_url(j):
    """
    return URL for source file for the latest version
    return "" in errors
    """
    v = j["info"]["version"]
    rs = j["releases"][v]
    for r in rs:
        if r["packagetype"] == "sdist":
            return r["url"]
    return ""


def transform_module_name(n):
    """
    return module name with version restriction.
    Any string with '.' or '/' is considered file, and will be ignored
    Modules start with python- will be changed to python3- for consistency.
    """
    # remove ()
    ns = re.split("[()]", n)
93
    ver_constrain = []
Z
Zhipeng Xie 已提交
94 95 96 97 98 99 100
    ns[0] = ns[0].strip()
    if ns[0].startswith("python-"):
        ns[0] = ns[0].replace("python-", "python3-")
    else:
        ns[0] = "python3-" + ns[0] 
        if ns[0].find("/") != -1 or ns[0].find(".") != -1:
            return ""
101 102 103 104 105 106 107 108 109 110
    """
    if len(ns) > 1:
        vers = ns[1].split(",")
        for ver in vers:
            m = re.match("([<>=]+)( *)(\d.*)", ver.strip())
            ver_constrain.append(ns[0] + " " + m[1] + " " + m[3])
        return ", ".join(ver_constrain)
    else:
    """
    return ns[0]
Z
Zhipeng Xie 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225


def get_requires(j):
    """
    return all requires no matter if extra is required.
    """
    rs = j["info"]["requires_dist"]
    if rs is None:
        return
    for r in rs:
        idx = r.find(";")
        mod = transform_module_name(r[:idx])
        print("Requires:\t" + mod)


def refine_requires(req):
    """
    return only requires without ';' (thus no extra)
    """
    ra = req.split(";", 1)
    #
    # Do not add requires which has ;, which is often has very complicated precondition
    # TODO: need more parsing of the denpency after ;
    return transform_module_name(ra[0])

def get_build_requires(resp):
    req_list=[]
    rds = resp["info"]["requires_dist"]
    if rds is not None:
        for rp in rds:
            br = refine_requires(rp)
            if (br == ""):
                continue
            #
            # Do not output BuildRequires: 
            # just collect all build requires and using pip to install
            # than can help to build all rpm withoud trap into 
            # build dependency nightmare
            #
            #print(buildreq_tag_template.format(req=br))
            name=str.lstrip(br).split(" ")
            req_list.append(name[0])
    return req_list

def get_buildarch(j):
    """
    If this module has a prebuild package for amd64, then it is arch dependent.
    print BuildArch tag if needed.
    """
    v = j["info"]["version"]
    rs = j["releases"][v]
    for r in rs:
        if r["packagetype"] == "bdist_wheel":
            if r["url"].find("amd64") != -1:
                global build_noarch
                build_noarch = False
                return
    print("BuildArch:\tnoarch")


def get_description(j):
    """
    return description.
    Usually it's json["info"]["description"]
    If it's rst style, then only use the content for the first paragraph, and remove all tag line.
    For empty description, use summary instead.
    """
    desc = j["info"]["description"].splitlines()
    res = []
    paragraph = 0
    for d in desc:
        if len(d.strip()) == 0:
            continue
        first_char = d.strip()[0]
        ignore_line = False
        if d.strip().startswith("===") or d.strip().startswith("---"):
            paragraph = paragraph + 1
            ignore_line = True
        elif d.strip().startswith(":") or d.strip().startswith(".."):
            ignore_line = True
        if ignore_line != True and paragraph == 1:
            res.append(d)
        if paragraph >= 2:
            del res[-1]
            return "\n".join(res)
    if res != []:
        return "\n".join(res)
    elif paragraph == 0:
        return j["info"]["description"]
    else:
        return j["info"]["summary"]


def store_json(j, pkg, spath):
    """
    save json file
    """
    fname = json_file_template.format(pkg_name=pkg)
    json_file = os.path.join(spath, fname)
    
    # if file exist, do nothing 
    if path.exists(json_file) and path.isfile(json_file):
        with open(json_file, 'r') as f:
            resp = json.load(f)
    else:
        with open(json_file, 'w') as f:
            json.dump(j, f)


def get_pkg_json(pkg):
    """
    recieve json from pypi.org
    """
    url = url_template.format(pkg_name=pkg)

226 227 228
    resp = ""
    with urllib.request.urlopen(url) as u:
        resp = json.loads(u.read().decode('utf-8'))
Z
Zhipeng Xie 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    return resp


def download_source(j, tgtpath):
    """
    download source file from url, and save it to target path
    """
    if (os.path.exists(tgtpath) == False):
        print("download path %s does not exist\n", tgtpath)
        return False
    s_url = get_source_url(j)
    return subprocess.call(["wget", s_url, "-P", tgtpath])


244
def prepare_rpm_build_env(root):
Z
Zhipeng Xie 已提交
245 246 247
    """
    prepare environment for rpmbuild
    """
248 249 250 251 252
    if (os.path.exists(root) == False):
        print("Root path %s does not exist\n" & buildroot)
        return ""

    buildroot = os.path.join(root, "rpmbuild")
Z
Zhipeng Xie 已提交
253
    if (os.path.exists(buildroot) == False):
254
        os.mkdir(buildroot)
Z
Zhipeng Xie 已提交
255 256 257 258 259 260

    for sdir in ['SPECS', 'BUILD', 'SOURCES', 'SRPMS', 'RPMS', 'BUILDROOT']:
        bpath = os.path.join(buildroot, sdir)
        if (os.path.exists(bpath) == False):
            os.mkdir(bpath)

261
    return buildroot
Z
Zhipeng Xie 已提交
262 263


264
def try_pip_install_package(pkg):
Z
Zhipeng Xie 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    """
    install packages listed in build requires
    """
    # try pip installation
    pip_name = pkg.split("-")
    if len(pip_name) == 2:
        ret = subprocess.call(["pip3", "install", "--user", pip_name[1]])
    else:
        ret = subprocess.call(["pip3", "install", "--user", pip_name[0]])

    if ret != 0:
        print("%s can not be installed correctly, Fix it later, go ahead to do building..." % pip_name)

    #
    # TODO: try to build anyway, fix it later
    #
    return True

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
def package_installed(pkg):
    print(pkg)
    ret = subprocess.call(["rpm", "-qi", pkg])
    if ret == 0:
        return True

    return False


def dependencies_ready(req_list):
    """ 
    TODO: do not need to do dependency check here, do it in pyporter_run
    """
    #    if (try_pip_install_package(req) == False):
    #        return req
Z
Zhipeng Xie 已提交
298 299 300 301 302 303 304 305 306 307
    return ""

def build_package(specfile):
    """
    build rpm package with rpmbuild
    """
    ret = subprocess.call(["rpmbuild", "-ba", specfile])
    return ret


308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
def build_install_rpm(j, rootpath):
    ret = build_rpm(j, rootpath)
    if (ret != ""):
        return ret

    arch = "noarch"
    if (build_noarch == False):
        arch = platform.machine()

    pkgname = os.path.join(rootpath, "rpmbuild", "RPMS", arch, "python3-" + j["info"]["name"] + "*")
    ret = subprocess.call(["rpm", "-ivh", pkgname])
    if (ret != 0):
        return "Install failed\n"

    return ""

def build_rpm(j, rootpath):
Z
Zhipeng Xie 已提交
325 326 327
    """
    full process to build rpm
    """
328 329
    buildroot = prepare_rpm_build_env(rootpath) 
    if (buildroot == ""):
Z
Zhipeng Xie 已提交
330 331 332 333 334
        return False

    specfile = os.path.join(buildroot, "SPECS", "python-" + j["info"]["name"] + ".spec")

    req_list = build_spec(j, specfile)
335
    ret = dependencies_ready(req_list)
Z
Zhipeng Xie 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    if ret != "":
        print("%s can not be installed automatically, Please handle it" % ret)
        return ret

    download_source(j, os.path.join(buildroot, "SOURCES"))

    build_package(specfile)

    return ""


def build_spec(resp, output):
    """
    print out the spec file
    """
    if os.path.isdir(output):
        output = os.path.join(output, "python3-" + resp["info"]["name"]) 
    tmp = sys.stdout
    if (output == ""):
        print()
    else:
        sys.stdout = open(output, 'w+')
358 359
   
    print("%global _empty_manifest_terminate_build 0")
Z
Zhipeng Xie 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    print(name_tag_template.format(pkg_name=resp["info"]["name"]))
    print(version_tag_template.format(pkg_ver=resp["info"]["version"]))
    print(release_tag_template)
    print(summary_tag_template.format(pkg_sum=resp["info"]["summary"]))
    print(license_tag_template.format(pkg_lic=get_license(resp)))
    print(home_tag_template.format(pkg_home=resp["info"]["project_urls"]["Homepage"]))
    print(source_tag_template.format(pkg_source=get_source_url(resp)))
    get_buildarch(resp)
    print("")
    get_requires(resp)
    print("")
    print("%description")
    print(get_description(resp))
    print("")
    print("%package -n python3-{name}".format(name=resp["info"]["name"]))
    print(summary_tag_template.format(pkg_sum=resp["info"]["summary"]))
    print("Provides:\tpython-" + resp["info"]["name"])
    print(buildreq_tag_template.format(req='python3-devel'))
    print(buildreq_tag_template.format(req='python3-setuptools'))

    if build_noarch == False:
        print(buildreq_tag_template.format(req='python3-cffi'))
        print(buildreq_tag_template.format(req='gcc'))
        print(buildreq_tag_template.format(req='gdb'))


    build_req_list=get_build_requires(resp)

    print("%description -n python3-" + resp["info"]["name"])
    print(get_description(resp))
    print("")
    print("%package help")
    print("Summary:\tDevelopment documents and examples for {name}".format(name=resp["info"]["name"]))
    print("Provides:\tpython3-{name}-doc".format(name=resp["info"]["name"]))
    print("%description help")
    print(get_description(resp))
    print("")
    print("%prep")
    print("%autosetup -n {name}-{ver}".format(name=resp["info"]["name"], ver=resp["info"]["version"]))
    print("")
    print("%build")
    print("%py3_build")
    print("")
    print("%install")
    print("%py3_install")
    print("install -d -m755 %{buildroot}/%{_pkgdocdir}")
    print("if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi")
    print("if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi")
    print("if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi")
    print("if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi")
    print("pushd %{buildroot}")
    print("if [ -d usr/lib ]; then")
    print("\tfind usr/lib -type f -printf \"/%h/%f\\n\" >> filelist.lst")
    print("fi")
    print("if [ -d usr/lib64 ]; then")
    print("\tfind usr/lib64 -type f -printf \"/%h/%f\\n\" >> filelist.lst")
    print("fi")
    print("if [ -d usr/bin ]; then")
    print("\tfind usr/bin -type f -printf \"/%h/%f\\n\" >> filelist.lst")
    print("fi")
    print("if [ -d usr/sbin ]; then")
    print("\tfind usr/sbin -type f -printf \"/%h/%f\\n\" >> filelist.lst")
    print("fi")
423 424 425 426
    print("touch doclist.lst")
    print("if [ -d usr/share/man ]; then")
    print("\tfind usr/share/man -type f -printf \"/%h/%f.gz\\n\" >> doclist.lst")
    print("fi")
Z
Zhipeng Xie 已提交
427 428
    print("popd")
    print("mv %{buildroot}/filelist.lst .")
429
    print("mv %{buildroot}/doclist.lst .")
Z
Zhipeng Xie 已提交
430 431 432 433 434 435 436 437 438 439 440
    print("")
    print("%files -n python3-{name} -f filelist.lst".format(name=resp["info"]["name"]))
#   print("%{python3_sitelib}/*.egg-info/")
#   print("%{python3_sitelib}/" + resp["info"]["name"])

    if build_noarch:
        print("%dir %{python3_sitelib}/*")
    else:
        print("%dir %{python3_sitearch}/*")

    print("")
441
    print("%files help -f doclist.lst")
Z
Zhipeng Xie 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455
    print("%{_pkgdocdir}")
    print("")
    print("%changelog")
    date_str = datetime.date.today().strftime("%a %b %d %Y")
    print("* {today} Python_Bot <Python_Bot@openeuler.org>".format(today=date_str))
    print("- Package Spec generated")

    sys.stdout = tmp

    return build_req_list


if __name__ == "__main__":

456
    dft_root_path=os.path.join(str(Path.home()))
Z
Zhipeng Xie 已提交
457 458 459 460

    parser = argparse.ArgumentParser()

    parser.add_argument("-s", "--spec", help="Create spec file", action="store_true")
461
    parser.add_argument("-R", "--requires", help="Get required python modules", action="store_true")
Z
Zhipeng Xie 已提交
462
    parser.add_argument("-b", "--build", help="Build rpm package", action="store_true")
463
    parser.add_argument("-B", "--buildinstall", help="Build&Install rpm package", action="store_true")
Z
Zhipeng Xie 已提交
464 465 466 467 468 469 470 471 472
    parser.add_argument("-r", "--rootpath", help="Build rpm package in root path", type=str, default=dft_root_path)
    parser.add_argument("-d", "--download", help="Download source file indicated path", action="store_true")
    parser.add_argument("-p", "--path", help="indicated path to store files", type=str, default=os.getcwd())
    parser.add_argument("-j", "--json", help="Get Package JSON info", action="store_true")
    parser.add_argument("-o", "--output", help="Output to file", type=str, default="")
    parser.add_argument("pkg", type=str, help="The Python Module Name")
    args = parser.parse_args()

    response = get_pkg_json(args.pkg)
473 474 475 476 477 478 479 480 481 482
    if (response == ""):
        print("Get %s module json info failed\n" % args.pkg)
        sys.exit(1)

    if (args.requires):
        reqlist = get_build_requires(response)
        if reqlist is not None:
            for req in reqlist:
                print(req)
        sys.exit(0)
Z
Zhipeng Xie 已提交
483 484 485 486 487 488 489

    if (args.spec):
        build_spec(response, args.output)

    if (args.build):
        ret = build_rpm(response, args.rootpath)
        if ret != "":
490 491 492 493 494 495 496 497
            print("build failed : BuildRequire : %s\n" % ret)
            sys.exit(1)

    if (args.buildinstall):
        ret = build_install_rpm(response, args.rootpath)
        if ret != "":
            print("Build & install failed\n")
            sys.exit(1)
Z
Zhipeng Xie 已提交
498 499 500 501 502 503 504

    if (args.download):
        download_source(response, args.path)

    if (args.json):
        store_json(response, args.pkg, args.path)