You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.6 KiB
Plaintext

#!/usr/bin/python3
import sys
import os
import hashlib
import pickle
from subprocess import run
tasks = []
class Task:
name = ""
ifchange = []
commands = []
def show(self):
print(self.name)
print(self.ifchange)
print(self.commands)
dragon_ascii = """
,,
`""*$b..
""*$o.
"$$o.
"*$$o.
"$$$o.
"$$$$bo... ..o:
"$$$$$$$$booocS$$$ .. ,.
". "*$$$$SP V$o..o$$. .$$$b
"$$o. .$$$$$o. ...A$$$$$$$$$$$$$$b
""bo. "*$$$$$$$$$$$$$$$$$$$$P*$$$$$$$$:
"$$. V$$$$$$$$$P"**""*"' VP * "l
"$$$o.4$$$$$$$$X
"*$$$$$$$$$$$$$AoA$o..oooooo.. .b
.X$$$$$$$$$$$P"" ""*oo,, ,$P
$$P""V$$$$$$$: . ""*****"
.*" A$$$$$$$$o.4; .
.oP"" "$$$$$$b. .$;
A$$$$$$$$$$P
" "$$$$$P"
$$P*"
mls .$"
"
"""
help_message = """
tasks :
A simple way to define tasks and execute them.
Usage: tasks file
"""
def print_help():
""" print help and usage """
print(dragon_ascii)
print(help_message)
def check_if_file(in_file, check_file):
""" Checks it the given file exists, else print error and exit """
if os.path.isfile(check_file):
return True
else:
print("In " + in_file + " : " + check_file + " is not a file")
exit(1)
def getsha1(filename):
""" Return the sha1 value of the given filename """
with open(filename, 'rb') as file_to_check:
data = file_to_check.read()
sha1_returned = hashlib.sha1(data).hexdigest()
return sha1_returned
def do_tasks(tasks_file):
""" Do the tasks stored in tasks list """
stored_entries = {}
if os.path.isfile("." + tasks_file + ".bin"):
stored_entries = pickle.load(open("." + tasks_file + ".bin", 'rb'))
for task in tasks:
redo_task = False
""" If change check """
for f in task.ifchange:
if stored_entries.get(f, "") != getsha1(f):
stored_entries[f] = getsha1(f)
redo_task = True
""" Update the stored_entries for this tasks file """
pickle.dump(stored_entries, open("." + tasks_file + ".bin", 'wb'))
if len(task.ifchange) < 1:
redo_task = True
if redo_task == False:
continue
""" Run Commands of the Task """
for c in task.commands:
print("+ " + c)
p = run(c.split())
if p.returncode != 0:
print(p)
print("Task Failed : " + task.name , file=sys.stderr )
exit(p.returncode)
def get_tasks(filename):
""" Take a tasks file and convert it to Task structures, stored in a global tasks list """
number_of_tasks = 0
# Possible sections : comment, ifchange, commands
current_section = ""
with open(filename, 'r') as f:
for line in f.readlines():
l = line.strip()
if len(l) > 0 and l[0] == '*':
number_of_tasks += 1
tasks.append(Task())
tasks[number_of_tasks - 1].name = l[1:].strip()
tasks[number_of_tasks - 1].ifchange = []
tasks[number_of_tasks - 1].commands = []
current_section = "comment"
continue
elif l == "#ifchange":
current_section = "ifchange"
continue
elif l == "#do":
current_section = "commands"
continue
if current_section == "ifchange":
for entry in l.split():
check_if_file(filename, entry)
tasks[number_of_tasks - 1].ifchange.append(entry)
elif current_section == "commands" and len(l) > 0:
tasks[number_of_tasks - 1].commands.append(l)
def main():
tasks_file = "main.tasks"
if len(sys.argv) > 1:
if os.path.isfile(sys.argv[1]):
tasks_file = sys.argv[1]
else:
print_help()
exit(1)
get_tasks(tasks_file)
print("<-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <->")
for task in tasks:
print("For task named " + task.name)
task.show()
print("<-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <-> <->")
do_tasks(tasks_file)
if __name__ == "__main__":
main()
pass