-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskQueue.py
92 lines (77 loc) · 2.26 KB
/
taskQueue.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
'''
Author: Chad Clough
Created: 4/24/2015
'''
import sys
import uuid
from subprocess import call
class TaskQueue:
def __init__(self):
'''
Creates an empty TaskQueue
'''
self._tasks = []
def push(self, task):
'''
Pushes a Task into the queue
'''
self._tasks.append(task)
def pop(self):
'''
Removes and returns the first Task in the queue
'''
return self._tasks.pop()
def peek_all(self):
'''
Returns a list of al of the Tasks in the queue
'''
return self._tasks
def peek_next(self):
'''
Returns the next Task in the queue; None if the
queue is empty
'''
if (not self.is_empty()):
return self._tasks[0]
else:
return None
def count(self):
'''
Returns the number of tasks currently in the queue
'''
return len(self._tasks)
def is_empty(self):
'''
Returns True if there are no Tasks currently in the
queue; False otherwise
'''
return len(self._tasks) == 0
class Task:
def __init__(self, description, command):
'''
Creates a Task with the given description and command
and generates a GUID for the Task
'''
self.description = description
self.command = command
self.GUID = uuid.uuid4()
def execute(self):
'''
Executes the command
**WARNING SECURITY HAZARD**
This code executes shell commands for unsanitized input
'''
try:
call(self.command, shell=True)
except:
traceback.print_exc(file=sys.stdout)
if __name__ == '__main__':
simple_tasks = TaskQueue()
simple_tasks.push(Task(description='My first task',
command='ping -c 3 google-public-dns-a.google.com'))
simple_tasks.push(Task(description='My second task',
command='ping -c 3 google-public-dns-a.google.com'))
while simple_tasks.peek_next():
t = simple_tasks.pop()
sys.stdout.write('Running task %s\n' % t.GUID)
t.execute()