55import sys
66import tarfile
77
8+ from zipfile import ZipFile
9+
810from setuptools import setup
11+ from setuptools .command .develop import develop
912from setuptools .command .install import install
1013
1114import pact
1215
1316
17+ IS_64 = sys .maxsize > 2 ** 32
18+ PACT_STANDALONE_VERSION = '1.0.0'
19+
20+
21+ class PactPythonDevelopCommand (develop ):
22+ """
23+ Custom develop mode installer for pact-python.
24+
25+ When the package is installed using `python setup.py develop` or
26+ `pip install -e` it will download and unpack the appropriate Pact
27+ mock service and provider verifier.
28+ """
29+ def run (self ):
30+ develop .run (self )
31+ bin_path = os .path .join (os .path .dirname (__file__ ), 'pact' , 'bin' )
32+ if not os .path .exists (bin_path ):
33+ os .mkdir (bin_path )
34+
35+ install_ruby_app (bin_path )
36+
37+
1438class PactPythonInstallCommand (install ):
1539 """
1640 Custom installer for pact-python.
1741
1842 Installs the Python package and unpacks the platform appropriate version
19- of Python mock service.
43+ of the Ruby mock service and provider verifier .
2044 """
2145 def run (self ):
2246 install .run (self )
2347 bin_path = os .path .join (self .install_lib , 'pact' , 'bin' )
24- self .mock_service (bin_path )
25- self .verifier (bin_path )
26-
27- def mock_service (self , bin_path ):
28- """Install the Ruby mock service for this platform."""
29- is_64 = sys .maxsize > 2 ** 32
30- target_platform = platform .platform ().lower ()
31- if 'darwin' in target_platform :
32- platform_tar = 'pact-mock-service-darwin.tar.gz'
33- elif 'linux' in target_platform and is_64 :
34- platform_tar = 'pact-mock-service-linux-x64.tar.gz'
35- elif 'linux' in target_platform :
36- platform_tar = 'pact-mock-service-ia32.tar.gz'
37- elif 'windows' in target_platform :
38- platform_tar = 'pact-mock-service-win32.tar.gz'
39- else :
40- msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
41- ' Windows, and OSX are currently supported.' ).format (
42- platform .platform ())
43- raise Exception (msg )
44-
45- self .announce (u'Extracting {} to {}' .format (platform_tar , bin_path ))
46- with tarfile .open (os .path .join (bin_path , platform_tar )) as f :
47- f .extractall (os .path .join (bin_path , 'mock-service' ))
48-
49- def verifier (self , bin_path ):
50- """Install the Ruby Pact Verifier for this platform."""
51- is_64 = sys .maxsize > 2 ** 32
52- target_platform = platform .platform ().lower ()
53- if 'darwin' in target_platform :
54- platform_tar = 'pact-provider-verifier-darwin.tar.gz'
55- elif 'linux' in target_platform and is_64 :
56- platform_tar = 'pact-provider-verifier-linux-x64.tar.gz'
57- elif 'linux' in target_platform :
58- platform_tar = 'pact-provider-verifier-linux-ia32.tar.gz'
59- elif 'windows' in target_platform :
60- platform_tar = 'pact-provider-verifier-win32.tar.gz'
61- else :
62- msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
63- ' Windows, and OSX are currently supported.' ).format (
64- platform .platform ())
65- raise Exception (msg )
66-
67- self .announce (u'Extracting {} to {}' .format (platform_tar , bin_path ))
68- with tarfile .open (os .path .join (bin_path , platform_tar )) as f :
69- f .extractall (os .path .join (bin_path , 'verifier' ))
48+ os .mkdir (bin_path )
49+ install_ruby_app (bin_path )
50+
51+
52+ def install_ruby_app (bin_path ):
53+ """
54+ Download a Ruby application and install it for use.
55+
56+ :param bin_path: The path where binaries should be installed.
57+ """
58+ target_platform = platform .platform ().lower ()
59+ uri = ('https://github.com/pact-foundation/pact-ruby-standalone/releases'
60+ '/download/v{version}/pact-{version}-{suffix}' )
61+
62+ if 'darwin' in target_platform :
63+ suffix = 'osx.tar.gz'
64+ elif 'linux' in target_platform and IS_64 :
65+ suffix = 'linux-x86_64.tar.gz'
66+ elif 'linux' in target_platform :
67+ suffix = 'linux-x86.tar.gz'
68+ elif 'windows' in target_platform :
69+ suffix = 'win32.zip'
70+ else :
71+ msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
72+ ' Windows, and OSX are currently supported.' ).format (
73+ platform .platform ())
74+ raise Exception (msg )
75+
76+ if sys .version_info .major == 2 :
77+ from urllib import urlopen
78+ else :
79+ from urllib .request import urlopen
80+
81+ path = os .path .join (bin_path , suffix )
82+ resp = urlopen (uri .format (version = PACT_STANDALONE_VERSION , suffix = suffix ))
83+ with open (path , 'wb' ) as f :
84+ f .write (resp .read ())
85+
86+ if 'windows' in platform .platform ().lower ():
87+ with ZipFile (path ) as f :
88+ f .extractall (bin_path )
89+ else :
90+ with tarfile .open (path ) as f :
91+ f .extractall (bin_path )
7092
7193
7294def read (filename ):
@@ -82,7 +104,8 @@ def read(filename):
82104 dependencies .append ('subprocess32' )
83105
84106setup_args = dict (
85- cmdclass = {'install' : PactPythonInstallCommand },
107+ cmdclass = {'develop' : PactPythonDevelopCommand ,
108+ 'install' : PactPythonInstallCommand },
86109 name = 'pact-python' ,
87110 version = pact .__version__ ,
88111 description = ('Tools for creating and verifying consumer driven contracts'
0 commit comments