def check_for_shell(msys_directory=None): """Check various locations for MSYS shell or root directory. May raise MsysException. """ if msys_directory is not None: try: dir_path = find_msys_version_subdir(msys_directory) except MsysException: pass else: return as_shell(dir_path) try: shell = os.environ['SHELL'] except KeyError: pass else: if is_msys(): return shell + '.exe' return shell try: dir_path = find_msys_registry() except LookupError: pass else: return as_shell(dir_path) return as_shell(input_msys_dir())
def is_msys_mingw(): """Return true if this in an MinGW/MSYS build The user may prompted for confirmation so only call this function once. """ if msysio.is_msys(): return 1 if ('MINGW_ROOT_DIRECTORY' in os.environ or os.path.isfile(mingwcfg.path)): return confirm("Is this an mingw/msys build") return 0
def check_for_shell(msys_directory=None): """Check various locations for MSYS shell or root directory. May raise MsysException. """ if msys_directory is not None: try: dir_path = find_msys_version_subdir(msys_directory) except MsysException: pass else: return as_shell(dir_path) try: shell = os.environ['MSYS_ROOT_DIRECTORY'] except KeyError: pass else: if not re.search(r'[\\/]1\.[0-9]', shell): shell = os.path.join(shell, '1.0') return os.path.join(shell, 'bin', 'sh.exe') if is_msys(): try: return os.environ['WD'] + os.environ['MSYSCON'] except KeyError: pass try: dir_path = find_msys_registry() except LookupError: pass else: return as_shell(dir_path) return as_shell(input_msys_dir())
class Msys(object): """Return a new Msys environment; May raise MsysException Msys([msys_directory, [require_mingw]]) msys_directory: A string giving the path of the MSYS directory. Either or both keyword arguments can be omitted. If msys_directory is not provided then the environment variable SHELL and the Windows registry are checked. Finally the user is prompted for the directory path. If require_mingw is True, the default, the mingw directory path is retrieved from the MSYS fstab file. An MsysException is raised if the required paths are not found. """ _is_msys = is_msys() def __init__(self, msys_directory=None, require_mingw=None): """New environment May raise MsysException""" if require_mingw is None: require_mingw = True self._environ = os.environ.copy() self._shell = find_msys_shell(msys_directory) self._msys_root = os.path.split(os.path.split( self.shell)[0])[0].lower() try: self._mingw_root = find_mingw_root(self.msys_root) except MsysException: if require_mingw: raise self._mingw_root = None else: self.environ['MINGW_ROOT_DIRECTORY'] = self._mingw_root environ = property(lambda self: self._environ, doc="Environment variables") shell = property(lambda self: self._shell, doc="MSYS shell program path") msys_root = property(lambda self: self._msys_root, doc="MSYS root directory path") mingw_root = property(lambda self: self._mingw_root, doc="MinGW root directory path") is_msys = property(lambda self: self._is_msys, doc="True if the execution environment is MSYS") def windows_to_msys(self, path): """Return an MSYS translation of an absolute Windows path""" msys_root = self.msys_root mingw_root = self.mingw_root path_lower = path.lower() if path_lower.startswith(msys_root.lower()): return '/usr' + path[len(msys_root):].replace(os.sep, '/') if mingw_root is not None and path_lower.startswith( mingw_root.lower()): return '/mingw' + path[len(mingw_root):].replace(os.sep, '/') drive, tail = os.path.splitdrive(path) return '/%s%s' % (drive[0], tail.replace(os.sep, '/')) def msys_to_windows(self, path): """Return a Windows translation of an MSYS path The Unix path separator is uses as it survives the distutils setup file read process. Raises a ValueError if the path cannot be translated. """ msys_root = self.msys_root mingw_root = self.mingw_root if path.startswith('/usr'): path = msys_root + path[4:] elif path.startswith('/mingw'): if mingw_root is None: raise ValueError('Unable to map the MinGW directory') path = mingw_root + path[6:] elif has_drive(path): path = path[1] + ":" + path[2:] elif path == '/': path = msys_root elif path.startswith('/'): path = msys_root + path return path.replace(os.sep, '/') def run_shell_script(self, script): """Run the MSYS shell script and return the shell return code script is a string representing the contents of the script. """ cmd = [self.shell] if not self._is_msys: cmd.append('--login') previous_cwd = os.getcwd() try: process = subprocess.Popen(cmd, stdin=subprocess.PIPE, env=self.environ) process.communicate(script) return process.returncode finally: time.sleep(2) # Allow shell subprocesses to terminate. os.chdir(previous_cwd) def run_shell_command(self, command): """Run the MSYS shell command and return stdout output as a string command is a list of strings giving the command and its arguments. The first list entry must be the MSYS path name of a bash shell script file. """ args = [self.shell] if not self._is_msys: args.append('--login') args.extend(command) previous_cwd = os.getcwd() try: return subprocess.Popen(args, stdout=subprocess.PIPE, env=self.environ).communicate()[0] finally: time.sleep(3) # Allow shell subprocesses to terminate. os.chdir(previous_cwd)