From 36331a849cf122faaf4c6e8917e56a47adc53fa9 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Thu, 12 Aug 2021 21:17:36 -0400 Subject: [PATCH] Refactor: Fix flake8 F841 Resolves "F841 local variable ____ is assigned to but never used". Change-Id: I4ab246dd96c96f2fdf33bae1d7d6b2d2c6fe6190 Signed-off-by: Thanh Ha --- lftools/api/endpoints/gerrit.py | 2 +- lftools/cli/__init__.py | 4 ++-- lftools/cli/jenkins/token.py | 2 +- lftools/cli/ldap_cli.py | 2 +- lftools/nexus/cmd.py | 10 +++++----- tests/test_deploy.py | 36 ++++++++++++++---------------------- tests/test_release_docker_hub.py | 10 ++++------ 7 files changed, 28 insertions(+), 38 deletions(-) diff --git a/lftools/api/endpoints/gerrit.py b/lftools/api/endpoints/gerrit.py index d33d2457..4060e1e2 100644 --- a/lftools/api/endpoints/gerrit.py +++ b/lftools/api/endpoints/gerrit.py @@ -110,7 +110,7 @@ class Gerrit(client.RestApi): # Setup signed_off_by = config.get_setting(fqdn, "sob") gerrit_project_dashed = gerrit_project.replace("/", "-") - gerrit_project_encoded = urllib.parse.quote(gerrit_project, safe="", encoding=None, errors=None) + urllib.parse.quote(gerrit_project, safe="", encoding=None, errors=None) filename = "{}.yaml".format(gerrit_project_dashed) if not reviewid: diff --git a/lftools/cli/__init__.py b/lftools/cli/__init__.py index 26cc15a2..458950c1 100644 --- a/lftools/cli/__init__.py +++ b/lftools/cli/__init__.py @@ -61,7 +61,7 @@ def cli(ctx, debug, interactive, password, username): else: try: username = conf.get_setting("global", "username") - except (configparser.NoOptionError, configparser.NoSectionError) as e: + except (configparser.NoOptionError, configparser.NoSectionError): username = None if password is None: @@ -70,7 +70,7 @@ def cli(ctx, debug, interactive, password, username): else: try: password = conf.get_setting("global", "password") - except (configparser.NoOptionError, configparser.NoSectionError) as e: + except (configparser.NoOptionError, configparser.NoSectionError): password = None ctx.obj["username"] = username diff --git a/lftools/cli/jenkins/token.py b/lftools/cli/jenkins/token.py index bc660705..3ec386ef 100644 --- a/lftools/cli/jenkins/token.py +++ b/lftools/cli/jenkins/token.py @@ -130,7 +130,7 @@ def reset(ctx, servers): with open(jenkins.config_file, "w") as configfile: config.write(configfile) return token - except requests.exceptions.ConnectionError as e: + except requests.exceptions.ConnectionError: return None fail = 0 diff --git a/lftools/cli/ldap_cli.py b/lftools/cli/ldap_cli.py index 9e82153f..1d6d6f44 100644 --- a/lftools/cli/ldap_cli.py +++ b/lftools/cli/ldap_cli.py @@ -131,7 +131,7 @@ def csv(ctx, ldap_server, ldap_group_base, ldap_user_base, groups): if result_type == ldap.RES_SEARCH_ENTRY: result_set.append(result_data) return result_set - except ldap.LDAPError as e: + except ldap.LDAPError: sys.exit(1) def package_groups(groups): diff --git a/lftools/nexus/cmd.py b/lftools/nexus/cmd.py index 950c7107..f2523c98 100644 --- a/lftools/nexus/cmd.py +++ b/lftools/nexus/cmd.py @@ -92,7 +92,7 @@ def reorder_staged_repos(settings_file): try: repo_id = _nexus.get_repo_group("Staging Repositories") - except LookupError as e: + except LookupError: log.error("Staging repository 'Staging Repositories' cannot be found") sys.exit(1) @@ -155,7 +155,7 @@ def create_repos(config_file, settings_file, url): # Create target try: target_id = _nexus.get_target(name) - except LookupError as e: + except LookupError: target_id = _nexus.create_target(name, targets) # Create privileges @@ -171,21 +171,21 @@ def create_repos(config_file, settings_file, url): try: privs[priv] = _nexus.get_priv(name, priv) log.info("Creating {} privileges.".format(priv)) - except LookupError as e: + except LookupError: privs[priv] = _nexus.create_priv(name, target_id, priv) # Create Role try: role_id = _nexus.get_role(name) log.info("Role {} already exists.".format(role_id)) - except LookupError as e: + except LookupError: role_id = _nexus.create_role(name, privs) # Create user try: _nexus.get_user(name) log.info("User {} already exists.".format(name)) - except LookupError as e: + except LookupError: _nexus.create_user(name, email, role_id, password, extra_privs) def build_repo(repo, repoId, config, base_groupId, global_privs, email_domain, strict=True): diff --git a/tests/test_deploy.py b/tests/test_deploy.py index 88d2e6ee..1fd1ab5a 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -285,7 +285,6 @@ def test_remove_duplicates_and_sort(): def test_deploy_logs(cli_runner, datafiles, responses): """Test deploy_logs() command for expected upload cases.""" os.chdir(str(datafiles)) - workspace_dir = os.path.join(str(datafiles), "workspace") # Test successful upload build_url = "https://jenkins.example.org/job/builder-check-poms/204" @@ -464,44 +463,43 @@ def test__request_post_file(responses, mocker): """Test _request_post_file.""" zip_file = "zip-test-files/test.zip" - resp = {} test_url = "http://connection.error.test" exception = requests.exceptions.ConnectionError(test_url) responses.add(responses.POST, test_url, body=exception) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file) + deploy_sys._request_post_file(test_url, zip_file) assert "Could not connect to URL" in str(excinfo.value) test_url = "http://invalid.url.test:8081" exception = requests.exceptions.InvalidURL(test_url) responses.add(responses.POST, test_url, body=exception) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file) + deploy_sys._request_post_file(test_url, zip_file) assert "Invalid URL" in str(excinfo.value) test_url = "http://missing.schema.test:8081" exception = requests.exceptions.MissingSchema(test_url) responses.add(responses.POST, test_url, body=exception) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file) + deploy_sys._request_post_file(test_url, zip_file) assert "Not valid URL" in str(excinfo.value) test_url = "http://repository.read.only:8081" responses.add(responses.POST, test_url, body=None, status=400) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file) + deploy_sys._request_post_file(test_url, zip_file) assert "Repository is read only" in str(excinfo.value) test_url = "http://repository.not.found:8081" responses.add(responses.POST, test_url, body=None, status=404) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file) + deploy_sys._request_post_file(test_url, zip_file) assert "Did not find repository" in str(excinfo.value) test_url = "http://other.upload.error:8081" responses.add(responses.POST, test_url, body=None, status=500) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file) + deploy_sys._request_post_file(test_url, zip_file) assert "Failed to upload to Nexus with status code" in str(excinfo.value) @@ -510,44 +508,43 @@ def test__request_post_file_data(responses, mocker): param = {"r": (None, "testing")} zip_file = "zip-test-files/test.zip" - resp = {} test_url = "http://connection.error.test" exception = requests.exceptions.ConnectionError(test_url) responses.add(responses.POST, test_url, body=exception) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file, param) + deploy_sys._request_post_file(test_url, zip_file, param) assert "Could not connect to URL" in str(excinfo.value) test_url = "http://invalid.url.test:8081" exception = requests.exceptions.InvalidURL(test_url) responses.add(responses.POST, test_url, body=exception) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file, param) + deploy_sys._request_post_file(test_url, zip_file, param) assert "Invalid URL" in str(excinfo.value) test_url = "http://missing.schema.test:8081" exception = requests.exceptions.MissingSchema(test_url) responses.add(responses.POST, test_url, body=exception) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file, param) + deploy_sys._request_post_file(test_url, zip_file, param) assert "Not valid URL" in str(excinfo.value) test_url = "http://repository.read.only:8081" responses.add(responses.POST, test_url, body=None, status=400) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file, param) + deploy_sys._request_post_file(test_url, zip_file, param) assert "Repository is read only" in str(excinfo.value) test_url = "http://repository.not.found:8081" responses.add(responses.POST, test_url, body=None, status=404) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file, param) + deploy_sys._request_post_file(test_url, zip_file, param) assert "Did not find repository" in str(excinfo.value) test_url = "http://other.upload.error:8081" responses.add(responses.POST, test_url, body=None, status=500) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys._request_post_file(test_url, zip_file, param) + deploy_sys._request_post_file(test_url, zip_file, param) assert "Failed to upload to Nexus with status code" in str(excinfo.value) @@ -729,15 +726,10 @@ def test__upload_maven_file_to_nexus(responses, mocker): artifact_id = "ArtId2" version = "1.2.7" packaging = "tar.xz" - classified = None - - resp = {} test_url = "http://all.ok.upload:8081" responses.add(responses.POST, "{}/{}".format(test_url, common_urlpart), body=None, status=201) - resp = deploy_sys.upload_maven_file_to_nexus( - test_url, nexus_repo_id, group_id, artifact_id, version, packaging, zip_file - ) + deploy_sys.upload_maven_file_to_nexus(test_url, nexus_repo_id, group_id, artifact_id, version, packaging, zip_file) xml_other_error = """ @@ -748,7 +740,7 @@ def test__upload_maven_file_to_nexus(responses, mocker): test_url = "http://something.went.wrong:8081" responses.add(responses.POST, "{}/{}".format(test_url, common_urlpart), body=xml_other_error, status=405) with pytest.raises(requests.HTTPError) as excinfo: - resp = deploy_sys.upload_maven_file_to_nexus( + deploy_sys.upload_maven_file_to_nexus( test_url, nexus_repo_id, group_id, artifact_id, version, packaging, zip_file ) assert "Something went wrong" in str(excinfo.value) diff --git a/tests/test_release_docker_hub.py b/tests/test_release_docker_hub.py index b7547d07..63dcae35 100644 --- a/tests/test_release_docker_hub.py +++ b/tests/test_release_docker_hub.py @@ -212,7 +212,6 @@ class TestTagsRegExpClass: def test_tag_class_manual_version_regexp_str_from_file_valid(self, datafiles): org = "onap" test_regexp_from_file = os.path.join(str(datafiles), "releasedockerhub_good_regexp") - repo_from_file = False rdh.initialize(org, test_regexp_from_file) assert rdh.validate_regexp() == True assert rdh.VERSION_REGEXP == "^\d+.\d+" @@ -220,7 +219,6 @@ class TestTagsRegExpClass: def test_tag_class_manual_version_regexp_str_from_file_invalid(self, datafiles): org = "onap" test_regexp_from_file = os.path.join(str(datafiles), "releasedockerhub_bad_regexp") - repo_from_file = False rdh.initialize(org, test_regexp_from_file) assert rdh.validate_regexp() == False assert rdh.VERSION_REGEXP == "[" @@ -458,25 +456,25 @@ class TestProjectClass: # Verify that 90 timeout's on any stage failes. self.nbr_exc.pull = 90 - with pytest.raises(requests.HTTPError) as excinfo: + with pytest.raises(requests.HTTPError): test_proj.docker_pull_tag_push() self.counter.pull = self.counter.tag = self.counter.push = self.counter.cleanup = 0 self.nbr_exc.pull = 0 self.nbr_exc.tag = 90 - with pytest.raises(requests.HTTPError) as excinfo: + with pytest.raises(requests.HTTPError): test_proj.docker_pull_tag_push() self.counter.pull = self.counter.tag = self.counter.push = self.counter.cleanup = 0 self.nbr_exc.pull = self.nbr_exc.tag = 0 self.nbr_exc.push = 90 - with pytest.raises(requests.HTTPError) as excinfo: + with pytest.raises(requests.HTTPError): test_proj.docker_pull_tag_push() self.counter.pull = self.counter.tag = self.counter.push = self.counter.cleanup = 0 self.nbr_exc.pull = self.nbr_exc.tag = self.nbr_exc.push = 0 self.nbr_exc.cleanup = 90 - with pytest.raises(requests.HTTPError) as excinfo: + with pytest.raises(requests.HTTPError): test_proj.docker_pull_tag_push() # Verify 89 timeouts and the 90 is ok per stage -- 2.16.6