Get absolute path to resource, works for dev and for PyInstaller
509132
def base_uri(relative_path=''):
    
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        if 'pytest' in sys.modules:
            for arg in reversed(sys.argv):
                path = os.path.realpath(arg)

                if os.path.exists(path):
                    base_path = path if os.path.isdir(path) else os.path.dirname(path)
                    break
        else:
            base_path = os.path.dirname(os.path.realpath(sys.argv[0]))

    if not os.path.exists(base_path):
        raise ValueError('Path %s does not exist' % base_path)

    return 'file://%s' % os.path.join(base_path, relative_path)
1137737
def absolute_path(self, path, start):
        
        if posixpath.isabs(path):
            path = posixpath.join(staticfiles_storage.location, path)
        else:
            path = posixpath.join(start, path)
        return posixpath.normpath(path)
712759
def abspath(fpath):
    
    from os import path, getcwd, chdir
    original = getcwd()
    chdir(reporoot)
    result = path.abspath(path.expanduser(fpath))
    chdir(original)
    return result
65321
def get_path(*args, module=a99):
    

    p = os.path.abspath(os.path.join(os.path.split(module.__file__)[0], *args))
    return p
1158475
def find_executable(executable, path=None):
    
    if sys.platform != 'win32':
        return distutils.spawn.find_executable(executable, path)

    if path is None:
        path = os.environ['PATH']

    paths = path.split(os.pathsep)
    extensions = os.environ.get('PATHEXT', '.exe').split(os.pathsep)
    base, ext = os.path.splitext(executable)

    if not os.path.isfile(executable):
        for p in paths:
            for ext in extensions:
                f = os.path.join(p, base + ext)
                if os.path.isfile(f):
                    return f
        return None
    else:
        return executable
301488
def _absolute_path(path, relative_to=None):
    '''
    
    '''

    if path and os.path.isabs(path):
        return path
    if path and relative_to is not None:
        _abspath = os.path.join(relative_to, path)
        if os.path.isfile(_abspath):
            log.debug(
                'Relative path \'%s\' converted to existing absolute path '
                '\'%s\'', path, _abspath
            )
            return _abspath
    return path
1278378
def resource_path(relative):
    """"""
    if hasattr(sys, "_MEIPASS"):
        return os.path.join(sys._MEIPASS, relative)
    return os.path.join(relative)
484341
def getModulePath(project_path,module_name,verbose):
    ''''''
    if not module_name:
        return None
    sys.path.append(project_path)
    try:
        package = pkgutil.get_loader(module_name)
    except ImportError:
        if verbose:
            print("Parent module for "+module_name+" not found.")
        return None
    except:
        if verbose:
            print(module_name+" not loaded for bizarre reasons")
    try:
        if package: 
                if package.get_code(module_name):
                    filename = package.get_code(module_name).co_filename
                    return filename
                elif package.find_spec(module_name).has_location==False:
                    return None #built-in module such as itertools
                else:
                    pass #perhaps filename is in package.find_spec(module_name).origin?
                    pass #a good reference is https://www.python.org/dev/peps/pep-0302/
    except ImportError:
        if verbose:
            print("Code object unavailable for "+module_name)
        return None
    except AttributeError:
        if verbose:
            print(module_name+" is an ExtensionFileLoader object")
        return None
    except:
        if verbose:
            print(module_name+" not loaded for bizarre reasons")
        return None
    else:
        if verbose:
            print ("Module "+module_name+" not found.")
        return None
763580
def relative_resources(pathstring, failover='nifstd/resources'):
    

    if working_dir is None:
        return Path(failover, pathstring).resolve()
    else:
        return Path(devconfig.resources, pathstring).resolve().relative_to(working_dir.resolve())
273351
def _get_ericscript_path(self):
        
        es = utils.which(os.path.join(utils.get_bcbio_bin(), self.EXECUTABLE))
        return os.path.dirname(os.path.realpath(es))
1160693
def GetPythonLibraryDirectoryPath():
  """"""
  path = sysconfig.get_python_lib(True)
  _, _, path = path.rpartition(sysconfig.PREFIX)

  if path.startswith(os.sep):
    path = path[1:]

  return path
626336
def get_abs_template_path(template_name, directory, extension):
    
    # Get the relative path
    relative_path = join(directory, template_name)
    file_with_ext = template_name

    if extension:
        # If there is a default extension, but no file extension, then add it
        file_name, file_ext = splitext(file_with_ext)
        if not file_ext:
            file_with_ext = extsep.join(
                (file_name, extension.replace(extsep, '')))
            # Rebuild the relative path
            relative_path = join(directory, file_with_ext)

    return abspath(relative_path)
281264
def get_relative_file_path(absolute_file_path):
    '''
    
    '''
    global country_package_dir_path
    assert country_package_dir_path is not None
    relative_file_path = absolute_file_path[len(country_package_dir_path):]
    if relative_file_path.startswith('/'):
        relative_file_path = relative_file_path[1:]
    return relative_file_path
504548
def get_runtime_path(self, filename=None):
        """"""
        return self.get_path(prefix=settings.FLOW_EXECUTOR['RUNTIME_DIR'], filename=filename)
978124
def get_path(filename):
    
    path = abspath(filename) if os.path.isdir(filename) else dirname(abspath(filename))
    return path
73326
def get_include_path():
    
    f1 = os.path.basename(sys.argv[0]).lower()  # script filename
    f2 = os.path.basename(sys.executable).lower()  # Executable filename

    # If executable filename and script name are the same, we are
    if f1 == f2 or f2 == f1 + '.exe':  # under a "compiled" python binary
        result = os.path.dirname(os.path.realpath(sys.executable))
    else:
        result = os.path.dirname(os.path.realpath(__file__))

    return result
1358122
def _load_resources(self, abs_path=False):
        """"""
        from metapack.doc import MetapackDoc

        assert type(self.doc) == MetapackDoc

        for r in self.datafiles:

            # Special handling for SQL is probably a really bad idea. It should be handled as
            # a Rowgenerator.
            if r.term_is('root.sql'):

                if not r.value:
                    self.warn("No value for SQL URL for {} ".format(r.term))
                    continue

                try:
                    self._load_resource(r, abs_path)
                except Exception as e:
                    if r.props.get('ignoreerrors'):
                        self.warn(f"Ignoring errors for {r.name}: {str(e)}")
                        pass
                    else:
                        raise e

            else:

                if not r.url:
                    self.warn("No value for URL for {} ".format(r.term))
                    continue

                try:
                    if self._resource.exists(r):
                        self.prt("Resource '{}' exists, skipping".format(r.name))
                    continue
                except AttributeError:
                    pass

                self.prt("Reading resource {} from {} ".format(r.name, r.resolved_url))

                try:
                    if not r.headers:
                        raise PackageError("Resource {} does not have header. Have schemas been generated?"
                                            .format(r.name))
                except AttributeError:
                    raise PackageError("Resource '{}' of type {} does not have a headers property"
                                       .format(r.url, type(r)))

                try:
                    self._load_resource(r, abs_path)
                except Exception as e:
                    if r.props.get('ignoreerrors'):
                        self.warn(f"Ignoring errors for {r.name}: {str(e)}")
                        pass
                    else:
                        raise e