# Buggy code
def win_verify_env(dirs, permissive=False, pki_dir="", skip_extra=False):
    """
    Verify that the named directories are in place and that the environment
    can shake the salt
    """
    import salt.utils.win_functions
    import salt.utils.win_dacl

    # Get the root path directory where salt is installed
    path = dirs[0]
    while os.path.basename(path) not in ["salt", "salt-tests-tmpdir"]:
        path, base = os.path.split(path)

    # Create the root path directory if missing
    if not os.path.isdir(path):
        os.makedirs(path)

    # Set permissions to the root path directory
    current_user = salt.utils.win_functions.get_current_user()
    if salt.utils.win_functions.is_admin(current_user):
        try:
            # Make the Administrators group owner
            # Use the SID to be locale agnostic
            salt.utils.win_dacl.set_owner(path, "S-1-5-32-544")

        except CommandExecutionError:
            msg = 'Unable to securely set the owner of "{0}".'.format(path)
            if is_console_configured():
                log.critical(msg)
            else:
                sys.stderr.write("CRITICAL: {0}\n".format(msg))

        if not permissive:
            try:
                # Get a clean dacl by not passing an obj_name
                dacl = salt.utils.win_dacl.Dacl()

                # Add aces to the dacl, use the GUID (locale non-specific)
                # Administrators Group
                dacl.add_ace(
                    "S-1-5-32-544",
                    "grant",
                    "full_control",
                    "this_folder_subfolders_files",
                )
                # System
                dacl.add_ace(
                    "S-1-5-18", "grant", "full_control", "this_folder_subfolders_files"
                )
                # Owner
                dacl.add_ace(
                    "S-1-3-4", "grant", "full_control", "this_folder_subfolders_files"
                )

                # Save the dacl to the object
                dacl.save(path, True)

            except CommandExecutionError:
                msg = 'Unable to securely set the permissions of "{0}".'.format(path)
                if is_console_configured():
                    log.critical(msg)
                else:
                    sys.stderr.write("CRITICAL: {0}\n".format(msg))

    # Create the directories
    for dir_ in dirs:
        if not dir_:
            continue
        if not os.path.isdir(dir_):
            try:
                os.makedirs(dir_)
            except OSError as err:
                msg = 'Failed to create directory path "{0}" - {1}\n'
                sys.stderr.write(msg.format(dir_, err))
                sys.exit(err.errno)

        # The PKI dir gets its own permissions
        if dir_ == pki_dir:
            try:
                # Make Administrators group the owner
                salt.utils.win_dacl.set_owner(path, "S-1-5-32-544")

                # Give Admins, System and Owner permissions
                # Get a clean dacl by not passing an obj_name
                dacl = salt.utils.win_dacl.Dacl()

                # Add aces to the dacl, use the GUID (locale non-specific)
                # Administrators Group
                dacl.add_ace(
                    "S-1-5-32-544",
                    "grant",
                    "full_control",
                    "this_folder_subfolders_files",
                )
                # System
                dacl.add_ace(
                    "S-1-5-18", "grant", "full_control", "this_folder_subfolders_files"
                )
                # Owner
                dacl.add_ace(
                    "S-1-3-4", "grant", "full_control", "this_folder_subfolders_files"
                )

                # Save the dacl to the object
                dacl.save(dir_, True)

            except CommandExecutionError:
                msg = 'Unable to securely set the permissions of "{0}".'
                msg = msg.format(dir_)
                if is_console_configured():
                    log.critical(msg)
                else:
                    sys.stderr.write("CRITICAL: {0}\n".format(msg))

    if skip_extra is False:
        # Run the extra verification checks
        zmq_version()


# 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 win_verify_env(dirs, permissive=False, pki_dir="", skip_extra=False):
    """
    Verify that the named directories are in place and that the environment
    can shake the salt
    """
    import salt.utils.win_functions
    import salt.utils.win_dacl

    # Get the root path directory where salt is installed
    path = dirs[0]
    while os.path.basename(path) not in ["salt", "salt-tests-tmpdir"]:
        path, base = os.path.split(path)

    # Create the root path directory if missing
    if not os.path.isdir(path):
        os.makedirs(path)

    # Set permissions to the root path directory
    current_user = salt.utils.win_functions.get_current_user()
    if salt.utils.win_functions.is_admin(current_user):
        try:
            # Make the Administrators group owner
            # Use the SID to be locale agnostic
            salt.utils.win_dacl.set_owner(path, "S-1-5-32-544")

        except CommandExecutionError:
            msg = 'Unable to securely set the owner of "{0}".'.format(path)
            if is_console_configured():
                log.critical(msg)
            else:
                sys.stderr.write("CRITICAL: {0}\n".format(msg))

        if not permissive:
            try:
                # Get a clean dacl by not passing an obj_name
                dacl = salt.utils.win_dacl.dacl()

                # Add aces to the dacl, use the GUID (locale non-specific)
                # Administrators Group
                dacl.add_ace(
                    "S-1-5-32-544",
                    "grant",
                    "full_control",
                    "this_folder_subfolders_files",
                )
                # System
                dacl.add_ace(
                    "S-1-5-18", "grant", "full_control", "this_folder_subfolders_files"
                )
                # Owner
                dacl.add_ace(
                    "S-1-3-4", "grant", "full_control", "this_folder_subfolders_files"
                )

                # Save the dacl to the object
                dacl.save(path, True)

            except CommandExecutionError:
                msg = 'Unable to securely set the permissions of "{0}".'.format(path)
                if is_console_configured():
                    log.critical(msg)
                else:
                    sys.stderr.write("CRITICAL: {0}\n".format(msg))

    # Create the directories
    for dir_ in dirs:
        if not dir_:
            continue
        if not os.path.isdir(dir_):
            try:
                os.makedirs(dir_)
            except OSError as err:
                msg = 'Failed to create directory path "{0}" - {1}\n'
                sys.stderr.write(msg.format(dir_, err))
                sys.exit(err.errno)

        # The PKI dir gets its own permissions
        if dir_ == pki_dir:
            try:
                # Make Administrators group the owner
                salt.utils.win_dacl.set_owner(path, "S-1-5-32-544")

                # Give Admins, System and Owner permissions
                # Get a clean dacl by not passing an obj_name
                dacl = salt.utils.win_dacl.dacl()

                # Add aces to the dacl, use the GUID (locale non-specific)
                # Administrators Group
                dacl.add_ace(
                    "S-1-5-32-544",
                    "grant",
                    "full_control",
                    "this_folder_subfolders_files",
                )
                # System
                dacl.add_ace(
                    "S-1-5-18", "grant", "full_control", "this_folder_subfolders_files"
                )
                # Owner
                dacl.add_ace(
                    "S-1-3-4", "grant", "full_control", "this_folder_subfolders_files"
                )

                # Save the dacl to the object
                dacl.save(dir_, True)

            except CommandExecutionError:
                msg = 'Unable to securely set the permissions of "{0}".'
                msg = msg.format(dir_)
                if is_console_configured():
                    log.critical(msg)
                else:
                    sys.stderr.write("CRITICAL: {0}\n".format(msg))

    if skip_extra is False:
        # Run the extra verification checks
        zmq_version()