annotate toolfactory/toolwatcher.py @ 6:efefe43f23c8 draft default tip

Uploaded
author fubar
date Fri, 30 Apr 2021 02:10:32 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
1 #!/usr/bin/python
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
2 from datetime import datetime, timedelta
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
3 from io import BytesIO as BIO
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
4 import logging
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
5 import os
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
6 import subprocess
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
7 import tarfile
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
8 import time
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
9 from watchdog.observers import Observer
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
10 from watchdog.events import FileSystemEventHandler
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
11 from watchdog.events import PatternMatchingEventHandler
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
12
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
13 class ToolHandler(PatternMatchingEventHandler):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
14
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
15 def __init__(self, watchme):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
16 PatternMatchingEventHandler.__init__(self, patterns=['*.xml'],
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
17 ignore_directories=False, case_sensitive=False)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
18 self.last_modified = datetime.now()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
19 self.tool_dir = watchme
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
20 self.work_dir = os.getcwd()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
21 self.galaxy_root = os.path.split(watchme)[0]
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
22 logging.info(self.galaxy_root)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
23 self.tar_dir = os.path.join(self.galaxy_root, 'tooltardir')
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
24 if not os.path.exists(self.tar_dir):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
25 os.mkdir(self.tar_dir)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
26
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
27 def on_created(self, event):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
28 self.on_modified(event)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
29
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
30 def on_modified(self, event):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
31 if datetime.now() - self.last_modified < timedelta(seconds=1):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
32 return
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
33 else:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
34 if os.path.exists(event.src_path):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
35 self.last_modified = datetime.now()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
36 logging.info(f"{event.src_path} was {event.event_type}")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
37 p = self.planemo_test(event.src_path)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
38 if p:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
39 if p.returncode == 0:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
40 newtarpath = self.makeToolTar(event.src_path)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
41 logging.info('### Tested toolshed tarball %s written' % newtarpath)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
42 else:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
43 logging.debug('### planemo stdout:')
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
44 logging.debug(p.stdout)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
45 logging.debug('### planemo stderr:')
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
46 logging.debug(p.stderr)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
47 logging.info('### Planemo call return code =' % p.returncode)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
48 else:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
49 logging.info('Directory %s deleted' % event.src_path)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
50
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
51 def planemo_test(self, xml_path):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
52 toolpath, toolfile = os.path.split(xml_path)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
53 dirlist = os.listdir(toolpath)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
54 toolname = os.path.basename(toolpath)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
55 logging.info('### test dirlist %s, path %s toolname %s' % (dirlist, xml_path, toolname))
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
56 xmls = [x for x in dirlist if os.path.splitext(x)[1] == '.xml']
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
57 if not len(xmls) > 0:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
58 logging.warning('Found no xml files after change to %s' % xml_path)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
59 return None
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
60 tool_test_output = os.path.join(toolpath, f"{toolname}_planemo_test_report.html")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
61 cll = [
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
62 "planemo",
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
63 "test",
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
64 "--test_output",
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
65 tool_test_output,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
66 "--galaxy_root",
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
67 self.galaxy_root,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
68 "--update_test_data",
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
69 xml_path,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
70 ]
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
71 logging.info('### calling %s' % ' '.join(cll))
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
72 p = subprocess.run(
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
73 cll,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
74 cwd = toolpath,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
75 shell=False,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
76 capture_output=True,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
77 encoding='utf8',
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
78 )
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
79 return p
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
80
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
81 def makeToolTar(self, xml_path):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
82 """move outputs into test-data and prepare the tarball"""
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
83 excludeme = "_planemo_test_report.html"
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
84
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
85 def exclude_function(tarinfo):
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
86 filename = tarinfo.name
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
87 return None if filename.endswith(excludeme) else tarinfo
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
88
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
89 tooldir, xml_file = os.path.split(xml_path)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
90 os.chdir(self.tool_dir)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
91 toolname = os.path.splitext(xml_file)[0]
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
92 newtarpath = os.path.join(self.tar_dir, '%s_toolshed.gz' % toolname)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
93 tf = tarfile.open(newtarpath, "w:gz")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
94 tf.add(
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
95 name=toolname,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
96 arcname=toolname,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
97 filter=exclude_function,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
98 )
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
99 tf.close()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
100 os.chdir(self.work_dir)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
101 return newtarpath
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
102
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
103
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
104 if __name__ == "__main__":
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
105 watchme = '/home/ross/gal21/tools'
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
106 logging.basicConfig(level=logging.INFO,
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
107 #filename = os.path.join(watchme,"toolwatcher.log")
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
108 #filemode = "w",
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
109 format='%(asctime)s - %(message)s',
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
110 datefmt='%Y-%m-%d %H:%M:%S')
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
111 event_handler = ToolHandler(watchme=watchme)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
112 observer = Observer()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
113 observer.schedule(event_handler, path=watchme, recursive=True)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
114 observer.start()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
115 try:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
116 while True:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
117 time.sleep(1)
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
118 except KeyboardInterrupt:
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
119 observer.stop()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
120 observer.join()
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
121
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
122
efefe43f23c8 Uploaded
fubar
parents:
diff changeset
123