/
__init__.py
5163 lines (4280 loc) · 187 KB
/
__init__.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Galaxy data model classes
Naming: try to use class names that have a distinct plural form so that
the relationship cardinalities are obvious (e.g. prefer Dataset to Data)
"""
import base64
import errno
import json
import logging
import numbers
import operator
import os
import pwd
import random
import string
import time
from datetime import datetime, timedelta
from string import Template
from uuid import UUID, uuid4
from six import string_types
from social_core.storage import AssociationMixin, CodeMixin, NonceMixin, PartialMixin, UserMixin
from sqlalchemy import (
alias,
and_,
func,
inspect,
join,
not_,
or_,
select,
true,
type_coerce,
types)
from sqlalchemy.ext import hybrid
from sqlalchemy.orm import aliased, joinedload, object_session
from sqlalchemy.schema import UniqueConstraint
import galaxy.model.metadata
import galaxy.model.orm.now
import galaxy.security.passwords
import galaxy.util
from galaxy.managers import tags
from galaxy.model.item_attrs import UsesAnnotations
from galaxy.model.util import pgcalc
from galaxy.security import get_permitted_actions
from galaxy.util import (directory_hash_id, ready_name_for_url,
unicodify, unique_id)
from galaxy.util.bunch import Bunch
from galaxy.util.dictifiable import Dictifiable
from galaxy.util.hash_util import new_secure_hash
from galaxy.util.sanitize_html import sanitize_html
from galaxy.web.form_builder import (AddressField, CheckboxField, HistoryField,
PasswordField, SelectField, TextArea, TextField, WorkflowField,
WorkflowMappingField)
from galaxy.web.framework.helpers import to_unicode
log = logging.getLogger(__name__)
_datatypes_registry = None
# When constructing filters with in for a fixed set of ids, maximum
# number of items to place in the IN statement. Different databases
# are going to have different limits so it is likely best to not let
# this be unlimited - filter in Python if over this limit.
MAX_IN_FILTER_LENGTH = 100
# The column sizes for job metrics. Note: changing these values does not change the column sizes, a migration must be
# performed to do that.
JOB_METRIC_MAX_LENGTH = 1023
JOB_METRIC_PRECISION = 26
JOB_METRIC_SCALE = 7
class NoConverterException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class ConverterDependencyException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def _get_datatypes_registry():
if _datatypes_registry is None:
raise Exception("galaxy.model.set_datatypes_registry must be called before performing certain DatasetInstance operations.")
return _datatypes_registry
def set_datatypes_registry(d_registry):
"""
Set up datatypes_registry
"""
global _datatypes_registry
_datatypes_registry = d_registry
class HasTags(object):
dict_collection_visible_keys = ['tags']
dict_element_visible_keys = ['tags']
def to_dict(self, *args, **kwargs):
rval = super(HasTags, self).to_dict(*args, **kwargs)
rval['tags'] = self.make_tag_string_list()
return rval
def make_tag_string_list(self):
# add tags string list
tags_str_list = []
for tag in self.tags:
tag_str = tag.user_tname
if tag.value is not None:
tag_str += ":" + tag.user_value
tags_str_list.append(tag_str)
return tags_str_list
def copy_tags_from(self, target_user, source):
for source_tag_assoc in source.tags:
new_tag_assoc = source_tag_assoc.copy()
new_tag_assoc.user = target_user
self.tags.append(new_tag_assoc)
class HasName(object):
def get_display_name(self):
"""
These objects have a name attribute can be either a string or a unicode
object. If string, convert to unicode object assuming 'utf-8' format.
"""
name = self.name
name = unicodify(name, 'utf-8')
return name
class UsesCreateAndUpdateTime(object):
@property
def seconds_since_updated(self):
update_time = self.update_time or galaxy.model.orm.now.now() # In case not yet flushed
return (galaxy.model.orm.now.now() - update_time).total_seconds()
@property
def seconds_since_created(self):
create_time = self.create_time or galaxy.model.orm.now.now() # In case not yet flushed
return (galaxy.model.orm.now.now() - create_time).total_seconds()
class JobLike(object):
MAX_NUMERIC = 10**(JOB_METRIC_PRECISION - JOB_METRIC_SCALE) - 1
def _init_metrics(self):
self.text_metrics = []
self.numeric_metrics = []
def add_metric(self, plugin, metric_name, metric_value):
plugin = unicodify(plugin, 'utf-8')
metric_name = unicodify(metric_name, 'utf-8')
number = isinstance(metric_value, numbers.Number)
if number and int(metric_value) <= JobLike.MAX_NUMERIC:
metric = self._numeric_metric(plugin, metric_name, metric_value)
self.numeric_metrics.append(metric)
elif number:
log.warning("Cannot store metric due to database column overflow (max: %s): %s: %s",
JobLike.MAX_NUMERIC, metric_name, metric_value)
else:
metric_value = unicodify(metric_value, 'utf-8')
if len(metric_value) > (JOB_METRIC_MAX_LENGTH - 1):
# Truncate these values - not needed with sqlite
# but other backends must need it.
metric_value = metric_value[:(JOB_METRIC_MAX_LENGTH - 1)]
metric = self._text_metric(plugin, metric_name, metric_value)
self.text_metrics.append(metric)
@property
def metrics(self):
# TODO: Make iterable, concatenate with chain
return self.text_metrics + self.numeric_metrics
def set_streams(self, stdout, stderr):
stdout = galaxy.util.unicodify(stdout)
stderr = galaxy.util.unicodify(stderr)
if (len(stdout) > galaxy.util.DATABASE_MAX_STRING_SIZE):
stdout = galaxy.util.shrink_string_by_size(stdout, galaxy.util.DATABASE_MAX_STRING_SIZE, join_by="\n..\n", left_larger=True, beginning_on_size_error=True)
log.info("stdout for %s %d is greater than %s, only a portion will be logged to database", type(self), self.id, galaxy.util.DATABASE_MAX_STRING_SIZE_PRETTY)
self.stdout = stdout
if (len(stderr) > galaxy.util.DATABASE_MAX_STRING_SIZE):
stderr = galaxy.util.shrink_string_by_size(stderr, galaxy.util.DATABASE_MAX_STRING_SIZE, join_by="\n..\n", left_larger=True, beginning_on_size_error=True)
log.info("stderr for %s %d is greater than %s, only a portion will be logged to database", type(self), self.id, galaxy.util.DATABASE_MAX_STRING_SIZE_PRETTY)
self.stderr = stderr
def log_str(self):
extra = ""
safe_id = getattr(self, "id", None)
if safe_id is not None:
extra += "id=%s" % safe_id
else:
extra += "unflushed"
return "%s[%s,tool_id=%s]" % (self.__class__.__name__, extra, self.tool_id)
class User(Dictifiable):
use_pbkdf2 = True
"""
Data for a Galaxy user or admin and relations to their
histories, credentials, and roles.
"""
# attributes that will be accessed and returned when calling to_dict( view='collection' )
dict_collection_visible_keys = ['id', 'email', 'username', 'deleted', 'active', 'last_password_change']
# attributes that will be accessed and returned when calling to_dict( view='element' )
dict_element_visible_keys = ['id', 'email', 'username', 'total_disk_usage', 'nice_total_disk_usage', 'deleted', 'active', 'last_password_change']
def __init__(self, email=None, password=None, username=None):
self.email = email
self.password = password
self.external = False
self.deleted = False
self.purged = False
self.active = False
self.activation_token = None
self.username = username
self.last_password_change = None
# Relationships
self.histories = []
self.credentials = []
# ? self.roles = []
@property
def extra_preferences(self):
data = {}
extra_user_preferences = self.preferences.get('extra_user_preferences')
if extra_user_preferences:
try:
data = json.loads(extra_user_preferences)
except Exception:
pass
return data
def set_password_cleartext(self, cleartext):
"""
Set user password to the digest of `cleartext`.
"""
if User.use_pbkdf2:
self.password = galaxy.security.passwords.hash_password(cleartext)
else:
self.password = new_secure_hash(text_type=cleartext)
self.last_password_change = datetime.now()
def set_random_password(self, length=16):
"""
Sets user password to a random string of the given length.
:return: void
"""
self.set_password_cleartext(
''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(length)))
def check_password(self, cleartext):
"""
Check if `cleartext` matches user password when hashed.
"""
return galaxy.security.passwords.check_password(cleartext, self.password)
def system_user_pwent(self, real_system_username):
"""
Gives the system user pwent entry based on e-mail or username depending
on the value in real_system_username
"""
system_user_pwent = None
if real_system_username == 'user_email':
try:
system_user_pwent = pwd.getpwnam(self.email.split('@')[0])
except KeyError:
pass
elif real_system_username == 'username':
try:
system_user_pwent = pwd.getpwnam(self.username)
except KeyError:
pass
else:
try:
system_user_pwent = pwd.getpwnam(real_system_username)
except KeyError:
log.warning("invalid configuration of real_system_username")
system_user_pwent = None
pass
return system_user_pwent
def all_roles(self):
"""
Return a unique list of Roles associated with this user or any of their groups.
"""
try:
db_session = object_session(self)
user = db_session.query(
User
).filter_by( # don't use get, it will use session variant.
id=self.id
).options(
joinedload("roles"),
joinedload("roles.role"),
joinedload("groups"),
joinedload("groups.group"),
joinedload("groups.group.roles"),
joinedload("groups.group.roles.role")
).one()
except Exception:
# If not persistent user, just use models normaly and
# skip optimizations...
user = self
roles = [ura.role for ura in user.roles]
for group in [uga.group for uga in user.groups]:
for role in [gra.role for gra in group.roles]:
if role not in roles:
roles.append(role)
return roles
def all_roles_exploiting_cache(self):
"""
"""
roles = [ura.role for ura in self.roles]
for group in [uga.group for uga in self.groups]:
for role in [gra.role for gra in group.roles]:
if role not in roles:
roles.append(role)
return roles
def get_disk_usage(self, nice_size=False):
"""
Return byte count of disk space used by user or a human-readable
string if `nice_size` is `True`.
"""
rval = 0
if self.disk_usage is not None:
rval = self.disk_usage
if nice_size:
rval = galaxy.util.nice_size(rval)
return rval
def set_disk_usage(self, bytes):
"""
Manually set the disk space used by a user to `bytes`.
"""
self.disk_usage = bytes
total_disk_usage = property(get_disk_usage, set_disk_usage)
def adjust_total_disk_usage(self, amount):
if amount != 0:
self.disk_usage = func.coalesce(self.table.c.disk_usage, 0) + amount
@property
def nice_total_disk_usage(self):
"""
Return byte count of disk space used in a human-readable string.
"""
return self.get_disk_usage(nice_size=True)
def calculate_disk_usage(self):
"""
Return byte count total of disk space used by all non-purged, non-library
HDAs in non-purged histories.
"""
# maintain a list so that we don't double count
dataset_ids = []
total = 0
# this can be a huge number and can run out of memory, so we avoid the mappers
db_session = object_session(self)
for history in db_session.query(History).enable_eagerloads(False).filter_by(user_id=self.id, purged=False).yield_per(1000):
for hda in db_session.query(HistoryDatasetAssociation).enable_eagerloads(False).filter_by(history_id=history.id, purged=False).yield_per(1000):
# TODO: def hda.counts_toward_disk_usage():
# return ( not self.dataset.purged and not self.dataset.library_associations )
if hda.dataset.id not in dataset_ids and not hda.dataset.purged and not hda.dataset.library_associations:
dataset_ids.append(hda.dataset.id)
total += hda.dataset.get_total_size()
return total
def calculate_and_set_disk_usage(self):
"""
Calculates and sets user disk usage.
"""
new = None
db_session = object_session(self)
current = self.get_disk_usage()
if db_session.get_bind().dialect.name not in ('postgres', 'postgresql'):
done = False
while not done:
new = self.calculate_disk_usage()
db_session.refresh(self)
# make sure usage didn't change while calculating
# set done if it has not, otherwise reset current and iterate again.
if self.get_disk_usage() == current:
done = True
else:
current = self.get_disk_usage()
else:
new = pgcalc(db_session, self.id)
if new not in (current, None):
self.set_disk_usage(new)
db_session.add(self)
db_session.flush()
@staticmethod
def user_template_environment(user):
"""
>>> env = User.user_template_environment(None)
>>> env['__user_email__']
'Anonymous'
>>> env['__user_id__']
'Anonymous'
>>> user = User('foo@example.com')
>>> user.id = 6
>>> user.username = 'foo2'
>>> env = User.user_template_environment(user)
>>> env['__user_id__']
'6'
>>> env['__user_name__']
'foo2'
"""
if user:
user_id = '%d' % user.id
user_email = str(user.email)
user_name = str(user.username)
else:
user = None
user_id = 'Anonymous'
user_email = 'Anonymous'
user_name = 'Anonymous'
environment = {}
environment['__user__'] = user
environment['__user_id__'] = environment['userId'] = user_id
environment['__user_email__'] = environment['userEmail'] = user_email
environment['__user_name__'] = user_name
return environment
@staticmethod
def expand_user_properties(user, in_string):
"""
"""
environment = User.user_template_environment(user)
return Template(in_string).safe_substitute(environment)
def is_active(self):
return self.active
def is_authenticated(self):
# TODO: is required for python social auth (PSA); however, a user authentication is relative to the backend.
# For instance, a user who is authenticated with Google, is not necessarily authenticated
# with Amazon. Therefore, this function should also receive the backend and check if this
# user is already authenticated on that backend or not. For now, returning always True
# seems reasonable. Besides, this is also how a PSA example is implemented:
# https://github.com/python-social-auth/social-examples/blob/master/example-cherrypy/example/db/user.py
return True
class PasswordResetToken(object):
def __init__(self, user, token=None):
if token:
self.token = token
else:
self.token = unique_id()
self.user = user
self.expiration_time = galaxy.model.orm.now.now() + timedelta(hours=24)
class BaseJobMetric(object):
def __init__(self, plugin, metric_name, metric_value):
self.plugin = plugin
self.metric_name = metric_name
self.metric_value = metric_value
class JobMetricText(BaseJobMetric):
pass
class JobMetricNumeric(BaseJobMetric):
pass
class TaskMetricText(BaseJobMetric):
pass
class TaskMetricNumeric(BaseJobMetric):
pass
class Job(JobLike, UsesCreateAndUpdateTime, Dictifiable):
dict_collection_visible_keys = ['id', 'state', 'exit_code', 'update_time', 'create_time']
dict_element_visible_keys = ['id', 'state', 'exit_code', 'update_time', 'create_time']
"""
A job represents a request to run a tool given input datasets, tool
parameters, and output datasets.
"""
_numeric_metric = JobMetricNumeric
_text_metric = JobMetricText
states = Bunch(NEW='new',
RESUBMITTED='resubmitted',
UPLOAD='upload',
WAITING='waiting',
QUEUED='queued',
RUNNING='running',
OK='ok',
ERROR='error',
PAUSED='paused',
DELETED='deleted',
DELETED_NEW='deleted_new')
terminal_states = [states.OK,
states.ERROR,
states.DELETED]
#: job states where the job hasn't finished and the model may still change
non_ready_states = [
states.NEW,
states.RESUBMITTED,
states.UPLOAD,
states.WAITING,
states.QUEUED,
states.RUNNING,
]
# Please include an accessor (get/set pair) for any new columns/members.
def __init__(self):
self.session_id = None
self.user_id = None
self.tool_id = None
self.tool_version = None
self.copied_from_job_id = None
self.command_line = None
self.dependencies = []
self.param_filename = None
self.parameters = []
self.input_datasets = []
self.output_datasets = []
self.input_dataset_collections = []
self.output_dataset_collection_instances = []
self.output_dataset_collections = []
self.input_library_datasets = []
self.output_library_datasets = []
self.state = Job.states.NEW
self.info = None
self.job_runner_name = None
self.job_runner_external_id = None
self.destination_id = None
self.destination_params = None
self.post_job_actions = []
self.state_history = []
self.imported = False
self.handler = None
self.exit_code = None
self._init_metrics()
self.state_history.append(JobStateHistory(self))
@property
def finished(self):
states = self.states
return self.state in [
states.OK,
states.ERROR,
states.DELETED,
states.DELETED_NEW,
]
# TODO: Add accessors for members defined in SQL Alchemy for the Job table and
# for the mapper defined to the Job table.
def get_external_output_metadata(self):
"""
The external_output_metadata is currently a reference from Job to
JobExternalOutputMetadata. It exists for a job but not a task.
"""
return self.external_output_metadata
def get_session_id(self):
return self.session_id
def get_user_id(self):
return self.user_id
def get_tool_id(self):
return self.tool_id
def get_tool_version(self):
return self.tool_version
def get_command_line(self):
return self.command_line
def get_dependencies(self):
return self.dependencies
def get_param_filename(self):
return self.param_filename
def get_parameters(self):
return self.parameters
def get_copied_from_job_id(self):
return self.copied_from_job_id
def get_input_datasets(self):
return self.input_datasets
def get_output_datasets(self):
return self.output_datasets
def get_input_library_datasets(self):
return self.input_library_datasets
def get_output_library_datasets(self):
return self.output_library_datasets
def get_state(self):
return self.state
def get_info(self):
return self.info
def get_job_runner_name(self):
# This differs from the Task class in that job_runner_name is
# accessed instead of task_runner_name. Note that the field
# runner_name is not the same thing.
return self.job_runner_name
def get_job_runner_external_id(self):
# This is different from the Task just in the member accessed:
return self.job_runner_external_id
def get_post_job_actions(self):
return self.post_job_actions
def get_imported(self):
return self.imported
def get_handler(self):
return self.handler
def get_params(self):
return self.params
def get_user(self):
# This is defined in the SQL Alchemy mapper as a relation to the User.
return self.user
def get_id(self):
# This is defined in the SQL Alchemy's Job table (and not in the model).
return self.id
def get_tasks(self):
# The tasks member is pert of a reference in the SQL Alchemy schema:
return self.tasks
def get_id_tag(self):
"""
Return a tag that can be useful in identifying a Job.
This returns the Job's get_id
"""
return "%s" % self.id
def set_session_id(self, session_id):
self.session_id = session_id
def set_user_id(self, user_id):
self.user_id = user_id
def set_tool_id(self, tool_id):
self.tool_id = tool_id
def set_tool_version(self, tool_version):
self.tool_version = tool_version
def set_command_line(self, command_line):
self.command_line = command_line
def set_dependencies(self, dependencies):
self.dependencies = dependencies
def set_param_filename(self, param_filename):
self.param_filename = param_filename
def set_parameters(self, parameters):
self.parameters = parameters
def set_copied_from_job_id(self, job_id):
self.copied_from_job_id = job_id
def set_input_datasets(self, input_datasets):
self.input_datasets = input_datasets
def set_output_datasets(self, output_datasets):
self.output_datasets = output_datasets
def set_input_library_datasets(self, input_library_datasets):
self.input_library_datasets = input_library_datasets
def set_output_library_datasets(self, output_library_datasets):
self.output_library_datasets = output_library_datasets
def set_info(self, info):
self.info = info
def set_runner_name(self, job_runner_name):
self.job_runner_name = job_runner_name
def get_job(self):
# Added so job and task have same interface (.get_job() ) to get at
# underlying job object.
return self
def set_runner_external_id(self, job_runner_external_id):
self.job_runner_external_id = job_runner_external_id
def set_post_job_actions(self, post_job_actions):
self.post_job_actions = post_job_actions
def set_imported(self, imported):
self.imported = imported
def set_handler(self, handler):
self.handler = handler
def set_params(self, params):
self.params = params
def add_parameter(self, name, value):
self.parameters.append(JobParameter(name, value))
def add_input_dataset(self, name, dataset=None, dataset_id=None):
assoc = JobToInputDatasetAssociation(name, dataset)
if dataset is None and dataset_id is not None:
assoc.dataset_id = dataset_id
self.input_datasets.append(assoc)
def add_output_dataset(self, name, dataset):
self.output_datasets.append(JobToOutputDatasetAssociation(name, dataset))
def add_input_dataset_collection(self, name, dataset_collection):
self.input_dataset_collections.append(JobToInputDatasetCollectionAssociation(name, dataset_collection))
def add_output_dataset_collection(self, name, dataset_collection_instance):
self.output_dataset_collection_instances.append(JobToOutputDatasetCollectionAssociation(name, dataset_collection_instance))
def add_implicit_output_dataset_collection(self, name, dataset_collection):
self.output_dataset_collections.append(JobToImplicitOutputDatasetCollectionAssociation(name, dataset_collection))
def add_input_library_dataset(self, name, dataset):
self.input_library_datasets.append(JobToInputLibraryDatasetAssociation(name, dataset))
def add_output_library_dataset(self, name, dataset):
self.output_library_datasets.append(JobToOutputLibraryDatasetAssociation(name, dataset))
def add_post_job_action(self, pja):
self.post_job_actions.append(PostJobActionAssociation(pja, self))
def set_state(self, state):
"""
Save state history
"""
self.state = state
self.state_history.append(JobStateHistory(self))
def get_param_values(self, app, ignore_errors=False):
"""
Read encoded parameter values from the database and turn back into a
dict of tool parameter values.
"""
param_dict = self.raw_param_dict()
tool = app.toolbox.get_tool(self.tool_id, tool_version=self.tool_version)
param_dict = tool.params_from_strings(param_dict, app, ignore_errors=ignore_errors)
return param_dict
def raw_param_dict(self):
param_dict = dict([(p.name, p.value) for p in self.parameters])
return param_dict
def check_if_output_datasets_deleted(self):
"""
Return true if all of the output datasets associated with this job are
in the deleted state
"""
for dataset_assoc in self.output_datasets:
dataset = dataset_assoc.dataset
# only the originator of the job can delete a dataset to cause
# cancellation of the job, no need to loop through history_associations
if not dataset.deleted:
return False
return True
def mark_deleted(self, track_jobs_in_database=False):
"""
Mark this job as deleted, and mark any output datasets as discarded.
"""
if self.finished:
# Do not modify the state/outputs of jobs that are already terminal
return
if track_jobs_in_database:
self.state = Job.states.DELETED_NEW
else:
self.state = Job.states.DELETED
self.info = "Job output deleted by user before job completed."
for dataset_assoc in self.output_datasets:
dataset = dataset_assoc.dataset
dataset.deleted = True
dataset.state = dataset.states.DISCARDED
for dataset in dataset.dataset.history_associations:
# propagate info across shared datasets
dataset.deleted = True
dataset.blurb = 'deleted'
dataset.peek = 'Job deleted'
dataset.info = 'Job output deleted by user before job completed'
def to_dict(self, view='collection', system_details=False):
rval = super(Job, self).to_dict(view=view)
rval['tool_id'] = self.tool_id
if system_details:
# System level details that only admins should have.
rval['external_id'] = self.job_runner_external_id
rval['command_line'] = self.command_line
if view == 'element':
param_dict = dict([(p.name, p.value) for p in self.parameters])
rval['params'] = param_dict
input_dict = {}
for i in self.input_datasets:
if i.dataset is not None:
input_dict[i.name] = {
"id" : i.dataset.id, "src" : "hda",
"uuid" : str(i.dataset.dataset.uuid) if i.dataset.dataset.uuid is not None else None
}
for i in self.input_library_datasets:
if i.dataset is not None:
input_dict[i.name] = {
"id" : i.dataset.id, "src" : "ldda",
"uuid": str(i.dataset.dataset.uuid) if i.dataset.dataset.uuid is not None else None
}
for k in input_dict:
if k in param_dict:
del param_dict[k]
rval['inputs'] = input_dict
output_dict = {}
for i in self.output_datasets:
if i.dataset is not None:
output_dict[i.name] = {
"id" : i.dataset.id, "src" : "hda",
"uuid" : str(i.dataset.dataset.uuid) if i.dataset.dataset.uuid is not None else None
}
for i in self.output_library_datasets:
if i.dataset is not None:
output_dict[i.name] = {
"id" : i.dataset.id, "src" : "ldda",
"uuid" : str(i.dataset.dataset.uuid) if i.dataset.dataset.uuid is not None else None
}
rval['outputs'] = output_dict
return rval
def set_final_state(self, final_state):
self.set_state(final_state)
if self.workflow_invocation_step:
self.workflow_invocation_step.update()
def get_destination_configuration(self, config, key, default=None):
""" Get a destination parameter that can be defaulted back
in specified config if it needs to be applied globally.
"""
param_unspecified = object()
config_value = (self.destination_params or {}).get(key, param_unspecified)
if config_value is param_unspecified:
config_value = getattr(config, key, param_unspecified)
if config_value is param_unspecified:
config_value = default
return config_value
class Task(JobLike):
"""
A task represents a single component of a job.
"""
_numeric_metric = TaskMetricNumeric
_text_metric = TaskMetricText
states = Bunch(NEW='new',
WAITING='waiting',
QUEUED='queued',
RUNNING='running',
OK='ok',
ERROR='error',
DELETED='deleted')
# Please include an accessor (get/set pair) for any new columns/members.
def __init__(self, job, working_directory, prepare_files_cmd):
self.command_line = None
self.parameters = []
self.state = Task.states.NEW
self.info = None
self.working_directory = working_directory
self.task_runner_name = None
self.task_runner_external_id = None
self.job = job
self.stdout = ""
self.stderr = ""
self.exit_code = None
self.prepare_input_files_cmd = prepare_files_cmd
self._init_metrics()
def get_param_values(self, app):
"""
Read encoded parameter values from the database and turn back into a
dict of tool parameter values.
"""
param_dict = dict([(p.name, p.value) for p in self.job.parameters])
tool = app.toolbox.get_tool(self.job.tool_id, tool_version=self.job.tool_version)
param_dict = tool.params_from_strings(param_dict, app)
return param_dict
def get_id(self):
# This is defined in the SQL Alchemy schema:
return self.id
def get_id_tag(self):
"""
Return an id tag suitable for identifying the task.
This combines the task's job id and the task's own id.
"""
return "%s_%s" % (self.job.get_id(), self.get_id())
def get_command_line(self):
return self.command_line
def get_parameters(self):
return self.parameters
def get_state(self):
return self.state
def get_info(self):
return self.info
def get_working_directory(self):
return self.working_directory
def get_task_runner_name(self):
return self.task_runner_name
def get_task_runner_external_id(self):
return self.task_runner_external_id
def get_job(self):
return self.job
def get_stdout(self):
return self.stdout
def get_stderr(self):
return self.stderr
def get_prepare_input_files_cmd(self):
return self.prepare_input_files_cmd
# The following accessors are for members that are in the Job class but
# not in the Task class. So they can either refer to the parent Job
# or return None, depending on whether Tasks need to point to the parent
# (e.g., for a session) or never use the member (e.g., external output
# metdata). These can be filled in as needed.
def get_external_output_metadata(self):
"""
The external_output_metadata is currently a backref to
JobExternalOutputMetadata. It exists for a job but not a task,
and when a task is cancelled its corresponding parent Job will
be cancelled. So None is returned now, but that could be changed
to self.get_job().get_external_output_metadata().
"""
return None
def get_job_runner_name(self):
"""
Since runners currently access Tasks the same way they access Jobs,
this method just refers to *this* instance's runner.
"""
return self.task_runner_name
def get_job_runner_external_id(self):
"""
Runners will use the same methods to get information about the Task
class as they will about the Job class, so this method just returns