コード例 #1
0
ファイル: ls.py プロジェクト: shaishasag/instl
def wtar_item_ls_func(item, ls_format):
    the_parts = dict()
    for format_char in ls_format:
        if format_char == 'R':
            the_parts[format_char] = utils.unix_permissions_to_str(
                item.mode)  # permissions
        elif format_char == 'u':
            the_parts[format_char] = item.uid
        elif format_char == 'U':
            the_parts[format_char] = item.uname
        elif format_char == 'g':
            the_parts[format_char] = item.gid
        elif format_char == 'G':
            the_parts[format_char] = item.gname
        elif format_char == 'S':
            the_parts[format_char] = item.size
        elif format_char == 'T':
            the_parts[format_char] = time.strftime(
                "%Y/%m/%d-%H:%M:%S",
                time.gmtime(item.mtime))  # modification time
        elif format_char == 'C':
            the_parts[format_char] = item.pax_headers.get("checksum", "")
        elif format_char == 'P' or format_char == 'p':
            path_to_return = item.name
            if item.isdir() and 'D' in ls_format:
                path_to_return += '/'

            if 'E' in ls_format:
                if item.issym():
                    path_to_return += '@'
                elif item.isfifo():
                    path_to_return += '|'

            the_parts[format_char] = path_to_return
    return the_parts
コード例 #2
0
    def test_Chmod_non_recursive(self):
        """ test Chmod
            A file is created and it's permissions are changed several times
        """
        # TODO: Add test for symbolic links
        file_to_chmod = self.pbt.path_inside_test_folder("file-to-chmod")
        touch(file_to_chmod)
        mod_before = stat.S_IMODE(os.stat(file_to_chmod).st_mode)
        os.chmod(file_to_chmod, Chmod.all_read)
        initial_mode = utils.unix_permissions_to_str(
            stat.S_IMODE(os.stat(file_to_chmod).st_mode))
        expected_mode = utils.unix_permissions_to_str(Chmod.all_read)
        self.assertEqual(
            initial_mode, expected_mode,
            f"{self.pbt.which_test}: failed to chmod on test file before tests: {initial_mode} != {expected_mode}"
        )

        # change to rwxrwxrwx
        new_mode = stat.S_IMODE(stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
                                | stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
        if sys.platform == 'darwin':  # Adding executable bit for mac
            new_mode = stat.S_IMODE(new_mode | stat.S_IXUSR | stat.S_IXGRP
                                    | stat.S_IXOTH)

        new_mode_symbolic = 'a=rwx'
        self.pbt.batch_accum.clear(section_name="doit")
        self.pbt.batch_accum += Chmod(file_to_chmod, new_mode_symbolic)

        self.pbt.exec_and_capture_output("chmod_a=rwx")

        mod_after = stat.S_IMODE(os.stat(file_to_chmod).st_mode)
        self.assertEqual(
            new_mode, mod_after,
            f"{self.pbt.which_test}: failed to chmod to {utils.unix_permissions_to_str(new_mode)} got {utils.unix_permissions_to_str(mod_after)}"
        )

        # pass inappropriate symbolic mode should result in ValueError exception and permissions should remain
        new_mode_symbolic = 'a=rwi'  # i is not a legal mode
        self.pbt.batch_accum.clear(section_name="doit")
        self.pbt.batch_accum += Chmod(file_to_chmod, new_mode_symbolic)

        self.pbt.exec_and_capture_output("chmod_a=rwi",
                                         expected_exception=ValueError)

        mod_after = stat.S_IMODE(os.stat(file_to_chmod).st_mode)
        self.assertEqual(
            new_mode, mod_after,
            f"{self.pbt.which_test}: mode should remain {utils.unix_permissions_to_str(new_mode)} got {utils.unix_permissions_to_str(mod_after)}"
        )

        # change to rw-rw-rw-
        new_mode = stat.S_IMODE(stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
                                | stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
        new_mode_symbolic = 'a-x'
        self.pbt.batch_accum.clear(section_name="doit")
        self.pbt.batch_accum += Chmod(file_to_chmod, new_mode_symbolic)

        self.pbt.exec_and_capture_output("chmod_a-x")

        mod_after = stat.S_IMODE(os.stat(file_to_chmod).st_mode)
        self.assertEqual(
            new_mode, mod_after,
            f"{self.pbt.which_test}: failed to chmod to {utils.unix_permissions_to_str(new_mode)} got {utils.unix_permissions_to_str(mod_after)}"
        )

        if sys.platform == 'darwin':  # Windows doesn't have an executable bit, test is skipped
            # change to rwxrwxrw-
            new_mode = stat.S_IMODE(stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
                                    | stat.S_IWUSR | stat.S_IWGRP
                                    | stat.S_IWOTH | stat.S_IXUSR
                                    | stat.S_IXGRP)
            new_mode_symbolic = 'ug+x'
            self.pbt.batch_accum.clear(section_name="doit")
            self.pbt.batch_accum += Chmod(file_to_chmod, new_mode_symbolic)

            self.pbt.exec_and_capture_output("chmod_ug+x")

            mod_after = stat.S_IMODE(os.stat(file_to_chmod).st_mode)
            self.assertEqual(
                new_mode, mod_after,
                f"{self.pbt.which_test}: failed to chmod to {utils.unix_permissions_to_str(new_mode)} got {utils.unix_permissions_to_str(mod_after)}"
            )

        # change to r--r--r--
        new_mode = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
        self.pbt.batch_accum.clear(section_name="doit")
        self.pbt.batch_accum += Chmod(file_to_chmod, 'u-wx')
        self.pbt.batch_accum += Chmod(file_to_chmod, 'g-wx')
        self.pbt.batch_accum += Chmod(file_to_chmod, 'o-wx')

        self.pbt.exec_and_capture_output("chmod_a-wx")

        mod_after = stat.S_IMODE(os.stat(file_to_chmod).st_mode)
        self.assertEqual(
            new_mode, mod_after,
            f"{self.pbt.which_test}: failed to chmod to {utils.unix_permissions_to_str(new_mode)} got {utils.unix_permissions_to_str(mod_after)}"
        )
コード例 #3
0
ファイル: ls.py プロジェクト: shaishasag/instl
def unix_item_ls(the_path, ls_format, root_folder=None):
    import grp
    import pwd

    the_parts = dict()
    the_error = None
    the_path_str = os.fspath(the_path)
    if 'p' in ls_format:
        the_parts['p'] = the_path_str
    elif 'P' in ls_format:
        the_parts['P'] = the_path_str

    try:
        the_stats = os.lstat(the_path)

        for format_char in ls_format:
            if format_char == 'I':
                the_parts[format_char] = the_stats[stat.ST_INO]  # inode number
            elif format_char == 'R':
                the_parts[format_char] = utils.unix_permissions_to_str(
                    the_stats.st_mode)  # permissions
            elif format_char == 'L':
                the_parts[format_char] = the_stats[stat.ST_NLINK]  # num links
            elif format_char == 'u':
                try:
                    the_parts[format_char] = str(the_stats[stat.ST_UID])[
                        0]  # unknown user name, get the number
                except Exception:
                    the_parts[format_char] = "no_uid"
            elif format_char == 'U':
                try:
                    the_parts[format_char] = pwd.getpwuid(
                        the_stats[stat.ST_UID])[0]  # user
                except KeyError:
                    the_parts[format_char] = str(the_stats[stat.ST_UID])[
                        0]  # unknown user name, get the number
                except Exception:
                    the_parts[format_char] = "no_uid"
            elif format_char == 'g':
                try:
                    the_parts[format_char] = str(the_stats[stat.ST_GID])[
                        0]  # unknown group name, get the number
                except Exception:
                    the_parts[format_char] = "no_gid"
            elif format_char == 'G':
                try:
                    the_parts[format_char] = grp.getgrgid(
                        the_stats[stat.ST_GID])[0]  # group
                except KeyError:
                    the_parts[format_char] = str(the_stats[stat.ST_GID])[
                        0]  # unknown group name, get the number
                except Exception:
                    the_parts[format_char] = "no_gid"
            elif format_char == 'S':
                the_parts[format_char] = the_stats[
                    stat.ST_SIZE]  # size in bytes
            elif format_char == 'T':
                the_parts[format_char] = time.strftime(
                    "%Y/%m/%d-%H:%M:%S", time.gmtime(
                        (the_stats[stat.ST_MTIME])))  # modification time
            elif format_char == 'C':
                if not (stat.S_ISLNK(the_stats.st_mode)
                        or stat.S_ISDIR(the_stats.st_mode)):
                    the_parts[format_char] = utils.get_file_checksum(the_path)
                else:
                    the_parts[format_char] = ""
            elif format_char == 'P' or format_char == 'p':
                path_to_return = the_path_str
                if format_char == 'p' and root_folder is not None:
                    path_to_return = os.path.relpath(the_path,
                                                     start=root_folder)

                # E will bring us Extra data (path postfix) but we want to know if it's DIR in any case
                if stat.S_ISDIR(the_stats.st_mode) and 'D' in ls_format:
                    path_to_return += '/'

                if 'E' in ls_format:
                    if stat.S_ISLNK(the_stats.st_mode):
                        path_to_return += '@'
                    elif not stat.S_ISDIR(the_stats.st_mode) and (
                            the_stats.st_mode &
                        (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)):
                        path_to_return += '*'
                    elif stat.S_ISSOCK(the_stats.st_mode):
                        path_to_return += '='
                    elif stat.S_ISFIFO(the_stats.st_mode):
                        path_to_return += '|'

                the_parts[format_char] = path_to_return
            elif format_char == 'a' or format_char == 'f':
                import subprocess
                completed_process = subprocess.run(f'ls -lO "{the_path_str}"',
                                                   shell=True,
                                                   stdout=subprocess.PIPE,
                                                   stderr=subprocess.PIPE)
                if completed_process.returncode != 0:
                    the_parts[format_char] = utils.unicodify(
                        completed_process.stderr)
                else:
                    ls_line = utils.unicodify(completed_process.stdout)
                    flag_matches = re.findall(
                        "arch|archived|opaque|nodump|sappnd|sappend|schg|schange|simmutable|uappnd|uappend|uchg|uchange|uimmutable|hidden",
                        ls_line)
                    if flag_matches:
                        the_parts[format_char] = ",".join(flag_matches)
                    else:
                        the_parts[format_char] = "[]"

    except Exception as ex:
        the_error = [the_path_str, ex.strerror]

    return the_parts, the_error
コード例 #4
0
def unix_item_ls(the_path, ls_format, root_folder=None):
    import grp
    import pwd

    the_parts = dict()
    if 'p' in ls_format or 'P' in ls_format:
        the_parts['p'] = the_path

    try:
        the_stats = os.lstat(the_path)

        for format_char in ls_format:
            if format_char == 'I':
                the_parts[format_char] = the_stats[stat.ST_INO]  # inode number
            elif format_char == 'R':
                the_parts[format_char] = utils.unix_permissions_to_str(
                    the_stats.st_mode)  # permissions
            elif format_char == 'L':
                the_parts[format_char] = the_stats[stat.ST_NLINK]  # num links
            elif format_char == 'u':
                try:
                    the_parts[format_char] = str(the_stats[stat.ST_UID])[
                        0]  # unknown user name, get the number
                except Exception:
                    the_parts[format_char] = "no_uid"
            elif format_char == 'U':
                try:
                    the_parts[format_char] = pwd.getpwuid(
                        the_stats[stat.ST_UID])[0]  # user
                except KeyError:
                    the_parts[format_char] = str(the_stats[stat.ST_UID])[
                        0]  # unknown user name, get the number
                except Exception:
                    the_parts[format_char] = "no_uid"
            elif format_char == 'g':
                try:
                    the_parts[format_char] = str(the_stats[stat.ST_GID])[
                        0]  # unknown group name, get the number
                except Exception:
                    the_parts[format_char] = "no_gid"
            elif format_char == 'G':
                try:
                    the_parts[format_char] = grp.getgrgid(
                        the_stats[stat.ST_GID])[0]  # group
                except KeyError:
                    the_parts[format_char] = str(the_stats[stat.ST_GID])[
                        0]  # unknown group name, get the number
                except Exception:
                    the_parts[format_char] = "no_gid"
            elif format_char == 'S':
                the_parts[format_char] = the_stats[
                    stat.ST_SIZE]  # size in bytes
            elif format_char == 'T':
                the_parts[format_char] = time.strftime(
                    "%Y/%m/%d-%H:%M:%S", time.gmtime(
                        (the_stats[stat.ST_MTIME])))  # modification time
            elif format_char == 'C':
                if not (stat.S_ISLNK(the_stats.st_mode)
                        or stat.S_ISDIR(the_stats.st_mode)):
                    the_parts[format_char] = utils.get_file_checksum(the_path)
                else:
                    the_parts[format_char] = ""
            elif format_char == 'P' or format_char == 'p':
                path_to_return = the_path
                if format_char == 'p' and root_folder is not None:
                    path_to_return = os.path.relpath(the_path,
                                                     start=root_folder)

                # E will bring us Extra data (path postfix) but we want to know if it's DIR in any case
                if stat.S_ISDIR(the_stats.st_mode) and 'D' in ls_format:
                    path_to_return += '/'

                if 'E' in ls_format:
                    if stat.S_ISLNK(the_stats.st_mode):
                        path_to_return += '@'
                    elif not stat.S_ISDIR(the_stats.st_mode) and (
                            the_stats.st_mode &
                        (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)):
                        path_to_return += '*'
                    elif stat.S_ISSOCK(the_stats.st_mode):
                        path_to_return += '='
                    elif stat.S_ISFIFO(the_stats.st_mode):
                        path_to_return += '|'

                the_parts[format_char] = path_to_return

    except Exception as ex:
        pass

    return the_parts