def print_files(args, process, files_dict, file_permissions):
    """Print a list of files.
    If a process and file_permissions dictionary is supplied, also print
    the permissions the process has on each file."""
    extension = "txt"
    # Sanitize arguments
    device_out = re.sub(r'[^a-zA-Z-_0-9.:]', r'-', args.device)
    if args.out:
        file_out = os.path.abspath(args.out)
    if process:
        process_out = re.sub(r'[^a-zA-Z-_0-9.:]', r'-', process.name)

    # Print to file
    if args.out:
        # If we are filtering by a process, name the output file accordingly
        if process:
            output = "{0}_files_{1}_{2.pid}_{2.context}_{3}.{4}".format(
                file_out, device_out, process, process_out, extension)
        # We are not filtering by a process, name the output file accordingly
        else:
            output = "{}_files_{}.{}".format(file_out, device_out, extension)
        logging.info("Printing to \"%s\"...", output)
        # Open file for printing
        with open(output, "w") as thefile:
            if process and file_permissions:
                # Print only the files a process has permissions to
                for fname, f in files_dict.iteritems():
                    if fname in file_permissions:
                        out_line = fname
                        # -Z or --context option
                        if args.context:
                            out_line = str(f.context) + " " + out_line
                        # --permissions option
                        if args.permissions:
                            out_line += "\t" + f.security_class + " {"
                            out_line += " ".join(
                                sorted(file_permissions[fname])) + "}"
                        print>>thefile, out_line
            else:
                # Print all files
                for fname, f in files_dict.iteritems():
                    out_line = fname
                    # -Z or --context option
                    if args.context:
                        out_line = str(f.context) + " " + out_line
                    print>>thefile, out_line
    # Print to stdout
    else:
        if process and file_permissions:
            # Print only the files a process has permissions to
            for fname, f in files_dict.iteritems():
                if fname in file_permissions:
                    out_line = fname
                    # -Z or --context option
                    if args.context:
                        out_line = str(f.context) + " " + out_line
                    # --permissions option
                    if args.permissions:
                        out_line += "\t" + f.security_class + " {"
                        out_line += " ".join(
                            sorted(file_permissions[fname])) + "}"
                    print out_line
        else:
            # Print all files
            for fname, f in files_dict.iteritems():
                out_line = fname
                # -Z or --context option
                if args.context:
                    out_line = str(f.context) + " " + out_line
                print out_line

    print "The device contains {} files.".format(len(files_dict))
    if process and file_permissions:
        print "The process has access to {} files.".format(
            len(file_permissions))


########################################
# Processes
dataset/ETHPy150Open seandroid-analytics/seal/seal.py/print_files/original