9
9
10
10
import os
11
11
import socket
12
+ import sys
12
13
import time
13
14
import subprocess
14
15
import logging
@@ -109,6 +110,46 @@ def requires_ldapi():
109
110
return identity
110
111
111
112
113
+ def _which (cmd ):
114
+ """Specialized which command based on shutil.which() from Python 3.6.
115
+
116
+ * simplified
117
+ * always adds /sbin directories to path
118
+ """
119
+
120
+ def _access_check (fn ):
121
+ return (os .path .exists (fn ) and os .access (fn , os .F_OK | os .X_OK )
122
+ and not os .path .isdir (fn ))
123
+
124
+ # Path with directory part skips PATH lookup.
125
+ if os .path .dirname (cmd ):
126
+ if _access_check (cmd ):
127
+ return cmd
128
+ return None
129
+
130
+ path = os .environ .get ("PATH" , os .defpath ).split (os .pathsep )
131
+
132
+ if sys .platform == 'win32' :
133
+ if os .curdir not in path :
134
+ path .insert (0 , os .curdir )
135
+ # include path extension (.exe)
136
+ pathext = os .environ .get ("PATHEXT" , "" ).split (os .pathsep )
137
+ files = [cmd + ext for ext in pathext ]
138
+ else :
139
+ # always include sbin for slapd binary
140
+ for sbin in ['/usr/local/sbin' , '/sbin' , '/usr/sbin' ]:
141
+ if sbin not in path :
142
+ path .append (sbin )
143
+ files = [cmd ]
144
+
145
+ for directory in path :
146
+ for name in files :
147
+ name = os .path .join (directory , name )
148
+ if _access_check (name ):
149
+ return name
150
+ return None
151
+
152
+
112
153
def combined_logger (
113
154
log_name ,
114
155
log_level = logging .WARN ,
@@ -172,8 +213,6 @@ class SlapdObject(object):
172
213
)
173
214
174
215
TMPDIR = os .environ .get ('TMP' , os .getcwd ())
175
- SBINDIR = os .environ .get ('SBIN' , '/usr/sbin' )
176
- BINDIR = os .environ .get ('BIN' , '/usr/bin' )
177
216
if 'SCHEMA' in os .environ :
178
217
SCHEMADIR = os .environ ['SCHEMA' ]
179
218
elif os .path .isdir ("/etc/openldap/schema" ):
@@ -182,12 +221,14 @@ class SlapdObject(object):
182
221
SCHEMADIR = "/etc/ldap/schema"
183
222
else :
184
223
SCHEMADIR = None
185
- PATH_LDAPADD = os .path .join (BINDIR , 'ldapadd' )
186
- PATH_LDAPDELETE = os .path .join (BINDIR , 'ldapdelete' )
187
- PATH_LDAPMODIFY = os .path .join (BINDIR , 'ldapmodify' )
188
- PATH_LDAPWHOAMI = os .path .join (BINDIR , 'ldapwhoami' )
189
- PATH_SLAPD = os .environ .get ('SLAPD' , os .path .join (SBINDIR , 'slapd' ))
190
- PATH_SLAPTEST = os .path .join (SBINDIR , 'slaptest' )
224
+ # _check_requirements turns paths into absolute paths
225
+ PATH_LDAPADD = 'ldapadd'
226
+ PATH_LDAPDELETE = 'ldapdelete'
227
+ PATH_LDAPMODIFY = 'ldapmodify'
228
+ PATH_LDAPWHOAMI = 'ldapwhoami'
229
+ # The following two binaries are usually in /usr/sbin.
230
+ PATH_SLAPD = os .environ .get ('SLAPD' , 'slapd' )
231
+ PATH_SLAPTEST = 'slaptest'
191
232
192
233
# time in secs to wait before trying to access slapd via LDAP (again)
193
234
_start_sleep = 1.5
@@ -223,13 +264,19 @@ def __init__(self):
223
264
self .clientkey = os .path .join (HERE , 'certs/client.key' )
224
265
225
266
def _check_requirements (self ):
226
- binaries = [
227
- self . PATH_LDAPADD , self . PATH_LDAPMODIFY , self . PATH_LDAPWHOAMI ,
228
- self . PATH_SLAPD , self . PATH_SLAPTEST
267
+ names = [
268
+ " PATH_LDAPADD" , " PATH_LDAPMODIFY" , "PATH_LDAPDELETE" ,
269
+ "PATH_LDAPWHOAMI" , " PATH_SLAPD" , " PATH_SLAPTEST" ,
229
270
]
230
- for binary in binaries :
231
- if not os .path .isfile (binary ):
232
- raise ValueError ('Binary {} is missing.' .format (binary ))
271
+ for name in names :
272
+ value = getattr (self , name )
273
+ binary = _which (value )
274
+ if binary is None :
275
+ raise ValueError (
276
+ "Command '{}' not found in PATH" .format (value )
277
+ )
278
+ else :
279
+ setattr (self , name , binary )
233
280
if self .SCHEMADIR is None :
234
281
raise ValueError ('SCHEMADIR is None, ldap schemas are missing.' )
235
282
@@ -359,7 +406,7 @@ def _start_slapd(self):
359
406
self .PATH_SLAPD ,
360
407
'-f' , self ._slapd_conf ,
361
408
'-F' , self .testrundir ,
362
- '-h' , '%s' % ' ' .join (urls ),
409
+ '-h' , ' ' .join (urls ),
363
410
]
364
411
if self ._log .isEnabledFor (logging .DEBUG ):
365
412
slapd_args .extend (['-d' , '-1' ])
0 commit comments