Example #1
0
    def open(self, lock, ami_path, f_mode):
        try:
            # special names
            uname = ami_path.upper()
            # thor: NIL: and CONSOLE: also work as device names
            # and the file names behind are ignored.
            if uname.startswith("NIL:"):
                sys_name = "/dev/null"
                if f_mode == "rwb+":
                    f_mode = "rb+"
                fobj = open(sys_name, f_mode)
                fh = FileHandle(fobj, ami_path, sys_name, is_nil=True)
            elif uname == "*" or uname.startswith("CONSOLE:"):
                sys_name = ""
                fh = self._create_fh(sys.stdout, "wb", "*", "")
            else:
                # map to system path
                sys_path = self.path_mgr.ami_to_sys_path(lock,
                                                         ami_path,
                                                         searchMulti=True)
                if sys_path == None:
                    log_file.info("file not found: '%s' -> '%s'" %
                                  (ami_path, sys_path))
                    return None

                # make some checks on existing file
                if os.path.exists(sys_path):
                    # if not writeable -> no append mode
                    if f_mode == "rwb+":
                        f_mode = "rb+"
                    if not os.access(sys_path, os.W_OK):
                        if f_mode[-1] == "+":
                            f_mode = f_mode[:-1]
                else:
                    # if the file does not exist, but the mode is MODE_READWRITE, create it.
                    if f_mode == "rwb+":
                        f_mode = "wb+"

                log_file.debug("opening file: '%s' -> '%s' f_mode=%s" %
                               (ami_path, sys_path, f_mode))
                fobj = open(sys_path, f_mode)
                fh = FileHandle(fobj, ami_path, sys_path)

            self._register_file(fh)
            return fh
        except IOError as e:
            log_file.info("error opening: '%s' -> '%s' f_mode=%s -> %s" %
                          (ami_path, sys_path, f_mode, e))
            return None
Example #2
0
 def _create_stdout_fh(self):
     fobj = sys.stdout.buffer
     # try to get a fd from fobj
     try:
         fileno = fobj.fileno()
         # create unbuffered raw stream if its a tty
         if os.isatty(fileno):
             fobj = open(fileno, "wb", buffering=0)
             log_file.debug(
                 "open no buffering: fileno=%s -> %s, fileno=%s",
                 fileno,
                 fobj,
                 fobj.fileno(),
             )
     except Exception:
         pass
     return FileHandle(fobj, "<STDOUT>", "/dev/stdout", need_close=False)
Example #3
0
  def open(self, lock, ami_path, f_mode):
    try:
      # special names
      uname = ami_path.upper()
      # thor: NIL: and CONSOLE: also work as device names
      # and the file names behind are ignored.
      if uname.startswith('NIL:'):
        sys_name = "/dev/null"
        if f_mode == "rwb+":
          f_mode = "rb+"
        fobj = open(sys_name, f_mode)
        fh   = FileHandle(fobj, ami_path, sys_name, is_nil = True)
      elif uname == '*' or uname.startswith('CONSOLE:'):
        sys_name = ''
        fh = FileHandle(sys.stdout,'*','',need_close=False)
      else:
        # map to system path
        sys_path = self.path_mgr.ami_to_sys_path(lock,ami_path,searchMulti=True)
        if sys_path == None:
          log_file.info("file not found: '%s' -> '%s'" % (ami_path, sys_path))
          return None

        # make some checks on existing file
        if os.path.exists(sys_path):
          # if not writeable -> no append mode
          if f_mode == "rwb+":
            f_mode = "rb+"
          if not os.access(sys_path, os.W_OK):
            if f_mode[-1] == '+':
              f_mode = f_mode[:-1]
        else:
          # if the file does not exist, but the mode is MODE_READWRITE, create it.
          if f_mode == "rwb+":
            f_mode = "wb+"

        log_file.debug("opening file: '%s' -> '%s' f_mode=%s" % (ami_path, sys_path, f_mode))
        fobj = open(sys_path, f_mode)
        fh = FileHandle(fobj, ami_path, sys_path)

      self._register_file(fh)
      return fh
    except IOError as e:
      log_file.info("error opening: '%s' -> '%s' f_mode=%s -> %s" % (ami_path, sys_path, f_mode, e))
      return None