# Buggy code
def mod_data(opts):
    """
    Generate the module arguments for the shim data
    """
    # TODO, change out for a fileserver backend
    sync_refs = [
        "modules",
        "states",
        "grains",
        "renderers",
        "returners",
    ]
    ret = {}
    for env in opts["file_roots"]:
        for path in opts["file_roots"][env]:
            for ref in sync_refs:
                mod_str = ""
                pl_dir = os.path.join(path, "_{0}".format(ref))
                if os.path.isdir(pl_dir):
                    for fn_ in os.listdir(pl_dir):
                        mod_path = os.path.join(pl_dir, fn_)
                        with open(mod_path) as fp_:
                            code_str = fp_.read().encode("base64")
                        mod_str += "{0}|{1},".format(fn_, code_str)
                mod_str = mod_str.rstrip(",")
                ret[ref] = mod_str
    return ret


# Traceback (IOError)
% sudo salt-ssh silver test.ping                                                                                                                                   1   2014.7
[sudo] password for mp:
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
IOError: [Errno 21] Is a directory: '/srv/salt/_modules/a'
Traceback (most recent call last):
File "/home/mp/Devel/salt/scripts/salt-ssh", line 10, in <module>
salt_ssh()
File "/home/mp/Devel/salt/salt/scripts.py", line 166, in salt_ssh
client.run()
File "/home/mp/Devel/salt/salt/cli/__init__.py", line 474, in run
ssh = salt.client.ssh.SSH(self.config)
File "/home/mp/Devel/salt/salt/client/ssh/__init__.py", line 227, in __init__
self.mods = mod_data(self.opts)
File "/home/mp/Devel/salt/salt/client/ssh/__init__.py", line 1021, in mod_data
with open(mod_path) as fp_:
IOError: [Errno 21] Is a directory: '/srv/salt/_modules/a'
Traceback (most recent call last):
File "/home/mp/Devel/salt/scripts/salt-ssh", line 10, in <module>
salt_ssh()
File "/home/mp/Devel/salt/salt/scripts.py", line 166, in salt_ssh
client.run()
File "/home/mp/Devel/salt/salt/cli/__init__.py", line 474, in run
ssh = salt.client.ssh.SSH(self.config)
File "/home/mp/Devel/salt/salt/client/ssh/__init__.py", line 227, in __init__
self.mods = mod_data(self.opts)
File "/home/mp/Devel/salt/salt/client/ssh/__init__.py", line 1021, in mod_data
with open(mod_path) as fp_:
IOError: [Errno 21] Is a directory: '/srv/salt/_modules/a'

# Fixed code
def mod_data(opts):
    """
    Generate the module arguments for the shim data
    """
    # TODO, change out for a fileserver backend
    sync_refs = [
        "modules",
        "states",
        "grains",
        "renderers",
        "returners",
    ]
    ret = {}
    for env in opts["file_roots"]:
        for path in opts["file_roots"][env]:
            for ref in sync_refs:
                mod_str = ""
                pl_dir = os.path.join(path, "_{0}".format(ref))
                if os.path.isdir(pl_dir):
                    for fn_ in os.listdir(pl_dir):
                        if not os.path.isfile(fn_):
                            continue
                        mod_path = os.path.join(pl_dir, fn_)
                        with open(mod_path) as fp_:
                            code_str = fp_.read().encode("base64")
                        mod_str += "{0}|{1},".format(fn_, code_str)
                mod_str = mod_str.rstrip(",")
                ret[ref] = mod_str
    return ret