-
Notifications
You must be signed in to change notification settings - Fork 14
/
embedded_server_startup.py
74 lines (53 loc) · 1.78 KB
/
embedded_server_startup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys
import os
import subprocess
sys.path.append(os.path.abspath(os.getcwd() + '/tools'))
import embedded_server_utility as util
#list
def pip_list():
import pkg_resources
return [p.project_name for p in pkg_resources.working_set]
#read requirements file in server script path
def requirements():
result = {}
#open requirements file
with open(util.script_root('requirements.txt')) as f:
lines = f.read().splitlines()
for line in lines:
nameVer = line.split('==')
result[nameVer[0]] = nameVer[1]
return result
#get a list of missing packages
def pip_missing():
installed = pip_list()
required = requirements()
missing = {}
for package in required:
if package not in installed:
missing[package] = required[package]
return missing
def install_packages(packages):
import pip
for package in packages:
pip.main(["install", package + '==' + packages[package]])
#fullcommand = python_root('python.exe -m pip')
print('packages installed')
#install_result = subprocess.check_output(fullcommand, shell=True, stderr=subprocess.STDOUT)
def install_missing_packages():
missing = pip_missing()
if len(missing) > 0:
install_packages(missing)
print('requirements.txt installed')
else:
print('requirements.txt already met')
def add_paths():
sys.path.append(util.python_root('/Lib/site-packages'))
sys.path.append(util.python_root('/Scripts'))
def startup():
add_paths()
#check for pip, we need to use a subprocess loop for this due to import complexity
fullcommand = util.python_root('python.exe') + ' "' + util.script_root('/tools/embedded_server_pip_handler.py') + '"'
output = subprocess.check_output(fullcommand, shell=True, stderr=subprocess.STDOUT, env=util.copy_environment()).decode('UTF-8')
util.print_output(output)
install_missing_packages()
startup()