From 0834ab407c947efb332d0b07c238be2c656a77a0 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 22 Jul 2014 16:27:07 -0700 Subject: [PATCH] Initial pass at the basic logic --- README.rst | 18 ++++++++++------ dox/__init__.py | 3 +-- dox/cmd.py | 49 +++++++++++++++++++------------------------ dox/locations.py | 49 +++++++++++++++++++++++++++++++++++++++++++ dox/payloads.py | 49 +++++++++++++++++++++++++++++++++++++++++++ dox/tests/__init__.py | 13 ------------ requirements.txt | 3 ++- 7 files changed, 134 insertions(+), 50 deletions(-) create mode 100644 dox/locations.py create mode 100644 dox/payloads.py diff --git a/README.rst b/README.rst index 6bf3feb..ffe0777 100644 --- a/README.rst +++ b/README.rst @@ -9,9 +9,13 @@ tox and virtualenv for python. There are two elements to its configuration: * In what image should they be run? -If there is a dox.yml file, you're set. You want a docker section to specify -what image to use and a testenv section to specify the commands to run. You -win. +If there is a dox.yml file, you're set. You want to specify what image to +use and what commands to run. You win:: + + image: ubuntu:trusty + commands: | + pip install . -r test-requirements.txt + python setup.py test You might either not be willing to commit to dox as a way of life yet, or you may want to use dox in a project that similarly has not done so. @@ -50,9 +54,11 @@ If there is a tox.ini file, and it contains a [docker] section, the value in [docker] image=ubuntu:trusty -If there is not an image key in the docker section but there is a Dockerfile -in the repo, an image will be built using the Dockerfile and the test -commands will be run inside of the image. +If there is not an image key in the docker section or an image key in the +dox.yml but there is a Dockerfile in the repo, an new image will be built +using the Dockerfile and the test commands will be run inside of the image. + +If all of that fails, tests are going to run in a bare ubuntu image. Good luck! Additional information ---------------------- diff --git a/dox/__init__.py b/dox/__init__.py index beb6243..541bad1 100644 --- a/dox/__init__.py +++ b/dox/__init__.py @@ -15,5 +15,4 @@ import pbr.version -__version__ = pbr.version.VersionInfo( - 'dox').version_string() \ No newline at end of file +__version__ = pbr.version.VersionInfo('dox').version_string() diff --git a/dox/cmd.py b/dox/cmd.py index efcef3a..2ccd48e 100644 --- a/dox/cmd.py +++ b/dox/cmd.py @@ -1,30 +1,23 @@ -import docker -import docker.unixconn -from docker.unixconn import unixconn -import requests +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import dox.locations +import dox.payloads -def _get_docker_api_version(): - session = requests.Session() - session.mount( - "http+unix://", - docker.unixconn.unixconn.UnixAdapter( - "http+unix://var/run/docker.sock", 60)) - response = session.get('/version') - try: - api_version = response.json()['ApiVersion'] - except KeyError: - # For now, fall back to 1.10 as a safety net - api_version = '1.10' - return api_version - - -def _version_string_to_tuple(version): - return tuple([int(f) for f in version.split('.')]) - - -class Dox(object): - - - def __init__(self): - self.client = docker.Client(version=_get_docker_api_version()) +def main(): + location = dox.locations.get_location() + payload = dox.payloads.get_payload() + return location.run(payload) diff --git a/dox/locations.py b/dox/locations.py new file mode 100644 index 0000000..74ab541 --- /dev/null +++ b/dox/locations.py @@ -0,0 +1,49 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import ConfigParser +import os + +import yaml + + +def get_location(): + '''Examine the local environment and figure out where we should run.''' + + # Set default image value + if os.path.exists('Dockerfile'): + image = None + else: + image = 'ubuntu' + + if os.path.exists('dox.yml'): + dox_yaml = yaml.load(open('dox.yml', 'r')) + image = dox_yaml.get('image', image) + elif os.path.exists('tox.ini'): + tox_ini = ConfigParser.ConfigParser() + tox_ini.read('tox.ini') + if tox_ini.has_option('docker', 'image'): + image = tox_ini.get('docker', 'image') + return Location(image=image) + + +class Location(object): + + def __init__(self, image): + self.image = image + + def run(self, payload): + print("Going to run {0} in {1}".format( + payload.get_payload(), self.image)) diff --git a/dox/payloads.py b/dox/payloads.py new file mode 100644 index 0000000..ee9fe01 --- /dev/null +++ b/dox/payloads.py @@ -0,0 +1,49 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import ConfigParser +import os + +import yaml + + +def get_payload(): + '''Examine the local environment and figure out what we should run.''' + + if os.path.exists('dox.yml'): + dox_yml = yaml.load(open('dox.yml')) + payload = dox_yml.get('commands', None) + if payload is None and os.path.exists('tox.ini'): + tox_ini = ConfigParser.ConfigParser() + tox_ini.read('tox.ini') + if tox_ini.has_option('testenv', 'commands'): + payload = tox_ini.get('testenv', 'commands') + else: + payload = None + if payload is None and os.path.exists('.travis.yml'): + travis_yml = yaml.load(open('.travis.yml')) + payload = travis_yml.get('script') + return Payload(payload=payload) + + +class Payload(object): + + def __init__(self, payload): + self.payload = payload + + def get_payload(self): + if hasattr(self.payload, 'append'): + return "\n".join(self.payload) + return self.payload diff --git a/dox/tests/__init__.py b/dox/tests/__init__.py index f88664e..e69de29 100644 --- a/dox/tests/__init__.py +++ b/dox/tests/__init__.py @@ -1,13 +0,0 @@ -# -*- coding: utf-8 -*- - -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 24d61b0..1e4bae2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pbr>=0.5.21,<1.0 -docker-py +PyYAML +sh