-
Notifications
You must be signed in to change notification settings - Fork 0
/
fourcat_install.py
159 lines (131 loc) · 5.36 KB
/
fourcat_install.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
4CAT Installation of Selenium gecko webdriver and firefox browser
"""
import subprocess
import argparse
import re
import sys
import shutil
from common.config_manager import config
if __name__ == "__main__":
cli = argparse.ArgumentParser()
cli.add_argument("--force", "-f", default=False,
help="Force installation of Firefox and Geckodriver even if already installed.",
action="store_true")
cli.add_argument("--component", "-c", default="backend",
help="Which component of 4CAT to migrate. Nothing is installed when set to 'frontend'") # Necessary to work with 4CAT migrate.py
cli.add_argument("--no-pip", "-p", default=False,
help="Run pip to install any python requirements.",
action="store_true")
args, extras = cli.parse_known_args()
def run_command(command, error_message):
"""
Convenence function to run subprocess and check result
"""
result = subprocess.run(command.split(" "), stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if result.returncode != 0:
print(error_message)
print(command)
print(result.stdout.decode("ascii"))
print(result.stderr.decode("ascii"))
exit(1)
return result
def pip_install():
"""
Install python requirements
"""
print("Installing python requirements")
interpreter = sys.executable
command = f"{interpreter} -m pip install -r requirements.txt"
run_command(command, "Error installing python requirements")
if args.component == "frontend":
# Frontend still needs packages though only to import modules successfully
if not args.no_pip:
try:
import selenium
except ImportError:
pip_install()
print("Installed required packages for extension.")
exit(0)
print("4CAT frontend component selected. No installation required.")
exit(0)
elif args.component not in ["backend", "both"]:
print("Invalid component selected. Exiting.")
exit(1)
# Check for Linux OS
if sys.platform != "linux":
print("This installation is only for Linux OS\nPlease download Firefox and Geckodriver manually.")
exit(1)
print(f"args: {args} and {args.no_pip}")
firefox_installed = False
geckodriver_installed = False
if not args.force:
# Check if Firefox and Geckodriver are already installed
print("Checking if Firefox and Geckodriver are already installed")
firefox_path = shutil.which("firefox")
if firefox_path is not None:
command = "firefox --version"
result = subprocess.run(command.split(" "), stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if result.returncode == 0:
print("Firefox is already installed")
firefox_installed = True
geckodriver_path = shutil.which("geckodriver")
if geckodriver_path is not None:
command = "geckodriver --version"
result = subprocess.run(command.split(" "), stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if result.returncode == 0:
print("Geckodriver is already installed")
geckodriver_installed = True
if firefox_installed and geckodriver_installed:
exit(0)
# Install additional packages
print("Ensuring required packages are installed")
PACKAGES = "wget bzip2 libgtk-3-0 libasound2 libdbus-glib-1-2 libx11-xcb1 libxtst6"
command = f"apt-get install --no-install-recommends -y {PACKAGES}"
run_command(command, "Error installing packages")
print(f"Installed packages: {PACKAGES}")
# Identify latest geckodriver
if not geckodriver_installed:
print("Identifying latest geckodriver")
command = "curl -i https://github.com/mozilla/geckodriver/releases/latest"
geckodriver_github_page = run_command(command, "Error identifying latest geckodriver (curl)")
match = re.search("v[0-9]+.[0-9]+.[0-9]+", str(geckodriver_github_page.stdout))
if match:
GECKODRIVER_VERSION = match.group()
else:
print("Error identifying latest geckodriver (regex)")
exit(1)
# Download and set up geckodriver
print(f'Installing geckodriver version {GECKODRIVER_VERSION}')
command = f"wget https://github.com/mozilla/geckodriver/releases/download/{GECKODRIVER_VERSION}/geckodriver-{GECKODRIVER_VERSION}-linux64.tar.gz"
run_command(command, "Error downloading geckodriver")
command = f"tar -zxf geckodriver-{GECKODRIVER_VERSION}-linux64.tar.gz -C /usr/local/bin"
run_command(command, "Error unziping geckodriver")
command = "chmod +x /usr/local/bin/geckodriver"
run_command(command, "Error changing ownership of geckodriver")
command = f"rm geckodriver-{GECKODRIVER_VERSION}-linux64.tar.gz"
run_command(command, "Error removing temp download files")
# Install latest firefox
if not firefox_installed:
print("Installing the latest version of Firefox")
FIREFOX_SETUP = "firefox-setup.tar.bz2"
command = "apt-get purge firefox"
run_command(command, "Error removing existing firefox")
command = f'wget -O {FIREFOX_SETUP} https://download.mozilla.org/?product=firefox-latest&os=linux64'
run_command(command, "Error downloading firefox")
command = f"tar xjf {FIREFOX_SETUP} -C /opt/"
run_command(command, "Error unzipping firefox")
command = "ln -sf /opt/firefox/firefox /usr/bin/firefox"
run_command(command, "Error creating symbolic link to firefox")
command = f"rm {FIREFOX_SETUP}"
run_command(command, "Error removing temp download files")
if not args.no_pip:
pip_install()
config.with_db()
config.set('selenium.selenium_executable_path', "/usr/local/bin/geckodriver")
config.set('selenium.browser', 'firefox')
print("Firefox and Geckodriver installation complete")
exit(0)