def latest_build_dir(check_executable=True): '''Look for the most recently built version of this project''' masterBuildDir = os.path.join(project_root_dir(), 'build') if not os.path.isdir(masterBuildDir): raise test_exceptions.NotBuiltException( detail='no version of this project have yet been built') # -- find the build directory with the most recent mtime canidatePath = None canidateMtime = None for name in os.listdir(masterBuildDir): path = os.path.join(masterBuildDir, name) if os.path.isdir(path) and (name in ('release', 'debug') or name.startswith('debug_') or name.startswith('release_')): if check_executable == True: if not os.path.isfile(os.path.join(path, 'rethinkdb')): continue mtime = os.path.getmtime(path) if canidateMtime is None or mtime > canidateMtime: canidateMtime = mtime canidatePath = path if canidatePath is None: raise test_exceptions.NotBuiltException( detail='no version of this project have yet been built') else: return canidatePath
def latest_build_dir(check_executable=True, mode=None): '''Look for the most recently built version of this project''' candidatePath = None if os.getenv('RETHINKDB_BUILD_DIR') is not None: candidatePath = os.path.realpath(os.getenv('RETHINKDB_BUILD_DIR')) else: masterBuildDir = os.path.join(project_root_dir, 'build') if not os.path.isdir(masterBuildDir): raise test_exceptions.NotBuiltException( detail='no version of this project has yet been built') if mode in (None, ''): mode = ['release', 'debug'] elif not hasattr(mode, '__iter__'): mode = [str(mode)] # -- find the build directory with the most recent mtime candidateMtime = None for name in os.listdir(masterBuildDir): path = os.path.join(masterBuildDir, name) if os.path.isdir(path) and any( map( lambda x: name.startswith(x + '_') or name.lower() == x, mode)): if check_executable == True: if not os.path.isfile(os.path.join(path, 'rethinkdb')): continue mtime = os.path.getmtime(path) if candidateMtime is None or mtime > candidateMtime: candidateMtime = mtime candidatePath = path if candidatePath is None: raise test_exceptions.NotBuiltException( detail='no built version of the server could be found') if candidatePath is None or (check_executable is True and not os.access( os.path.join(candidatePath, 'rethinkdb'), os.X_OK)): raise test_exceptions.NotBuiltException( detail= 'the rethinkdb server executable was not present/runnable in: %s' % candidatePath) return candidatePath
def import_python_driver(): '''return the rethinkdb Python driver, defaulting (and building) the in-source driver, but following PYTHON_DRIVER''' global __loadedPythonDriver # -- short-circut if we already have it loaded if __loadedPythonDriver: return __loadedPythonDriver # -- loadedDriver = None driverPath = driverPaths['Python']['driverPath'] sourcePath = driverPaths['Python']['sourcePath'] # -- short-circut if the installed driver is called for if driverPath == '--installed--': # - load the driver try: loadedDriver = __import__('rethinkdb') driverPaths['Python']['driverPath'] = os.path.dirname(loadedDriver.__file__) os.environ['PYTHON_DRIVER'] = driverPaths['Python']['driverPath'] return loadedDriver except ImportError as e: raise ImportError('Unable to load system-installed `rethinkdb` module - %s' % str(e)) # -- build the driver if that is called for if sourcePath: try: build_in_folder(sourcePath, waitNotification='Building the python drivers. This make take a few moments.') except test_exceptions.NotBuiltException as e: raise test_exceptions.NotBuiltException(detail='Failed making Python driver from: %s' % sourcePath, debugInfo=e.debugInfo) # -- validate the built driver if not all(map(lambda x: os.path.isfile(os.path.join(driverPath, x)), ['__init__.py', 'ast.py', 'docs.py'])): raise ValueError('Invalid Python driver: %s' % driverPath) # -- load the driver keptPaths = sys.path[:] driverName = os.path.basename(driverPath) try: sys.path.insert(0, os.path.dirname(driverPath)) loadedDriver = __import__(driverName) finally: sys.path = keptPaths # -- check that it is from where we assert it to be if not loadedDriver.__file__.startswith(driverPath): raise ImportError('Loaded Python driver was %s, rather than the expected one from %s' % (loadedDriver.__file__, driverPath)) # -- return the loaded module __loadedPythonDriver = loadedDriver return __loadedPythonDriver
def find_rethinkdb_executable(mode=None): result_path = os.environ.get('RDB_EXE_PATH') or os.path.join(latest_build_dir(check_executable=True, mode=mode), 'rethinkdb') if not os.access(result_path, os.X_OK): raise test_exceptions.NotBuiltException(detail='The rethinkdb server executable is not available: %s' % str(result_path)) return result_path
def build_in_folder(targetFolder, waitNotification=None, notificationTimeout=2, buildOptions=None): '''Call `make -C` on a folder to build it. If waitNotification is given wait notificationTimeout seconds and then print the notification''' outputFile = tempfile.NamedTemporaryFile('w+') notificationDeadline = None if waitNotification not in ("", None): notificationDeadline = time.time() + notificationTimeout makeProcess = subprocess.Popen(['make', '-C', targetFolder], stdout=outputFile, stderr=subprocess.STDOUT) if notificationDeadline is not None: while makeProcess.poll() is None and time.time( ) < notificationDeadline: time.sleep(.1) if time.time() > notificationDeadline: print(waitNotification) if makeProcess.wait() != 0: raise test_exceptions.NotBuiltException(detail='Failed making: %s' % targetFolder, debugInfo=outputFile)
def import_python_driver(targetDir=None): '''import the latest built version of the python driver into the caller's namespace, ensuring that the drivers are built''' import imp # note: deprecated but not gone in 3.4, will have to add importlib at some point # TODO: modify this to allow for system-installed drivers # -- figure out what sort of path we got if targetDir is None: if 'PYTHON_DRIVER_DIR' in os.environ: targetDir = os.environ['PYTHON_DRIVER_DIR'] elif 'PYTHON_DRIVER_SRC_DIR' in os.environ: targetDir = os.environ['PYTHON_DRIVER_SRC_DIR'] else: targetDir = project_root_dir driverDir = None srcDir = None if not os.path.isdir(targetDir): raise ValueError('import_python_driver got a non-directory path: %s' % str(targetDir)) targetDir = os.path.realpath(targetDir) validSourceFolder = lambda path: os.path.basename(path) == 'rethinkdb' and all(map(lambda x: os.path.isfile(os.path.join(path, x)), ['__init__.py', 'ast.py', 'docs.py'])) builtDriver = lambda path: validSourceFolder(path) and os.path.isfile(os.path.join(path, 'ql2_pb2.py')) # normalize path if not os.path.dirname(targetDir) == 'rethinkdb' and os.path.isdir(os.path.join(targetDir, 'rethinkdb')): targetDir = os.path.join(targetDir, 'rethinkdb') # - project directory if all(map(lambda x: os.path.isdir(os.path.join(targetDir, x)), ['src', 'drivers', 'admin'])): buildDriver = True driverDir = os.path.join(targetDir, driverPaths['python']['driverPath']) srcDir = os.path.join(targetDir, driverPaths['python']['sourcePath']) # - built driver - it does not matter if this is source, build, or installed, it looks complete elif builtDriver(targetDir): buildDriver = False driverDir = targetDir srcDir = None # - source folder elif validSourceFolder(targetDir) and os.path.isfile(os.path.join(os.path.dirname(targetDir), 'Makefile')): buildDriver = True driverDir = os.path.join(targetDir, os.path.pardir, os.path.relpath(driverPaths['python']['driverPath'], driverPaths['python']['sourcePath'])) srcDir = os.path.dirname(targetDir) else: raise ValueError('import_python_driver was unable to determine the locations from: %s' % targetDir) # -- build if needed if buildDriver == True: try: build_in_folder(srcDir, waitNotification='Building the python drivers. This make take a few moments.') except test_exceptions.NotBuiltException as e: raise test_exceptions.NotBuiltException(detail='Failed making Python driver from: %s' % srcDir, debugInfo=e.debugInfo) # -- if not os.path.isdir(driverDir) or not os.path.basename(driverDir) == 'rethinkdb' or not os.path.isfile(os.path.join(driverDir, '__init__.py')): # ToDo: handle zipped egg case raise ValueError('import_python_driver got an invalid driverDir: %s' % driverDir) # - return the imported module keptPaths = sys.path[:] try: moduleFile, pathname, desc = imp.find_module('rethinkdb', [os.path.dirname(driverDir)]) driverModule = imp.load_module('rethinkdb', moduleFile, pathname, desc) if moduleFile is not None: moduleFile.close() loadedFrom = os.path.dirname(os.path.realpath(driverModule.__file__)) assert loadedFrom.startswith(driverDir), "The wrong version or the rethinkdb Python driver got imported. It should have been in %s but was from %s" % (driverDir, loadedFrom) return driverModule finally: sys.path = keptPaths