Clean up code

Summary:
  * Remove unused import
  * Remove unused variables
  * Empty string is always False, don't check it twice
  * Use os.path.join to create a path

Test Plan: Check that the behaviour is the same as before.

Reviewers: tsimonq2

Reviewed By: tsimonq2

Differential Revision: https://phab.lubuntu.me/D65
master
apt-ghetto 5 years ago committed by Simon Quigley
parent ce9e4a5080
commit 04ea168cff

@ -16,8 +16,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import git import git
import jenkinsapi from os import getenv, path
from os import getenv
from yaml import CLoader from yaml import CLoader
from yaml import load as yaml_load from yaml import load as yaml_load
from jinja2 import Template from jinja2 import Template
@ -25,6 +24,7 @@ from shutil import rmtree
from tempfile import mkdtemp from tempfile import mkdtemp
from jenkinsapi.jenkins import Jenkins from jenkinsapi.jenkins import Jenkins
class Generator: class Generator:
def clone_metadata(self): def clone_metadata(self):
"""Clone the metadata repository using the values set in the env vars """Clone the metadata repository using the values set in the env vars
@ -43,8 +43,7 @@ class Generator:
# because otherwise we have no metadata to actually pull from # because otherwise we have no metadata to actually pull from
metadata_url = getenv("METADATA_URL") metadata_url = getenv("METADATA_URL")
metadata_repo_name = getenv("METADATA_REPO_NAME") metadata_repo_name = getenv("METADATA_REPO_NAME")
if not metadata_url or metadata_url == "" or not metadata_repo_name \ if not metadata_url or not metadata_repo_name:
or metadata_repo_name == "":
raise ValueError("METADATA_URL and METADATA_REPO_NAME must be set") raise ValueError("METADATA_URL and METADATA_REPO_NAME must be set")
# Create a temporary directory in the most secure manner possible and # Create a temporary directory in the most secure manner possible and
@ -54,8 +53,10 @@ class Generator:
git.Git(metadata_loc).clone(metadata_url) git.Git(metadata_loc).clone(metadata_url)
# Load ci.conf and parse it # Load ci.conf and parse it
with open(metadata_loc + "/" + metadata_repo_name + "/ci.conf") \ config_file = path.join(metadata_loc,
as metadata_conf_file: metadata_repo_name,
"ci.conf")
with open(config_file) as metadata_conf_file:
metadata_conf = yaml_load(metadata_conf_file, Loader=CLoader) metadata_conf = yaml_load(metadata_conf_file, Loader=CLoader)
finally: finally:
rmtree(metadata_loc) rmtree(metadata_loc)
@ -80,11 +81,11 @@ class Generator:
for package in metadata_conf["repositories"]: for package in metadata_conf["repositories"]:
# Load defaults in if they're not there, ignore the optional ones # Load defaults in if they're not there, ignore the optional ones
for mkey in metadata_req_keys: for mkey in metadata_req_keys:
if not mkey in package and mkey in metadata_conf["default"]: if mkey not in package and mkey in metadata_conf["default"]:
package[mkey] = metadata_conf["default"][mkey] package[mkey] = metadata_conf["default"][mkey]
# Don't proceed if any of the keys in the config are invalid # Don't proceed if any of the keys in the config are invalid
for mkey in package: for mkey in package:
if not mkey in metadata_req_keys and not mkey in \ if mkey not in metadata_req_keys and mkey not in \
metadata_opt_keys: metadata_opt_keys:
raise ValueError("Invalid key present:", mkey) raise ValueError("Invalid key present:", mkey)
@ -102,7 +103,7 @@ class Generator:
api_user = getenv("API_USER") api_user = getenv("API_USER")
api_key = getenv("API_KEY") api_key = getenv("API_KEY")
for envvar in [api_site, api_user, api_key]: for envvar in [api_site, api_user, api_key]:
if not envvar or envvar == "": if not envvar:
raise ValueError("API_SITE, API_USER, and API_KEY must be", raise ValueError("API_SITE, API_USER, and API_KEY must be",
"defined") "defined")
# Authenticate to the server # Authenticate to the server
@ -118,7 +119,9 @@ class Generator:
# The template name should always correspond with the job type # The template name should always correspond with the job type
# Regardless of the job type, there should always be a template # Regardless of the job type, there should always be a template
with open("templates/" + job_type + ".xml") as templatef: template_path = path.join("templates",
job_type + ".xml")
with open(template_path) as templatef:
template = "" template = ""
for text in templatef.readlines(): for text in templatef.readlines():
template += text template += text
@ -176,9 +179,8 @@ class Generator:
# just create it # just create it
metadata = self.parse_metadata() metadata = self.parse_metadata()
jobs = [] jobs = []
releases = set()
for job_name, job_instance in server.get_jobs(): for job_name in server.get_jobs():
jobs.append(job_name) jobs.append(job_name)
total_rel = set() total_rel = set()

Loading…
Cancel
Save