Example #1
0
def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False,
          include_hidden=False):
    """Return an iterator which yields the paths matching a pathname pattern.

    The pattern may contain simple shell-style wildcards a la
    fnmatch. However, unlike fnmatch, filenames starting with a
    dot are special cases that are not matched by '*' and '?'
    patterns.

    If recursive is true, the pattern '**' will match any files and
    zero or more directories and subdirectories.
    """
    sys.audit("glob.glob", pathname, recursive)
    sys.audit("glob.glob/2", pathname, recursive, root_dir, dir_fd)
    if root_dir is not None:
        root_dir = os.fspath(root_dir)
    else:
        root_dir = pathname[:0]
    it = _iglob(pathname, root_dir, dir_fd, recursive, False,
                include_hidden=include_hidden)
    if not pathname or recursive and _isrecursive(pathname[:2]):
        try:
            s = next(it)  # skip empty string
            if s:
                it = itertools.chain((s,), it)
        except StopIteration:
            pass
    return it
Example #2
0
 def connect(self, host='', port=0, timeout=-999, source_address=None):
     '''Connect to host.  Arguments are:
      - host: hostname to connect to (string, default previous host)
      - port: port to connect to (integer, default previous port)
      - timeout: the timeout to set against the ftp socket(s)
      - source_address: a 2-tuple (host, port) for the socket to bind
        to as its source address before connecting.
     '''
     if host != '':
         self.host = host
     if port > 0:
         self.port = port
     if timeout != -999:
         self.timeout = timeout
     if source_address is not None:
         self.source_address = source_address
     sys.audit("ftplib.connect", self, self.host, self.port)
     self.sock = socket.create_connection(
         (self.host, self.port),
         self.timeout,
         source_address=self.source_address)
     self.af = self.sock.family
     self.file = self.sock.makefile('r', encoding=self.encoding)
     self.welcome = self.getresp()
     return self.welcome
Example #3
0
def add_manager(manager, thread=-1):
    sys.audit("secman.add_manager", manager, thread)
    if not isinstance(manager, SecurityManagerBase):
        raise TypeError("Manager must be a subclass of SecurityManagerBase")
    managers = add_manager.managers
    if managers.get(thread) is not None:
        raise errors.ManagerException("Manager already registered for thread")
    managers[thread] = manager
Example #4
0
 def putline(self, line):
     if '\r' in line or '\n' in line:
         raise ValueError('an illegal newline character should not be contained')
     sys.audit("ftplib.sendcmd", self, line)
     line = line + CRLF
     if self.debugging > 1:
         print('*put*', self.sanitize(line))
     self.sock.sendall(line.encode(self.encoding))
Example #5
0
def remove_manager(manager, thread=-1):
    sys.audit("secman.remove_manager", manager, thread)
    managers = remove_manager.managers
    if managers.get(thread) is None:
        raise errors.ManagerException("No manager registered for thread")
    if managers.get(thread) is not manager:
        raise errors.ManagerException(
            "Manager on thread does not match passed manager")
    del managers[thread]
Example #6
0
 def __init__(self, host, port=POP3_PORT,
              timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
     self.host = host
     self.port = port
     self._tls_established = False
     sys.audit("poplib.connect", self, host, port)
     self.sock = self._create_socket(timeout)
     self.file = self.sock.makefile('rb')
     self._debugging = 0
     self.welcome = self._getresp()
Example #7
0
 def write(self, buffer):
     """Write a string to the socket, doubling any IAC characters.
     Can block if the connection is blocked.  May raise
     OSError if the connection is closed.
     """
     if IAC in buffer:
         buffer = buffer.replace(IAC, IAC + IAC)
     sys.audit("telnetlib.Telnet.write", self, buffer)
     self.msg("send %r", buffer)
     self.sock.sendall(buffer)
Example #8
0
 def rglob(self, pattern):
     """Recursively yield all existing files (of any kind, including
     directories) matching the given relative pattern, anywhere in
     this subtree.
     """
     sys.audit("pathlib.Path.rglob", self, pattern)
     drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
     if drv or root:
         raise NotImplementedError("Non-relative patterns are unsupported")
     selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour)
     for p in selector.select_from(self):
         yield p
Example #9
0
def copytree(src,
             dst,
             symlinks=False,
             ignore=None,
             copy_function=copy2,
             ignore_dangling_symlinks=False,
             dirs_exist_ok=False):
    """Recursively copy a directory tree and return the destination directory.

    dirs_exist_ok dictates whether to raise an exception in case dst or any
    missing parent directory already exists.

    If exception(s) occur, an Error is raised with a list of reasons.

    If the optional symlinks flag is true, symbolic links in the
    source tree result in symbolic links in the destination tree; if
    it is false, the contents of the files pointed to by symbolic
    links are copied. If the file pointed by the symlink doesn't
    exist, an exception will be added in the list of errors raised in
    an Error exception at the end of the copy process.

    You can set the optional ignore_dangling_symlinks flag to true if you
    want to silence this exception. Notice that this has no effect on
    platforms that don't support os.symlink.

    The optional ignore argument is a callable. If given, it
    is called with the `src` parameter, which is the directory
    being visited by copytree(), and `names` which is the list of
    `src` contents, as returned by os.listdir():

        callable(src, names) -> ignored_names

    Since copytree() is called recursively, the callable will be
    called once for each directory that is copied. It returns a
    list of names relative to the `src` directory that should
    not be copied.

    The optional copy_function argument is a callable that will be used
    to copy each file. It will be called with the source path and the
    destination path as arguments. By default, copy2() is used, but any
    function that supports the same signature (like copy()) can be used.

    """
    sys.audit("shutil.copytree", src, dst)
    with os.scandir(src) as entries:
        return _copytree(entries=entries,
                         src=src,
                         dst=dst,
                         symlinks=symlinks,
                         ignore=ignore,
                         copy_function=copy_function,
                         ignore_dangling_symlinks=ignore_dangling_symlinks,
                         dirs_exist_ok=dirs_exist_ok)
Example #10
0
    def faults(self,
               fault_id: Optional[FaultID] = None
               ):  # noqa: C901  # ignore "is too complex" message
        method = cherrypy.request.method

        sys.audit("charybdisfs.api", method, fault_id, cherrypy.request)

        if method == "GET":
            if fault_id is None:
                return {"faults_ids": Configuration.get_all_faults_ids()}
            if fault := Configuration.get_fault_by_uuid(fault_id=fault_id):
                return {"fault_id": fault_id, "fault": fault.to_dict()}
            raise cherrypy.NotFound()