def default_env(env=None): """Constructs a default xonsh environment.""" # in order of increasing precedence ctx = dict(BASE_ENV) ctx.update(os.environ) ctx.update(bash_env()) if ON_WINDOWS: # Windows default prompt doesn't work. ctx['PROMPT'] = DEFAULT_PROMPT # remove these bash variables which only cause problems. for ev in ['HOME', 'OLDPWD']: if ev in ctx: del ctx[ev] # Override path-related bash variables; on Windows bash uses # /c/Windows/System32 syntax instead of C:\\Windows\\System32 # which messes up these environment variables for xonsh. for ev in ['PATH', 'TEMP', 'TMP']: if ev in os.environ: ctx[ev] = os.environ[ev] elif ev in ctx: del ctx[ev] ctx['PWD'] = _get_cwd() # finalize env recursive_base_env_update(ctx) if env is not None: ctx.update(env) return ctx
def default_env(env=None): """Constructs a default xonsh environment.""" # in order of increasing precedence ctx = dict(BASE_ENV) ctx.update(os.environ) ctx.update(bash_env()) if ON_WINDOWS: # Windows default prompt doesn't work. ctx["PROMPT"] = DEFAULT_PROMPT # remove these bash variables which only cause problems. for ev in ["HOME", "OLDPWD"]: if ev in ctx: del ctx[ev] # Override path-related bash variables; on Windows bash uses # /c/Windows/System32 syntax instead of C:\\Windows\\System32 # which messes up these environment variables for xonsh. for ev in ["PATH", "TEMP", "TMP"]: if ev in os.environ: ctx[ev] = os.environ[ev] elif ev in ctx: del ctx[ev] ctx["PWD"] = _get_cwd() # finalize env recursive_base_env_update(ctx) if env is not None: ctx.update(env) return ctx
def default_env(env=None, config=None, login=True): """Constructs a default xonsh environment.""" # in order of increasing precedence ctx = dict(BASE_ENV) ctx.update(os.environ) ctx['PWD'] = _get_cwd() or '' # other shells' PROMPT definitions generally don't work in XONSH: try: del ctx['PROMPT'] except KeyError: pass if login: conf = load_static_config(ctx, config=config) foreign_env = load_foreign_envs(shells=conf.get('foreign_shells', ()), issue_warning=False) if ON_WINDOWS: windows_foreign_env_fixes(foreign_env) foreign_env_fixes(foreign_env) ctx.update(foreign_env) # Do static config environment last, to allow user to override any of # our environment choices ctx.update(conf.get('env', ())) # finalize env if env is not None: ctx.update(env) return ctx
def default_env(env=None, config=None, login=True): """Constructs a default xonsh environment.""" # in order of increasing precedence ctx = dict(BASE_ENV) ctx.update(os.environ) ctx["PWD"] = _get_cwd() or "" # other shells' PROMPT definitions generally don't work in XONSH: try: del ctx["PROMPT"] except KeyError: pass if login: conf = load_static_config(ctx, config=config) foreign_env = load_foreign_envs(shells=conf.get("foreign_shells", ()), issue_warning=False) if ON_WINDOWS: windows_foreign_env_fixes(foreign_env) foreign_env_fixes(foreign_env) ctx.update(foreign_env) # Do static config environment last, to allow user to override any of # our environment choices ctx.update(conf.get("env", ())) # finalize env if env is not None: ctx.update(env) return ctx
def update(self, ctx): # call the subprocess only if cwd changed from xonsh.dirstack import _get_cwd cwd = _get_cwd() if cwd != self._cwd: self._cwd = cwd super().update(ctx)
def wrapper(*args, **kwargs): # Get cwd or bail kwargs['cwd'] = kwargs.get('cwd', _get_cwd()) if kwargs['cwd'] is None: return # step out completely if git is not installed if locate_binary('git', kwargs['cwd']) is None: return return func(*args, **kwargs)
def wrapper(*args, **kwargs): # Get cwd or bail kwargs["cwd"] = kwargs.get("cwd", _get_cwd()) if kwargs["cwd"] is None: return # step out completely if git is not installed if locate_binary("git", kwargs["cwd"]) is None: return return func(*args, **kwargs)
def current_branch(cwd=None, pad=True): """Gets the branch for a current working directory. Returns None if the cwd is not a repository. This currently only works for git, bust should be extended in the future. """ branch = None cwd = _get_cwd() if cwd is None else cwd if cwd is None: return '' # step out completely if git is not installed try: binary_location = subprocess.check_output(['which', 'git'], cwd=cwd, stderr=subprocess.PIPE, universal_newlines=True) if not binary_location: return branch except subprocess.CalledProcessError: return branch prompt_scripts = ['/usr/lib/git-core/git-sh-prompt', '/usr/local/etc/bash_completion.d/git-prompt.sh'] for script in prompt_scripts: # note that this is about 10x faster than bash -i "__git_ps1" _input = ('source {}; __git_ps1 "${{1:-%s}}"'.format(script)) try: branch = subprocess.check_output(['bash', ], cwd=cwd, input=_input, stderr=subprocess.PIPE, universal_newlines=True) or None except subprocess.CalledProcessError: continue # fall back to using the git binary if the above failed if branch is None: try: cmd = ['git', 'rev-parse', '--abbrev-ref', 'HEAD'] s = subprocess.check_output(cmd, stderr=subprocess.PIPE, cwd=cwd, universal_newlines=True) s = s.strip() if len(s) > 0: branch = s except subprocess.CalledProcessError: pass if pad and branch is not None: branch = ' ' + branch return branch or ''
def sudo(args, sdin=None): if len(args) < 1: print("You need to provide an executable to run as " "Administrator.") return cmd = args[0] if locate_binary(cmd): return winutils.sudo(cmd, args[1:]) elif cmd.lower() in windows_cmd_aliases: args = ["/D", "/C", "CD", _get_cwd(), "&&"] + args return winutils.sudo("cmd", args) else: msg = 'Cannot find the path for executable "{0}".' print(msg.format(cmd))
def sudo(args): if len(args) < 1: print("You need to provide an executable to run as " "Administrator.") return cmd = args[0] if locate_binary(cmd): return winutils.sudo(cmd, args[1:]) elif cmd.lower() in windows_cmd_aliases: args = ["/D", "/C", "CD", _get_cwd(), "&&"] + args return winutils.sudo("cmd", args) else: msg = 'Cannot find the path for executable "{0}".' print(msg.format(cmd))
def sudo(args, sdin=None): if len(args) < 1: print('You need to provide an executable to run as ' 'Administrator.') return cmd = args[0] if locate_binary(cmd): return winutils.sudo(cmd, args[1:]) elif cmd.lower() in windows_cmd_aliases: args = ['/D', '/C', 'CD', _get_cwd(), '&&'] + args return winutils.sudo('cmd', args) else: msg = 'Cannot find the path for executable "{0}".' print(msg.format(cmd))
def locate_binary(name): if os.path.isfile(name) and name != os.path.basename(name): return name directories = builtins.__xonsh_env__.get('PATH') # Windows users expect t obe able to execute files in the same directory without `./` if ON_WINDOWS: directories = [_get_cwd()] + directories try: return next(chain.from_iterable(yield_executables(directory, name) for directory in directories if os.path.isdir(directory))) except StopIteration: return None
def windows_foreign_env_fixes(ctx): """Environment fixes for Windows. Operates in-place.""" # remove these bash variables which only cause problems. for ev in ["HOME", "OLDPWD"]: if ev in ctx: del ctx[ev] # Override path-related bash variables; on Windows bash uses # /c/Windows/System32 syntax instead of C:\\Windows\\System32 # which messes up these environment variables for xonsh. for ev in ["PATH", "TEMP", "TMP"]: if ev in os.environ: ctx[ev] = os.environ[ev] elif ev in ctx: del ctx[ev] ctx["PWD"] = _get_cwd() or ""
def windows_foreign_env_fixes(ctx): """Environment fixes for Windows. Operates in-place.""" # remove these bash variables which only cause problems. for ev in ['HOME', 'OLDPWD']: if ev in ctx: del ctx[ev] # Override path-related bash variables; on Windows bash uses # /c/Windows/System32 syntax instead of C:\\Windows\\System32 # which messes up these environment variables for xonsh. for ev in ['PATH', 'TEMP', 'TMP']: if ev in os.environ: ctx[ev] = os.environ[ev] elif ev in ctx: del ctx[ev] ctx['PWD'] = _get_cwd() or ''
def wrapper(*args, **kwargs): cwd = kwargs.get('cwd', _get_cwd()) if cwd is None: return # step out completely if git is not installed if locate_binary('git') is None: return root_path = _get_parent_dir_for(cwd, '.git') # Bail if we're not in a repo if not root_path: return kwargs['cwd'] = cwd return func(*args, **kwargs)
def windows_env_fixes(ctx): """Environment fixes for Windows. Operates in-place.""" # Windows default prompt doesn't work. ctx['PROMPT'] = DEFAULT_PROMPT # remove these bash variables which only cause problems. for ev in ['HOME', 'OLDPWD']: if ev in ctx: del ctx[ev] # Override path-related bash variables; on Windows bash uses # /c/Windows/System32 syntax instead of C:\\Windows\\System32 # which messes up these environment variables for xonsh. for ev in ['PATH', 'TEMP', 'TMP']: if ev in os.environ: ctx[ev] = os.environ[ev] elif ev in ctx: del ctx[ev] ctx['PWD'] = _get_cwd()
def default_env(env=None): """Constructs a default xonsh environment.""" # in order of increasing precedence ctx = dict(BASE_ENV) ctx.update(os_environ) ctx['PWD'] = _get_cwd() or '' # These can cause problems for programs (#2543) ctx.pop('LINES', None) ctx.pop('COLUMNS', None) # other shells' PROMPT definitions generally don't work in XONSH: try: del ctx['PROMPT'] except KeyError: pass # finalize env if env is not None: ctx.update(env) return ctx
def lazy_locate_binary(self, name): """Locates an executable in the cache, without checking its validity.""" possibilities = self.get_possible_names(name) if ON_WINDOWS: # Windows users expect to be able to execute files in the same # directory without `./` cwd = _get_cwd() local_bin = next(( full_name for full_name in possibilities if os.path.isfile(full_name) ), None) if local_bin: return os.path.abspath(os.path.relpath(local_bin, cwd)) cached = next((cmd for cmd in possibilities if cmd in self._cmds_cache), None) if cached: return self._cmds_cache[cached][0] elif os.path.isfile(name) and name != os.path.basename(name): return name
def wrapper(*args, **kwargs): # getting the branch was slow on windows, disabling for now. if ON_WINDOWS: return '' # Get cwd or bail kwargs['cwd'] = kwargs.get('cwd', _get_cwd()) if kwargs['cwd'] is None: return # step out completely if git is not installed try: binary_location = subprocess.check_output(['which', 'git'], cwd=kwargs['cwd'], stderr=subprocess.PIPE, universal_newlines=True) if not binary_location: return except subprocess.CalledProcessError: return return func(*args, **kwargs)
def wrapper(*args, **kwargs): kwargs['cwd'] = kwargs.get('cwd', _get_cwd()) if kwargs['cwd'] is None: return # walk up the directory tree to see if we are inside an hg repo path = kwargs['cwd'].split(os.path.sep) while len(path) > 0: if os.path.exists(os.path.sep.join(path + ['.hg'])): break del path[-1] # bail if we aren't inside a repository if path == []: return kwargs['root'] = os.path.sep.join(path) # step out completely if hg is not installed if locate_binary('hg', kwargs['cwd']) is None: return return func(*args, **kwargs)
def wrapper(*args, **kwargs): kwargs["cwd"] = kwargs.get("cwd", _get_cwd()) if kwargs["cwd"] is None: return # walk up the directory tree to see if we are inside an hg repo path = kwargs["cwd"].split(os.path.sep) while len(path) > 0: if os.path.exists(os.path.sep.join(path + [".hg"])): break del path[-1] # bail if we aren't inside a repository if path == []: return kwargs["root"] = os.path.sep.join(path) # step out completely if hg is not installed if locate_binary("hg", kwargs["cwd"]) is None: return return func(*args, **kwargs)