Exemple #1
0
def get_bin_lib_dirs(base_dir):
    """
    Return a tuple of bin and lib sub-directories of a `base_dir`. bin and lib
    directories are None if they do not exist.

    On POSIX, all files in these directories are made executable.
    The lib dir defaults to bin dir if lib did is not present.
    """

    if not base_dir:
        return None, None

    bin_dir = os.path.join(base_dir, 'bin')

    if os.path.exists(bin_dir):
        fileutils.chmod(bin_dir, fileutils.RX, recurse=True)
    else:
        bin_dir = None

    lib_dir = os.path.join(base_dir, 'lib')

    if os.path.exists(lib_dir):
        fileutils.chmod(bin_dir, fileutils.RX, recurse=True)
    else:
        # default to bin for lib if it exists
        lib_dir = bin_dir or None

    return bin_dir, lib_dir
Exemple #2
0
    def test_chmod_read_write_non_recursively_on_dir(self):
        test_dir = self.get_test_loc('fileutils/executable', copy=True)
        test_file = join(test_dir, 'deep1', 'deep2', 'ctags')
        test_dir = join(test_dir, 'deep1', 'deep2')
        parent = join(test_dir, 'deep1')

        try:
            # setup
            make_non_writable(test_file)
            assert not filetype.is_writable(test_file)

            make_non_writable(test_dir)
            if on_posix:
                assert not filetype.is_writable(test_dir)
            else:
                # windows is different
                assert filetype.is_writable(test_dir)

            fileutils.chmod(parent, fileutils.RW, recurse=False)
            # test: the perms should be the same
            assert not filetype.is_writable(test_file)

            if on_posix:
                assert not filetype.is_writable(test_dir)
            else:
                # windows is different
                assert filetype.is_writable(test_dir)
        finally:
            fileutils.chmod(test_dir, fileutils.RW, recurse=True)
Exemple #3
0
    def test_copytree_does_not_keep_non_writable_permissions(self):
        src = self.get_test_loc('fileutils/exec', copy=True)
        dst = self.get_temp_dir()

        try:
            src_file = join(src, 'subtxt/a.txt')
            make_non_writable(src_file)
            assert not filetype.is_writable(src_file)

            src_dir = join(src, 'subtxt')
            make_non_writable(src_dir)
            if on_posix:
                assert not filetype.is_writable(src_dir)

            # copy proper
            dest_dir = join(dst, 'dest')
            fileutils.copytree(src, dest_dir)

            dst_file = join(dest_dir, 'subtxt/a.txt')
            assert os.path.exists(dst_file)
            assert filetype.is_writable(dst_file)

            dest_dir2 = join(dest_dir, 'subtxt')
            assert os.path.exists(dest_dir2)
            assert filetype.is_writable(dest_dir)
        finally:
            fileutils.chmod(src, fileutils.RW, recurse=True)
            fileutils.chmod(dst, fileutils.RW, recurse=True)
Exemple #4
0
    def remove_vcs(self, test_dir):
        """
        Remove some version control directories and some temp editor files.
        """
        vcses = ('CVS', '.svn', '.git', '.hg')
        if on_linux and py2:
            vcses = tuple(fsencode(p) for p in vcses)
            test_dir = fsencode(test_dir)

        for root, dirs, files in os.walk(test_dir):
            for vcs_dir in vcses:
                if vcs_dir in dirs:
                    for vcsroot, vcsdirs, vcsfiles in os.walk(test_dir):
                        for vcsfile in vcsdirs + vcsfiles:
                            vfile = path.join(vcsroot, vcsfile)
                            fileutils.chmod(vfile, fileutils.RW, recurse=False)
                    shutil.rmtree(path.join(root, vcs_dir), False)

            # editors temp file leftovers
            tilde = b'~' if on_linux and py2 else '~'
            tilde_files = [
                path.join(root, file_loc) for file_loc in files
                if file_loc.endswith(tilde)
            ]
            for tf in tilde_files:
                os.remove(tf)
def get_bin_lib_dirs(base_dir):
    """
    Return a tuple of bin and lib sub-directories of a `base_dir`. bin and lib
    directories are None if they do not exist.

    On POSIX, all files in these directories are made executable.
    The lib dir defaults to bin dir if lib did is not present.
    """

    if not base_dir:
        return None, None

    bin_dir = os.path.join(base_dir, 'bin')

    if os.path.exists(bin_dir):
        fileutils.chmod(bin_dir, fileutils.RX, recurse=True)
    else:
        bin_dir = None

    lib_dir = os.path.join(base_dir, 'lib')

    if os.path.exists(lib_dir):
        fileutils.chmod(bin_dir, fileutils.RX, recurse=True)
    else:
        # default to bin for lib if it exists
        lib_dir = bin_dir or None

    return bin_dir, lib_dir
Exemple #6
0
    def test_chmod_read_write_recursively_on_dir(self):
        test_dir = self.get_test_loc('fileutils/executable', copy=True)
        test_file = join(test_dir, 'deep1', 'deep2', 'ctags')
        test_dir2 = join(test_dir, 'deep1', 'deep2')
        parent = join(test_dir, 'deep1')

        try:
            make_non_writable(test_file)
            assert not filetype.is_writable(test_file)
            if on_posix:
                make_non_executable(test_file)
                assert not filetype.is_executable(test_file)

            if on_posix:
                make_non_executable(test_dir2)
                assert not filetype.is_executable(test_dir2)
            make_non_writable(test_dir)
            if on_posix:
                assert not filetype.is_writable(test_dir2)

            fileutils.chmod(parent, fileutils.RW, recurse=True)

            assert filetype.is_readable(test_dir2) is True
            assert filetype.is_writable(test_dir2)
            if on_posix:
                assert filetype.is_executable(test_dir2)
        finally:
            fileutils.chmod(test_dir, fileutils.RW, recurse=True)
Exemple #7
0
    def test_copytree_copies_unreadable_files(self):
        src = self.get_test_loc('fileutils/exec', copy=True)
        dst = self.get_temp_dir()
        src_file1 = join(src, 'a.bat')
        src_file2 = join(src, 'subtxt', 'a.txt')

        try:
            # make some unreadable source files
            make_non_readable(src_file1)
            if on_posix:
                assert not filetype.is_readable(src_file1)

            make_non_readable(src_file2)
            if on_posix:
                assert not filetype.is_readable(src_file2)

            # copy proper
            dest_dir = join(dst, 'dest')
            fileutils.copytree(src, dest_dir)

            dest_file1 = join(dest_dir, 'a.bat')
            assert os.path.exists(dest_file1)
            assert filetype.is_readable(dest_file1)

            dest_file2 = join(dest_dir, 'subtxt', 'a.txt')
            assert os.path.exists(dest_file2)
            assert filetype.is_readable(dest_file2)

        finally:
            fileutils.chmod(src, fileutils.RW, recurse=True)
            fileutils.chmod(dst, fileutils.RW, recurse=True)
Exemple #8
0
    def test_chmod_read_write_file(self):
        test_dir = self.get_test_loc('fileutils/executable', copy=True)
        test_file = join(test_dir, 'deep1', 'deep2', 'ctags')

        try:
            make_non_writable(test_file)
            assert not filetype.is_writable(test_file)

            fileutils.chmod(test_file, fileutils.RW)
            assert filetype.is_readable(test_file)
            assert filetype.is_writable(test_file)
        finally:
            fileutils.chmod(test_dir, fileutils.RW, recurse=True)
Exemple #9
0
    def test_copyfile_does_not_keep_permissions(self):
        src_file = self.get_temp_file()
        dest = self.get_temp_dir()
        with open(src_file, 'wb') as f:
            f.write('')
        try:
            make_non_readable(src_file)
            if on_posix:
                assert not filetype.is_readable(src_file)

            fileutils.copyfile(src_file, dest)
            dest_file = join(dest, os.listdir(dest)[0])
            assert filetype.is_readable(dest_file)
        finally:
            fileutils.chmod(src_file, fileutils.RW, recurse=True)
            fileutils.chmod(dest, fileutils.RW, recurse=True)
Exemple #10
0
    def remove_vcs(self, test_dir):
        """
        Remove some version control directories and some temp editor files.
        """
        for root, dirs, files in os.walk(test_dir):
            for vcs_dir in 'CVS', '.svn', '.git', '.hg':
                if vcs_dir in dirs:
                    for vcsroot, vcsdirs, vcsfiles in os.walk(test_dir):
                        for vcsfile in vcsdirs + vcsfiles:
                            vfile = os.path.join(vcsroot, vcsfile)
                            fileutils.chmod(vfile, fileutils.RW, recurse=False)
                    shutil.rmtree(os.path.join(root, vcs_dir), False)

            # editors temp file leftovers
            map(os.remove, [os.path.join(root, file_loc)
                            for file_loc in files if file_loc.endswith('~')])
    def test_is_readable_is_writeable_file(self):
        base_dir = self.get_test_loc('filetype/readwrite', copy=True)
        test_file = os.path.join(os.path.join(base_dir, 'sub'), 'file')

        try:
            assert filetype.is_readable(test_file)
            assert filetype.is_writable(test_file)

            make_non_readable(test_file)
            if on_posix:
                assert not filetype.is_readable(test_file)

            make_non_writable(test_file)
            assert not filetype.is_writable(test_file)
        finally:
            fileutils.chmod(base_dir, fileutils.RW, recurse=True)
    def remove_vcs(self, test_dir):
        """
        Remove some version control directories and some temp editor files.
        """
        for root, dirs, files in os.walk(test_dir):
            for vcs_dir in 'CVS', '.svn', '.git', '.hg':
                if vcs_dir in dirs:
                    for vcsroot, vcsdirs, vcsfiles in os.walk(test_dir):
                        for vcsfile in vcsdirs + vcsfiles:
                            vfile = os.path.join(vcsroot, vcsfile)
                            fileutils.chmod(vfile, fileutils.RW, recurse=False)
                    shutil.rmtree(os.path.join(root, vcs_dir), False)

            # editors temp file leftovers
            map(os.remove, [os.path.join(root, file_loc)
                            for file_loc in files if file_loc.endswith('~')])
    def test_chmod_read_write_exec_dir(self):
        test_dir = self.get_test_loc("fileutils/executable", copy=True)
        test_file = join(test_dir, "deep1", "deep2", "ctags")

        try:
            if on_posix:
                make_non_executable(test_dir)
                assert not filetype.is_executable(test_file)
            make_non_writable(test_dir)

            fileutils.chmod(test_dir, fileutils.RWX, recurse=True)
            assert filetype.is_readable(test_file)
            assert filetype.is_writable(test_file)
            if on_posix:
                assert filetype.is_executable(test_file)
        finally:
            fileutils.chmod(test_dir, fileutils.RW, recurse=True)
Exemple #14
0
    def test_delete_unwritable_directory_and_files(self):
        base_dir = self.get_test_loc('fileutils/readwrite', copy=True)
        test_dir = join(base_dir, 'sub')
        test_file = join(test_dir, 'file')

        try:
            # note: there are no unread/writable dir on windows
            make_non_readable(test_file)
            make_non_executable(test_file)
            make_non_writable(test_file)

            make_non_readable(test_dir)
            make_non_executable(test_dir)
            make_non_writable(test_dir)

            fileutils.delete(test_dir)
            assert not os.path.exists(test_dir)
        finally:
            fileutils.chmod(base_dir, fileutils.RW, recurse=True)
Exemple #15
0
def get_locations(cmd,
                  root_dir,
                  _os_arch=current_os_arch,
                  _os_noarch=current_os_noarch,
                  _noarch=noarch,
                  must_exist=True):
    """
    Return a tuple of (cmd_loc, bin_dir, lib_dir), where `cmd_loc` is the
    location of an  `cmd` external command, bin_dir and lib_dir are the
    corresponding bin and lib directories. `root_dir` is used to resolve where
    a local/vendored executable `cmd` is stored. Return a tuple of None if no
    local/vendored executable was found or no `root_dir` was provided or
    found.
    If `must_exist` is False, the existence of the cmd is not performed.
    In this case the first existing bin and lib dirs will be returned.

    On POSIX, the command file is made executable when found locally.
    On Windows, an '.exe' extension is added to `cmd`.
    """
    cmd_loc = bin_dir = lib_dir = None
    if not root_dir:
        return cmd_loc, bin_dir, lib_dir

    if must_exist:
        # we do not use on_windows here to support cross OS testing
        if any(x and 'win' in x for x in (_os_arch, _os_noarch, _noarch)):
            cmd = cmd + '.exe'

        for base_dir in get_base_dirs(root_dir, _os_arch, _os_noarch, _noarch):
            bin_dir, lib_dir = get_bin_lib_dirs(base_dir)
            cmd_loc = os.path.join(bin_dir, cmd)
            if os.path.exists(cmd_loc):
                fileutils.chmod(cmd_loc, fileutils.RX, recurse=False)
                return cmd_loc, bin_dir, lib_dir
    else:
        # we just care for getting the dirs and grab the first one
        for base_dir in get_base_dirs(root_dir, _os_arch, _os_noarch, _noarch):
            bin_dir, lib_dir = get_bin_lib_dirs(base_dir)
            return None, bin_dir, lib_dir

    return None, None, None
def get_locations(cmd, root_dir,
                  _os_arch=current_os_arch,
                  _os_noarch=current_os_noarch,
                  _noarch=noarch,
                  must_exist=True):
    """
    Return a tuple of (cmd_loc, bin_dir, lib_dir), where `cmd_loc` is the
    location of an  `cmd` external command, bin_dir and lib_dir are the
    corresponding bin and lib directories. `root_dir` is used to resolve where
    a local/vendored executable `cmd` is stored. Return a tuple of None if no
    local/vendored executable was found or no `root_dir` was provided or
    found.
    If `must_exist` is False, the existence of the cmd is not performed.
    In this case the first existing bin and lib dirs will be returned.

    On POSIX, the command file is made executable when found locally.
    On Windows, an '.exe' extension is added to `cmd`.
    """
    cmd_loc = bin_dir = lib_dir = None
    if not root_dir:
        return cmd_loc, bin_dir, lib_dir

    if must_exist:
        # we do not use on_windows here to support cross OS testing
        if any(x and 'win' in x for x in (_os_arch, _os_noarch, _noarch)):
            cmd = cmd + '.exe'

        for base_dir in get_base_dirs(root_dir, _os_arch, _os_noarch, _noarch):
            bin_dir, lib_dir = get_bin_lib_dirs(base_dir)
            cmd_loc = os.path.join(bin_dir, cmd)
            if os.path.exists(cmd_loc):
                fileutils.chmod(cmd_loc, fileutils.RX, recurse=False)
                return cmd_loc, bin_dir, lib_dir
    else:
        # we just care for getting the dirs and grab the first one
        for base_dir in get_base_dirs(root_dir, _os_arch, _os_noarch, _noarch):
            bin_dir, lib_dir = get_bin_lib_dirs(base_dir)
            return None, bin_dir, lib_dir

    return None, None, None
    def remove_vcs(self, test_dir):
        """
        Remove some version control directories and some temp editor files.
        """
        vcses = ('CVS', '.svn', '.git', '.hg')
        if on_linux:
            vcses = tuple(path_to_bytes(p) for p in vcses)
            test_dir = path_to_bytes(test_dir)

        for root, dirs, files in os.walk(test_dir):
            for vcs_dir in vcses:
                if vcs_dir in dirs:
                    for vcsroot, vcsdirs, vcsfiles in os.walk(test_dir):
                        for vcsfile in vcsdirs + vcsfiles:
                            vfile = os.path.join(vcsroot, vcsfile)
                            fileutils.chmod(vfile, fileutils.RW, recurse=False)
                    shutil.rmtree(os.path.join(root, vcs_dir), False)

            # editors temp file leftovers
            tilde = b'~' if on_linux else '~'
            map(os.remove, [os.path.join(root, file_loc)
                            for file_loc in files if file_loc.endswith(tilde)])
    def test_is_readable_is_writeable_dir(self):
        base_dir = self.get_test_loc('filetype/readwrite', copy=True)
        test_dir = os.path.join(base_dir, 'sub')

        try:
            assert filetype.is_readable(test_dir)
            assert filetype.is_writable(test_dir)

            make_non_readable(test_dir)
            if on_posix:
                assert not filetype.is_readable(test_dir)
            else:
                # dirs are always RW on windows
                assert filetype.is_readable(test_dir)
            make_non_writable(test_dir)
            if on_posix:
                assert not filetype.is_writable(test_dir)
            else:
                # dirs are always RW on windows
                assert filetype.is_writable(test_dir)
            # finally
        finally:
            fileutils.chmod(base_dir, fileutils.RW, recurse=True)
Exemple #19
0
 def test_chmod_on_non_existing_file_throws_no_exception(self):
     fileutils.chmod('some non existing dir', fileutils.RWX)