Skip to content

Commit

Permalink
Use the compiled Ruby applications from pact-mock-service and pact-pr…
Browse files Browse the repository at this point in the history
…ovider-verifier

- Switch the install scripts to download the appropriate package from the Github releases pages for each Ruby app
- Add a custom `develop` command that does the same installation when installing in `developer mode`
  • Loading branch information
matthewbalvanz-wf committed Jun 19, 2017
1 parent fd151d2 commit db3e7c3
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 52 deletions.
6 changes: 3 additions & 3 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include LICENSE
include *.gz
include *.txt
include *.md
prune *test
prune e2e/*
prune pact/test
prune pact/bin
prune e2e
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ e2e:


.PHONY: package
package: pact/bin
package:
python setup.py sdist


Expand Down
163 changes: 115 additions & 48 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,114 @@
import sys
import tarfile

import shutil
from zipfile import ZipFile

from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install

import pact


IS_64 = sys.maxsize > 2 ** 32
MOCK_SERVICE_URI = (
'https://github.com/bethesque/pact-mock_service/releases/download/v2.1.0/')
VERIFIER_URI = (
'https://github.com/pact-foundation/pact-provider-verifier'
'/releases/download/v1.0.2/')


class PactPythonDevelopCommand(develop):
"""
Custom develop mode installer for pact-python.
When the package is installed using `python setup.py develop` or
`pip install -e` it will download and unpack the appropriate Pact
mock service and provider verifier.
"""
def run(self):
develop.run(self)
bin_path = os.path.join(os.path.dirname(__file__), 'pact', 'bin')
if not os.path.exists(bin_path):
os.mkdir(bin_path)

mock_service(bin_path)
verifier(bin_path)


class PactPythonInstallCommand(install):
"""
Custom installer for pact-python.
Installs the Python package and unpacks the platform appropriate version
of Python mock service.
of the Ruby mock service and provider verifier.
"""
def run(self):
install.run(self)
bin_path = os.path.join(self.install_lib, 'pact', 'bin')
self.mock_service(bin_path)
self.verifier(bin_path)

def mock_service(self, bin_path):
"""Install the Ruby mock service for this platform."""
is_64 = sys.maxsize > 2 ** 32
target_platform = platform.platform().lower()
if 'darwin' in target_platform:
platform_tar = 'pact-mock-service-darwin.tar.gz'
elif 'linux' in target_platform and is_64:
platform_tar = 'pact-mock-service-linux-x64.tar.gz'
elif 'linux' in target_platform:
platform_tar = 'pact-mock-service-ia32.tar.gz'
elif 'windows' in target_platform:
platform_tar = 'pact-mock-service-win32.tar.gz'
else:
msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
' Windows, and OSX are currently supported.').format(
platform.platform())
raise Exception(msg)

self.announce(u'Extracting {} to {}'.format(platform_tar, bin_path))
with tarfile.open(os.path.join(bin_path, platform_tar)) as f:
f.extractall(os.path.join(bin_path, 'mock-service'))

def verifier(self, bin_path):
"""Install the Ruby Pact Verifier for this platform."""
is_64 = sys.maxsize > 2 ** 32
target_platform = platform.platform().lower()
if 'darwin' in target_platform:
platform_tar = 'pact-provider-verifier-darwin.tar.gz'
elif 'linux' in target_platform and is_64:
platform_tar = 'pact-provider-verifier-linux-x64.tar.gz'
elif 'linux' in target_platform:
platform_tar = 'pact-provider-verifier-linux-ia32.tar.gz'
elif 'windows' in target_platform:
platform_tar = 'pact-provider-verifier-win32.tar.gz'
else:
msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
' Windows, and OSX are currently supported.').format(
platform.platform())
raise Exception(msg)

self.announce(u'Extracting {} to {}'.format(platform_tar, bin_path))
with tarfile.open(os.path.join(bin_path, platform_tar)) as f:
f.extractall(os.path.join(bin_path, 'verifier'))
os.mkdir(bin_path)
mock_service(bin_path)
verifier(bin_path)


def install_ruby_app(bin_path, dir_name, platform_tar, repository_uri):
"""
Download a Ruby application and install it for use.
:param bin_path: The path where binaries should be installed.
:param platform_tar: The application tar or zip file to download.
:param dir_name: The directory name for the unpacked files.
:param repository_uri: The GitHub repository URI.
"""
if sys.version_info.major == 2:
from urllib import urlopen
else:
from urllib.request import urlopen

path = os.path.join(bin_path, platform_tar)
resp = urlopen(repository_uri + platform_tar)
with open(path, 'wb') as f:
f.write(resp.read())

if 'windows' in platform.platform().lower():
with ZipFile(path) as f:
f.extractall(bin_path)
else:
with tarfile.open(path) as f:
f.extractall(bin_path)

platform_name = platform_tar.replace('.tar.gz', '').replace('.zip', '')
shutil.move(os.path.join(bin_path, platform_name),
os.path.join(bin_path, dir_name))


def get_version():
"""Return latest version noted in CHANGES.txt."""
lastline = [line for line in read('CHANGES.txt').split('\n') if line][-1]
version = lastline.split(',')[0]
return version[1:]


def mock_service(bin_path):
"""Install the Ruby mock service for this platform."""
target_platform = platform.platform().lower()
if 'darwin' in target_platform:
platform_tar = 'pact-mock-service-2.1.0-1-osx.tar.gz'
elif 'linux' in target_platform and IS_64:
platform_tar = 'pact-mock-service-2.1.0-1-linux-x86_64.tar.gz'
elif 'linux' in target_platform:
platform_tar = 'pact-mock-service-2.1.0-1-linux-x86.tar.gz'
elif 'windows' in target_platform:
platform_tar = 'pact-mock-service-2.1.0-1-win32.zip'
else:
msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
' Windows, and OSX are currently supported.').format(
platform.platform())
raise Exception(msg)

install_ruby_app(
bin_path, 'mock-service', platform_tar, MOCK_SERVICE_URI)


def read(filename):
Expand All @@ -76,13 +122,34 @@ def read(filename):
return f.read().decode('utf-8')


def verifier(bin_path):
"""Install the Ruby Pact Verifier for this platform."""
target_platform = platform.platform().lower()
if 'darwin' in target_platform:
platform_tar = 'pact-provider-verifier-1.0.2-1-osx.tar.gz'
elif 'linux' in target_platform and IS_64:
platform_tar = 'pact-provider-verifier-1.0.2-1-linux-x86_64.tar.gz'
elif 'linux' in target_platform:
platform_tar = 'pact-provider-verifier-1.0.2-1-linux-x86.tar.gz'
elif 'windows' in target_platform:
platform_tar = 'pact-provider-verifier-1.0.2-1-win32.zip'
else:
msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
' Windows, and OSX are currently supported.').format(
platform.platform())
raise Exception(msg)

install_ruby_app(bin_path, 'verifier', platform_tar, VERIFIER_URI)


dependencies = read('requirements.txt').split()

if sys.version_info.major == 2:
dependencies.append('subprocess32')

setup_args = dict(
cmdclass={'install': PactPythonInstallCommand},
cmdclass={'develop': PactPythonDevelopCommand,
'install': PactPythonInstallCommand},
name='pact-python',
version=pact.__version__,
description=('Tools for creating and verifying consumer driven contracts'
Expand Down

0 comments on commit db3e7c3

Please sign in to comment.