Commit b2f1e8b9 authored by Matt Butcher's avatar Matt Butcher

Merge pull request #340 from runseb/flake8

Add flake8 test and fix pep8 style in expansion scripts
parents e0d7bad1 88b988a9
...@@ -41,7 +41,7 @@ clean: ...@@ -41,7 +41,7 @@ clean:
rm -rf bin rm -rf bin
.PHONY: test .PHONY: test
test: build test-style test-unit test: build test-style test-unit test-flake8
ROOTFS := rootfs ROOTFS := rootfs
...@@ -64,6 +64,12 @@ test-style: lint vet ...@@ -64,6 +64,12 @@ test-style: lint vet
echo "gofmt check failed:"; gofmt -e -d -s $(GO_DIRS); exit 1; \ echo "gofmt check failed:"; gofmt -e -d -s $(GO_DIRS); exit 1; \
fi fi
.PHONY: test-flake8
test-flake8:
@echo Running flake8...
flake8 expansion
@echo ----------------
.PHONY: lint .PHONY: lint
lint: lint:
@echo Running golint... @echo Running golint...
......
...@@ -19,6 +19,7 @@ dependencies: ...@@ -19,6 +19,7 @@ dependencies:
- export PATH="$HOME/bin:$PATH" GLIDE_HOME="$HOME/.glide" - export PATH="$HOME/bin:$PATH" GLIDE_HOME="$HOME/.glide"
- cd $GOPATH/src/$IMPORT_PATH - cd $GOPATH/src/$IMPORT_PATH
- sudo pip install -r expansion/requirements.txt - sudo pip install -r expansion/requirements.txt
- sudo pip install flake8
test: test:
override: override:
......
This diff is collapsed.
This diff is collapsed.
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
...@@ -19,31 +19,31 @@ from expansion import Expand ...@@ -19,31 +19,31 @@ from expansion import Expand
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print >>sys.stderr, 'No template specified.' print >>sys.stderr, 'No template specified.'
sys.exit(1) sys.exit(1)
template = '' template = ''
imports = {} imports = {}
try: try:
with open(sys.argv[1]) as f: with open(sys.argv[1]) as f:
template = f.read() template = f.read()
for imp in sys.argv[2:]: for imp in sys.argv[2:]:
import_contents = '' import_contents = ''
with open(imp) as f: with open(imp) as f:
import_contents = f.read() import_contents = f.read()
import_name = os.path.basename(imp) import_name = os.path.basename(imp)
imports[import_name] = import_contents imports[import_name] = import_contents
except IOError as e: except IOError as e:
print 'IOException: ', str(e) print 'IOException: ', str(e)
sys.exit(1) sys.exit(1)
env = {} env = {}
env['deployment'] = os.environ['DEPLOYMENT_NAME'] env['deployment'] = os.environ['DEPLOYMENT_NAME']
env['project'] = os.environ['PROJECT'] env['project'] = os.environ['PROJECT']
validate_schema = 'VALIDATE_SCHEMA' in os.environ validate_schema = 'VALIDATE_SCHEMA' in os.environ
print Expand(template, imports, env=env, validate_schema=validate_schema) print Expand(template, imports, env=env, validate_schema=validate_schema)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
...@@ -23,74 +23,77 @@ REQUIRED = 'required' ...@@ -23,74 +23,77 @@ REQUIRED = 'required'
def ExtendWithDefault(validator_class): def ExtendWithDefault(validator_class):
"""Takes a validator and makes it set default values on properties. """Takes a validator and makes it set default values on properties.
Args: Args:
validator_class: A class to add our overridden validators to validator_class: A class to add our overridden validators to
Returns: Returns:
A validator_class that will set default values and ignore required fields A validator_class that will set default values
""" and ignore required fields
validate_properties = validator_class.VALIDATORS['properties'] """
validate_properties = validator_class.VALIDATORS['properties']
def SetDefaultsInProperties(validator, user_schema, user_properties, def SetDefaultsInProperties(validator, user_schema, user_properties,
parent_schema): parent_schema):
SetDefaults(validator, user_schema or {}, user_properties, parent_schema, SetDefaults(validator, user_schema or {}, user_properties,
validate_properties) parent_schema, validate_properties)
return jsonschema.validators.extend( return jsonschema.validators.extend(
validator_class, {PROPERTIES: SetDefaultsInProperties, validator_class, {PROPERTIES: SetDefaultsInProperties,
REQUIRED: IgnoreKeyword}) REQUIRED: IgnoreKeyword})
def SetDefaults(validator, user_schema, user_properties, parent_schema, def SetDefaults(validator, user_schema, user_properties, parent_schema,
validate_properties): validate_properties):
"""Populate the default values of properties. """Populate the default values of properties.
Args: Args:
validator: A generator that validates the "properties" keyword of the schema validator: A generator that validates the "properties" keyword
user_schema: Schema which might define defaults, might be a nested part of of the schema
the entire schema file. user_schema: Schema which might define defaults, might be a nested
user_properties: User provided values which we are setting defaults on part of the entire schema file.
parent_schema: Schema object that contains the schema being evaluated on user_properties: User provided values which we are setting defaults on
this pass, user_schema. parent_schema: Schema object that contains the schema being
validate_properties: Validator function, called recursively. evaluated on this pass, user_schema.
""" validate_properties: Validator function, called recursively.
"""
for schema_property, subschema in user_schema.iteritems():
# The ordering of these conditions assumes that '$ref' blocks override for schema_property, subschema in user_schema.iteritems():
# all other schema info, which is what the jsonschema library assumes. # The ordering of these conditions assumes that '$ref' blocks override
# all other schema info, which is what the jsonschema library assumes.
# If the subschema has a reference,
# see if that reference defines a 'default' value # If the subschema has a reference,
if REF in subschema: # see if that reference defines a 'default' value
out = ResolveReferencedDefault(validator, subschema[REF]) if REF in subschema:
user_properties.setdefault(schema_property, out) out = ResolveReferencedDefault(validator, subschema[REF])
# Otherwise, see if the subschema has a 'default' value user_properties.setdefault(schema_property, out)
elif DEFAULT in subschema: # Otherwise, see if the subschema has a 'default' value
user_properties.setdefault(schema_property, subschema[DEFAULT]) elif DEFAULT in subschema:
user_properties.setdefault(schema_property, subschema[DEFAULT])
# Recursively apply defaults. This is a generator, so we must wrap with list()
list(validate_properties(validator, user_schema, # Recursively apply defaults. This is a generator, we must wrap with list()
user_properties, parent_schema)) list(validate_properties(validator, user_schema,
user_properties, parent_schema))
def ResolveReferencedDefault(validator, ref): def ResolveReferencedDefault(validator, ref):
"""Resolves a reference, and returns any default value it defines. """Resolves a reference, and returns any default value it defines.
Args: Args:
validator: A generator that validates the "$ref" keyword validator: A generator that validates the "$ref" keyword
ref: The target of the "$ref" keyword ref: The target of the "$ref" keyword
Returns: Returns:
The value of the 'default' field found in the referenced schema, or None The value of the 'default' field found in the referenced schema,
""" or None
with validator.resolver.resolving(ref) as resolved: """
if DEFAULT in resolved: with validator.resolver.resolving(ref) as resolved:
return resolved[DEFAULT] if DEFAULT in resolved:
return resolved[DEFAULT]
def IgnoreKeyword( def IgnoreKeyword(
unused_validator, unused_required, unused_instance, unused_schema): unused_validator, unused_required, unused_instance, unused_schema):
"""Validator for JsonSchema that does nothing.""" """Validator for JsonSchema that does nothing."""
pass pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment