forked from Emerah/Prototype-NI-MMK3-HID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_util.py
45 lines (40 loc) · 1.67 KB
/
sys_util.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
# -*- coding: utf-8 -*-
# @Project : MMK3-HID-Control
# @File : sys_util.py
# @Date : 2020-04-30 15:17:00
# @Author : Emerah (MaXaR) - [email protected]
# @Link : https://github.com/Emerah
# @Version : 1.0.0
import os
import re
import subprocess
import time
def process_is_currently_active(process):
s = subprocess.Popen(["ps", "axw"], stdout=subprocess.PIPE)
for i in s.stdout:
if re.search(process, i):
return True
return False
def suspend_ni_backend_support():
processes = [b'NIHardwareAgent', b'NIHostIntegrationAgent', b'NTKDaemon']
request_presmission_message = 'for this to work, we must suspend natinve instruments services. do you agree? (y/n)?: '
response = input(request_presmission_message)
if response.lower() == 'yes' or response == 'y':
for process in processes:
if process_is_currently_active(process):
proc = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
result, err = proc.communicate()
for line in result.splitlines():
if process in line:
pid = int(line.split(None, 1)[0])
res = os.system('kill -9 ' + str(pid))
if res == 0:
print('{} has been suspended'.format(str(process)))
else:
print('{} was not running'.format(str(process)))
elif response.lower() == 'no' or response.lower() == 'n':
print('you chose no .. unfortunately we can not access the device now.')
else:
print('this did not make sense. answer can be y or n ?')
print('exiting with grace')
time.sleep(6)