Пример #1
0
		def info(dentry):
			mode = dentry.mode
			mode_chars = [dentry.type.lsl_char]
			mode_chars.append('r' if mode & S_IRUSR else '-')
			mode_chars.append('w' if mode & S_IWUSR else '-')
			mode_chars.append(('s' if mode & S_IXUSR else 'S') if mode & S_ISUID else ('x' if mode & S_IXUSR else '-'))
			mode_chars.append('r' if mode & S_IRGRP else '-')
			mode_chars.append('w' if mode & S_IWGRP else '-')
			mode_chars.append(('s' if mode & S_IXGRP else 'S') if mode & S_ISGID else ('x' if mode & S_IXGRP else '-'))
			mode_chars.append('r' if mode & S_IROTH else '-')
			mode_chars.append('w' if mode & S_IWOTH else '-')
			mode_chars.append(('t' if mode & S_IXOTH else 'T') if mode & S_ISVTX else ('x' if mode & S_IXOTH else '-'))

			uid = dentry.uid
			user = passwd.get(uid, uid)
			gid = dentry.gid
			group = groups.get(gid, gid)

			size = dentry.size
			nonlocal total_blocks
			total_blocks += (size + 4095) // 4096

			description = [basename(fsdecode(dentry.name))]
			if dentry.is_hardlink:
				description.append("=>")
				description.append(basename(fsdecode(bytes(dentry.hardlink))))
			elif dentry.is_symlink:
				description.append("->")
				description.append(fsdecode(bytes(dentry.symlink)))
			if dentry.is_device:
				size = "%3d, %3d" % (dentry.rdev_major, dentry.rdev_minor)

			return ''.join(mode_chars), user, group, size, format_time(dentry.mtime), ' '.join(description)
Пример #2
0
def exec_command_all(*cmdargs, **kwargs):
    """
    Wrap creating subprocesses

    Return tuple (exit_code, stdout, stderr) of the invoked command.

    On Python 3, the 'encoding' kwarg controls how stdout and stderr are decoded to 'str'
    """
    proc = subprocess.Popen(cmdargs, bufsize=-1,  # Default OS buffer size.
            stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
    # Waits for subprocess to complete.
    out, err = proc.communicate()
    # Python 3 returns stdout/stderr as a byte array NOT as string.
    # Thus we need to convert that to proper encoding.
    if is_py3:
        encoding = kwargs.get('encoding')
        if encoding:
            out = out.decode(encoding)
            err = err.decode(encoding)
        else:
            # If no encoding is given, assume we're reading filenames from stdout
            # only because it's the common case.
            out = os.fsdecode(out)
            err = os.fsdecode(err)


    return proc.returncode, out, err
Пример #3
0
    def run_one_command(self, userargs, stdin=None):
        self.reset_timer()
        try:
            obj = wrapper.start_subprocess(
                self.filters, userargs,
                exec_dirs=self.config.exec_dirs,
                log=self.config.use_syslog,
                close_fds=True,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        except wrapper.FilterMatchNotExecutable:
            LOG.warning("Executable not found for: %s",
                        ' '.join(userargs))
            return cmd.RC_NOEXECFOUND, "", ""

        except wrapper.NoFilterMatched:
            LOG.warning("Unauthorized command: %s (no filter matched)",
                        ' '.join(userargs))
            return cmd.RC_UNAUTHORIZED, "", ""

        if six.PY3 and stdin is not None:
            stdin = os.fsencode(stdin)
        out, err = obj.communicate(stdin)
        if six.PY3:
            out = os.fsdecode(out)
            err = os.fsdecode(err)
        return obj.returncode, out, err
    def check_undecodable_bytes_error(self, binary):
        out_bytes = b'out: password="******" ' + UNDECODABLE_BYTES
        err_bytes = b'err: password="******" ' + UNDECODABLE_BYTES
        conn = FakeSshConnection(1, out=out_bytes, err=err_bytes)

        out_bytes = b'out: password="******" ' + UNDECODABLE_BYTES
        err_bytes = b'err: password="******" ' + UNDECODABLE_BYTES

        exc = self.assertRaises(processutils.ProcessExecutionError,
                                processutils.ssh_execute,
                                conn, 'ls',
                                binary=binary, check_exit_code=True)

        out = exc.stdout
        err = exc.stderr
        if six.PY3:
            # On Python 3, stdout and stderr attributes of
            # ProcessExecutionError must always be Unicode
            self.assertEqual(os.fsdecode(out_bytes), out)
            self.assertEqual(os.fsdecode(err_bytes), err)
        else:
            # On Python 2, stdout and stderr attributes of
            # ProcessExecutionError must always be bytes
            self.assertEqual(out_bytes, out)
            self.assertEqual(err_bytes, err)
Пример #5
0
def exec_command_all(*cmdargs, **kwargs):
    """
    Run the command specified by the passed positional arguments, optionally
    configured by the passed keyword arguments.

    .. DANGER::
       **Ignore this function's return value.** If this command's standard
       output consists solely of pathnames, consider calling `exec_command()`;
       else, consider calling `exec_command_stdout()`.

    Parameters
    ----------
    cmdargs : list
        Variadic list whose:
        1. Mandatory first element is the absolute path, relative path,
           or basename in the current `${PATH}` of the command to run.
        1. Optional remaining elements are arguments to pass to this command.
    encoding : str, optional
        Optional keyword argument specifying the encoding with which to decode
        this command's standard output under Python 3. As this function's return
        value should be ignored, this argument should _never_ be passed.

    All remaining keyword arguments are passed as is to the `subprocess.Popen()`
    constructor.

    Returns
    ----------
    (int, str, str)
        Ignore this 3-element tuple `(exit_code, stdout, stderr)`. See the
        `exec_command()` function for discussion.
    """
    encoding = kwargs.pop('encoding', None)
    proc = subprocess.Popen(cmdargs, bufsize=-1,  # Default OS buffer size.
            stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
    # Waits for subprocess to complete.
    out, err = proc.communicate()
    # Python 3 returns stdout/stderr as a byte array NOT as string.
    # Thus we need to convert that to proper encoding.
    if is_py3:
        try:
            if encoding:
                out = out.decode(encoding)
                err = err.decode(encoding)
            else:
                # If no encoding is given, assume we're reading filenames from
                # stdout only because it's the common case.
                out = os.fsdecode(out)
                err = os.fsdecode(err)
        except UnicodeDecodeError as e:
            # The sub-process used a different encoding,
            # provide more information to ease debugging.
            print('--' * 20, file=sys.stderr)
            print(str(e), file=sys.stderr)
            print('These are the bytes around the offending byte:',
                  file=sys.stderr)
            print('--' * 20, file=sys.stderr)
            raise

    return proc.returncode, out, err
Пример #6
0
    def _set_properties(self, path):
        statdata = os.stat(path)

        self.title = os.fsdecode(os.path.splitext(os.path.basename(path))[0])
        self.url = path2url(os.fsdecode(path))
        self.created = datetime.datetime.fromtimestamp(statdata.st_ctime)
        self.modified = datetime.datetime.fromtimestamp(statdata.st_mtime)
        self.size = statdata.st_size
Пример #7
0
def ssh_execute(ssh, cmd, process_input=None, addl_env=None, check_exit_code=True, binary=False):
    """Run a command through SSH.

    .. versionchanged:: 1.9
       Added *binary* optional parameter.
    """
    sanitized_cmd = strutils.mask_password(cmd)
    LOG.debug("Running cmd (SSH): %s", sanitized_cmd)
    if addl_env:
        raise InvalidArgumentError(_("Environment not supported over SSH"))

    if process_input:
        # This is (probably) fixable if we need it...
        raise InvalidArgumentError(_("process_input not supported over SSH"))

    stdin_stream, stdout_stream, stderr_stream = ssh.exec_command(cmd)
    channel = stdout_stream.channel

    # NOTE(justinsb): This seems suspicious...
    # ...other SSH clients have buffering issues with this approach
    stdout = stdout_stream.read()
    stderr = stderr_stream.read()

    stdin_stream.close()

    exit_status = channel.recv_exit_status()

    if six.PY3:
        # Decode from the locale using using the surrogateescape error handler
        # (decoding cannot fail). Decode even if binary is True because
        # mask_password() requires Unicode on Python 3
        stdout = os.fsdecode(stdout)
        stderr = os.fsdecode(stderr)
    stdout = strutils.mask_password(stdout)
    stderr = strutils.mask_password(stderr)

    # exit_status == -1 if no exit code was returned
    if exit_status != -1:
        LOG.debug("Result was %s" % exit_status)
        if check_exit_code and exit_status != 0:
            raise ProcessExecutionError(exit_code=exit_status, stdout=stdout, stderr=stderr, cmd=sanitized_cmd)

    if binary:
        if six.PY2:
            # On Python 2, stdout is a bytes string if mask_password() failed
            # to decode it, or an Unicode string otherwise. Encode to the
            # default encoding (ASCII) because mask_password() decodes from
            # the same encoding.
            if isinstance(stdout, unicode):
                stdout = stdout.encode()
            if isinstance(stderr, unicode):
                stderr = stderr.encode()
        else:
            # fsencode() is the reverse operation of fsdecode()
            stdout = os.fsencode(stdout)
            stderr = os.fsencode(stderr)

    return (stdout, stderr)
Пример #8
0
def _group_from_gstruct(res):
    i = 0
    mem = []
    while res.contents.gr_mem[i]:
        mem.append(res.contents.gr_mem[i])
        i += 1
    return struct_group((os.fsdecode(res.contents.gr_name),
                         os.fsdecode(res.contents.gr_passwd),
                         res.contents.gr_gid,
                         mem))
Пример #9
0
def run_plain(cmd):
    obj = subprocess.Popen(cmd,
                           stdin=subprocess.PIPE,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    out, err = obj.communicate()
    if six.PY3:
        out = os.fsdecode(out)
        err = os.fsdecode(err)
    return obj.returncode, out, err
Пример #10
0
def run_sixer(operation, *args):
    args = (sys.executable, SIXER, '--write', operation) + args
    proc = subprocess.Popen(args,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    with proc:
        stdout, stderr = proc.communicate()
        exitcode = proc.wait()

    return (exitcode, os.fsdecode(stdout), os.fsdecode(stderr))
Пример #11
0
def _group_from_gstruct(res):
    i = 0
    members = []
    while res.gr_mem[i]:
        members.append(os.fsdecode(ffi.string(res.gr_mem[i])))
        i += 1
    return struct_group([
        os.fsdecode(ffi.string(res.gr_name)),
        os.fsdecode(ffi.string(res.gr_passwd)),
        res.gr_gid,
        members])
 def check_undecodable_bytes(self, binary):
     out_bytes = b'out: ' + UNDECODABLE_BYTES
     err_bytes = b'err: ' + UNDECODABLE_BYTES
     out, err = self.execute_undecodable_bytes(out_bytes, err_bytes,
                                               binary=binary)
     if six.PY3 and not binary:
         self.assertEqual(os.fsdecode(out_bytes), out)
         self.assertEqual(os.fsdecode(err_bytes), err)
     else:
         self.assertEqual(out, out_bytes)
         self.assertEqual(err, err_bytes)
Пример #13
0
 def symlink(self, inode_p, name, target, ctx):
     name = fsdecode(name)
     target = fsdecode(target)
     parent = self._inode_to_path(inode_p)
     path = os.path.join(parent, name)
     try:
         os.symlink(target, path)
     except OSError as exc:
         raise FUSEError(exc.errno)
     stat = os.lstat(path)
     self._add_path(stat.st_ino, path)
     return self.getattr(stat.st_ino)
    def check_undecodable_bytes(self, binary):
        out_bytes = b'out: ' + UNDECODABLE_BYTES
        err_bytes = b'err: ' + UNDECODABLE_BYTES
        conn = FakeSshConnection(0, out=out_bytes, err=err_bytes)

        out, err = processutils.ssh_execute(conn, 'ls', binary=binary)
        if six.PY3 and not binary:
            self.assertEqual(os.fsdecode(out_bytes), out)
            self.assertEqual(os.fsdecode(err_bytes), err)
        else:
            self.assertEqual(out_bytes, out)
            self.assertEqual(err_bytes, err)
Пример #15
0
def get_build_args():
    python_name = os.path.splitext(os.path.basename(sys.executable))[0]
    python_config = os.path.join(
        os.path.dirname(sys.executable), python_name + "-config"
    )

    cflags = subprocess.check_output(["sh", python_config, "--cflags"]).strip()
    libs = subprocess.check_output(["sh", python_config, "--libs"]).strip()

    cflags = os.fsdecode(cflags)
    libs = os.fsdecode(libs)
    return shlex.split(cflags) + shlex.split(libs)
Пример #16
0
    def periodical_task(self):
        folder = os.fsdecode(self.config.get("folder"))
        file = os.fsdecode(self.config.get("file"))

        try:
            if not os.path.isdir(os.path.join(folder, "finished")):
                os.makedirs(os.path.join(folder, "finished"), exist_ok=True)

            if self.config.get("watchfile"):
                with open(file, mode="a+") as file:
                    file.seek(0)
                    content = file.read().strip()

                if content:
                    file = open(file, mode="w")
                    file.close()

                    name = "{}_{}.txt".format(file, time.strftime("%H-%M-%S_%d%b%Y"))

                    with open(
                        os.path.join(folder, "finished", name), mode="wb"
                    ) as file:
                        file.write(content)

                    self.pyload.api.add_package(file.name, [file.name], 1)

            for entry in os.listdir(folder):
                path = os.path.join(folder, entry)

                if (
                    not os.path.isfile(path)
                    or entry.endswith("~")
                    or entry.startswith("#")
                    or entry.startswith(".")
                ):
                    continue

                newpath = os.path.join(
                    folder,
                    "finished",
                    "tmp_" + entry if self.config.get("delete") else entry,
                )
                shutil.move(path, newpath)

                self.log_info(self._("Added {} from HotFolder").format(entry))
                self.pyload.api.add_package(entry, [newpath], 1)

        except (IOError, OSError) as exc:
            self.log_error(
                exc, exc_info=self.pyload.debug > 1, stack_info=self.pyload.debug > 2
            )
Пример #17
0
def run_normal_checks(
    tracker: ProblemTracker,
    instance: EdenInstance,
    out: ui.Output,
    mount_table: mtab.MountTable,
    fs_util: filesystem.FsUtil,
) -> None:
    checkouts: Dict[Path, CheckoutInfo] = {}
    # Get information about the checkouts currently known to the running edenfs process
    with instance.get_thrift_client() as client:
        for mount in client.listMounts():
            # Old versions of edenfs did not return a mount state field.
            # These versions only listed running mounts, so treat the mount state
            # as running in this case.
            mount_state = mount.state if mount.state is not None else MountState.RUNNING
            path = Path(os.fsdecode(mount.mountPoint))
            checkout = CheckoutInfo(
                instance,
                path,
                running_state_dir=Path(os.fsdecode(mount.edenClientPath)),
                state=mount_state,
            )
            checkouts[path] = checkout

    # Get information about the checkouts listed in the config file
    for configured_checkout in instance.get_checkouts():
        checkout_info = checkouts.get(configured_checkout.path, None)
        if checkout_info is None:
            checkout_info = CheckoutInfo(instance, configured_checkout.path)
            checkout_info.configured_state_dir = configured_checkout.state_dir
            checkouts[checkout_info.path] = checkout_info

        checkout_info.configured_state_dir = configured_checkout.state_dir

    check_filesystems.check_eden_directory(tracker, instance)
    check_stale_mounts.check_for_stale_mounts(tracker, mount_table)
    check_edenfs_version(tracker, instance)
    check_filesystems.check_disk_usage(
        tracker, list(instance.get_mount_paths()), instance
    )

    watchman_info = check_watchman.pre_check()

    for path, checkout in sorted(checkouts.items()):
        out.writeln(f"Checking {path}")
        try:
            check_mount(tracker, checkout, mount_table, fs_util, watchman_info)
        except Exception as ex:
            tracker.add_problem(
                Problem(f"unexpected error while checking {path}: {ex}")
            )
Пример #18
0
    def test_nonascii_abspath(self):
        name = b'\xe7w\xf0'
        if sys.platform == 'win32':
            try:
                os.fsdecode(name)
            except UnicodeDecodeError:
                self.skipTest("the filename %a is not decodable "
                              "from the ANSI code page %s"
                              % (name, sys.getfilesystemencoding()))

        # Test non-ASCII, non-UTF8 bytes in the path.
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", DeprecationWarning)
            with support.temp_cwd(name):
                self.test_abspath()
Пример #19
0
        def _findLib_crle(name, is64):
            if not os.path.exists('/usr/bin/crle'):
                return None

            env = dict(os.environ)
            env['LC_ALL'] = 'C'

            if is64:
                args = ('/usr/bin/crle', '-64')
            else:
                args = ('/usr/bin/crle',)

            paths = None
            try:
                proc = subprocess.Popen(args,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.DEVNULL,
                                        env=env)
            except OSError:  # E.g. bad executable
                return None
            with proc:
                for line in proc.stdout:
                    line = line.strip()
                    if line.startswith(b'Default Library Path (ELF):'):
                        paths = os.fsdecode(line).split()[4]

            if not paths:
                return None

            for dir in paths.split(":"):
                libfile = os.path.join(dir, "lib%s.so" % name)
                if os.path.exists(libfile):
                    return libfile

            return None
Пример #20
0
def exec_cmd(*args, archiver=None, fork=False, exe=None, **kw):
    if fork:
        try:
            if exe is None:
                borg = (sys.executable, '-m', 'borg.archiver')
            elif isinstance(exe, str):
                borg = (exe, )
            elif not isinstance(exe, tuple):
                raise ValueError('exe must be None, a tuple or a str')
            output = subprocess.check_output(borg + args, stderr=subprocess.STDOUT)
            ret = 0
        except subprocess.CalledProcessError as e:
            output = e.output
            ret = e.returncode
        return ret, os.fsdecode(output)
    else:
        stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
        try:
            sys.stdin = StringIO()
            sys.stdout = sys.stderr = output = StringIO()
            if archiver is None:
                archiver = Archiver()
            args = archiver.parse_args(list(args))
            ret = archiver.run(args)
            return ret, output.getvalue()
        finally:
            sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
Пример #21
0
 def __init__(self,
              path,
              url_prefix='',
              result_html_prefix='',
              result_html_suffix='<br>'):
     """
     Parameters
     ----------
     path : str
         path to the file or directory that should be formatted
     url_prefix : str
         prefix to be prepended to all files to form a working link [default:
         '']
     result_html_prefix : str
         text to append to beginning to link [default: '']
     result_html_suffix : str
         text to append at the end of link [default: '<br>']
     """
     if isdir(path):
         raise ValueError("Cannot display a directory using FileLink. "
           "Use FileLinks to display '%s'." % path)
     self.path = fsdecode(path)
     self.url_prefix = url_prefix
     self.result_html_prefix = result_html_prefix
     self.result_html_suffix = result_html_suffix
Пример #22
0
 def listxattr(path, *, follow_symlinks=True):
     ns = EXTATTR_NAMESPACE_USER
     if isinstance(path, str):
         path = os.fsencode(path)
     if isinstance(path, int):
         func = libc.extattr_list_fd
     elif follow_symlinks:
         func = libc.extattr_list_file
     else:
         func = libc.extattr_list_link
     n = _check(func(path, ns, None, 0), path)
     if n == 0:
         return []
     namebuf = create_string_buffer(n)
     n2 = _check(func(path, ns, namebuf, n), path)
     if n2 != n:
         raise Exception('listxattr failed')
     names = []
     mv = memoryview(namebuf.raw)
     while mv:
         length = mv[0]
         # Python < 3.3 returns bytes instead of int
         if isinstance(length, bytes):
             length = ord(length)
         names.append(os.fsdecode(bytes(mv[1:1+length])))
         mv = mv[1+length:]
     return names
Пример #23
0
def compute_checksum(filename, hashtype):
    file = os.fsdecode(filename)

    if not exists(file):
        return None

    buf = fsbsize(filename)

    if hashtype in ("adler32", "crc32"):
        hf = getattr(zlib, hashtype)
        last = 0

        with open(file, mode="rb") as file:
            for chunk in iter(lambda: file.read(buf), ""):
                last = hf(chunk, last)

        return "{:x}".format(last)

    elif hashtype in hashlib.algorithms_available:
        h = hashlib.new(hashtype)

        with open(file, mode="rb") as file:
            for chunk in iter(lambda: file.read(buf * h.block_size), ""):
                h.update(chunk)

        return h.hexdigest()

    else:
        return None
Пример #24
0
    def decrypt(self, pyfile):
        fs_filename = os.fsdecode(pyfile.url)

        with open(fs_filename, mode="rb") as file:
            dlc_content = requests.post(
                "http://service.jdownloader.net/dlcrypt/getDLC.php",
                data={"src": "ccf", "filename": "test.ccf"},
                files={"upload": file},
            ).read()

        dl_folder = self.pyload.config.get("general", "storage_folder")
        dlc_file = os.path.join(dl_folder, "tmp_{}.dlc".format(pyfile.name))

        try:
            dlc = base64.b64decode(
                re.search(r"<dlc>(.+)</dlc>", dlc_content, re.S).group(1)
            )

        except AttributeError:
            self.fail(self._("Container is corrupted"))

        with open(dlc_file, mode="w") as tempdlc:
            tempdlc.write(dlc)

        self.links = [dlc_file]
Пример #25
0
 def __new__(cls, filename):
     self = object.__new__(cls)
     self._font = {}
     self._filename = os.fsdecode(filename)
     with open(filename, 'rb') as file:
         self._parse(file)
     return self
Пример #26
0
        def _findSoname_ldconfig(name):
            import struct
            if struct.calcsize('l') == 4:
                machine = os.uname().machine + '-32'
            else:
                machine = os.uname().machine + '-64'
            mach_map = {
                'x86_64-64': 'libc6,x86-64',
                'ppc64-64': 'libc6,64bit',
                'sparc64-64': 'libc6,64bit',
                's390x-64': 'libc6,64bit',
                'ia64-64': 'libc6,IA-64',
                }
            abi_type = mach_map.get(machine, 'libc6')

            # XXX assuming GLIBC's ldconfig (with option -p)
            regex = r'\s+(lib%s\.[^\s]+)\s+\(%s'
            regex = os.fsencode(regex % (re.escape(name), abi_type))
            try:
                with subprocess.Popen(['/sbin/ldconfig', '-p'],
                                      stdin=subprocess.DEVNULL,
                                      stderr=subprocess.DEVNULL,
                                      stdout=subprocess.PIPE,
                                      env={'LC_ALL': 'C', 'LANG': 'C'}) as p:
                    res = re.search(regex, p.stdout.read())
                    if res:
                        return os.fsdecode(res.group(1))
            except OSError:
                pass
Пример #27
0
        def setUp(self):
            if support.TESTFN_UNENCODABLE:
                self.dir = support.TESTFN_UNENCODABLE
            else:
                self.dir = support.TESTFN
            self.bdir = os.fsencode(self.dir)

            bytesfn = []
            def add_filename(fn):
                try:
                    fn = os.fsencode(fn)
                except UnicodeEncodeError:
                    return
                bytesfn.append(fn)
            add_filename(support.TESTFN_UNICODE)
            if support.TESTFN_UNENCODABLE:
                add_filename(support.TESTFN_UNENCODABLE)
            if not bytesfn:
                self.skipTest("couldn't create any non-ascii filename")

            self.unicodefn = set()
            os.mkdir(self.dir)
            try:
                for fn in bytesfn:
                    f = open(os.path.join(self.bdir, fn), "w")
                    f.close()
                    fn = os.fsdecode(fn)
                    if fn in self.unicodefn:
                        raise ValueError("duplicate filename")
                    self.unicodefn.add(fn)
            except:
                shutil.rmtree(self.dir)
                raise
Пример #28
0
def get_accession_id(dirname):
    """
    Call get-accession-number and return literal_eval stdout as accession ID.

    get-accession-number should be in the same directory as transfer.py. Its
    only output to stdout should be the accession number surrounded by
    quotes.  Eg. "accession number"

    :param str dirname: Directory name of folder to become transfer
    :returns: accession number or None.
    """
    script_path = os.path.join(THIS_DIR, 'get-accession-number')
    try:
        p = subprocess.Popen([script_path, dirname], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except Exception:
        LOGGER.info('Error when trying to run %s', script_path)
        return None
    output, err = p.communicate()
    if p.returncode != 0:
        LOGGER.info('Error running %s %s: RC: %s; stdout: %s; stderr: %s', script_path, dirname, p.returncode, output, err)
        return None
    output = fsdecode(output)
    try:
        return ast.literal_eval(output)
    except Exception:
        LOGGER.info('Unable to parse output from %s. Output: %s', script_path, output)
        return None
Пример #29
0
def name_from_path(path):
    """Extract name from file path."""
    name, _ = os.path.splitext(os.path.basename(path))
    try:
        return fsdecode(name)
    except UnicodeError:
        return None
Пример #30
0
 def cmd(self, *args, **kw):
     exit_code = kw.get('exit_code', 0)
     fork = kw.get('fork', False)
     if fork:
         try:
             output = subprocess.check_output((sys.executable, '-m', 'borg.archiver') + args)
             ret = 0
         except subprocess.CalledProcessError as e:
             output = e.output
             ret = e.returncode
         output = os.fsdecode(output)
         if ret != exit_code:
             print(output)
         self.assert_equal(exit_code, ret)
         return output
     args = list(args)
     stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
     try:
         sys.stdin = StringIO()
         output = StringIO()
         sys.stdout = sys.stderr = output
         ret = self.archiver.run(args)
         sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
         if ret != exit_code:
             print(output.getvalue())
         self.assert_equal(exit_code, ret)
         return output.getvalue()
     finally:
         sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
Пример #31
0
    def dispatch(self, event, match_time=True):
        """Dispatch events to the appropriate methods.

        Parameters
        ----------
        event : FileSystemEvent
            Event object representing the file system event.

        match_time : bool
            If False, do not check the matched file's time against the
            handler's starttime and endtime.

        """
        if self.ignore_directories and event.is_directory:
            return

        src_match = False
        if event.src_path:
            src_path = fsdecode(event.src_path)
            if not any(r.match(src_path) for r in self.ignore_regexes):
                for r in self.regexes:
                    m = r.match(src_path)
                    if m:
                        src_match = True
                        match = m

        dest_match = False
        if getattr(event, "dest_path", None) is not None:
            dest_path = fsdecode(event.dest_path)
            if not any(r.match(dest_path) for r in self.ignore_regexes):
                for r in self.regexes:
                    m = r.match(dest_path)
                    if m:
                        dest_match = True
                        match = m

            # change move event to deleted/created if both regexes didn't match
            if src_match and not dest_match:
                event = FileDeletedEvent(event.src_path)
            elif dest_match and not src_match:
                event = FileCreatedEvent(event.dest_path)

        if not src_match and not dest_match:
            return

        # regexes matched, now check the time
        if match_time:
            try:
                secs = int(match.group("secs"))
            except (IndexError, TypeError):
                # no time, don't need to check it
                pass
            else:
                try:
                    msecs = int(match.group("frac"))
                except (IndexError, TypeError):
                    msecs = 0
                time = datetime.timedelta(seconds=secs, milliseconds=msecs)
                if self.starttime is not None and time < self.starttime:
                    return
                elif self.endtime is not None and time > self.endtime:
                    return

        # the event matched, including time if applicable, dispatch
        # we've handled matching, so jump to the regex handler's parent for dispatching
        super(RegexMatchingEventHandler, self).dispatch(event)
Пример #32
0
 def normpath(path):
     """Normalize path, eliminating double slashes, etc."""
     path = os.fspath(path)
     if isinstance(path, bytes):
         return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
     return _path_normpath(path) or "."