55import sys
66import tarfile
77
8+ import shutil
9+ from zipfile import ZipFile
10+
811from setuptools import setup
12+ from setuptools .command .develop import develop
913from setuptools .command .install import install
1014
1115import pact
1216
1317
18+ IS_64 = sys .maxsize > 2 ** 32
19+ MOCK_SERVICE_URI = (
20+ 'https://github.com/bethesque/pact-mock_service/releases/download/v2.1.0/' )
21+ VERIFIER_URI = (
22+ 'https://github.com/pact-foundation/pact-provider-verifier'
23+ '/releases/download/v1.0.2/' )
24+
25+
26+ class PactPythonDevelopCommand (develop ):
27+ """
28+ Custom develop mode installer for pact-python.
29+
30+ When the package is installed using `python setup.py develop` or
31+ `pip install -e` it will download and unpack the appropriate Pact
32+ mock service and provider verifier.
33+ """
34+ def run (self ):
35+ develop .run (self )
36+ bin_path = os .path .join (os .path .dirname (__file__ ), 'pact' , 'bin' )
37+ if not os .path .exists (bin_path ):
38+ os .mkdir (bin_path )
39+
40+ mock_service (bin_path )
41+ verifier (bin_path )
42+
43+
1444class PactPythonInstallCommand (install ):
1545 """
1646 Custom installer for pact-python.
1747
1848 Installs the Python package and unpacks the platform appropriate version
19- of Python mock service.
49+ of the Ruby mock service and provider verifier .
2050 """
2151 def run (self ):
2252 install .run (self )
2353 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' ))
54+ os .mkdir (bin_path )
55+ mock_service (bin_path )
56+ verifier (bin_path )
57+
58+
59+ def install_ruby_app (bin_path , dir_name , platform_tar , repository_uri ):
60+ """
61+ Download a Ruby application and install it for use.
62+
63+ :param bin_path: The path where binaries should be installed.
64+ :param platform_tar: The application tar or zip file to download.
65+ :param dir_name: The directory name for the unpacked files.
66+ :param repository_uri: The GitHub repository URI.
67+ """
68+ if sys .version_info .major == 2 :
69+ from urllib import urlopen
70+ else :
71+ from urllib .request import urlopen
72+
73+ path = os .path .join (bin_path , platform_tar )
74+ resp = urlopen (repository_uri + platform_tar )
75+ with open (path , 'wb' ) as f :
76+ f .write (resp .read ())
77+
78+ if 'windows' in platform .platform ().lower ():
79+ with ZipFile (path ) as f :
80+ f .extractall (bin_path )
81+ else :
82+ with tarfile .open (path ) as f :
83+ f .extractall (bin_path )
84+
85+ platform_name = platform_tar .replace ('.tar.gz' , '' ).replace ('.zip' , '' )
86+ shutil .move (os .path .join (bin_path , platform_name ),
87+ os .path .join (bin_path , dir_name ))
88+
89+
90+ def get_version ():
91+ """Return latest version noted in CHANGES.txt."""
92+ lastline = [line for line in read ('CHANGES.txt' ).split ('\n ' ) if line ][- 1 ]
93+ version = lastline .split (',' )[0 ]
94+ return version [1 :]
95+
96+
97+ def mock_service (bin_path ):
98+ """Install the Ruby mock service for this platform."""
99+ target_platform = platform .platform ().lower ()
100+ if 'darwin' in target_platform :
101+ platform_tar = 'pact-mock-service-2.1.0-1-osx.tar.gz'
102+ elif 'linux' in target_platform and IS_64 :
103+ platform_tar = 'pact-mock-service-2.1.0-1-linux-x86_64.tar.gz'
104+ elif 'linux' in target_platform :
105+ platform_tar = 'pact-mock-service-2.1.0-1-linux-x86.tar.gz'
106+ elif 'windows' in target_platform :
107+ platform_tar = 'pact-mock-service-2.1.0-1-win32.zip'
108+ else :
109+ msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
110+ ' Windows, and OSX are currently supported.' ).format (
111+ platform .platform ())
112+ raise Exception (msg )
113+
114+ install_ruby_app (
115+ bin_path , 'mock-service' , platform_tar , MOCK_SERVICE_URI )
70116
71117
72118def read (filename ):
@@ -76,13 +122,34 @@ def read(filename):
76122 return f .read ().decode ('utf-8' )
77123
78124
125+ def verifier (bin_path ):
126+ """Install the Ruby Pact Verifier for this platform."""
127+ target_platform = platform .platform ().lower ()
128+ if 'darwin' in target_platform :
129+ platform_tar = 'pact-provider-verifier-1.0.2-1-osx.tar.gz'
130+ elif 'linux' in target_platform and IS_64 :
131+ platform_tar = 'pact-provider-verifier-1.0.2-1-linux-x86_64.tar.gz'
132+ elif 'linux' in target_platform :
133+ platform_tar = 'pact-provider-verifier-1.0.2-1-linux-x86.tar.gz'
134+ elif 'windows' in target_platform :
135+ platform_tar = 'pact-provider-verifier-1.0.2-1-win32.zip'
136+ else :
137+ msg = ('Unfortunately, {} is not a supported platform. Only Linux,'
138+ ' Windows, and OSX are currently supported.' ).format (
139+ platform .platform ())
140+ raise Exception (msg )
141+
142+ install_ruby_app (bin_path , 'verifier' , platform_tar , VERIFIER_URI )
143+
144+
79145dependencies = read ('requirements.txt' ).split ()
80146
81147if sys .version_info .major == 2 :
82148 dependencies .append ('subprocess32' )
83149
84150setup_args = dict (
85- cmdclass = {'install' : PactPythonInstallCommand },
151+ cmdclass = {'develop' : PactPythonDevelopCommand ,
152+ 'install' : PactPythonInstallCommand },
86153 name = 'pact-python' ,
87154 version = pact .__version__ ,
88155 description = ('Tools for creating and verifying consumer driven contracts'
0 commit comments