'Update the alias table with all executable files in $PATH.
rehashx explicitly checks that every entry in $PATH is a file
with execute access (os.X_OK).
Under Windows, it checks executability as a match against a
\'|\'-separated string of extensions, stored in the IPython config
variable win_exec_ext.  This defaults to \'exe|com|bat\'.
This function also resets the root module cache of module completer,
used on slow filesystems.'

@line_magic
def rehashx(self, parameter_s=''):

    from IPython.core.alias import InvalidAliasError
    del self.shell.db['rootmodules_cache']
    path = [os.path.abspath(os.path.expanduser(p)) for p in os.environ.get('PATH', '').split(os.pathsep)]
    syscmdlist = []
    if (os.name == 'posix'):
        isexec = (lambda fname: (os.path.isfile(fname) and os.access(fname, os.X_OK)))
    else:
        try:
            winext = os.environ['pathext'].replace(';', '|').replace('.', '')
        except KeyError:
            winext = 'exe|com|bat|py'
        if ('py' not in winext):
            winext += '|py'
        execre = re.compile(('(.*)\\.(%s)$' % winext), re.IGNORECASE)
        isexec = (lambda fname: (os.path.isfile(fname) and execre.match(fname)))
    savedir = os.getcwd()
    try:
        if (os.name == 'posix'):
            for pdir in path:
                try:
                    os.chdir(pdir)
                    dirlist = os.listdir(pdir)
                except OSError:
                    continue
                for ff in dirlist:
                    if isexec(ff):
                        try:
                            if (not self.shell.alias_manager.is_alias(ff)):
                                self.shell.alias_manager.define_alias(ff.replace('.', ''), ff)
                        except InvalidAliasError:
                            pass
                        else:
                            syscmdlist.append(ff)
        else:
            no_alias = Alias.blacklist
            for pdir in path:
                try:
                    os.chdir(pdir)
                    dirlist = os.listdir(pdir)
                except OSError:
                    continue
                for ff in dirlist:
                    (base, ext) = os.path.splitext(ff)
                    if (isexec(ff) and (base.lower() not in no_alias)):
                        if (ext.lower() == '.exe'):
                            ff = base
                            try:
                                self.shell.alias_manager.define_alias(base.lower().replace('.', ''), ff)
                            except InvalidAliasError:
                                pass
                            syscmdlist.append(ff)
        self.shell.db['syscmdlist'] = syscmdlist
    finally:
        os.chdir(savedir)
