From c69be030ec51d9a1a683b945203c083064c10bfd Mon Sep 17 00:00:00 2001 From: llj Date: Sat, 3 Jun 2023 13:14:35 +0800 Subject: [PATCH 1/2] update pyporter and tests --- pyporter/pyporter.py | 37 +++++--------- tests/TestLicense.json | 1 + tests/TestNoOSIApproved.json | 1 + tests/TestOSIApproved.json | 1 + tests/test_get_home.py | 2 +- tests/test_get_license.py | 98 ++++++++++++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 25 deletions(-) create mode 100644 tests/TestLicense.json create mode 100644 tests/TestNoOSIApproved.json create mode 100644 tests/TestOSIApproved.json create mode 100644 tests/test_get_license.py diff --git a/pyporter/pyporter.py b/pyporter/pyporter.py index b4f0ce0..e0c2c3e 100755 --- a/pyporter/pyporter.py +++ b/pyporter/pyporter.py @@ -33,7 +33,7 @@ import platform import logging from pathlib import Path import hashlib -from retry import retry_call +from retry.api import retry_call # init logging logger = logging.getLogger() @@ -113,22 +113,6 @@ class PyPorter: def get_module_name(self): return self.__module_name - def get_archive_name(self): - s_info = self.get_source_info() - if s_info: - # filename is {archive_name}-{version}.{suffix} - filename = s_info.get("filename") - if not filename: - return "" - filename = ( - filename[: -len(".tar.gz")] - if "tar.gz" in filename - else filename[: -len(".zip")] - ) - v = self.get_version() - return filename.replace("-" + v, "") - return "" - def get_pkg_name(self): return self.__pkg_name @@ -160,13 +144,14 @@ class PyPorter: 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 + or License :: Public Domain, seems like the license is at the end """ 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 ks[-1].strip() return "" def get_source_info(self): @@ -280,9 +265,9 @@ class PyPorter: if br == "": continue # - # Do not output BuildRequires: + # Do not output BuildRequires: # just collect all build requires and using pip to install - # than can help to build all rpm withoud trap into + # than can help to build all rpm withoud trap into # build dependency nightmare # # print(buildreq_tag_template.format(req=br)) @@ -318,7 +303,7 @@ class PyPorter: fname = json_file_template.format(pkg_name=self.__pkg_name) 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): with open(json_file, 'r') as f: resp = json.load(f) @@ -378,6 +363,7 @@ def download_source(porter, tgtpath): print("analyze source info error") return False s_url = s_info.get("url") + print(s_url, tgtpath) s_path = os.path.join(tgtpath, s_info.get("filename")) if os.path.exists(s_path): with open(s_path, 'rb') as f: @@ -440,7 +426,7 @@ def package_installed(pkg): 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): @@ -467,6 +453,9 @@ def build_install_rpm(porter, rootpath): else: arch = platform.machine() + if not os.path.exists(os.path.join(rootpath, "rpmbuild", "RPMS", arch)): + os.mkdir(os.path.join(rootpath, "rpmbuild", "RPMS", arch)) + pkgname = os.path.join(rootpath, "rpmbuild", "RPMS", arch, porter.get_pkg_name() + "*") ret = subprocess.call(["rpm", "-ivh", pkgname]) if ret != 0: @@ -506,7 +495,7 @@ def build_spec(porter, output): output = os.path.join(output, porter.get_spec_name() + ".spec") tmp = sys.stdout if output != "": - sys.stdout = open(output, 'w+') + sys.stdout = open(output, 'w+', encoding='utf-8') print("%global _empty_manifest_terminate_build 0") print(name_tag_template.format(pkg_name=porter.get_spec_name())) @@ -542,7 +531,7 @@ def build_spec(porter, output): print(porter.get_description()) print("") print("%prep") - print("%autosetup -n {name}-{ver}".format(name=porter.get_archive_name(), ver=porter.get_version())) + print("%autosetup -n {name}-{ver}".format(name=porter.get_module_name(), ver=porter.get_version())) print("") print("%build") porter.prepare_pkg_build() diff --git a/tests/TestLicense.json b/tests/TestLicense.json new file mode 100644 index 0000000..044f2ca --- /dev/null +++ b/tests/TestLicense.json @@ -0,0 +1 @@ +{"info":{"author":"Roland Hedberg","author_email":"roland@catalogix.se","bugtrack_url":null,"classifiers":["Development Status :: 4 - Beta","License :: OSI Approved :: Apache Software License","Programming Language :: Python :: 2.7","Programming Language :: Python :: 3.5","Topic :: Software Development :: Libraries :: Python Modules"],"description":"","description_content_type":"","docs_url":null,"download_url":"","downloads":{"last_day":-1,"last_month":-1,"last_week":-1},"home_page": null,"keywords":"","license":"Apache 2.0","maintainer":"","maintainer_email":"","name":"pyjwkest","package_url":null,"platform":"","project_url":null,"project_urls":null,"release_url":"https://pypi.org/project/pyjwkest/1.4.2/","requires_dist":null,"requires_python":"","summary":"Python implementation of JWT, JWE, JWS and JWK","version":"1.4.2","yanked":false,"yanked_reason":null},"last_serial":5455412,"releases":{"1.4.2":[{"comment_text":"","digests":{"blake2b_256":"531713eb3b49999171e265a8eff6415a39006c63af2ff8427d88639541605f30","md5":"e5da7de4c94266b99b4c79f635d73991","sha256":"35ace8f52d87dd4d5b108e615882d04f40eb3151cc6e3b370ed65409814bcc3d"},"downloads":-1,"filename":"pyjwkest-0.3.0.tar.gz","has_sig":false,"md5_digest":"e5da7de4c94266b99b4c79f635d73991","packagetype":"sdist","python_version":"source","requires_python":null,"size":35970,"upload_time":"2014-03-07T08:44:35","upload_time_iso_8601":"2014-03-07T08:44:35.936721Z","url":"https://files.pythonhosted.org/packages/53/17/13eb3b49999171e265a8eff6415a39006c63af2ff8427d88639541605f30/pyjwkest-0.3.0.tar.gz","yanked":false,"yanked_reason":null}]}} diff --git a/tests/TestNoOSIApproved.json b/tests/TestNoOSIApproved.json new file mode 100644 index 0000000..babd082 --- /dev/null +++ b/tests/TestNoOSIApproved.json @@ -0,0 +1 @@ +{"info":{"author":"Fabien Coelho","author_email":"flask.auth@coelho.net","bugtrack_url":null,"classifiers":["Development Status :: 5 - Production/Stable","License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication","Programming Language :: Python","Topic :: Software Development :: Libraries :: Python Modules"],"description":"","description_content_type":"","docs_url":null,"download_url":"","downloads":{"last_day":-1,"last_month":-1,"last_week":-1},"home_page": "https://github.com/zx80/flask-simple-auth","keywords":"","license":"","maintainer":"","maintainer_email":"","name":"FlaskSimpleAuth","package_url":"https://pypi.org/project/FlaskSimpleAuth/","platform":"","project_url":null,"project_urls":null,"release_url":"https://pypi.org/project/pyjwkest/1.4.2/","requires_dist":null,"requires_python":"","summary":"Python implementation of JWT, JWE, JWS and JWK","version":"1.4.2","yanked":false,"yanked_reason":null},"last_serial":5455412,"releases":{"1.4.2":[{"comment_text":"","digests":{"blake2b_256":"531713eb3b49999171e265a8eff6415a39006c63af2ff8427d88639541605f30","md5":"e5da7de4c94266b99b4c79f635d73991","sha256":"35ace8f52d87dd4d5b108e615882d04f40eb3151cc6e3b370ed65409814bcc3d"},"downloads":-1,"filename":"pyjwkest-0.3.0.tar.gz","has_sig":false,"md5_digest":"e5da7de4c94266b99b4c79f635d73991","packagetype":"sdist","python_version":"source","requires_python":null,"size":35970,"upload_time":"2014-03-07T08:44:35","upload_time_iso_8601":"2014-03-07T08:44:35.936721Z","url":"https://files.pythonhosted.org/packages/53/17/13eb3b49999171e265a8eff6415a39006c63af2ff8427d88639541605f30/pyjwkest-0.3.0.tar.gz","yanked":false,"yanked_reason":null}]}} diff --git a/tests/TestOSIApproved.json b/tests/TestOSIApproved.json new file mode 100644 index 0000000..15e3257 --- /dev/null +++ b/tests/TestOSIApproved.json @@ -0,0 +1 @@ +{"info":{"author":"Roland Hedberg","author_email":"roland@catalogix.se","bugtrack_url":null,"classifiers":["Development Status :: 4 - Beta","License :: OSI Approved :: Apache Software License","Programming Language :: Python :: 2.7","Programming Language :: Python :: 3.5","Topic :: Software Development :: Libraries :: Python Modules"],"description":"","description_content_type":"","docs_url":null,"download_url":"","downloads":{"last_day":-1,"last_month":-1,"last_week":-1},"home_page": null,"keywords":"","license":"","maintainer":"","maintainer_email":"","name":"pyjwkest","package_url":null,"platform":"","project_url":null,"project_urls":null,"release_url":"https://pypi.org/project/pyjwkest/1.4.2/","requires_dist":null,"requires_python":"","summary":"Python implementation of JWT, JWE, JWS and JWK","version":"1.4.2","yanked":false,"yanked_reason":null},"last_serial":5455412,"releases":{"1.4.2":[{"comment_text":"","digests":{"blake2b_256":"531713eb3b49999171e265a8eff6415a39006c63af2ff8427d88639541605f30","md5":"e5da7de4c94266b99b4c79f635d73991","sha256":"35ace8f52d87dd4d5b108e615882d04f40eb3151cc6e3b370ed65409814bcc3d"},"downloads":-1,"filename":"pyjwkest-0.3.0.tar.gz","has_sig":false,"md5_digest":"e5da7de4c94266b99b4c79f635d73991","packagetype":"sdist","python_version":"source","requires_python":null,"size":35970,"upload_time":"2014-03-07T08:44:35","upload_time_iso_8601":"2014-03-07T08:44:35.936721Z","url":"https://files.pythonhosted.org/packages/53/17/13eb3b49999171e265a8eff6415a39006c63af2ff8427d88639541605f30/pyjwkest-0.3.0.tar.gz","yanked":false,"yanked_reason":null}]}} diff --git a/tests/test_get_home.py b/tests/test_get_home.py index 5ff8941..5a375bb 100644 --- a/tests/test_get_home.py +++ b/tests/test_get_home.py @@ -7,7 +7,7 @@ from pyporter.pyporter import porter_creator class TestNoHomepageNone(unittest.TestCase): def setUp(self): - self.f = open(os.path.join('tests', __class__.__name__+'.json')) + self.f = open(os.path.join(__class__.__name__+'.json')) self.data = self.f.read().encode() @patch('urllib.request.urlopen') diff --git a/tests/test_get_license.py b/tests/test_get_license.py new file mode 100644 index 0000000..828c8a4 --- /dev/null +++ b/tests/test_get_license.py @@ -0,0 +1,98 @@ +import unittest +import io +import os +import urllib.request +from pathlib import Path +from unittest.mock import patch, MagicMock +from pyporter.pyporter import porter_creator + + +class TestNoOSIApproved(unittest.TestCase): + """ + case 1: json["info"]["license"] == "" and the License in json["info"]["classifiers"] is not OSI approved + """ + def setUp(self): + self.f = open(os.path.join(__class__.__name__ + '.json')) + self.data = self.f.read().encode() + + @patch('urllib.request.urlopen') + def test_no_OSI_approved(self, m): + cm = MagicMock() + cm.read.return_value = self.data + cm.__enter__.return_value = cm + m.return_value = cm + + args = MagicMock() + args.configure_mock(type="python") + args.pkg = "oic" + args.pkgversion = "1.5.0" + args.arch = None + args.mirror = "" + + p = porter_creator(args) + self.assertEqual('CC0 1.0 Universal (CC0 1.0) Public Domain Dedication', p.get_license()) + + def tearDown(self): + self.f.close() + + +class TestOSIApproved(unittest.TestCase): + """ + case 2: json["info"]["license"] == "" and the License in json["info"]["classifiers"] is OSI approved + """ + def setUp(self): + self.f = open(os.path.join(__class__.__name__ + '.json')) + self.data = self.f.read().encode() + + @patch('urllib.request.urlopen') + def test_OSI_approved(self, m): + cm = MagicMock() + cm.read.return_value = self.data + cm.__enter__.return_value = cm + m.return_value = cm + + args = MagicMock() + args.configure_mock(type="python") + args.pkg = "oic" + args.pkgversion = "1.5.0" + args.arch = None + args.mirror = "" + + p = porter_creator(args) + self.assertEqual('Apache Software License', p.get_license()) + + def tearDown(self): + self.f.close() + + +class TestLicense(unittest.TestCase): + """ + case 3: json["info"]["license"] != "" + """ + def setUp(self): + self.f = open(os.path.join(__class__.__name__ + '.json')) + self.data = self.f.read().encode() + + @patch('urllib.request.urlopen') + def test_OSI_approved(self, m): + cm = MagicMock() + cm.read.return_value = self.data + cm.__enter__.return_value = cm + m.return_value = cm + + args = MagicMock() + args.configure_mock(type="python") + args.pkg = "oic" + args.pkgversion = "1.5.0" + args.arch = None + args.mirror = "" + + p = porter_creator(args) + self.assertEqual('Apache 2.0', p.get_license()) + + def tearDown(self): + self.f.close() + + +if __name__ == '__main__': + unittest.main() -- Gitee From d8692e50455b47ed1916a13b09eaf438d3534c08 Mon Sep 17 00:00:00 2001 From: llj Date: Sat, 3 Jun 2023 17:46:18 +0800 Subject: [PATCH 2/2] update tests --- pyporter/pyporter.py | 1 + tests/test_get_license.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pyporter/pyporter.py b/pyporter/pyporter.py index e0c2c3e..e2c9ea9 100755 --- a/pyporter/pyporter.py +++ b/pyporter/pyporter.py @@ -651,5 +651,6 @@ def main(): elif args.json: porter.store_json(args.path) + if __name__ == "__main__": main() diff --git a/tests/test_get_license.py b/tests/test_get_license.py index 828c8a4..1227f93 100644 --- a/tests/test_get_license.py +++ b/tests/test_get_license.py @@ -12,7 +12,7 @@ class TestNoOSIApproved(unittest.TestCase): case 1: json["info"]["license"] == "" and the License in json["info"]["classifiers"] is not OSI approved """ def setUp(self): - self.f = open(os.path.join(__class__.__name__ + '.json')) + self.f = open(os.path.join('tests', __class__.__name__ + '.json')) self.data = self.f.read().encode() @patch('urllib.request.urlopen') @@ -41,7 +41,7 @@ class TestOSIApproved(unittest.TestCase): case 2: json["info"]["license"] == "" and the License in json["info"]["classifiers"] is OSI approved """ def setUp(self): - self.f = open(os.path.join(__class__.__name__ + '.json')) + self.f = open(os.path.join('tests', __class__.__name__ + '.json')) self.data = self.f.read().encode() @patch('urllib.request.urlopen') @@ -70,7 +70,7 @@ class TestLicense(unittest.TestCase): case 3: json["info"]["license"] != "" """ def setUp(self): - self.f = open(os.path.join(__class__.__name__ + '.json')) + self.f = open(os.path.join('tests', __class__.__name__ + '.json')) self.data = self.f.read().encode() @patch('urllib.request.urlopen') -- Gitee