Skip to content

Commit

Permalink
Fixing pep8 issues in code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammedzee1000 committed Jun 11, 2018
1 parent 3898f2c commit 605e6e3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 43 deletions.
33 changes: 16 additions & 17 deletions ci/container_index/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import ci.container_index.lib.constants as constants
import imp


class Engine(object):

def _load_validators(self, v_type,v_list):
def _load_validators(self, v_type, v_list):
"""
Loads a list of validators, using reflection.
"""
Expand All @@ -20,7 +21,7 @@ def _load_validators(self, v_type,v_list):

def __init__(
self, test_bench_path="~/.index_ci_bench", schema_validators=None,
value_validators=None,index_location="./", verbose=True
value_validators=None, index_location="./", verbose=True
):
"""
Initializes the test engine
Expand All @@ -30,31 +31,32 @@ def __init__(
raise Exception(
"Could not find location specified for index."
)

# Goto the index location and collect the index files
self.index_location = abspath(index_location)
self.index_d = join(self.index_location, "index.d")
self.index_files = glob(
str.format("{}/*.y*ml", self.index_d)
)

if len(self.index_files) == 0 or\
(len(self.index_files) == 1 and\
any("index_template" in s for s in self.index_files)):
if (len(self.index_files) == 0 or
(len(self.index_files) == 1 and
any("index_template" in s for s
in self.index_files))):
raise Exception("No index files to match.")

# Collect the validators that need to run.
self.validators = []

## Schema Validators:
if not schema_validators or not isinstance(schema_validators, list)\
or len(schema_validators) <= 0:
# - Schema Validators:
if (not schema_validators or not
isinstance(schema_validators, list) or
len(schema_validators) <= 0):
v_list = config.schema_validators
else:
v_list = schema_validators
self._load_validators(schema_validation, v_list)

## Value Validators
# - Value Validators
# TODO

self.summary = {}
Expand All @@ -70,8 +72,8 @@ def run(self):

# Perform primary validation
m = schema_validation.TopLevelProjectsValidator(
file_data, index_file
).validate()
file_data, index_file
).validate()
messages.append(m)
# If primary validation was successful, move forward.
if m.success:
Expand All @@ -87,8 +89,5 @@ def run(self):
overall_success = False

self.add_summary(index_file, messages)

return overall_success, self.summary



return overall_success, self.summary
10 changes: 5 additions & 5 deletions ci/container_index/lib/checks/basevalidation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from ci.container_index.lib.utils import IndexCIMessage
import os


class Validator(object):

def __init__(
self, validation_data, file_name
):
def __init__(self, validation_data, file_name):
self.message = IndexCIMessage(validation_data)
self.validation_data = validation_data
self.file_name = file_name
Expand All @@ -28,7 +27,8 @@ def _perform_validation(self):
def validate(self):
"""
Runs the validator to validate based on provided data.
:return: Returns a flag to indicate success or falure and an IndexCIMessage object.
:return: Returns a flag to indicate success or falure
and an IndexCIMessage object.
"""
self._perform_validation()
return self.message
Expand Down Expand Up @@ -56,7 +56,7 @@ def _extra_validation(self):


class StringFieldValidator(BasicSchemaValidator):

def __init__(self, validation_data, file_name):
super(StringFieldValidator, self).__init__(validation_data, file_name)
self.field_name = ""
Expand Down
48 changes: 32 additions & 16 deletions ci/container_index/lib/checks/schema_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ci.container_index.lib.utils import\
load_yaml, dump_yaml


def gen_tracking_file():
dir_path = dirname(realpath(__file__))
return join(
Expand All @@ -18,7 +19,8 @@ def gen_tracking_file():
class TopLevelProjectsValidator(Validator):

def __init__(self, validation_data, file_name):
super(TopLevelProjectsValidator, self).__init__(validation_data, file_name)
super(TopLevelProjectsValidator, self).__init__(
validation_data, file_name)

def _perform_validation(self):
self.message.title = "Top level projects"
Expand All @@ -28,10 +30,10 @@ def _perform_validation(self):
self._invalidate(
"Index data should begin with "
"top level \"Projects:\""
)
)
return
if not isinstance(
self.validation_data.get(FieldKeys.PROJECTS),
self.validation_data.get(FieldKeys.PROJECTS),
list
):
self._invalidate(
Expand All @@ -43,7 +45,8 @@ def _perform_validation(self):
class IDValidator(BasicSchemaValidator):

def __init__(self, validation_data, file_name):
super(IDValidator, self).__init__(validation_data, file_name)
super(IDValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.ID

def _extra_validation(self):
Expand All @@ -54,23 +57,28 @@ def _extra_validation(self):
self._invalidate("id field must be an integer.")
return


class DependsOnValidator(BasicSchemaValidator):

def __init__(self, validation_data, file_name):
super(DependsOnValidator, self).__init__(validation_data, file_name)
super(DependsOnValidator, self).__init__(
validation_data, file_name)

def _extra_validation(self):
self.message.title = "Depends On validation"


class AppIDValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(AppIDValidator, self).__init__(validation_data, file_name)
super(AppIDValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.APP_ID
self.message.title = "App ID Validation"

def _extra_validation_1(self):
if self.validation_data.get(self.field_name) != self.file_base_name.split(".")[0]:
if (self.validation_data.get(self.field_name) !=
self.file_base_name.split(".")[0]):
self._invalidate(
str.format(
"{} must be the same as the file name",
Expand All @@ -83,62 +91,70 @@ def _extra_validation_1(self):
class JobIDValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(JobIDValidator, self). __init__(validation_data, file_name)
super(JobIDValidator, self). __init__(
validation_data, file_name)
self.field_name = FieldKeys.JOB_ID
self.message.title = "Job ID Validation"


class DesiredTagValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(DesiredTagValidator, self).__init__(validation_data, file_name)
super(DesiredTagValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.DESIRED_TAG
self.message.title = "Desired Tag Validation"


class GitURLValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(GitURLValidator, self).__init__(validation_data, file_name)
super(GitURLValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.GIT_URL
self.message.title = "Git URL Validation"


class GitPathValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(GitPathValidator, self).__init__(validation_data, file_name)
super(GitPathValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.GIT_PATH
self.message.title = "Git Path Validation"


class GitBranchValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(GitBranchValidator, self).__init__(validation_data, file_name)
super(GitBranchValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.GIT_BRANCH
self.message.title = "Git Branch Validation"


class TargetFileValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(TargetFileValidator, self).__init__(validation_data, file_name)
super(TargetFileValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.TARGET_FILE
self.message.title = "Target File Validation"


class NotifyEmailValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(NotifyEmailValidator, self).__init__(validation_data, file_name)
super(NotifyEmailValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.NOTIFY_EMAIL
self.message.title = "Notify Email Validation"


class BuildContextValidator(StringFieldValidator):

def __init__(self, validation_data, file_name):
super(BuildContextValidator, self).__init__(validation_data, file_name)
super(BuildContextValidator, self).__init__(
validation_data, file_name)
self.field_name = FieldKeys.BUILD_CONTEXT
self.message.title = "Build Context Validation"
17 changes: 12 additions & 5 deletions ci/container_index/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from shutil import rmtree
from subprocess import check_call, CalledProcessError, STDOUT


def execute_command(cmd):
"""Execute a specified command"""
try:
Expand All @@ -15,6 +16,7 @@ def execute_command(cmd):
except CalledProcessError:
return False


def load_yaml(file_path):
data = None
try:
Expand All @@ -24,28 +26,31 @@ def load_yaml(file_path):
pass
return data


def dump_yaml(file_path, data):
try:
with open(file_path, "w") as f:
yaml.dump(f)
except Exception as e:
pass


def update_git_repo(git_url, git_branch, clone_location="."):
t = git_url.split(':')[1] if ':' in git_url else git_url
clone_path = path.join(clone_location, t)
ret = None

if not path.exists(clone_path):
clone_command = ["git", "clone", git_url, clone_path]
if not execute_command(clone_command):
return None

get_back = getcwd()
chdir(clone_path)

branches_cmd = "git branch -r | grep -v '\->' | while read remote; do git branch --track \"${remote#origin/}\"" \
" \"$remote\" &> /dev/null; done"
branches_cmd = r"""git branch -r | grep -v '\->' | while
read remote; do git branch --track "${remote#origin/}"
$remote" &> /dev/null; done"""

system(branches_cmd)
cmd = ["git", "fetch", "--all"]
Expand All @@ -60,13 +65,15 @@ def update_git_repo(git_url, git_branch, clone_location="."):

if execute_command(cmd):
ret = clone_path
chdir(get_back)
return ret


class IndexCIMessage(object):

def __init__(self, data, title=None):
self.title = title if title else "Untitled"
self.success = True
self.data = data
self.errors = []
self.warnings = []
self.warnings = []

0 comments on commit 605e6e3

Please sign in to comment.