# Buggy code
def _check_directory_win(
    name, win_owner, win_perms=None, win_deny_perms=None, win_inheritance=None
):
    """
    Check what changes need to be made on a directory
    """
    changes = {}

    if not os.path.isdir(name):
        changes = {"directory": "new"}
    else:
        # Check owner
        owner = salt.utils.win_dacl.get_owner(name)
        if not owner.lower() == win_owner.lower():
            changes["owner"] = win_owner

        # Check perms
        perms = salt.utils.win_dacl.get_permissions(name)

        # Verify Permissions
        if win_perms is not None:
            for user in win_perms:
                # Check that user exists:
                try:
                    salt.utils.win_dacl.get_name(user)
                except CommandExecutionError:
                    continue

                grant_perms = []
                # Check for permissions
                if isinstance(win_perms[user]["perms"], six.string_types):
                    if not salt.utils.win_dacl.has_permission(
                        name, user, win_perms[user]["perms"]
                    ):
                        grant_perms = win_perms[user]["perms"]
                else:
                    for perm in win_perms[user]["perms"]:
                        if not salt.utils.win_dacl.has_permission(
                            name, user, perm, exact=False
                        ):
                            grant_perms.append(win_perms[user]["perms"])
                if grant_perms:
                    if "grant_perms" not in changes:
                        changes["grant_perms"] = {}
                    if user not in changes["grant_perms"]:
                        changes["grant_perms"][user] = {}
                    changes["grant_perms"][user]["perms"] = grant_perms

                # Check Applies to
                if "applies_to" not in win_perms[user]:
                    applies_to = "this_folder_subfolders_files"
                else:
                    applies_to = win_perms[user]["applies_to"]

                if user in perms:
                    user = salt.utils.win_dacl.get_name(user)

                    # Get the proper applies_to text
                    at_flag = salt.utils.win_dacl.Flags.ace_prop["file"][applies_to]
                    applies_to_text = salt.utils.win_dacl.Flags.ace_prop["file"][
                        at_flag
                    ]

                    if "grant" in perms[user]:
                        if not perms[user]["grant"]["applies to"] == applies_to_text:
                            if "grant_perms" not in changes:
                                changes["grant_perms"] = {}
                            if user not in changes["grant_perms"]:
                                changes["grant_perms"][user] = {}
                            changes["grant_perms"][user]["applies_to"] = applies_to

        # Verify Deny Permissions
        if win_deny_perms is not None:
            for user in win_deny_perms:
                # Check that user exists:
                try:
                    salt.utils.win_dacl.get_name(user)
                except CommandExecutionError:
                    continue

                deny_perms = []
                # Check for permissions
                if isinstance(win_deny_perms[user]["perms"], six.string_types):
                    if not salt.utils.win_dacl.has_permission(
                        name, user, win_deny_perms[user]["perms"], "deny"
                    ):
                        deny_perms = win_deny_perms[user]["perms"]
                else:
                    for perm in win_deny_perms[user]["perms"]:
                        if not salt.utils.win_dacl.has_permission(
                            name, user, perm, "deny", exact=False
                        ):
                            deny_perms.append(win_deny_perms[user]["perms"])
                if deny_perms:
                    if "deny_perms" not in changes:
                        changes["deny_perms"] = {}
                    if user not in changes["deny_perms"]:
                        changes["deny_perms"][user] = {}
                    changes["deny_perms"][user]["perms"] = deny_perms

                # Check Applies to
                if "applies_to" not in win_deny_perms[user]:
                    applies_to = "this_folder_subfolders_files"
                else:
                    applies_to = win_deny_perms[user]["applies_to"]

                if user in perms:
                    user = salt.utils.win_dacl.get_name(user)

                    # Get the proper applies_to text
                    at_flag = salt.utils.win_dacl.Flags.ace_prop["file"][applies_to]
                    applies_to_text = salt.utils.win_dacl.Flags.ace_prop["file"][
                        at_flag
                    ]

                    if "deny" in perms[user]:
                        if not perms[user]["deny"]["applies to"] == applies_to_text:
                            if "deny_perms" not in changes:
                                changes["deny_perms"] = {}
                            if user not in changes["deny_perms"]:
                                changes["deny_perms"][user] = {}
                            changes["deny_perms"][user]["applies_to"] = applies_to

        # Check inheritance
        if win_inheritance is not None:
            if not win_inheritance == salt.utils.win_dacl.get_inheritance(name):
                changes["inheritance"] = win_inheritance

    if changes:
        return None, 'The directory "{0}" will be changed'.format(name), changes

    return True, "The directory {0} is in the correct state".format(name), changes


# Traceback (NameError)
Feb 10 14:33:00 echo salt-master[2373]: [ERROR   ] Failed to import utils win_dacl, this is due most likely to a syntax error:
Feb 10 14:33:00 echo salt-master[2373]: Traceback (most recent call last):
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1334, in _load_module
Feb 10 14:33:00 echo salt-master[2373]:     ), fn_, fpath, desc)
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/win_dacl.py", line 154, in <module>
Feb 10 14:33:00 echo salt-master[2373]:     class Flags(object):
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/win_dacl.py", line 366, in Flags
Feb 10 14:33:00 echo salt-master[2373]:     'grant': win32security.ACCESS_ALLOWED_ACE_TYPE,
Feb 10 14:33:00 echo salt-master[2373]: NameError: name 'win32security' is not defined
Feb 10 14:33:00 echo salt-master[2373]: [ERROR   ] Failed to import utils saltminionservice, this is due most likely to a syntax error:
Feb 10 14:33:00 echo salt-master[2373]: Traceback (most recent call last):
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1334, in _load_module
Feb 10 14:33:00 echo salt-master[2373]:     ), fn_, fpath, desc)
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/saltminionservice.py", line 8, in <module>
Feb 10 14:33:00 echo salt-master[2373]:     from salt.utils.winservice import Service, instart
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/winservice.py", line 33, in <module>
Feb 10 14:33:00 echo salt-master[2373]:     class Service(win32serviceutil.ServiceFramework):
Feb 10 14:33:00 echo salt-master[2373]: NameError: name 'win32serviceutil' is not defined
Feb 10 14:33:00 echo salt-master[2373]: [ERROR   ] Failed to import utils winservice, this is due most likely to a syntax error:
Feb 10 14:33:00 echo salt-master[2373]: Traceback (most recent call last):
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1334, in _load_module
Feb 10 14:33:00 echo salt-master[2373]:     ), fn_, fpath, desc)
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/winservice.py", line 33, in <module>
Feb 10 14:33:00 echo salt-master[2373]:     class Service(win32serviceutil.ServiceFramework):
Feb 10 14:33:00 echo salt-master[2373]: NameError: name 'win32serviceutil' is not defined
Feb 10 14:33:01 echo salt-master[2373]: [ERROR   ] Failed to import utils win_osinfo, this is due most likely to a syntax error:
Feb 10 14:33:01 echo salt-master[2373]: Traceback (most recent call last):
Feb 10 14:33:01 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1334, in _load_module
Feb 10 14:33:01 echo salt-master[2373]:     ), fn_, fpath, desc)
Feb 10 14:33:01 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/win_osinfo.py", line 32, in <module>
Feb 10 14:33:01 echo salt-master[2373]:     class OSVERSIONINFO(ctypes.Structure):
Feb 10 14:33:01 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/win_osinfo.py", line 33, in OSVERSIONINFO
Feb 10 14:33:01 echo salt-master[2373]:     _fields_ = (('dwOSVersionInfoSize', DWORD),
Feb 10 14:33:01 echo salt-master[2373]: NameError: name 'DWORD' is not defined
Feb 10 14:33:01 echo salt-master[2373]: [ERROR   ] Exception raised when processing __virtual__ function for boto_kinesis. Module will not be loaded 'boto3.assign_funcs'
Feb 10 14:33:01 echo salt-master[2373]: [WARNING ] salt.loaded.int.module.boto_kinesis.__virtual__() is wrongly returning `None`. It should either return `True`, `False` or a new name. If you're the developer of the module 'boto_kinesis', please fix this.

# Fixed code
def _check_directory_win(
    name, win_owner, win_perms=None, win_deny_perms=None, win_inheritance=None
):
    """
    Check what changes need to be made on a directory
    """
    changes = {}

    if not os.path.isdir(name):
        changes = {"directory": "new"}
    else:
        # Check owner
        owner = salt.utils.win_dacl.get_owner(name)
        if not owner.lower() == win_owner.lower():
            changes["owner"] = win_owner

        # Check perms
        perms = salt.utils.win_dacl.get_permissions(name)

        # Verify Permissions
        if win_perms is not None:
            for user in win_perms:
                # Check that user exists:
                try:
                    salt.utils.win_dacl.get_name(user)
                except CommandExecutionError:
                    continue

                grant_perms = []
                # Check for permissions
                if isinstance(win_perms[user]["perms"], six.string_types):
                    if not salt.utils.win_dacl.has_permission(
                        name, user, win_perms[user]["perms"]
                    ):
                        grant_perms = win_perms[user]["perms"]
                else:
                    for perm in win_perms[user]["perms"]:
                        if not salt.utils.win_dacl.has_permission(
                            name, user, perm, exact=False
                        ):
                            grant_perms.append(win_perms[user]["perms"])
                if grant_perms:
                    if "grant_perms" not in changes:
                        changes["grant_perms"] = {}
                    if user not in changes["grant_perms"]:
                        changes["grant_perms"][user] = {}
                    changes["grant_perms"][user]["perms"] = grant_perms

                # Check Applies to
                if "applies_to" not in win_perms[user]:
                    applies_to = "this_folder_subfolders_files"
                else:
                    applies_to = win_perms[user]["applies_to"]

                if user in perms:
                    user = salt.utils.win_dacl.get_name(user)

                    # Get the proper applies_to text
                    at_flag = salt.utils.win_dacl.flags().ace_prop["file"][applies_to]
                    applies_to_text = salt.utils.win_dacl.flags().ace_prop["file"][
                        at_flag
                    ]

                    if "grant" in perms[user]:
                        if not perms[user]["grant"]["applies to"] == applies_to_text:
                            if "grant_perms" not in changes:
                                changes["grant_perms"] = {}
                            if user not in changes["grant_perms"]:
                                changes["grant_perms"][user] = {}
                            changes["grant_perms"][user]["applies_to"] = applies_to

        # Verify Deny Permissions
        if win_deny_perms is not None:
            for user in win_deny_perms:
                # Check that user exists:
                try:
                    salt.utils.win_dacl.get_name(user)
                except CommandExecutionError:
                    continue

                deny_perms = []
                # Check for permissions
                if isinstance(win_deny_perms[user]["perms"], six.string_types):
                    if not salt.utils.win_dacl.has_permission(
                        name, user, win_deny_perms[user]["perms"], "deny"
                    ):
                        deny_perms = win_deny_perms[user]["perms"]
                else:
                    for perm in win_deny_perms[user]["perms"]:
                        if not salt.utils.win_dacl.has_permission(
                            name, user, perm, "deny", exact=False
                        ):
                            deny_perms.append(win_deny_perms[user]["perms"])
                if deny_perms:
                    if "deny_perms" not in changes:
                        changes["deny_perms"] = {}
                    if user not in changes["deny_perms"]:
                        changes["deny_perms"][user] = {}
                    changes["deny_perms"][user]["perms"] = deny_perms

                # Check Applies to
                if "applies_to" not in win_deny_perms[user]:
                    applies_to = "this_folder_subfolders_files"
                else:
                    applies_to = win_deny_perms[user]["applies_to"]

                if user in perms:
                    user = salt.utils.win_dacl.get_name(user)

                    # Get the proper applies_to text
                    at_flag = salt.utils.win_dacl.flags().ace_prop["file"][applies_to]
                    applies_to_text = salt.utils.win_dacl.flags().ace_prop["file"][
                        at_flag
                    ]

                    if "deny" in perms[user]:
                        if not perms[user]["deny"]["applies to"] == applies_to_text:
                            if "deny_perms" not in changes:
                                changes["deny_perms"] = {}
                            if user not in changes["deny_perms"]:
                                changes["deny_perms"][user] = {}
                            changes["deny_perms"][user]["applies_to"] = applies_to

        # Check inheritance
        if win_inheritance is not None:
            if not win_inheritance == salt.utils.win_dacl.get_inheritance(name):
                changes["inheritance"] = win_inheritance

    if changes:
        return None, 'The directory "{0}" will be changed'.format(name), changes

    return True, "The directory {0} is in the correct state".format(name), changes