3
3
from browserstack .bserrors import BrowserStackLocalError
4
4
5
5
class Local :
6
- def __init__ (self , key = None , binary_path = None ):
7
- self .options = {
8
- 'key' : key ,
9
- 'logfile_flag' : '-logFile' ,
10
- 'logfile_path' : os .path .join (os .getcwd (), 'local.log' )
11
- }
12
- self .local_folder_path = None
13
- self .local_logfile_path = self .options ['logfile_path' ]
6
+ def __init__ (self , key = os .environ ['BROWSERSTACK_ACCESS_KEY' ], binary_path = None ):
7
+ self .key = key
8
+ self .options = None
9
+ self .local_logfile_path = os .path .join (os .getcwd (), 'local.log' )
14
10
15
- def __xstr (self , obj ):
16
- if obj is None :
17
- return ''
18
- return str (obj )
11
+ def __xstr (self , key , value ):
12
+ if key is None :
13
+ return ['' ]
14
+ if str (value ).lower () == "true" :
15
+ return ['-' + key ]
16
+ else :
17
+ return ['-' + key , value ]
19
18
20
19
def _generate_cmd (self ):
21
- options_order = ['logfile_flag' , 'logfile_path' , 'folder_flag' , 'key' , 'folder_path' , 'forcelocal' , 'local_identifier' , 'only' , 'only_automate' , 'proxy_host' , 'proxy_port' , 'proxy_user' , 'proxy_pass' , 'forceproxy' , 'force' , 'verbose' , 'hosts' ]
22
- cmd = [self .__xstr (self .options .get (o )) for o in options_order if self .options .get (o ) is not None ]
23
- return [self .binary_path ] + cmd
20
+ cmd = [self .binary_path , '-logFile' , self .local_logfile_path , self .key ]
21
+ for o in self .options .keys ():
22
+ if self .options .get (o ) is not None :
23
+ cmd = cmd + self .__xstr (o , self .options .get (o ))
24
+ return cmd
24
25
25
26
def start (self , ** kwargs ):
26
- for key , value in kwargs .items ():
27
- self .__add_arg (key , value )
28
-
27
+ self .options = kwargs
28
+
29
29
if 'binarypath' in self .options :
30
30
self .binary_path = binary_path
31
31
else :
32
32
self .binary_path = LocalBinary ().get_binary ()
33
33
34
+ if 'logfile' in self .options :
35
+ self .local_logfile_path = self .options ['logfile' ]
36
+ del self .options ['logfile' ]
37
+
34
38
if "onlyCommand" in kwargs and kwargs ["onlyCommand" ]:
35
39
return
36
40
41
+ print self ._generate_cmd ()
37
42
self .proc = subprocess .Popen (self ._generate_cmd (), stdout = subprocess .PIPE )
38
43
self .stderr = self .proc .stderr
39
44
@@ -56,43 +61,6 @@ def isRunning(self):
56
61
return True if self .proc .poll () is None else False
57
62
return False
58
63
59
- def __add_arg (self , key , value ):
60
- if key == 'verbose' and value :
61
- self .options ['verbose' ] = '-v'
62
- elif key == 'force' and value :
63
- self .options ['force' ] = '-force'
64
- elif key == 'only' and value :
65
- self .options ['only' ] = '-only'
66
- elif key == 'onlyAutomate' and value :
67
- self .options ['only_automate' ] = '-onlyAutomate'
68
- elif key == 'forcelocal' and value :
69
- self .options ['forcelocal' ] = '-forcelocal'
70
- elif key == 'localIdentifier' :
71
- self .options ['local_identifier' ] = '-localIdentifier ' + str (value )
72
- elif key == 'f' :
73
- self .options ['folder_flag' ] = '-f'
74
- self .options ['folder_path' ] = str (value )
75
- elif key == 'proxyHost' :
76
- self .options ['proxy_host' ] = '-proxyHost ' + str (value )
77
- elif key == 'proxyPort' :
78
- self .options ['proxy_port' ] = '-proxyPort ' + str (value )
79
- elif key == 'proxyUser' :
80
- self .options ['proxy_user' ] = '-proxyUser ' + str (value )
81
- elif key == 'proxyPass' :
82
- self .options ['proxy_pass' ] = '-proxyPass ' + str (value )
83
- elif key == 'hosts' :
84
- self .options ['hosts' ] = str (value )
85
- elif key == 'forceproxy' and value :
86
- self .options ['forceproxy' ] = '-forceproxy'
87
- elif key == 'logfile' :
88
- self .options ['logfile_flag' ] = '-logFile'
89
- self .options ['logfile' ] = str (value )
90
- self .local_logfile_path = str (value )
91
- elif key == 'binarypath' :
92
- self .options ['binarypath' ] = str (value )
93
- elif key != 'onlyCommand' :
94
- raise BrowserStackLocalError ('Attempted to pass invalid option to binary' )
95
-
96
64
def stop (self ):
97
65
try :
98
66
self .proc .terminate ()
0 commit comments