-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomation.py
95 lines (81 loc) · 2.89 KB
/
automation.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
import pyautogui
import pyperclip
import subprocess
import psutil
import time
def execute_cmd(cmd:str, shell:bool=True)->int:
'''
@param cmd(str) : cmd is a string which you want to execute with shell
@param shell(bool) : shell is set to True to execute all commands in shell without any restrictions
return 'returncode'(int) : 0 for successful.
any other num for unsuccessful command or if any error is thrown by the command.
'''
print("Executing cmd '%s'" %(cmd))
returncode = -1
### safety code ###
restricted_cmds = ('rm',) # add your restricted cmds
words = cmd.split(' ')
for word in words :
if word in restricted_cmds :
print("'%s' word is restricted. Hence can't execute the following cmd %s" %(word,cmd))
return returncode
#######################
cmdinstance = subprocess.run(cmd,shell=shell)
returncode = cmdinstance.returncode
if returncode == 0:
print("Cmd successful : '%s'" %(cmd))
else :
print("Unsuccessful cmd : '%s'" %(cmd))
return returncode
def show_desktop():
pyautogui.hotkey('winleft','d')
def get_all_processes():
processes = [p.info for p in psutil.process_iter(attrs=['pid','name'])]
return processes
def print_all_process():
processes = [p.info for p in psutil.process_iter(attrs=['pid','name'])]
for process in processes:
print(process)
def close_app(name):
print('closing application %s' %(name))
processes = get_all_processes()
terminate_processes = [p for p in processes if name in p.info['name']]
pid_to_kill = list()
for process in terminate_processes:
pid_to_kill.append(process['pid'])
print(pid_to_kill)
########################
for p in pid_to_kill:
process = psutil.process(p)
print(process)
process.terminate()
def open_app(app_path):
# if windows replace '\' with '\\'
print('Starting app ',app_path)
cmd = 'start ' + app_path
subprocess.call(cmd,shell=True)
print(app_path,' is opened')
def focus_on_app(window_title):
winlist = pyautogui.getAllWindows()
for item in winlist:
if window_title in item.title:
if not item.maximize():
item.maximize()
if not item.isActive:
item.activate()
def locate(image_list):
for image in image_list:
print("searching for ",image)
for i in pyautogui.locateAllOnScreen(image):
pyautogui.moveTo(i.left + i.width/2,i.top + i.height/2)
if len(i)!=0:
print("found",image)
x,y = pyautogui.position()
print('mouse position is ',x,' ',y)
return
def main():
cmd = 'dir'
x = execute_cmd(cmd)
print(x)
if __name__ == "__main__":
main()