def getfullnameof(mod, xtrapath=None): """ Return the full path name of MOD. MOD is the basename of a dll or pyd. XTRAPATH is a path or list of paths to search first. Return the full path name of MOD. Will search the full Windows search path, as well as sys.path """ pywin32_paths = [] if compat.is_win: pywin32_paths = [os.path.join(get_python_lib(), 'pywin32_system32')] if compat.is_venv: pywin32_paths.append( os.path.join(compat.base_prefix, 'Lib', 'site-packages', 'pywin32_system32')) epath = ( sys.path + # Search sys.path first! pywin32_paths + winutils.get_system_path() + compat.getenv('PATH', '').split(os.pathsep)) if xtrapath is not None: if type(xtrapath) == type(''): epath.insert(0, xtrapath) else: epath = xtrapath + epath for p in epath: npth = os.path.join(p, mod) if os.path.exists(npth) and matchDLLArch(npth): return npth return ''
def _run_executable(self, prog, args, run_from_path, runtime): """ Run executable created by PyInstaller. :param args: CLI options to pass to the created executable. """ # Run the test in a clean environment to make sure they're really self-contained. prog_env = copy.deepcopy(os.environ) prog_env['PATH'] = '' del prog_env['PATH'] # For Windows we need to keep minimal PATH for successful running of some tests. if is_win: # Minimum Windows PATH is in most cases: C:\Windows\system32;C:\Windows prog_env['PATH'] = os.pathsep.join(winutils.get_system_path()) exe_path = prog if (run_from_path): # Run executable in the temp directory # Add the directory containing the executable to $PATH # Basically, pretend we are a shell executing the program from $PATH. prog_cwd = self._tmpdir prog_name = os.path.basename(prog) prog_env['PATH'] = os.pathsep.join( [prog_env.get('PATH', ''), os.path.dirname(prog)]) else: # Run executable in the directory where it is. prog_cwd = os.path.dirname(prog) # The executable will be called with argv[0] as relative not absolute path. prog_name = os.path.join(os.curdir, os.path.basename(prog)) # Workaround to enable win_codepage_test # If _distdir is 'bytes', PyI build fails with ASCII decode error # when it joins the 'bytes' _distdir with the 'unicode' filenames from bindep and # winmanifest. # # PyI succeeds with _distdir as 'unicode', but subprocess # fails with ASCII encode error. subprocess succeeds if progname is # mbcs-encoded 'bytes' if is_win and is_py2: if isinstance(exe_path, unicode): exe_path = exe_path.encode('mbcs') if isinstance(prog_name, unicode): prog_name = prog_name.encode('mbcs') if isinstance(prog_cwd, unicode): prog_cwd = prog_cwd.encode('mbcs') args = [prog_name] + args # Using sys.stdout/sys.stderr for subprocess fixes printing messages in # Windows command prompt. Py.test is then able to collect stdout/sterr # messages and display them if a test fails. for _ in range(_MAX_RETRIES): retcode = self.__run_executable(args, exe_path, prog_env, prog_cwd, runtime) if retcode != 1: # retcode == 1 means a timeout break return retcode
def _run_executable(self, prog, args, run_from_path, runtime): """ Run executable created by PyInstaller. :param args: CLI options to pass to the created executable. """ # Run the test in a clean environment to make sure they're really self-contained. prog_env = copy.deepcopy(os.environ) prog_env['PATH'] = '' del prog_env['PATH'] # For Windows we need to keep minimal PATH for successful running of some tests. if is_win: # Minimum Windows PATH is in most cases: C:\Windows\system32;C:\Windows prog_env['PATH'] = os.pathsep.join(winutils.get_system_path()) exe_path = prog if(run_from_path): # Run executable in the temp directory # Add the directory containing the executable to $PATH # Basically, pretend we are a shell executing the program from $PATH. prog_cwd = self._tmpdir prog_name = os.path.basename(prog) prog_env['PATH'] = os.pathsep.join([prog_env.get('PATH', ''), os.path.dirname(prog)]) else: # Run executable in the directory where it is. prog_cwd = os.path.dirname(prog) # The executable will be called with argv[0] as relative not absolute path. prog_name = os.path.join(os.curdir, os.path.basename(prog)) # Workaround to enable win_codepage_test # If _distdir is 'bytes', PyI build fails with ASCII decode error # when it joins the 'bytes' _distdir with the 'unicode' filenames from bindep and # winmanifest. # # PyI succeeds with _distdir as 'unicode', but subprocess # fails with ASCII encode error. subprocess succeeds if progname is # mbcs-encoded 'bytes' if is_win and is_py2: if isinstance(exe_path, unicode): exe_path = exe_path.encode('mbcs') if isinstance(prog_name, unicode): prog_name = prog_name.encode('mbcs') if isinstance(prog_cwd, unicode): prog_cwd = prog_cwd.encode('mbcs') args = [prog_name] + args # Using sys.stdout/sys.stderr for subprocess fixes printing messages in # Windows command prompt. Py.test is then able to collect stdout/sterr # messages and display them if a test fails. for _ in range(_MAX_RETRIES): retcode = self.__run_executable(args, exe_path, prog_env, prog_cwd, runtime) if retcode != 1: # retcode == 1 means a timeout break return retcode
def _run_executable(self, prog, args, run_from_path, runtime): """ Run executable created by PyInstaller. :param args: CLI options to pass to the created executable. """ # Run the test in a clean environment to make sure they're really self-contained. prog_env = copy.deepcopy(os.environ) prog_env['PATH'] = '' del prog_env['PATH'] # For Windows we need to keep minimal PATH for successful running of some tests. if is_win: # Minimum Windows PATH is in most cases: C:\Windows\system32;C:\Windows prog_env['PATH'] = os.pathsep.join(winutils.get_system_path()) exe_path = prog if (run_from_path): # Run executable in the temp directory # Add the directory containing the executable to $PATH # Basically, pretend we are a shell executing the program from $PATH. prog_cwd = str(self._tmpdir) prog_name = os.path.basename(prog) prog_env['PATH'] = os.pathsep.join( [prog_env.get('PATH', ''), os.path.dirname(prog)]) else: # Run executable in the directory where it is. prog_cwd = os.path.dirname(prog) # The executable will be called with argv[0] as relative not absolute path. prog_name = os.path.join(os.curdir, os.path.basename(prog)) args = [prog_name] + args # Using sys.stdout/sys.stderr for subprocess fixes printing messages in # Windows command prompt. Py.test is then able to collect stdout/sterr # messages and display them if a test fails. for _ in range(_MAX_RETRIES): retcode = self.__run_executable(args, exe_path, prog_env, prog_cwd, runtime) if retcode != 1: # retcode == 1 means a timeout break return retcode
def _run_created_exe(self, prog): """ Run executable created by PyInstaller. """ # Run the test in a clean environment to make sure they're # really self-contained path = compat.getenv('PATH') compat.unsetenv('PATH') # For Windows we need to keep minimal PATH for sucessful running of some tests. if is_win: # Minimum Windows PATH is in most cases: C:\Windows\system32;C:\Windows compat.setenv('PATH', os.pathsep.join(winutils.get_system_path())) self._plain_msg("RUNNING: " + prog) old_wd = os.getcwd() os.chdir(os.path.dirname(prog)) # Run executable. prog = os.path.join(os.curdir, os.path.basename(prog)) proc = subprocess.Popen([prog], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Prints stdout of subprocess continuously. self._msg('STDOUT %s' % self.test_name) while proc.poll() is None: # We need to read a line, not single bytes. Otherwise decoding # would ail. line = proc.stdout.readline() self._plain_msg(line.decode('utf-8'), newline=False) # Print any stdout that wasn't read before the process terminated. # See the conversation in https://github.com/pyinstaller/pyinstaller/pull/1092 # for examples of why this is necessary. self._plain_msg(proc.stdout.read().decode('utf-8'), newline=False) # Print possible stderr at the end. stderr = proc.stderr.read().decode('utf-8') self._msg('STDERR %s' % self.test_name) self._plain_msg(stderr) compat.setenv("PATH", path) # Restore current working directory os.chdir(old_wd) return proc.returncode, stderr
def getfullnameof(mod, xtrapath=None): """ Return the full path name of MOD. MOD is the basename of a dll or pyd. XTRAPATH is a path or list of paths to search first. Return the full path name of MOD. Will search the full Windows search path, as well as sys.path """ # TODO: Allow in import-hooks to specify additional paths where the PyInstaller # should look for other libraries. # Or allow to automatically look for dlls in directories where are .pyd files. # SciPy/Numpy Windows builds from http://www.lfd.uci.edu/~gohlke/pythonlibs # Contain some dlls in directory like C:\Python27\Lib\site-packages\numpy\core\ from distutils.sysconfig import get_python_lib numpy_core_paths = [os.path.join(get_python_lib(), 'numpy', 'core')] # In virtualenv numpy might be installed directly in real prefix path. # Then include this path too. if is_venv: numpy_core_paths.append( os.path.join(base_prefix, 'Lib', 'site-packages', 'numpy', 'core') ) # Search sys.path first! epath = sys.path + numpy_core_paths + winutils.get_system_path() if xtrapath is not None: if type(xtrapath) == type(''): epath.insert(0, xtrapath) else: epath = xtrapath + epath for p in epath: npth = os.path.join(p, mod) if os.path.exists(npth) and matchDLLArch(npth): return npth # second try: lower case filename for p in epath: npth = os.path.join(p, mod.lower()) if os.path.exists(npth) and matchDLLArch(npth): return npth return ''
def getfullnameof(mod, xtrapath=None): """ Return the full path name of MOD. MOD is the basename of a dll or pyd. XTRAPATH is a path or list of paths to search first. Return the full path name of MOD. Will search the full Windows search path, as well as sys.path """ # TODO: Allow in import-hooks to specify additional paths where the PyInstaller # should look for other libraries. # Or allow to automatically look for dlls in directories where are .pyd files. # SciPy/Numpy Windows builds from http://www.lfd.uci.edu/~gohlke/pythonlibs # Contain some dlls in directory like C:\Python27\Lib\site-packages\numpy\core\ from distutils.sysconfig import get_python_lib numpy_core_paths = [os.path.join(get_python_lib(), 'numpy', 'core')] # In virtualenv numpy might be installed directly in real prefix path. # Then include this path too. if is_venv: numpy_core_paths.append( os.path.join(base_prefix, 'Lib', 'site-packages', 'numpy', 'core')) # Search sys.path first! epath = sys.path + numpy_core_paths + winutils.get_system_path() if xtrapath is not None: if type(xtrapath) == type(''): epath.insert(0, xtrapath) else: epath = xtrapath + epath for p in epath: npth = os.path.join(p, mod) if os.path.exists(npth) and matchDLLArch(npth): return npth # second try: lower case filename for p in epath: npth = os.path.join(p, mod.lower()) if os.path.exists(npth) and matchDLLArch(npth): return npth return ''
def _run_created_exe(self, prog): """ Run executable created by PyInstaller. """ # Run the test in a clean environment to make sure they're # really self-contained path = compat.getenv('PATH') compat.unsetenv('PATH') # For Windows we need to keep minimal PATH for sucessful running of some tests. if is_win: # Minimum Windows PATH is in most cases: C:\Windows\system32;C:\Windows compat.setenv('PATH', os.pathsep.join(winutils.get_system_path())) self._plain_msg("RUNNING: " + prog) old_wd = compat.getcwd() os.chdir(os.path.dirname(prog)) # Run executable. prog = os.path.join(os.curdir, os.path.basename(prog)) proc = subprocess.Popen([prog], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Prints stdout of subprocess continuously. self._msg('STDOUT %s' % self.test_name) while proc.poll() is None: # We need to read a line, not single bytes. Otherwise decoding # would ail. line = proc.stdout.readline() self._plain_msg(line.decode('utf-8'), newline=False) # Print any stdout that wasn't read before the process terminated. # See the conversation in https://github.com/pyinstaller/pyinstaller/pull/1092 # for examples of why this is necessary. self._plain_msg(proc.stdout.read().decode('utf-8'), newline=False) # Print possible stderr at the end. stderr = proc.stderr.read().decode('utf-8') self._msg('STDERR %s' % self.test_name) self._plain_msg(stderr) compat.setenv("PATH", path) # Restore current working directory os.chdir(old_wd) return proc.returncode, stderr
def _run_executable(self, prog, args, run_from_path, runtime): """ Run executable created by PyInstaller. :param args: CLI options to pass to the created executable. """ # Run the test in a clean environment to make sure they're really self-contained. prog_env = copy.deepcopy(os.environ) prog_env['PATH'] = '' del prog_env['PATH'] # For Windows we need to keep minimal PATH for successful running of some tests. if is_win: # Minimum Windows PATH is in most cases: C:\Windows\system32;C:\Windows prog_env['PATH'] = os.pathsep.join(winutils.get_system_path()) exe_path = prog if(run_from_path): # Run executable in the temp directory # Add the directory containing the executable to $PATH # Basically, pretend we are a shell executing the program from $PATH. prog_cwd = self._tmpdir prog_name = os.path.basename(prog) prog_env['PATH'] = os.pathsep.join([prog_env.get('PATH', ''), os.path.dirname(prog)]) else: # Run executable in the directory where it is. prog_cwd = os.path.dirname(prog) # The executable will be called with argv[0] as relative not absolute path. prog_name = os.path.join(os.curdir, os.path.basename(prog)) # Workaround to enable win_codepage_test # If _distdir is 'bytes', PyI build fails with ASCII decode error # when it joins the 'bytes' _distdir with the 'unicode' filenames from bindep and # winmanifest. # # PyI succeeds with _distdir as 'unicode', but subprocess # fails with ASCII encode error. subprocess succeeds if progname is # mbcs-encoded 'bytes' if is_win and is_py2: if isinstance(exe_path, unicode): exe_path = exe_path.encode('mbcs') if isinstance(prog_name, unicode): prog_name = prog_name.encode('mbcs') if isinstance(prog_cwd, unicode): prog_cwd = prog_cwd.encode('mbcs') args = [prog_name] + args # Run executable. stderr is redirected to stdout. print('RUNNING: ', safe_repr(exe_path), ", args: ", safe_repr(args)) # Using sys.stdout/sys.stderr for subprocess fixes printing messages in # Windows command prompt. Py.test is then able to collect stdout/sterr # messages and display them if a test fails. process = psutil.Popen(args, executable=exe_path, stdout=sys.stdout, stderr=sys.stderr, env=prog_env, cwd=prog_cwd) # 'psutil' allows to use timeout in waiting for a subprocess. # If not timeout was specified then it is 'None' - no timeout, just waiting. # Runtime is useful mostly for interactive tests. try: timeout = runtime if runtime else _EXE_TIMEOUT retcode = process.wait(timeout=timeout) except psutil.TimeoutExpired: if runtime: # When 'runtime' is set then expired timeout is a good sing # that the executable was running successfully for a specified time. # TODO Is there a better way return success than 'retcode = 0'? retcode = 0 else: # Exe is still running and it is not an interactive test. Fail the test. retcode = 1 # Kill the subprocess and its child processes. for p in process.children(recursive=True): p.kill() process.kill() return retcode
def _run_executable(self, prog, args, run_from_path, runtime): """ Run executable created by PyInstaller. :param args: CLI options to pass to the created executable. """ # Run the test in a clean environment to make sure they're really self-contained. prog_env = copy.deepcopy(os.environ) prog_env['PATH'] = '' del prog_env['PATH'] # For Windows we need to keep minimal PATH for successful running of some tests. if is_win: # Minimum Windows PATH is in most cases: C:\Windows\system32;C:\Windows prog_env['PATH'] = os.pathsep.join(winutils.get_system_path()) exe_path = prog if (run_from_path): # Run executable in the temp directory # Add the directory containing the executable to $PATH # Basically, pretend we are a shell executing the program from $PATH. prog_cwd = self._tmpdir prog_name = os.path.basename(prog) prog_env['PATH'] = os.pathsep.join( [prog_env.get('PATH', ''), os.path.dirname(prog)]) else: # Run executable in the directory where it is. prog_cwd = os.path.dirname(prog) # The executable will be called with argv[0] as relative not absolute path. prog_name = os.path.join(os.curdir, os.path.basename(prog)) # Workaround to enable win_codepage_test # If _distdir is 'bytes', PyI build fails with ASCII decode error # when it joins the 'bytes' _distdir with the 'unicode' filenames from bindep and # winmanifest. # # PyI succeeds with _distdir as 'unicode', but subprocess # fails with ASCII encode error. subprocess succeeds if progname is # mbcs-encoded 'bytes' if is_win and is_py2: if isinstance(exe_path, unicode): exe_path = exe_path.encode('mbcs') if isinstance(prog_name, unicode): prog_name = prog_name.encode('mbcs') if isinstance(prog_cwd, unicode): prog_cwd = prog_cwd.encode('mbcs') args = [prog_name] + args # Run executable. stderr is redirected to stdout. print('RUNNING: ', safe_repr(exe_path), ", args: ", safe_repr(args)) # Using sys.stdout/sys.stderr for subprocess fixes printing messages in # Windows command prompt. Py.test is then able to collect stdout/sterr # messages and display them if a test fails. process = psutil.Popen(args, executable=exe_path, stdout=sys.stdout, stderr=sys.stderr, env=prog_env, cwd=prog_cwd) # 'psutil' allows to use timeout in waiting for a subprocess. # If not timeout was specified then it is 'None' - no timeout, just waiting. # Runtime is useful mostly for interactive tests. try: timeout = runtime if runtime else _EXE_TIMEOUT retcode = process.wait(timeout=timeout) except psutil.TimeoutExpired: if runtime: # When 'runtime' is set then expired timeout is a good sing # that the executable was running successfully for a specified time. # TODO Is there a better way return success than 'retcode = 0'? retcode = 0 else: # Exe is still running and it is not an interactive test. Fail the test. retcode = 1 # Kill the subprocess and its child processes. for p in process.children(recursive=True): p.kill() process.kill() return retcode
def _run_executable(self, prog, args, run_from_path, runtime): """ Run executable created by PyInstaller. :param args: CLI options to pass to the created executable. """ # Run the test in a clean environment to make sure they're really self-contained. prog_env = copy.deepcopy(os.environ) prog_env['PATH'] = '' del prog_env['PATH'] # For Windows we need to keep minimal PATH for successful running of some tests. if is_win: # Minimum Windows PATH is in most cases: C:\Windows\system32;C:\Windows prog_env['PATH'] = os.pathsep.join(winutils.get_system_path()) exe_path = prog if(run_from_path): # Run executable in the temp directory # Add the directory containing the executable to $PATH # Basically, pretend we are a shell executing the program from $PATH. prog_cwd = self._tmpdir prog_name = os.path.basename(prog) prog_env['PATH'] = os.pathsep.join([prog_env.get('PATH', ''), os.path.dirname(prog)]) else: # Run executable in the directory where it is. prog_cwd = os.path.dirname(prog) # The executable will be called with argv[0] as relative not absolute path. prog_name = os.path.join(os.curdir, os.path.basename(prog)) # Workaround to enable win_codepage_test # If _distdir is 'bytes', PyI build fails with ASCII decode error # when it joins the 'bytes' _distdir with the 'unicode' filenames from bindep and # winmanifest. # # PyI succeeds with _distdir as 'unicode', but subprocess # fails with ASCII encode error. subprocess succeeds if progname is # mbcs-encoded 'bytes' if is_win and is_py2: if isinstance(exe_path, unicode): exe_path = exe_path.encode('mbcs') if isinstance(prog_name, unicode): prog_name = prog_name.encode('mbcs') if isinstance(prog_cwd, unicode): prog_cwd = prog_cwd.encode('mbcs') args = [prog_name] + args # Run executable. stderr is redirected to stdout. print('RUNNING: ', safe_repr(exe_path), ", args: ", safe_repr(args)) # Using sys.stdout/sys.stderr for subprocess fixes printing messages in # Windows command prompt. Py.test is then able to collect stdout/sterr # messages and display them if a test fails. if is_py2: # Timeout keyword supported only in Python 3.3+ # TODO use module 'subprocess32' which implements timeout for Python 2.7. retcode = subprocess.call(args, executable=exe_path, stdout=sys.stdout, stderr=sys.stderr, env=prog_env, cwd=prog_cwd) else: try: retcode = subprocess.call(args, executable=exe_path, stdout=sys.stdout, stderr=sys.stderr, env=prog_env, cwd=prog_cwd, timeout=runtime) except subprocess.TimeoutExpired: # When 'timeout' is set then expired timeout is a good sing # that the executable was running successfully for a specified time. # TODO Is there a better way return success than 'retcode = 0'? retcode = 0 return retcode