Passed
Push — master ( 0fd795...73442f )
by Emmanuel
14:19
created

package_utils.get_file()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""Gives useful information about the current virtualenv, files locations if
2
stakkr is installed as a package or directly cloned"""
3
4 1
import os
5 1
import sys
6 1
from distutils.sysconfig import get_config_vars, get_python_lib
7
8
9 1
def get_venv_basedir():
10
    """Returns the base directory of the virtualenv, useful to read configuration and plugins"""
11
12 1
    exec_prefix = get_config_vars()['exec_prefix']
13 1
    has_real_prefix = hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)
14 1
    if has_real_prefix is False or (hasattr(sys, 'real_prefix') and exec_prefix.startswith(sys.real_prefix)):
15
        raise EnvironmentError('You must be in a virtual environment')
16
17 1
    return os.path.abspath(get_config_vars()['exec_prefix'] + '/../')
18
19
20 1
def get_dir(dirname: str):
21
    """Detects if stakkr is a package or a clone and gives the right path for a directory"""
22
23 1
    staticdir = os.path.dirname(os.path.realpath(__file__)) + '/' + dirname
24 1
    if os.path.isdir(staticdir) is True:
25 1
        return staticdir
26
27
    return get_python_lib() + '/stakkr/' + dirname
28
29
30 1
def get_file(dirname: str, filename: str):
31
    """Detects if stakkr is a package or a clone and gives the right path for a file"""
32
33
    return get_dir(dirname) + '/' + filename.lstrip('/')
34