Exemple #1
0
def GetResourceLoader():
    global _loader
    if _loader:
        return _loader

    # Ovm_Main in main.c sets this.
    if posix.environ.get('_OVM_IS_BUNDLE') == '1':
        ovm_path = posix.environ.get('_OVM_PATH')
        _loader = _ZipResourceLoader(ovm_path)

        # Now clear them so we don't pollute the environment.  In Python, this
        # calls unsetenv().
        del posix.environ['_OVM_IS_BUNDLE']
        del posix.environ['_OVM_PATH']

    elif posix.environ.get('_OVM_RESOURCE_ROOT'):  # Unit tests set this
        root_dir = posix.environ.get('_OVM_RESOURCE_ROOT')
        _loader = _FileResourceLoader(root_dir)

    else:
        # NOTE: This assumes all unit tests are one directory deep, e.g.
        # core/util_test.py.
        bin_dir = os_path.dirname(os_path.abspath(
            sys.argv[0]))  # ~/git/oil/bin
        root_dir = os_path.join(bin_dir, '..')  # ~/git/oil/osh
        _loader = _FileResourceLoader(root_dir)

    return _loader
Exemple #2
0
    def Run(self, cmd_val):
        # type: (cmd_value__Argv) -> int
        num_args = len(cmd_val.argv) - 1
        if num_args == 0:
            # TODO: It's suppose to try another dir before doing this?
            self.errfmt.Print('pushd: no other directory')
            return 1
        elif num_args > 1:
            raise args.UsageError('got too many arguments')

        # TODO: 'cd' uses normpath?  Is that inconsistent?
        dest_dir = os_path.abspath(cmd_val.argv[1])
        try:
            posix.chdir(dest_dir)
        except OSError as e:
            self.errfmt.Print("pushd: %r: %s",
                              dest_dir,
                              posix.strerror(e.errno),
                              span_id=cmd_val.arg_spids[1])
            return 1

        self.dir_stack.Push(dest_dir)
        _PrintDirStack(self.dir_stack, SINGLE_LINE, self.mem.GetVar('HOME'))
        state.ExportGlobalString(self.mem, 'PWD', dest_dir)
        self.mem.SetPwd(dest_dir)
        return 0
Exemple #3
0
def GetResourceLoader():
    # type: () -> _ResourceLoader
    global _loader
    if _loader:
        return _loader

    # Ovm_Main in main.c sets this.
    if posix.environ.get('_OVM_IS_BUNDLE') == '1':
        ovm_path = posix.environ.get('_OVM_PATH')
        _loader = _ZipResourceLoader(ovm_path)

        # Now clear them so we don't pollute the environment.  In Python, this
        # calls unsetenv().
        del posix.environ['_OVM_IS_BUNDLE']
        del posix.environ['_OVM_PATH']

    elif posix.environ.get('_OVM_RESOURCE_ROOT'):  # Unit tests set this
        root_dir = posix.environ.get('_OVM_RESOURCE_ROOT')
        _loader = _FileResourceLoader(root_dir)

    else:
        # Find resources relative to the binary, e.g.
        # ~/git/oilshell/oil/bin/oil.py.  But it also assumes that all unit tests
        # that use resources are are one directory deep, e.g. core/util_test.py.
        bin_dir = os_path.dirname(os_path.abspath(sys.argv[0]))
        root_dir = os_path.join(bin_dir, '..')  # ~/git/oilshell/oil
        _loader = _FileResourceLoader(root_dir)

    return _loader
Exemple #4
0
    def Run(self, cmd_val):
        # type: (cmd_value__Argv) -> int
        num_args = len(cmd_val.argv) - 1
        if num_args == 0:
            # TODO: It's suppose to try another dir before doing this?
            self.errfmt.Print_('pushd: no other directory')
            return 1
        elif num_args > 1:
            e_usage('got too many arguments')

        # TODO: 'cd' uses normpath?  Is that inconsistent?
        dest_dir = os_path.abspath(cmd_val.argv[1])
        err_num = pyos.Chdir(dest_dir)
        if err_num != 0:
            self.errfmt.Print_("pushd: %r: %s" %
                               (dest_dir, posix.strerror(err_num)),
                               span_id=cmd_val.arg_spids[1])
            return 1

        self.dir_stack.Push(dest_dir)
        _PrintDirStack(self.dir_stack, SINGLE_LINE,
                       state.MaybeString(self.mem, 'HOME'))
        state.ExportGlobalString(self.mem, 'PWD', dest_dir)
        self.mem.SetPwd(dest_dir)
        return 0
Exemple #5
0
def Pushd(argv, home_dir, dir_stack):
    num_args = len(argv)
    if num_args <= 0:
        util.error('pushd: no other directory')
        return 1
    elif num_args > 1:
        util.error('pushd: too many arguments')
        return 1

    dest_dir = os_path.abspath(argv[0])
    try:
        posix.chdir(dest_dir)
    except OSError as e:
        util.error("pushd: %r: %s", dest_dir, posix.strerror(e.errno))
        return 1

    dir_stack.Push(dest_dir)
    _PrintDirStack(dir_stack, SINGLE_LINE, home_dir)
    return 0
Exemple #6
0
  def __call__(self, arg_vec):
    num_args = len(arg_vec.strs) - 1
    if num_args == 0:
      # TODO: It's suppose to try another dir before doing this?
      self.errfmt.Print('pushd: no other directory')
      return 1
    elif num_args > 1:
      raise args.UsageError('got too many arguments')

    dest_dir = os_path.abspath(arg_vec.strs[1])
    try:
      posix.chdir(dest_dir)
    except OSError as e:
      self.errfmt.Print("pushd: %r: %s", dest_dir, posix.strerror(e.errno),
                        span_id=arg_vec.spids[1])
      return 1

    self.dir_stack.Push(dest_dir)
    _PrintDirStack(self.dir_stack, SINGLE_LINE, self.mem.GetVar('HOME'))
    state.SetGlobalString(self.mem, 'PWD', dest_dir)
    return 0
Exemple #7
0
def GetResourceLoader():
    global _loader
    if _loader:
        return _loader

    # Ovm_Main in main.c sets this.
    if posix.environ.get('_OVM_IS_BUNDLE') == '1':
        ovm_path = posix.environ.get('_OVM_PATH')
        #log('! OVM_PATH = %s', ovm_path)
        _loader = _ZipResourceLoader(ovm_path)
    elif posix.environ.get('_OVM_RESOURCE_ROOT'):  # Unit tests set this
        root_dir = posix.environ.get('_OVM_RESOURCE_ROOT')
        _loader = _FileResourceLoader(root_dir)
    else:
        # NOTE: This assumes all unit tests are one directory deep, e.g.
        # core/util_test.py.
        bin_dir = os_path.dirname(os_path.abspath(
            sys.argv[0]))  # ~/git/oil/bin
        root_dir = os_path.join(bin_dir, '..')  # ~/git/oil/osh
        _loader = _FileResourceLoader(root_dir)

    return _loader