mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-14 02:26:53 -07:00
Add feature to make libs importable
This commit is contained in:
parent
b115ecc1fe
commit
43ffbc7c34
4 changed files with 119 additions and 19 deletions
|
@ -11,26 +11,13 @@ import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import libs.autoload
|
||||||
|
import libs.util
|
||||||
|
|
||||||
# init libs
|
if not libs.autoload.completed:
|
||||||
PROGRAM_DIR = os.path.dirname(os.path.normpath(os.path.abspath(os.path.join(__file__, os.pardir))))
|
sys.exit('Could not load vendored libraries.')
|
||||||
|
|
||||||
LIBS_DIR = os.path.join(PROGRAM_DIR, 'libs')
|
PROGRAM_DIR = libs.util.module_root()
|
||||||
sys.path.insert(0, LIBS_DIR)
|
|
||||||
|
|
||||||
LIBS_DIR_COMMON = os.path.join(LIBS_DIR, 'common')
|
|
||||||
sys.path.insert(0, LIBS_DIR_COMMON)
|
|
||||||
|
|
||||||
LIBS_DIR_CUSTOM = os.path.join(LIBS_DIR, 'custom')
|
|
||||||
sys.path.insert(0, LIBS_DIR_CUSTOM)
|
|
||||||
|
|
||||||
if sys.version_info[0] == 2:
|
|
||||||
LIBS_DIR_PY2 = os.path.join(LIBS_DIR, 'py2')
|
|
||||||
sys.path.insert(0, LIBS_DIR_PY2)
|
|
||||||
|
|
||||||
if sys.platform.startswith('win32') or (os.name == 'nt'):
|
|
||||||
LIBS_DIR_WIN = os.path.join(LIBS_DIR, 'win')
|
|
||||||
sys.path.insert(0, LIBS_DIR_WIN)
|
|
||||||
|
|
||||||
# init preliminaries
|
# init preliminaries
|
||||||
SYS_ARGV = sys.argv[1:]
|
SYS_ARGV = sys.argv[1:]
|
||||||
|
@ -43,7 +30,7 @@ CONFIG_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg')
|
||||||
CONFIG_SPEC_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg.spec')
|
CONFIG_SPEC_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMedia.cfg.spec')
|
||||||
CONFIG_MOVIE_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMovie.cfg')
|
CONFIG_MOVIE_FILE = os.path.join(PROGRAM_DIR, 'autoProcessMovie.cfg')
|
||||||
CONFIG_TV_FILE = os.path.join(PROGRAM_DIR, 'autoProcessTv.cfg')
|
CONFIG_TV_FILE = os.path.join(PROGRAM_DIR, 'autoProcessTv.cfg')
|
||||||
TEST_FILE = os.path.join(os.path.join(PROGRAM_DIR, 'tests'), 'test.mp4')
|
TEST_FILE = os.path.join(PROGRAM_DIR, 'tests', 'test.mp4')
|
||||||
MYAPP = None
|
MYAPP = None
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
47
libs/__init__.py
Normal file
47
libs/__init__.py
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import libs.util
|
||||||
|
|
||||||
|
ROOT_DIR = libs.util.module_root()
|
||||||
|
LIB_DIR = os.path.join(ROOT_DIR, 'libs')
|
||||||
|
|
||||||
|
COMMON = 'common'
|
||||||
|
CUSTOM = 'custom'
|
||||||
|
PY2 = 'py2'
|
||||||
|
WIN = 'win'
|
||||||
|
|
||||||
|
LOADED = {}
|
||||||
|
MANDATORY = {
|
||||||
|
COMMON,
|
||||||
|
CUSTOM,
|
||||||
|
}
|
||||||
|
DIRECTORY = {
|
||||||
|
lib: os.path.join(LIB_DIR, lib)
|
||||||
|
for lib in [COMMON, CUSTOM, PY2, WIN]
|
||||||
|
}
|
||||||
|
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
MANDATORY.add(WIN)
|
||||||
|
|
||||||
|
if sys.version_info < (3, ):
|
||||||
|
MANDATORY.add(PY2)
|
||||||
|
|
||||||
|
|
||||||
|
def add_libs(name):
|
||||||
|
if name in MANDATORY and name not in LOADED:
|
||||||
|
path = libs.util.add_path(DIRECTORY[name])
|
||||||
|
if path:
|
||||||
|
LOADED[name] = path
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def add_all_libs():
|
||||||
|
for lib in MANDATORY:
|
||||||
|
add_libs(lib)
|
||||||
|
return is_finished()
|
||||||
|
|
||||||
|
|
||||||
|
def is_finished():
|
||||||
|
return MANDATORY.issubset(LOADED.keys())
|
6
libs/autoload.py
Normal file
6
libs/autoload.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
import libs
|
||||||
|
|
||||||
|
__all__ = ['completed']
|
||||||
|
|
||||||
|
completed = libs.add_all_libs()
|
60
libs/util.py
Normal file
60
libs/util.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'module_root',
|
||||||
|
'add_path',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def module_root(module=__file__):
|
||||||
|
try:
|
||||||
|
path = module.__file__
|
||||||
|
except AttributeError:
|
||||||
|
path = module
|
||||||
|
directory = os.path.dirname(path)
|
||||||
|
parent = os.path.join(directory, os.pardir)
|
||||||
|
absolute = os.path.abspath(parent)
|
||||||
|
normalized = os.path.normpath(absolute)
|
||||||
|
return normalized
|
||||||
|
|
||||||
|
|
||||||
|
def add_path(path, index=0):
|
||||||
|
sys.path.insert(index, path)
|
||||||
|
try:
|
||||||
|
sys.path.index(path)
|
||||||
|
except ValueError:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def install_requirements(
|
||||||
|
requirements,
|
||||||
|
upgrade=True,
|
||||||
|
path=None,
|
||||||
|
file=False,
|
||||||
|
executable=sys.executable,
|
||||||
|
):
|
||||||
|
|
||||||
|
args = [
|
||||||
|
executable,
|
||||||
|
'-m',
|
||||||
|
'pip',
|
||||||
|
'install',
|
||||||
|
]
|
||||||
|
|
||||||
|
if file:
|
||||||
|
args.append('-r')
|
||||||
|
args.append(requirements)
|
||||||
|
|
||||||
|
if upgrade:
|
||||||
|
args.append('--upgrade')
|
||||||
|
|
||||||
|
if path is not None:
|
||||||
|
args.append('--target')
|
||||||
|
args.append(path)
|
||||||
|
|
||||||
|
subprocess.call(args)
|
Loading…
Add table
Add a link
Reference in a new issue