Пример #1
0
    def create(self, datacr, objname, mode):
        """ Create a children file-node under node, open it
            @return open node_descriptor of the created node
        """
        objname = _to_unicode(objname)
        cr, node, rem = datacr
        try:
            child = node.child(cr, objname)
            if child:
                if child.type not in ("file", "content"):
                    raise OSError(1, "Operation not permited.")

                ret = child.open_data(cr, mode)
                cr.commit()
                assert ret, "Cannot create descriptor for %r: %r" % (child, ret)
                return ret
        except EnvironmentError:
            raise
        except Exception:
            self._log.exception("Cannot locate item %s at node %s", objname, repr(node))
            pass

        try:
            child = node.create_child(cr, objname, data=None)
            ret = child.open_data(cr, mode)
            assert ret, "cannot create descriptor for %r" % child
            cr.commit()
            return ret
        except EnvironmentError:
            raise
        except Exception:
            self._log.exception("Cannot create item %s at node %s", objname, repr(node))
            raise OSError(1, "Operation not permited.")
Пример #2
0
    def create(self, datacr, objname, mode):
        """ Create a children file-node under node, open it
            @return open node_descriptor of the created node
        """
        objname = _to_unicode(objname)
        cr , node, rem = datacr
        try:
            child = node.child(cr, objname)
            if child:
                if child.type not in ('file','content'):
                    raise OSError(1, 'Operation is not permitted.')

                ret = child.open_data(cr, mode)
                cr.commit()
                assert ret, "Cannot create descriptor for %r: %r." % (child, ret)
                return ret
        except EnvironmentError:
            raise
        except Exception:
            self._log.exception('Cannot locate item %s at node %s.', objname, repr(node))
            pass

        try:
            child = node.create_child(cr, objname, data=None)
            ret = child.open_data(cr, mode)
            assert ret, "Cannot create descriptor for %r." % child
            cr.commit()
            return ret
        except EnvironmentError:
            raise
        except Exception:
            self._log.exception('Cannot create item %s at node %s.', objname, repr(node))
            raise OSError(1, 'Operation is not permitted.')
Пример #3
0
    def mkdir(self, datacr, basename):
        """Create the specified directory."""
        cr, node, rem = datacr or (None, None, None)
        if not node:
            raise OSError(1, "Operation not permited.")

        try:
            basename = _to_unicode(basename)
            cdir = node.create_child_collection(cr, basename)
            self._log.debug("Created child dir: %r", cdir)
            cr.commit()
        except Exception:
            self._log.exception('Cannot create dir "%s" at node %s', basename, repr(node))
            raise OSError(1, "Operation not permited.")
Пример #4
0
    def mkdir(self, datacr, basename):
        """Create the specified directory."""
        cr, node, rem = datacr or (None, None, None)
        if not node:
            raise OSError(1, 'Operation is not permitted.')

        try:
            basename =_to_unicode(basename)
            cdir = node.create_child_collection(cr, basename)
            self._log.debug("Created child dir: %r", cdir)
            cr.commit()
        except Exception:
            self._log.exception('Cannot create dir "%s" at node %s.', basename, repr(node))
            raise OSError(1, 'Operation is not permitted.')
Пример #5
0
 def rename(self, src, datacr):
     """ Renaming operation, the effect depends on the src:
         * A file: read, create and remove
         * A directory: change the parent and reassign children to ressource
     """
     cr = datacr[0]
     try:
         nname = _to_unicode(datacr[2])
         ret = src.move_to(cr, datacr[1], new_name=nname)
         # API shouldn't wait for us to write the object
         assert (ret is True) or (ret is False)
         cr.commit()
     except EnvironmentError:
         raise
     except Exception:
         self._log.exception('Cannot rename "%s" to "%s" at "%s"', src, datacr[2], datacr[1])
         raise OSError(1, "Operation not permited.")
Пример #6
0
 def rename(self, src, datacr):
     """ Renaming operation, the effect depends on the src:
         * A file: read, create and remove
         * A directory: change the parent and reassign children to ressource
     """
     cr = datacr[0]
     try:
         nname = _to_unicode(datacr[2])
         ret = src.move_to(cr, datacr[1], new_name=nname)
         # API shouldn't wait for us to write the object
         assert (ret is True) or (ret is False)
         cr.commit()
     except EnvironmentError:
         raise
     except Exception:
         self._log.exception('Cannot rename "%s" to "%s" at "%s".', src, datacr[2], datacr[1])
         raise OSError(1,'Operation is not permitted.')
Пример #7
0
    def get_crdata(self, line, mode='file'):
        """ Get database cursor, node and remainder data, for commands

        This is the helper function that will prepare the arguments for
        any of the subsequent commands.
        It returns a tuple in the form of:
        @code        ( cr, node, rem_path=None )

        @param line An absolute or relative ftp path, as passed to the cmd.
        @param mode A word describing the mode of operation, so that this
                    function behaves properly in the different commands.
        """
        path = self.ftpnorm(line)
        if self.cwd_node is None:
            if not os.path.isabs(path):
                path = os.path.join(self.root, path)

        if path == '/' and mode in ('list', 'cwd'):
            return (None, None, None)

        path = _to_unicode(os.path.normpath(path))  # again, for '/db/../ss'
        if path == '.': path = ''

        if os.path.isabs(path) and self.cwd_node is not None \
                and path.startswith(self.cwd):
            # make relative, so that cwd_node is used again
            path = path[len(self.cwd):]
            if path.startswith('/'):
                path = path[1:]

        p_parts = path.split('/')  # hard-code the unix sep here, by spec.

        assert '..' not in p_parts

        rem_path = None
        if mode in ('create', ):
            rem_path = p_parts[-1]
            p_parts = p_parts[:-1]

        if os.path.isabs(path):
            # we have to start from root, again
            while p_parts and p_parts[0] == '':
                p_parts = p_parts[1:]
            # self._log.debug("Path parts: %r ", p_parts)
            if not p_parts:
                raise IOError(errno.EPERM,
                              'Cannot perform operation at root dir')
            dbname = p_parts[0]
            if dbname not in self.db_list():
                raise IOError(errno.ENOENT,
                              'Invalid database path: %s' % dbname)
            try:
                db = pooler.get_db(dbname)
            except Exception:
                raise OSError(1, 'Database cannot be used.')
            cr = db.cursor()
            try:
                uid = security.login(dbname, self.username, self.password)
            except Exception:
                cr.close()
                raise
            if not uid:
                cr.close()
                raise OSError(2, 'Authentification Required.')
            n = get_node_context(cr, uid, {})
            node = n.get_uri(cr, p_parts[1:])
            return (cr, node, rem_path)
        else:
            # we never reach here if cwd_node is not set
            if p_parts and p_parts[-1] == '':
                p_parts = p_parts[:-1]
            cr, uid = self.get_node_cr_uid(self.cwd_node)
            if p_parts:
                node = self.cwd_node.get_uri(cr, p_parts)
            else:
                node = self.cwd_node
            if node is False and mode not in ('???'):
                cr.close()
                raise IOError(errno.ENOENT, 'Path does not exist')
            return (cr, node, rem_path)
Пример #8
0
    def get_crdata(self, line, mode="file"):
        """ Get database cursor, node and remainder data, for commands

        This is the helper function that will prepare the arguments for
        any of the subsequent commands.
        It returns a tuple in the form of:
        @code        ( cr, node, rem_path=None )

        @param line An absolute or relative ftp path, as passed to the cmd.
        @param mode A word describing the mode of operation, so that this
                    function behaves properly in the different commands.
        """
        path = self.ftpnorm(line)
        if self.cwd_node is None:
            if not os.path.isabs(path):
                path = os.path.join(self.root, path)

        if path == "/" and mode in ("list", "cwd"):
            return (None, None, None)

        path = _to_unicode(os.path.normpath(path))  # again, for '/db/../ss'
        if path == ".":
            path = ""

        if os.path.isabs(path) and self.cwd_node is not None and path.startswith(self.cwd):
            # make relative, so that cwd_node is used again
            path = path[len(self.cwd) :]
            if path.startswith("/"):
                path = path[1:]

        p_parts = path.split("/")  # hard-code the unix sep here, by spec.

        assert ".." not in p_parts

        rem_path = None
        if mode in ("create",):
            rem_path = p_parts[-1]
            p_parts = p_parts[:-1]

        if os.path.isabs(path):
            # we have to start from root, again
            while p_parts and p_parts[0] == "":
                p_parts = p_parts[1:]
            # self._log.debug("Path parts: %r ", p_parts)
            if not p_parts:
                raise IOError(errno.EPERM, "Cannot perform operation at root dir")
            dbname = p_parts[0]
            if dbname not in self.db_list():
                raise IOError(errno.ENOENT, "Invalid database path: %s" % dbname)
            try:
                db = pooler.get_db(dbname)
            except Exception:
                raise OSError(1, "Database cannot be used.")
            cr = db.cursor()
            try:
                uid = security.login(dbname, self.username, self.password)
            except Exception:
                cr.close()
                raise
            if not uid:
                cr.close()
                raise OSError(2, "Authentification Required.")
            n = get_node_context(cr, uid, {})
            node = n.get_uri(cr, p_parts[1:])
            return (cr, node, rem_path)
        else:
            # we never reach here if cwd_node is not set
            if p_parts and p_parts[-1] == "":
                p_parts = p_parts[:-1]
            cr, uid = self.get_node_cr_uid(self.cwd_node)
            if p_parts:
                node = self.cwd_node.get_uri(cr, p_parts)
            else:
                node = self.cwd_node
            if node is False and mode not in ("???"):
                cr.close()
                raise IOError(errno.ENOENT, "Path does not exist")
            return (cr, node, rem_path)
Пример #9
0
    def get_crdata(self, line, mode='file'):
        """ Get database cursor, node and remainder data, for commands

        This is the helper function that will prepare the arguments for
        any of the subsequent commands.
        It returns a tuple in the form of:
        @code        ( cr, node, rem_path=None )

        @param line An absolute or relative ftp path, as passed to the cmd.
        @param mode A word describing the mode of operation, so that this
                    function behaves properly in the different commands.
        """
        path = self.ftpnorm(line)
        if self.cwd_node is None:
            if not os.path.isabs(path):
                path = os.path.join(self.root, path)

        if path == '/' and mode in ('list', 'cwd'):
            return (None, None, None )

        path = _to_unicode(os.path.normpath(path)) # again, for '/db/../ss'
        if path == '.': path = ''

        if os.path.isabs(path) and self.cwd_node is not None \
                and path.startswith(self.cwd):
            # make relative, so that cwd_node is used again
            path = path[len(self.cwd):]
            if path.startswith('/'):
                path = path[1:]

        p_parts = path.split(os.sep)

        assert '..' not in p_parts

        rem_path = None
        if mode in ('create',):
            rem_path = p_parts[-1]
            p_parts = p_parts[:-1]

        if os.path.isabs(path):
            # we have to start from root, again
            while p_parts and p_parts[0] == '':
                p_parts = p_parts[1:]
            # self._log.debug("Path parts: %r ", p_parts)
            if not p_parts:
                raise IOError(errno.EPERM, 'Cannot perform operation at root directory.')
            dbname = p_parts[0]
            if dbname not in self.db_list():
                raise IOError(errno.ENOENT,'Invalid database path: %s.' % dbname)
            try:
                db = openerp.registry(dbname).db
            except Exception:
                raise OSError(1, 'Database cannot be used.')
            cr = db.cursor()
            try:
                uid = security.login(dbname, self.username, self.password)
            except Exception:
                cr.close()
                raise
            if not uid:
                cr.close()
                raise OSError(2, 'Authentification required.')
            n = get_node_context(cr, uid, {})
            node = n.get_uri(cr, p_parts[1:])
            return (cr, node, rem_path)
        else:
            # we never reach here if cwd_node is not set
            if p_parts and p_parts[-1] == '':
                p_parts = p_parts[:-1]
            cr, uid = self.get_node_cr_uid(self.cwd_node)
            if p_parts:
                node = self.cwd_node.get_uri(cr, p_parts)
            else:
                node = self.cwd_node
            if node is False and mode not in ('???'):
                cr.close()
                raise IOError(errno.ENOENT, 'Path does not exist.')
            return (cr, node, rem_path)