Пример #1
0
    def test_other_hidden(self):
        if sys.platform == "darwin" or sys.platform == "windows":
            self.skipTest("sys.platform is known")
            return

        with tempfile.NamedTemporaryFile(prefix=".tmp") as f:
            self.assertTrue(hidden.is_hidden(f.name))
Пример #2
0
    def test_other_hidden(self):
        if sys.platform == 'darwin' or sys.platform == 'win32':
            self.skipTest('sys.platform is known')
            return

        with tempfile.NamedTemporaryFile(prefix='.tmp') as f:
            self.assertTrue(hidden.is_hidden(f.name))
Пример #3
0
    def test_other_hidden(self):
        if sys.platform == 'darwin' or sys.platform == 'windows':
            self.skipTest('sys.platform is known')
            return

        with tempfile.NamedTemporaryFile(prefix='.tmp') as f:
            self.assertTrue(hidden.is_hidden(f.name))
Пример #4
0
    def test_other_hidden(self):
        if sys.platform == 'darwin' or sys.platform == 'win32':
            self.skipTest('sys.platform is known')
            return

        with tempfile.NamedTemporaryFile(prefix='.tmp') as f:
            fn = util.bytestring_path(f.name)
            self.assertTrue(hidden.is_hidden(fn))
Пример #5
0
def sorted_walk(path, ignore=(), ignore_hidden=False, logger=None):
    """Like `os.walk`, but yields things in case-insensitive sorted,
    breadth-first order.  Directory and file names matching any glob
    pattern in `ignore` are skipped. If `logger` is provided, then
    warning messages are logged there when a directory cannot be listed.
    """
    # Make sure the pathes aren't Unicode strings.
    path = bytestring_path(path)
    ignore = [bytestring_path(i) for i in ignore]

    # Get all the directories and files at this level.
    try:
        contents = os.listdir(syspath(path))
    except OSError as exc:
        if logger:
            logger.warning(u'could not list directory {0}: {1}'.format(
                displayable_path(path), exc.strerror))
        return
    dirs = []
    files = []
    for base in contents:
        base = bytestring_path(base)

        # Skip ignored filenames.
        skip = False
        for pat in ignore:
            if fnmatch.fnmatch(base, pat):
                if logger:
                    logger.debug(u'ignoring {0} due to ignore rule {1}'.format(
                        base, pat))
                skip = True
                break
        if skip:
            continue

        # Add to output as either a file or a directory.
        cur = os.path.join(path, base)
        if (ignore_hidden and not hidden.is_hidden(cur)) or not ignore_hidden:
            if os.path.isdir(syspath(cur)):
                dirs.append(base)
            else:
                files.append(base)

    # Sort lists (case-insensitive) and yield the current level.
    dirs.sort(key=bytes.lower)
    files.sort(key=bytes.lower)
    yield (path, dirs, files)

    # Recurse into directories.
    for base in dirs:
        cur = os.path.join(path, base)
        # yield from sorted_walk(...)
        for res in sorted_walk(cur, ignore, ignore_hidden, logger):
            yield res
Пример #6
0
def sorted_walk(path, ignore=(), ignore_hidden=False, logger=None):
    """Like `os.walk`, but yields things in case-insensitive sorted,
    breadth-first order.  Directory and file names matching any glob
    pattern in `ignore` are skipped. If `logger` is provided, then
    warning messages are logged there when a directory cannot be listed.
    """
    # Make sure the pathes aren't Unicode strings.
    path = bytestring_path(path)
    ignore = [bytestring_path(i) for i in ignore]

    # Get all the directories and files at this level.
    try:
        contents = os.listdir(syspath(path))
    except OSError as exc:
        if logger:
            logger.warning(u'could not list directory {0}: {1}'.format(
                displayable_path(path), exc.strerror
            ))
        return
    dirs = []
    files = []
    for base in contents:
        base = bytestring_path(base)

        # Skip ignored filenames.
        skip = False
        for pat in ignore:
            if fnmatch.fnmatch(base, pat):
                skip = True
                break
        if skip:
            continue

        # Add to output as either a file or a directory.
        cur = os.path.join(path, base)
        if (ignore_hidden and not hidden.is_hidden(cur)) or not ignore_hidden:
            if os.path.isdir(syspath(cur)):
                dirs.append(base)
            else:
                files.append(base)

    # Sort lists (case-insensitive) and yield the current level.
    dirs.sort(key=bytes.lower)
    files.sort(key=bytes.lower)
    yield (path, dirs, files)

    # Recurse into directories.
    for base in dirs:
        cur = os.path.join(path, base)
        # yield from sorted_walk(...)
        for res in sorted_walk(cur, ignore, ignore_hidden, logger):
            yield res
Пример #7
0
    def test_windows_hidden(self):
        if not sys.platform == "windows":
            self.skipTest("sys.platform is not windows")
            return

        # FILE_ATTRIBUTE_HIDDEN = 2 (0x2) from GetFileAttributes documentation.
        hidden_mask = 2

        with tempfile.NamedTemporaryFile() as f:
            # Hide the file using
            success = ctypes.windll.kernel32.SetFileAttributesW(f.name, hidden_mask)

            if not success:
                self.skipTest("unable to set file attributes")

            self.assertTrue(hidden.is_hidden(f.name))
Пример #8
0
    def test_osx_hidden(self):
        if not sys.platform == "darwin":
            self.skipTest("sys.platform is not darwin")
            return

        with tempfile.NamedTemporaryFile(delete=False) as f:
            try:
                command = ["chflags", "hidden", f.name]
                subprocess.Popen(command).wait()
            except OSError as e:
                if e.errno == errno.ENOENT:
                    self.skipTest("unable to find chflags")
                else:
                    raise e

            self.assertTrue(hidden.is_hidden(f.name))
Пример #9
0
    def test_osx_hidden(self):
        if not sys.platform == 'darwin':
            self.skipTest('sys.platform is not darwin')
            return

        with tempfile.NamedTemporaryFile(delete=False) as f:
            try:
                command = ["chflags", "hidden", f.name]
                subprocess.Popen(command).wait()
            except OSError as e:
                if e.errno == errno.ENOENT:
                    self.skipTest("unable to find chflags")
                else:
                    raise e

            self.assertTrue(hidden.is_hidden(f.name))
Пример #10
0
    def test_windows_hidden(self):
        if not sys.platform == 'win32':
            self.skipTest('sys.platform is not windows')
            return

        # FILE_ATTRIBUTE_HIDDEN = 2 (0x2) from GetFileAttributes documentation.
        hidden_mask = 2

        with tempfile.NamedTemporaryFile() as f:
            # Hide the file using
            success = ctypes.windll.kernel32.SetFileAttributesW(f.name,
                                                                hidden_mask)

            if not success:
                self.skipTest("unable to set file attributes")

            self.assertTrue(hidden.is_hidden(f.name))