From 503ea5ad507d20fcc29f7c7b0729d0c31bc300f8 Mon Sep 17 00:00:00 2001 From: danghai Date: Wed, 8 Aug 2018 05:31:11 -0700 Subject: [PATCH] Enhance join_with_slash(), and move to misc.py Fixes #121 --- sktm/__init__.py | 18 ----------------- sktm/jenkins.py | 9 ++++----- sktm/misc.py | 20 +++++++++++++++++++ sktm/patchwork.py | 42 +++++++++++++++++--------------------- sktm/reporter.py | 2 +- tests/test_init.py | 32 ----------------------------- tests/test_misc.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 93 insertions(+), 80 deletions(-) create mode 100644 tests/test_misc.py diff --git a/sktm/__init__.py b/sktm/__init__.py index 4e51f7e..079bcda 100644 --- a/sktm/__init__.py +++ b/sktm/__init__.py @@ -26,24 +26,6 @@ import sktm.patchwork -def join_with_slash(base, *suffix): - """ - Join parts of URL or path by slashes - - Args: - base: Base URL or path. - *suffix: Array of suffixes - - Returns: - The URL or path string - """ - parts = [base.rstrip('/')] - for arg in suffix: - parts.append(arg.strip('/')) - ending = '/' if arg.endswith('/') else '' - return '/'.join(parts) + ending - - class JobType(enum.IntEnum): """Job type""" BASELINE = 0 diff --git a/sktm/jenkins.py b/sktm/jenkins.py index 07711ad..4cab4c5 100644 --- a/sktm/jenkins.py +++ b/sktm/jenkins.py @@ -18,8 +18,7 @@ import jenkinsapi -import sktm -from sktm.misc import TestResult +from sktm.misc import TestResult, join_with_slash class JenkinsProject(object): @@ -315,9 +314,9 @@ def get_result_url(self, buildid): Result: The URL of the build result. """ - return sktm.join_with_slash(self.server.base_server_url(), - "job", - str(buildid)) + return join_with_slash(self.server.base_server_url(), + "job", + str(buildid)) def get_result(self, buildid): """ diff --git a/sktm/misc.py b/sktm/misc.py index 4719b3f..5409f7d 100644 --- a/sktm/misc.py +++ b/sktm/misc.py @@ -22,3 +22,23 @@ class TestResult(enum.IntEnum): MERGE_FAILURE = 1 BUILD_FAILURE = 2 TEST_FAILURE = 4 + + +def join_with_slash(base, *suffix_tuple): + """ + Join parts of URL or path by slashes. Trailing slash of base, and each + arg in suffix_tupple are removed. It only keeps trailing slash at the + end of the part if it is specified. + + Args: + base: Base URL or path. + *suffix_tuple: Tuple of suffixes + + Returns: + The URL or path string + """ + parts = [base.rstrip('/')] + for arg in suffix_tuple: + parts.append(arg.strip('/')) + ending = '/' if arg.endswith('/') else '' + return '/'.join(parts) + ending diff --git a/sktm/patchwork.py b/sktm/patchwork.py index 119ba91..43528d8 100644 --- a/sktm/patchwork.py +++ b/sktm/patchwork.py @@ -25,8 +25,7 @@ import dateutil.parser import requests -import sktm -from sktm.misc import TestResult +from sktm.misc import TestResult, join_with_slash SKIP_PATTERNS = [ @@ -86,7 +85,7 @@ def get_mbox_url(self): Returns: URL pointing at the object's mbox. """ - return sktm.join_with_slash(self.url, self.mbox_sfx) + return join_with_slash(self.url, self.mbox_sfx) class SeriesSummary(object): @@ -309,10 +308,10 @@ def __get_patch_message(self, patch_id): of requests exceptions, Exception in case of unexpected return code (eg. nonexistent patch). """ - mbox_url = sktm.join_with_slash(self.baseurl, - 'patch', - str(patch_id), - self._get_mbox_url_sfx()) + mbox_url = join_with_slash(self.baseurl, + 'patch', + str(patch_id), + self._get_mbox_url_sfx()) try: response = requests.get(mbox_url) @@ -402,9 +401,9 @@ def _get_patch_url(self, patch): Returns: Patch URL. """ - return sktm.join_with_slash(self.baseurl, - 'patch', - str(patch.get('id'))) + return join_with_slash(self.baseurl, + 'patch', + str(patch.get('id'))) def _get_mbox_url_sfx(self): """ @@ -458,10 +457,8 @@ def _get_project_id(self, project_name): Returns: Integer representing project's ID. """ - response = requests.get( - sktm.join_with_slash(self.apiurls.get("projects"), - project_name) - ) + response = requests.get(join_with_slash(self.apiurls.get("projects"), + project_name)) if response.status_code != requests.codes.ok: raise Exception("Can't get project data: %s %d" % (project_name, response.status_code)) @@ -475,7 +472,7 @@ def __get_apiurls(self, baseurl): Returns: The JSON representation of the API URLs. """ - response = requests.get(sktm.join_with_slash(baseurl, "api")) + response = requests.get(join_with_slash(baseurl, "api")) if response.status_code != 200: raise Exception("Can't get apiurls: %d" % response.status_code) @@ -696,10 +693,8 @@ def get_patch_by_id(self, pid): set of supported attributes depends on which API versions are supported by a specific Patchwork instance. """ - response = requests.get( - sktm.join_with_slash(self.apiurls.get("patches"), - str(pid)) - ) + response = requests.get(join_with_slash(self.apiurls.get("patches"), + str(pid))) if response.status_code != 200: raise Exception("Can't get patch by id %d (%d)" % @@ -745,8 +740,7 @@ def __get_patchsets_by_patch(self, url, seen=set()): continue else: series_list += self.__get_series_from_url( - sktm.join_with_slash(self.apiurls.get("series"), - str(sid)) + join_with_slash(self.apiurls.get("series"), str(sid)) ) seen.add(sid) @@ -811,8 +805,8 @@ def get_patchsets(self, patchlist): sid = series.get("id") if sid not in seen: series_list += self.__get_series_from_url( - sktm.join_with_slash(self.apiurls.get("series"), - str(sid)) + join_with_slash(self.apiurls.get("series"), + str(sid)) ) seen.add(sid) @@ -870,7 +864,7 @@ def __get_rpc(self, baseurl): Returns: The XML RPC interface for the Patchwork """ - rpc = xmlrpclib.ServerProxy(sktm.join_with_slash(baseurl, "xmlrpc/")) + rpc = xmlrpclib.ServerProxy(join_with_slash(baseurl, "xmlrpc/")) try: ver = rpc.pw_rpc_version() # check for normal patchwork1 xmlrpc version numbers diff --git a/sktm/reporter.py b/sktm/reporter.py index 367082a..085b48e 100644 --- a/sktm/reporter.py +++ b/sktm/reporter.py @@ -19,7 +19,7 @@ import re import smtplib -from sktm import join_with_slash +from sktm.misc import join_with_slash SUBSTITUTE_RE = re.compile(r'\{[\w\.]+\}') diff --git a/tests/test_init.py b/tests/test_init.py index a91267d..6f1d2d7 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -21,38 +21,6 @@ import sktm -class TestIndependent(unittest.TestCase): - """Test cases for independent functions in __init__.py.""" - - def test_join_with_slash(self): - """Ensure join_with_slash return a good url, path string.""" - base = "path/to/dir" - suffix = "file" - self.assertEqual("path/to/dir/file", - sktm.join_with_slash(base, suffix)) - base = "path/to/dir/" - suffix = "file" - self.assertEqual("path/to/dir/file", - sktm.join_with_slash(base, suffix)) - base = "path/to/dir1/" - suffix = "dir2/" - self.assertEqual("path/to/dir1/dir2/", - sktm.join_with_slash(base, suffix)) - base = "path/to/dir1/" - suffix1 = "dir2/" - suffix2 = "file" - self.assertEqual("path/to/dir1/dir2/file", - sktm.join_with_slash(base, suffix1, suffix2)) - base = "http://url.com/" - suffix = "part" - self.assertEqual("http://url.com/part", - sktm.join_with_slash(base, suffix)) - base = "http://url.com" - suffix = "part" - self.assertEqual("http://url.com/part", - sktm.join_with_slash(base, suffix)) - - class TestInit(unittest.TestCase): """Test cases for the __init__ module.""" diff --git a/tests/test_misc.py b/tests/test_misc.py new file mode 100644 index 0000000..3f7493d --- /dev/null +++ b/tests/test_misc.py @@ -0,0 +1,50 @@ +# Copyright (c) 2018 Red Hat, Inc. All rights reserved. This copyrighted +# material is made available to anyone wishing to use, modify, copy, or +# redistribute it subject to the terms and conditions of the GNU General Public +# License v.2 or later. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 51 +# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +"""Tests for the misc.py.""" + +import unittest + +import sktm.misc + + +class TestIndependent(unittest.TestCase): + """Test cases for independent functions in misc.py.""" + + def test_join_with_slash(self): + """Ensure join_with_slash return a good url, path string.""" + base = "path/to/dir" + suffix = "file" + self.assertEqual("path/to/dir/file", + sktm.misc.join_with_slash(base, suffix)) + base = "path/to/dir/" + suffix = "file" + self.assertEqual("path/to/dir/file", + sktm.misc.join_with_slash(base, suffix)) + base = "path/to/dir1/" + suffix = "dir2/" + self.assertEqual("path/to/dir1/dir2/", + sktm.misc.join_with_slash(base, suffix)) + base = "path/to/dir1/" + suffix1 = "dir2/" + suffix2 = "file" + self.assertEqual("path/to/dir1/dir2/file", + sktm.misc.join_with_slash(base, suffix1, suffix2)) + base = "http://url.com/" + suffix = "part" + self.assertEqual("http://url.com/part", + sktm.misc.join_with_slash(base, suffix)) + base = "http://url.com" + suffix = "part" + self.assertEqual("http://url.com/part", + sktm.misc.join_with_slash(base, suffix))