forked from SOM-Research/busfactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.py
105 lines (75 loc) · 1.97 KB
/
tool.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
__author__ = 'atlanmod'
import subprocess
import os
import signal
import sys
import time
import psutil
from selenium import webdriver
from bus_factor_gui import BusFactor
WEB_BROWSER_PATH = 'C:\chromedriver_win32\chromedriver.exe'
pro = None
gui = None
#if this value is false, the script will prompt the content of the data folder in a HTML page
execute_bus_factor = False
def is_process_running(pid):
found = False
for p in psutil.process_iter():
try:
if p.pid == pid:
found = True
break
except psutil.Error:
pass
return found
def start_server():
global pro
print "Starting server..."
cmd = 'python -m SimpleHTTPServer'
pro = subprocess.Popen(cmd, shell=True)
def close_browser(driver):
driver.close()
def start_gui():
global gui
gui = subprocess.Popen([sys.executable, "bus_factor_gui.py"] + ["-n", "True"])
def open_browser():
driver = webdriver.Chrome(executable_path=WEB_BROWSER_PATH)
driver.get("http://localhost:8000/index.html")
driver.refresh()
return driver
def shutdown_server():
print "Shutting down server..."
os.kill(pro.pid, signal.SIGTERM)
def delete_notification():
if os.path.exists(BusFactor.NOTIFICATION):
os.remove(BusFactor.NOTIFICATION)
def browser_is_open(driver):
open = True
try:
driver.current_url
except:
open = False
return open
def notified():
flag = False
if os.path.exists(BusFactor.NOTIFICATION):
flag = True
delete_notification()
return flag
def main():
delete_notification()
if execute_bus_factor:
start_gui()
while is_process_running(gui.pid):
time.sleep(10)
if notified():
break
start_server()
driver = open_browser()
open = True
while open:
time.sleep(1)
open = browser_is_open(driver)
shutdown_server()
if __name__ == "__main__":
main()