提交 2ba3f2ce 编写于 作者: B BruceGW

fix PEP8 checkstyle warnings

上级 e5b65c59
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
""" """
This is a packager bot for python modules from pypi.org This is a packager bot for python modules from pypi.org
""" """
#****************************************************************************** # ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. # Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2. # licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2. # You can use this software according to the terms and conditions of the Mulan PSL v2.
...@@ -31,28 +31,29 @@ import os ...@@ -31,28 +31,29 @@ import os
import platform import platform
from pathlib import Path from pathlib import Path
import hashlib import hashlib
# python3-wget is not default available on openEuler yet. # python3-wget is not default available on openEuler yet.
# import wget # import wget
json_file_template = '{pkg_name}.json' json_file_template = '{pkg_name}.json'
name_tag_template = 'Name:\t\t{pkg_name}' name_tag_template = 'Name:\t\t{pkg_name}'
summary_tag_template = 'Summary:\t{pkg_sum}' summary_tag_template = 'Summary:\t{pkg_sum}'
version_tag_template = 'Version:\t{pkg_ver}' version_tag_template = 'Version:\t{pkg_ver}'
release_tag_template = 'Release:\t1' release_tag_template = 'Release:\t1'
license_tag_template = 'License:\t{pkg_lic}' license_tag_template = 'License:\t{pkg_lic}'
home_tag_template = 'URL:\t\t{pkg_home}' home_tag_template = 'URL:\t\t{pkg_home}'
source_tag_template = 'Source0:\t{pkg_source}' source_tag_template = 'Source0:\t{pkg_source}'
buildreq_tag_template = 'BuildRequires:\t{req}' buildreq_tag_template = 'BuildRequires:\t{req}'
# TODO List # TODO List
# 1. Need a reliable way to get description of module .. Partially done # 1. Need a reliable way to get description of module .. Partially done
# 2. requires_dist has some dependency restirction, need to present # 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 # 3. dependency outside python (i.e. pycurl depends on libcurl) doesn't exist in pipy
class PyPorter: class PyPorter:
__url_template = 'https://pypi.org/pypi/{pkg_name}/json' __url_template = 'https://pypi.org/pypi/{pkg_name}/json'
__build_noarch = True __build_noarch = True
...@@ -60,7 +61,7 @@ class PyPorter: ...@@ -60,7 +61,7 @@ class PyPorter:
__module_name = "" __module_name = ""
__spec_name = "" __spec_name = ""
__pkg_name = "" __pkg_name = ""
def __init__(self, pkg): def __init__(self, pkg):
""" """
receive json from pypi.org receive json from pypi.org
...@@ -69,15 +70,14 @@ class PyPorter: ...@@ -69,15 +70,14 @@ class PyPorter:
resp = "" resp = ""
with urllib.request.urlopen(url) as u: with urllib.request.urlopen(url) as u:
self.__json = json.loads(u.read().decode('utf-8')) self.__json = json.loads(u.read().decode('utf-8'))
if (self.__json is not None): if self.__json is not None:
self.__module_name = self.__json["info"]["name"] self.__module_name = self.__json["info"]["name"]
self.__spec_name = "python-" + self.__module_name self.__spec_name = "python-" + self.__module_name
self.__pkg_name = "python3-" + self.__module_name self.__pkg_name = "python3-" + self.__module_name
self.__build_noarch = self.__get_buildarch() self.__build_noarch = self.__get_buildarch()
def get_spec_name(self): def get_spec_name(self):
return self.__spec_name return self.__spec_name
def get_module_name(self): def get_module_name(self):
return self.__module_name return self.__module_name
...@@ -87,7 +87,7 @@ class PyPorter: ...@@ -87,7 +87,7 @@ class PyPorter:
def get_version(self): def get_version(self):
return self.__json["info"]["version"] return self.__json["info"]["version"]
def get_summary(self): def get_summary(self):
return self.__json["info"]["summary"] return self.__json["info"]["summary"]
...@@ -141,13 +141,6 @@ class PyPorter: ...@@ -141,13 +141,6 @@ class PyPorter:
idx = r.find(";") idx = r.find(";")
mod = transform_module_name(r[:idx]) mod = transform_module_name(r[:idx])
print("Requires:\t" + mod) print("Requires:\t" + mod)
if self.__json["info"]["license"] != "":
return self.__json["info"]["license"]
for k in self.__json["info"]["classifiers"]:
if k.startswith("License"):
ks = k.split("::")
return ks[2].strip()
return ""
def __get_buildarch(self): def __get_buildarch(self):
""" """
...@@ -194,7 +187,7 @@ class PyPorter: ...@@ -194,7 +187,7 @@ class PyPorter:
if paragraph >= 2: if paragraph >= 2:
del res[-1] del res[-1]
return "\n".join(res) return "\n".join(res)
if res != []: if res:
return "\n".join(res) return "\n".join(res)
elif paragraph == 0: elif paragraph == 0:
return self.__json["info"]["description"] return self.__json["info"]["description"]
...@@ -202,12 +195,12 @@ class PyPorter: ...@@ -202,12 +195,12 @@ class PyPorter:
return self.__json["info"]["summary"] return self.__json["info"]["summary"]
def get_build_requires(self): def get_build_requires(self):
req_list=[] req_list = []
rds = self.__json["info"]["requires_dist"] rds = self.__json["info"]["requires_dist"]
if rds is not None: if rds is not None:
for rp in rds: for rp in rds:
br = refine_requires(rp) br = refine_requires(rp)
if (br == ""): if br == "":
continue continue
# #
# Do not output BuildRequires: # Do not output BuildRequires:
...@@ -215,36 +208,38 @@ class PyPorter: ...@@ -215,36 +208,38 @@ class PyPorter:
# than can help to build all rpm withoud trap into # than can help to build all rpm withoud trap into
# build dependency nightmare # build dependency nightmare
# #
#print(buildreq_tag_template.format(req=br)) # print(buildreq_tag_template.format(req=br))
name=str.lstrip(br).split(" ") name = str.lstrip(br).split(" ")
req_list.append(name[0]) req_list.append(name[0])
return req_list return req_list
def prepare_build_requires(self): def prepare_build_requires(self):
print(buildreq_tag_template.format(req='python3-devel')) print(buildreq_tag_template.format(req='python3-devel'))
print(buildreq_tag_template.format(req='python3-setuptools')) print(buildreq_tag_template.format(req='python3-setuptools'))
if (self.__build_noarch == False): if not self.__build_noarch:
print(buildreq_tag_template.format(req='python3-cffi')) print(buildreq_tag_template.format(req='python3-cffi'))
print(buildreq_tag_template.format(req='gcc')) print(buildreq_tag_template.format(req='gcc'))
print(buildreq_tag_template.format(req='gdb')) print(buildreq_tag_template.format(req='gdb'))
def prepare_pkg_build(self): def prepare_pkg_build(self):
print("%py3_build") print("%py3_build")
def prepare_pkg_install(self): def prepare_pkg_install(self):
print("%py3_install") print("%py3_install")
def prepare_pkg_files(self): def prepare_pkg_files(self):
if self.__build_noarch: if self.__build_noarch:
print("%dir %{python3_sitelib}/*") print("%dir %{python3_sitelib}/*")
else: else:
print("%dir %{python3_sitearch}/*") print("%dir %{python3_sitearch}/*")
def store_json(self, spath): def store_json(self, spath):
""" """
save json file save json file
""" """
fname = json_file_template.format(pkg_name=self.__pkg_name) fname = json_file_template.format(pkg_name=self.__pkg_name)
json_file = os.path.join(spath, fname) json_file = os.path.join(spath, fname)
# if file exist, do nothing # if file exist, do nothing
if path.exists(json_file) and path.isfile(json_file): if path.exists(json_file) and path.isfile(json_file):
with open(json_file, 'r') as f: with open(json_file, 'r') as f:
...@@ -267,7 +262,7 @@ def transform_module_name(n): ...@@ -267,7 +262,7 @@ def transform_module_name(n):
if ns[0].startswith("python-"): if ns[0].startswith("python-"):
ns[0] = ns[0].replace("python-", "python3-") ns[0] = ns[0].replace("python-", "python3-")
else: else:
ns[0] = "python3-" + ns[0] ns[0] = "python3-" + ns[0]
if ns[0].find("/") != -1 or ns[0].find(".") != -1: if ns[0].find("/") != -1 or ns[0].find(".") != -1:
return "" return ""
""" """
...@@ -297,7 +292,7 @@ def download_source(porter, tgtpath): ...@@ -297,7 +292,7 @@ def download_source(porter, tgtpath):
""" """
download source file from url, and save it to target path download source file from url, and save it to target path
""" """
if (os.path.exists(tgtpath) == False): if not os.path.exists(tgtpath):
print("download path %s does not exist\n", tgtpath) print("download path %s does not exist\n", tgtpath)
return False return False
s_info = porter.get_source_info() s_info = porter.get_source_info()
...@@ -321,17 +316,17 @@ def prepare_rpm_build_env(root): ...@@ -321,17 +316,17 @@ def prepare_rpm_build_env(root):
""" """
prepare environment for rpmbuild prepare environment for rpmbuild
""" """
if (os.path.exists(root) == False): if not os.path.exists(root):
print("Root path %s does not exist\n" & buildroot) print("Root path %s does not exist\n" & buildroot)
return "" return ""
buildroot = os.path.join(root, "rpmbuild") buildroot = os.path.join(root, "rpmbuild")
if (os.path.exists(buildroot) == False): if not os.path.exists(buildroot):
os.mkdir(buildroot) os.mkdir(buildroot)
for sdir in ['SPECS', 'BUILD', 'SOURCES', 'SRPMS', 'RPMS', 'BUILDROOT']: for sdir in ['SPECS', 'BUILD', 'SOURCES', 'SRPMS', 'RPMS', 'BUILDROOT']:
bpath = os.path.join(buildroot, sdir) bpath = os.path.join(buildroot, sdir)
if (os.path.exists(bpath) == False): if not os.path.exists(bpath):
os.mkdir(bpath) os.mkdir(bpath)
return buildroot return buildroot
...@@ -356,6 +351,7 @@ def try_pip_install_package(pkg): ...@@ -356,6 +351,7 @@ def try_pip_install_package(pkg):
# #
return True return True
def package_installed(pkg): def package_installed(pkg):
print(pkg) print(pkg)
ret = subprocess.call(["rpm", "-qi", pkg]) ret = subprocess.call(["rpm", "-qi", pkg])
...@@ -373,6 +369,7 @@ def dependencies_ready(req_list): ...@@ -373,6 +369,7 @@ def dependencies_ready(req_list):
# return req # return req
return "" return ""
def build_package(specfile): def build_package(specfile):
""" """
build rpm package with rpmbuild build rpm package with rpmbuild
...@@ -383,28 +380,29 @@ def build_package(specfile): ...@@ -383,28 +380,29 @@ def build_package(specfile):
def build_install_rpm(porter, rootpath): def build_install_rpm(porter, rootpath):
ret = build_rpm(porter, rootpath) ret = build_rpm(porter, rootpath)
if (ret != ""): if ret != "":
return ret return ret
arch = "noarch" arch = "noarch"
if (porter.is_build_noarch()): if porter.is_build_noarch():
arch = "noarch" arch = "noarch"
else: else:
arch = platform.machine() arch = platform.machine()
pkgname = os.path.join(rootpath, "rpmbuild", "RPMS", arch, porter.get_pkg_name() + "*") pkgname = os.path.join(rootpath, "rpmbuild", "RPMS", arch, porter.get_pkg_name() + "*")
ret = subprocess.call(["rpm", "-ivh", pkgname]) ret = subprocess.call(["rpm", "-ivh", pkgname])
if (ret != 0): if ret != 0:
return "Install failed\n" return "Install failed\n"
return "" return ""
def build_rpm(porter, rootpath): def build_rpm(porter, rootpath):
""" """
full process to build rpm full process to build rpm
""" """
buildroot = prepare_rpm_build_env(rootpath) buildroot = prepare_rpm_build_env(rootpath)
if (buildroot == ""): if buildroot == "":
return False return False
specfile = os.path.join(buildroot, "SPECS", porter.get_spec_name() + ".spec") specfile = os.path.join(buildroot, "SPECS", porter.get_spec_name() + ".spec")
...@@ -427,11 +425,11 @@ def build_spec(porter, output): ...@@ -427,11 +425,11 @@ def build_spec(porter, output):
print out the spec file print out the spec file
""" """
if os.path.isdir(output): if os.path.isdir(output):
output = os.path.join(output, porter.get_spec_name() + ".spec") output = os.path.join(output, porter.get_spec_name() + ".spec")
tmp = sys.stdout tmp = sys.stdout
if (output != ""): if output != "":
sys.stdout = open(output, 'w+') sys.stdout = open(output, 'w+')
print("%global _empty_manifest_terminate_build 0") print("%global _empty_manifest_terminate_build 0")
print(name_tag_template.format(pkg_name=porter.get_spec_name())) print(name_tag_template.format(pkg_name=porter.get_spec_name()))
print(version_tag_template.format(pkg_ver=porter.get_version())) print(version_tag_template.format(pkg_ver=porter.get_version()))
...@@ -454,7 +452,7 @@ def build_spec(porter, output): ...@@ -454,7 +452,7 @@ def build_spec(porter, output):
porter.prepare_build_requires() porter.prepare_build_requires()
build_req_list=porter.get_build_requires() build_req_list = porter.get_build_requires()
print("%description -n " + porter.get_pkg_name()) print("%description -n " + porter.get_pkg_name())
print(porter.get_description()) print(porter.get_description())
...@@ -534,47 +532,45 @@ def do_args(root): ...@@ -534,47 +532,45 @@ def do_args(root):
return parser return parser
def porter_creator(t_str, pkg): def porter_creator(t_str, pkg):
if (t_str == "python"): if t_str == "python":
return PyPorter(pkg) return PyPorter(pkg)
return None return None
if __name__ == "__main__": if __name__ == "__main__":
dft_root_path=os.path.join(str(Path.home())) dft_root_path = os.path.join(str(Path.home()))
parser = do_args(dft_root_path) parser = do_args(dft_root_path)
args = parser.parse_args() args = parser.parse_args()
porter = porter_creator(args.type, args.pkg) porter = porter_creator(args.type, args.pkg)
if (porter is None): if porter is None:
print("Type %s is not supported now\n" % args.type) print("Type %s is not supported now\n" % args.type)
sys.exit(1) sys.exit(1)
if args.requires:
if (args.requires): req_list = porter.get_build_requires()
reqlist = porter.get_build_requires() if req_list is not None:
if reqlist is not None: for req in req_list:
for req in reqlist:
print(req) print(req)
elif (args.spec): elif args.spec:
build_spec(porter, args.output) build_spec(porter, args.output)
elif (args.build): elif args.build:
ret = build_rpm(porter, args.rootpath) ret = build_rpm(porter, args.rootpath)
if ret != "": if ret != "":
print("build failed : BuildRequire : %s\n" % ret) print("build failed : BuildRequire : %s\n" % ret)
sys.exit(1) sys.exit(1)
elif (args.buildinstall): elif args.buildinstall:
ret = build_install_rpm(porter, args.rootpath) ret = build_install_rpm(porter, args.rootpath)
if ret != "": if ret != "":
print("Build & install failed\n") print("Build & install failed\n")
sys.exit(1) sys.exit(1)
elif (args.download): elif args.download:
download_source(porter, args.path) download_source(porter, args.path)
elif (args.json): elif args.json:
porter.store_json(args.path) porter.store_json(args.path)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册