scipion-pyworkflow 3.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. pyworkflow/__init__.py +33 -0
  2. pyworkflow/apps/__init__.py +29 -0
  3. pyworkflow/apps/pw_manager.py +37 -0
  4. pyworkflow/apps/pw_plot.py +51 -0
  5. pyworkflow/apps/pw_project.py +113 -0
  6. pyworkflow/apps/pw_protocol_list.py +143 -0
  7. pyworkflow/apps/pw_protocol_run.py +51 -0
  8. pyworkflow/apps/pw_run_tests.py +267 -0
  9. pyworkflow/apps/pw_schedule_run.py +322 -0
  10. pyworkflow/apps/pw_sleep.py +37 -0
  11. pyworkflow/apps/pw_sync_data.py +439 -0
  12. pyworkflow/apps/pw_viewer.py +78 -0
  13. pyworkflow/config.py +536 -0
  14. pyworkflow/constants.py +212 -0
  15. pyworkflow/exceptions.py +18 -0
  16. pyworkflow/gui/__init__.py +36 -0
  17. pyworkflow/gui/browser.py +726 -0
  18. pyworkflow/gui/canvas.py +1190 -0
  19. pyworkflow/gui/dialog.py +976 -0
  20. pyworkflow/gui/form.py +2627 -0
  21. pyworkflow/gui/graph.py +247 -0
  22. pyworkflow/gui/graph_layout.py +271 -0
  23. pyworkflow/gui/gui.py +566 -0
  24. pyworkflow/gui/matplotlib_image.py +233 -0
  25. pyworkflow/gui/plotter.py +247 -0
  26. pyworkflow/gui/project/__init__.py +25 -0
  27. pyworkflow/gui/project/base.py +192 -0
  28. pyworkflow/gui/project/constants.py +139 -0
  29. pyworkflow/gui/project/labels.py +205 -0
  30. pyworkflow/gui/project/project.py +484 -0
  31. pyworkflow/gui/project/searchprotocol.py +154 -0
  32. pyworkflow/gui/project/searchrun.py +181 -0
  33. pyworkflow/gui/project/steps.py +166 -0
  34. pyworkflow/gui/project/utils.py +332 -0
  35. pyworkflow/gui/project/variables.py +179 -0
  36. pyworkflow/gui/project/viewdata.py +472 -0
  37. pyworkflow/gui/project/viewprojects.py +510 -0
  38. pyworkflow/gui/project/viewprotocols.py +2093 -0
  39. pyworkflow/gui/project/viewprotocols_extra.py +560 -0
  40. pyworkflow/gui/text.py +771 -0
  41. pyworkflow/gui/tooltip.py +185 -0
  42. pyworkflow/gui/tree.py +684 -0
  43. pyworkflow/gui/widgets.py +307 -0
  44. pyworkflow/mapper/__init__.py +26 -0
  45. pyworkflow/mapper/mapper.py +222 -0
  46. pyworkflow/mapper/sqlite.py +1578 -0
  47. pyworkflow/mapper/sqlite_db.py +145 -0
  48. pyworkflow/object.py +1512 -0
  49. pyworkflow/plugin.py +712 -0
  50. pyworkflow/project/__init__.py +31 -0
  51. pyworkflow/project/config.py +451 -0
  52. pyworkflow/project/manager.py +179 -0
  53. pyworkflow/project/project.py +1990 -0
  54. pyworkflow/project/scripts/clean_projects.py +77 -0
  55. pyworkflow/project/scripts/config.py +92 -0
  56. pyworkflow/project/scripts/create.py +77 -0
  57. pyworkflow/project/scripts/edit_workflow.py +90 -0
  58. pyworkflow/project/scripts/fix_links.py +39 -0
  59. pyworkflow/project/scripts/load.py +87 -0
  60. pyworkflow/project/scripts/refresh.py +83 -0
  61. pyworkflow/project/scripts/schedule.py +111 -0
  62. pyworkflow/project/scripts/stack2volume.py +41 -0
  63. pyworkflow/project/scripts/stop.py +81 -0
  64. pyworkflow/protocol/__init__.py +38 -0
  65. pyworkflow/protocol/bibtex.py +48 -0
  66. pyworkflow/protocol/constants.py +86 -0
  67. pyworkflow/protocol/executor.py +334 -0
  68. pyworkflow/protocol/hosts.py +313 -0
  69. pyworkflow/protocol/launch.py +270 -0
  70. pyworkflow/protocol/package.py +42 -0
  71. pyworkflow/protocol/params.py +744 -0
  72. pyworkflow/protocol/protocol.py +2554 -0
  73. pyworkflow/resources/Imagej.png +0 -0
  74. pyworkflow/resources/chimera.png +0 -0
  75. pyworkflow/resources/fa-exclamation-triangle_alert.png +0 -0
  76. pyworkflow/resources/fa-info-circle_alert.png +0 -0
  77. pyworkflow/resources/fa-search.png +0 -0
  78. pyworkflow/resources/fa-times-circle_alert.png +0 -0
  79. pyworkflow/resources/file_vol.png +0 -0
  80. pyworkflow/resources/loading.gif +0 -0
  81. pyworkflow/resources/no-image128.png +0 -0
  82. pyworkflow/resources/scipion_bn.png +0 -0
  83. pyworkflow/resources/scipion_icon.png +0 -0
  84. pyworkflow/resources/scipion_icon.svg +397 -0
  85. pyworkflow/resources/scipion_icon_proj.png +0 -0
  86. pyworkflow/resources/scipion_icon_projs.png +0 -0
  87. pyworkflow/resources/scipion_icon_prot.png +0 -0
  88. pyworkflow/resources/scipion_logo.png +0 -0
  89. pyworkflow/resources/scipion_logo_normal.png +0 -0
  90. pyworkflow/resources/scipion_logo_small.png +0 -0
  91. pyworkflow/resources/sprites.png +0 -0
  92. pyworkflow/resources/sprites.xcf +0 -0
  93. pyworkflow/resources/wait.gif +0 -0
  94. pyworkflow/template.py +322 -0
  95. pyworkflow/tests/__init__.py +29 -0
  96. pyworkflow/tests/test_utils.py +25 -0
  97. pyworkflow/tests/tests.py +341 -0
  98. pyworkflow/utils/__init__.py +38 -0
  99. pyworkflow/utils/dataset.py +414 -0
  100. pyworkflow/utils/echo.py +104 -0
  101. pyworkflow/utils/graph.py +196 -0
  102. pyworkflow/utils/log.py +284 -0
  103. pyworkflow/utils/path.py +527 -0
  104. pyworkflow/utils/process.py +132 -0
  105. pyworkflow/utils/profiler.py +92 -0
  106. pyworkflow/utils/progressbar.py +154 -0
  107. pyworkflow/utils/properties.py +627 -0
  108. pyworkflow/utils/reflection.py +129 -0
  109. pyworkflow/utils/utils.py +877 -0
  110. pyworkflow/utils/which.py +229 -0
  111. pyworkflow/viewer.py +328 -0
  112. pyworkflow/webservices/__init__.py +8 -0
  113. pyworkflow/webservices/config.py +11 -0
  114. pyworkflow/webservices/notifier.py +162 -0
  115. pyworkflow/webservices/repository.py +59 -0
  116. pyworkflow/webservices/workflowhub.py +74 -0
  117. pyworkflow/wizard.py +64 -0
  118. pyworkflowtests/__init__.py +51 -0
  119. pyworkflowtests/bibtex.py +51 -0
  120. pyworkflowtests/objects.py +830 -0
  121. pyworkflowtests/protocols.py +154 -0
  122. pyworkflowtests/tests/__init__.py +0 -0
  123. pyworkflowtests/tests/test_canvas.py +72 -0
  124. pyworkflowtests/tests/test_domain.py +45 -0
  125. pyworkflowtests/tests/test_logs.py +74 -0
  126. pyworkflowtests/tests/test_mappers.py +392 -0
  127. pyworkflowtests/tests/test_object.py +507 -0
  128. pyworkflowtests/tests/test_project.py +42 -0
  129. pyworkflowtests/tests/test_protocol_execution.py +72 -0
  130. pyworkflowtests/tests/test_protocol_export.py +78 -0
  131. pyworkflowtests/tests/test_protocol_output.py +158 -0
  132. pyworkflowtests/tests/test_streaming.py +47 -0
  133. pyworkflowtests/tests/test_utils.py +210 -0
  134. scipion_pyworkflow-3.7.0.dist-info/LICENSE.txt +674 -0
  135. scipion_pyworkflow-3.7.0.dist-info/METADATA +107 -0
  136. scipion_pyworkflow-3.7.0.dist-info/RECORD +140 -0
  137. scipion_pyworkflow-3.7.0.dist-info/WHEEL +5 -0
  138. scipion_pyworkflow-3.7.0.dist-info/dependency_links.txt +1 -0
  139. scipion_pyworkflow-3.7.0.dist-info/entry_points.txt +5 -0
  140. scipion_pyworkflow-3.7.0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,1990 @@
1
+ #!/usr/bin/env python
2
+ # **************************************************************************
3
+ # *
4
+ # * Authors: J.M. De la Rosa Trevin (delarosatrevin@scilifelab.se) [1]
5
+ # *
6
+ # * [1] SciLifeLab, Stockholm University
7
+ # *
8
+ # * This program is free software; you can redistribute it and/or modify
9
+ # * it under the terms of the GNU General Public License as published by
10
+ # * the Free Software Foundation; either version 3 of the License, or
11
+ # * (at your option) any later version.
12
+ # *
13
+ # * This program is distributed in the hope that it will be useful,
14
+ # * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # * GNU General Public License for more details.
17
+ # *
18
+ # * You should have received a copy of the GNU General Public License
19
+ # * along with this program; if not, write to the Free Software
20
+ # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21
+ # * 02111-1307 USA
22
+ # *
23
+ # * All comments concerning this program package may be sent to the
24
+ # * e-mail address 'scipion@cnb.csic.es'
25
+ # *
26
+ # **************************************************************************
27
+ import logging
28
+
29
+ from ..protocol.launch import _checkJobStatus
30
+
31
+ ROOT_NODE_NAME = "PROJECT"
32
+ logger = logging.getLogger(__name__)
33
+ from pyworkflow.utils.log import LoggingConfigurator
34
+ import datetime as dt
35
+ import json
36
+ import os
37
+ import re
38
+ import time
39
+ import traceback
40
+ from collections import OrderedDict
41
+
42
+ import pyworkflow as pw
43
+ from pyworkflow.constants import PROJECT_DBNAME, PROJECT_SETTINGS
44
+ import pyworkflow.object as pwobj
45
+ import pyworkflow.protocol as pwprot
46
+ import pyworkflow.utils as pwutils
47
+ from pyworkflow.mapper import SqliteMapper
48
+ from pyworkflow.protocol.constants import (MODE_RESTART, MODE_RESUME,
49
+ STATUS_INTERACTIVE, ACTIVE_STATUS,
50
+ UNKNOWN_JOBID, INITIAL_SLEEP_TIME, STATUS_FINISHED)
51
+ from pyworkflow.protocol.protocol import ProtImportBase, Protocol
52
+
53
+ from . import config
54
+
55
+
56
+ OBJECT_PARENT_ID = pwobj.OBJECT_PARENT_ID
57
+ PROJECT_LOGS = 'Logs'
58
+ PROJECT_RUNS = 'Runs'
59
+ PROJECT_TMP = 'Tmp'
60
+ PROJECT_UPLOAD = 'Uploads'
61
+ PROJECT_CONFIG = '.config'
62
+ PROJECT_CREATION_TIME = 'CreationTime'
63
+
64
+ # Regex to get numbering suffix and automatically propose runName
65
+ REGEX_NUMBER_ENDING = re.compile(r'(?P<prefix>.+)(?P<number>\(\d*\))\s*$')
66
+ REGEX_NUMBER_ENDING_CP = re.compile(r'(?P<prefix>.+\s\(copy)(?P<number>.*)\)\s*$')
67
+
68
+
69
+ class Project(object):
70
+ """This class will handle all information
71
+ related with a Project"""
72
+
73
+ @classmethod
74
+ def getDbName(cls):
75
+ """ Return the name of the database file of projects. """
76
+ return PROJECT_DBNAME
77
+
78
+ def __init__(self, domain, path):
79
+ """
80
+ Create a new Project instance.
81
+ :param domain: The application domain from where to get objects and
82
+ protocols.
83
+ :param path: Path where the project will be created/loaded
84
+ """
85
+ self._domain = domain
86
+ self.name = path
87
+ self.shortName = os.path.basename(path)
88
+ self.path = os.path.abspath(path)
89
+ self._isLink = os.path.islink(path)
90
+ self._isInReadOnlyFolder = False
91
+ self.pathList = [] # Store all related paths
92
+ self.dbPath = self.__addPath(PROJECT_DBNAME)
93
+ self.logsPath = self.__addPath(PROJECT_LOGS)
94
+ self.runsPath = self.__addPath(PROJECT_RUNS)
95
+ self.tmpPath = self.__addPath(PROJECT_TMP)
96
+ self.uploadPath = self.__addPath(PROJECT_UPLOAD)
97
+ self.settingsPath = self.__addPath(PROJECT_SETTINGS)
98
+ self.configPath = self.__addPath(PROJECT_CONFIG)
99
+ self.runs = None
100
+ self._runsGraph = None
101
+ self._transformGraph = None
102
+ self._sourceGraph = None
103
+ self.address = ''
104
+ self.port = pwutils.getFreePort()
105
+ self.mapper = None
106
+ self.settings:config.ProjectSettings = None
107
+ # Host configuration
108
+ self._hosts = None
109
+ # Creation time should be stored in project.sqlite when the project
110
+ # is created and then loaded with other properties from the database
111
+ self._creationTime = None
112
+ # Time stamp with the last run has been updated
113
+ self._lastRunTime = None
114
+
115
+ def getObjId(self):
116
+ """ Return the unique id assigned to this project. """
117
+ return os.path.basename(self.path)
118
+
119
+ def __addPath(self, *paths):
120
+ """Store a path needed for the project"""
121
+ p = self.getPath(*paths)
122
+ self.pathList.append(p)
123
+ return p
124
+
125
+ def getPath(self, *paths):
126
+ """Return path from the project root"""
127
+ if paths:
128
+ return os.path.join(*paths) # Why this is relative!!
129
+ else:
130
+ return self.path
131
+
132
+ def isLink(self):
133
+ """Returns if the project path is a link to another folder."""
134
+ return self._isLink
135
+
136
+ def getDbPath(self):
137
+ """ Return the path to the sqlite db. """
138
+ return self.dbPath
139
+
140
+ def getDbLastModificationDate(self):
141
+ """ Return the last modification date of the database """
142
+ pwutils.getFileLastModificationDate(self.getDbPath())
143
+
144
+ def getCreationTime(self):
145
+ """ Return the time when the project was created. """
146
+ # In project.create method, the first object inserted
147
+ # in the mapper should be the creation time
148
+ return self._creationTime
149
+
150
+ def getSettingsCreationTime(self):
151
+ return self.settings.getCreationTime()
152
+
153
+ def getElapsedTime(self):
154
+ """ Returns the time elapsed from the creation to the last
155
+ execution time. """
156
+ if self._creationTime and self._lastRunTime:
157
+ creationTs = self._creationTime
158
+ lastRunTs = self._lastRunTime.datetime()
159
+ return lastRunTs - creationTs
160
+ return None
161
+
162
+ def getLeftTime(self):
163
+ lifeTime = self.settings.getLifeTime()
164
+ if lifeTime:
165
+ td = dt.timedelta(hours=lifeTime)
166
+ return td - self.getElapsedTime()
167
+ else:
168
+ return None
169
+
170
+ def setDbPath(self, dbPath):
171
+ """ Set the project db path.
172
+ This function is used when running a protocol where
173
+ a project is loaded but using the protocol own sqlite file.
174
+ """
175
+ # First remove from pathList the old dbPath
176
+ self.pathList.remove(self.dbPath)
177
+ self.dbPath = os.path.abspath(dbPath)
178
+ self.pathList.append(self.dbPath)
179
+
180
+ def getName(self):
181
+ return self.name
182
+
183
+ def getDomain(self):
184
+ return self._domain
185
+
186
+ # TODO: maybe it has more sense to use this behaviour
187
+ # for just getName function...
188
+ def getShortName(self):
189
+ return self.shortName
190
+
191
+ def getTmpPath(self, *paths):
192
+ return self.getPath(PROJECT_TMP, *paths)
193
+
194
+ def getLogPath(self, *paths):
195
+ return self.getPath(PROJECT_LOGS, *paths)
196
+
197
+ def getProjectLog(self):
198
+ return os.path.join(self.path,self.getLogPath("project.log")) # For some reason getLogsPath is relative!
199
+
200
+ def getSettings(self):
201
+ return self.settings
202
+
203
+ def saveSettings(self):
204
+ # Read only mode
205
+ if not self.openedAsReadOnly():
206
+ self.settings.write()
207
+
208
+ def createSettings(self, runsView=1, readOnly=False):
209
+ self.settings = config.ProjectSettings()
210
+ self.settings.setRunsView(runsView)
211
+ self.settings.setReadOnly(readOnly)
212
+ self.settings.write(self.settingsPath)
213
+ return self.settings
214
+
215
+ def createMapper(self, sqliteFn):
216
+ """ Create a new SqliteMapper object and pass as classes dict
217
+ all globals and update with data and protocols from em.
218
+ """
219
+ classesDict = pwobj.Dict(default=pwprot.LegacyProtocol)
220
+ classesDict.update(self._domain.getMapperDict())
221
+ classesDict.update(config.__dict__)
222
+ return SqliteMapper(sqliteFn, classesDict)
223
+
224
+ def load(self, dbPath=None, hostsConf=None, protocolsConf=None, chdir=True,
225
+ loadAllConfig=True):
226
+ """
227
+ Load project data, configuration and settings.
228
+
229
+ :param dbPath: the path to the project database.
230
+ If None, use the project.sqlite in the project folder.
231
+ :param hostsConf: where to read the host configuration.
232
+ If None, check if exists in .config/hosts.conf
233
+ or read from ~/.config/scipion/hosts.conf
234
+ :param protocolsConf: Not used
235
+ :param chdir: If True, os.cwd will be set to project's path.
236
+ :param loadAllConfig: If True, settings from settings.sqlite will also be loaded
237
+
238
+ """
239
+
240
+ if not os.path.exists(self.path):
241
+ raise Exception("Cannot load project, path doesn't exist: %s"
242
+ % self.path)
243
+
244
+ # If folder is read only, flag it and warn about it.
245
+ if not os.access(self.path, os.W_OK):
246
+ self._isInReadOnlyFolder = True
247
+ logger.warning("Project \"%s\": you don't have write permissions "
248
+ "for project folder. Loading asd READ-ONLY." % self.shortName)
249
+
250
+ if chdir:
251
+ os.chdir(self.path) # Before doing nothing go to project dir
252
+
253
+ try:
254
+ self._loadDb(dbPath)
255
+ self._loadHosts(hostsConf)
256
+
257
+ if loadAllConfig:
258
+
259
+ # FIXME: Handle settings argument here
260
+
261
+ # It is possible that settings does not exists if
262
+ # we are loading a project after a Project.setDbName,
263
+ # used when running protocols
264
+ settingsPath = os.path.join(self.path, self.settingsPath)
265
+
266
+ logger.debug("settingsPath: %s" % settingsPath)
267
+
268
+ if os.path.exists(settingsPath):
269
+ self.settings = config.ProjectSettings.load(settingsPath)
270
+ else:
271
+ logger.info("settings is None")
272
+ self.settings = None
273
+
274
+ self._loadCreationTime()
275
+
276
+ # Catch DB not found exception (when loading a project from a folder
277
+ # without project.sqlite
278
+ except MissingProjectDbException as noDBe:
279
+ # Raise it at before: This is a critical error and should be raised
280
+ raise noDBe
281
+
282
+ # Catch any less severe exception..to allow at least open the project.
283
+ # except Exception as e:
284
+ # logger.info("ERROR: Project %s load failed.\n"
285
+ # " Message: %s\n" % (self.path, e))
286
+
287
+ def configureLogging(self):
288
+ LoggingConfigurator.setUpGUILogging(self.getProjectLog())
289
+ def _loadCreationTime(self):
290
+ # Load creation time, it should be in project.sqlite or
291
+ # in some old projects it is found in settings.sqlite
292
+
293
+ creationTime = self.mapper.selectBy(name=PROJECT_CREATION_TIME)
294
+
295
+ if creationTime: # CreationTime was found in project.sqlite
296
+ self._creationTime = creationTime[0].datetime()
297
+ else:
298
+ # We should read the creation time from settings.sqlite and
299
+ # update the CreationTime in the project.sqlite
300
+ self._creationTime = self.getSettingsCreationTime()
301
+ self._storeCreationTime(self._creationTime)
302
+
303
+ # ---- Helper functions to load different pieces of a project
304
+ def _loadDb(self, dbPath):
305
+ """ Load the mapper from the sqlite file in dbPath. """
306
+ if dbPath is not None:
307
+ self.setDbPath(dbPath)
308
+
309
+ absDbPath = os.path.join(self.path, self.dbPath)
310
+ if not os.path.exists(absDbPath):
311
+ raise MissingProjectDbException(
312
+ "Project database not found at '%s'" % absDbPath)
313
+ self.mapper = self.createMapper(absDbPath)
314
+
315
+ def closeMapper(self):
316
+ if self.mapper is not None:
317
+ self.mapper.close()
318
+ self.mapper = None
319
+
320
+ def getLocalConfigHosts(self):
321
+ """ Return the local file where the project will try to
322
+ read the hosts configuration. """
323
+ return self.getPath(PROJECT_CONFIG, pw.Config.SCIPION_HOSTS)
324
+
325
+ def _loadHosts(self, hosts):
326
+ """ Loads hosts configuration from hosts file. """
327
+ # If the host file is not passed as argument...
328
+ configHosts = pw.Config.SCIPION_HOSTS
329
+ projHosts = self.getLocalConfigHosts()
330
+
331
+ if hosts is None:
332
+ # Try first to read it from the project file .config./hosts.conf
333
+ if os.path.exists(projHosts):
334
+ hostsFile = projHosts
335
+ else:
336
+ localDir = os.path.dirname(pw.Config.SCIPION_LOCAL_CONFIG)
337
+ hostsFile = os.path.join(localDir, configHosts)
338
+ else:
339
+ pwutils.copyFile(hosts, projHosts)
340
+ hostsFile = hosts
341
+
342
+ self._hosts = pwprot.HostConfig.load(hostsFile)
343
+
344
+ def getHostNames(self):
345
+ """ Return the list of host name in the project. """
346
+ return list(self._hosts.keys())
347
+
348
+ def getHostConfig(self, hostName):
349
+ if hostName in self._hosts:
350
+ hostKey = hostName
351
+ else:
352
+ hostKey = self.getHostNames()[0]
353
+ logger.warning("Protocol host '%s' not found." % hostName)
354
+ logger.warning(" Using '%s' instead." % hostKey)
355
+
356
+ return self._hosts[hostKey]
357
+
358
+ def getProtocolView(self):
359
+ """ Returns de view selected in the tree when it was persisted"""
360
+ return self.settings.getProtocolView()
361
+
362
+ def create(self, runsView=1, readOnly=False, hostsConf=None,
363
+ protocolsConf=None):
364
+ """Prepare all required paths and files to create a new project.
365
+
366
+ :param runsView: default view to associate the project with
367
+ :param readOnly: If True, project will be loaded as read only.
368
+ :param hostsConf: Path to the host.conf to be used when executing protocols
369
+ :param protocolsConf: Not used.
370
+ """
371
+ # Create project path if not exists
372
+ pwutils.path.makePath(self.path)
373
+ os.chdir(self.path) # Before doing nothing go to project dir
374
+ self._cleanData()
375
+ logger.info("Creating project at %s" % os.path.abspath(self.dbPath))
376
+ # Create db through the mapper
377
+ self.mapper = self.createMapper(self.dbPath)
378
+ # Store creation time
379
+ self._storeCreationTime(dt.datetime.now())
380
+ # Load settings from .conf files and write .sqlite
381
+ self.settings = self.createSettings(runsView=runsView,
382
+ readOnly=readOnly)
383
+ # Create other paths inside project
384
+ for p in self.pathList:
385
+ pwutils.path.makePath(p)
386
+
387
+ self._loadHosts(hostsConf)
388
+
389
+ def _storeCreationTime(self, creationTime):
390
+ """ Store the creation time in the project db. """
391
+ # Store creation time
392
+ creation = pwobj.String(objName=PROJECT_CREATION_TIME)
393
+ creation.set(creationTime)
394
+ self.mapper.insert(creation)
395
+ self.mapper.commit()
396
+
397
+ def _cleanData(self):
398
+ """Clean all project data"""
399
+ pwutils.path.cleanPath(*self.pathList)
400
+
401
+ def _continueWorkflow(self, errorsList, continuedProtList=None):
402
+ """
403
+ This function continue a workflow from a selected protocol.
404
+ The previous results are preserved.
405
+ Actions done here are:
406
+ 1. if the protocol list exists (for each protocol)
407
+ 1.1 if the protocol is not an interactive protocol
408
+ 1.1.1. If the protocol is in streaming (CONTINUE ACTION):
409
+ - 'dataStreaming' parameter if the protocol is an import
410
+ protocol
411
+ - check if the __stepsCheck function exist and it's not
412
+ the same implementation of the base class
413
+ (worksInStreaming function)
414
+ 1.1.1.1 Open the protocol sets, store and save them in
415
+ the database
416
+ 1.1.1.2 Change the protocol status (SAVED)
417
+ 1.1.1.3 Schedule the protocol
418
+ Else Restart the workflow from that point (RESTART ACTION) if
419
+ at least one protocol in streaming has been launched
420
+ """
421
+ if continuedProtList is not None:
422
+ for protocol, level in continuedProtList.values():
423
+ if not protocol.isInteractive():
424
+ if protocol.isScheduled():
425
+ continue
426
+
427
+ # streaming ...
428
+ if protocol.worksInStreaming() and not protocol.isSaved():
429
+ attrSet = [attr for name, attr in
430
+ protocol.iterOutputAttributes(pwprot.Set)]
431
+ try:
432
+ if attrSet:
433
+ # Open output sets..
434
+ for attr in attrSet:
435
+ attr.setStreamState(attr.STREAM_OPEN)
436
+ attr.write()
437
+ attr.close()
438
+ protocol.setStatus(pwprot.STATUS_SAVED)
439
+ protocol._updateSteps(lambda step: step.setStatus(pwprot.STATUS_SAVED))
440
+ protocol.setMapper(self.createMapper(protocol.getDbPath()))
441
+ protocol._store()
442
+ self._storeProtocol(protocol)
443
+ self.scheduleProtocol(protocol,
444
+ initialSleepTime=level*INITIAL_SLEEP_TIME)
445
+ except Exception as ex:
446
+ errorsList.append("Error trying to launch the "
447
+ "protocol: %s\nERROR: %s\n" %
448
+ (protocol.getObjLabel(), ex))
449
+ break
450
+ else:
451
+ if level != 0:
452
+ # Not in streaming and not the first protocol.
453
+ if protocol.isActive():
454
+ self.stopProtocol(protocol)
455
+ self._restartWorkflow(errorsList,{protocol.getObjId(): (protocol, level)})
456
+
457
+ else: # First protocol not in streaming
458
+ if not protocol.isActive():
459
+ self.scheduleProtocol(protocol)
460
+
461
+
462
+
463
+ def _restartWorkflow(self, errorsList, restartedProtList=None):
464
+ """
465
+ This function restart a workflow from a selected protocol.
466
+ All previous results will be deleted
467
+ Actions done here are:
468
+ 1. Set the protocol run mode (RESTART). All previous results will be
469
+ deleted
470
+ 2. Schedule the protocol if not is an interactive protocol
471
+ 3. For each of the dependents protocols, repeat from step 1
472
+ """
473
+ if restartedProtList is not None:
474
+ for protocol, level in restartedProtList.values():
475
+ if not protocol.isInteractive():
476
+ try:
477
+ if protocol.isScheduled():
478
+ continue
479
+ elif protocol.isActive():
480
+ self.stopProtocol(protocol)
481
+ protocol.runMode.set(MODE_RESTART)
482
+ self.scheduleProtocol(protocol,
483
+ initialSleepTime=level*INITIAL_SLEEP_TIME)
484
+ except Exception as ex:
485
+ errorsList.append("Error trying to restart a protocol: %s"
486
+ "\nERROR: %s\n" % (protocol.getObjLabel(),
487
+ ex))
488
+ break
489
+ else:
490
+ protocol.setStatus(pwprot.STATUS_SAVED)
491
+ self._storeProtocol(protocol)
492
+ protocol.runMode.set(MODE_RESTART)
493
+ self._setupProtocol(protocol)
494
+ protocol.makePathsAndClean() # Create working dir if necessary
495
+ # Delete the relations created by this protocol
496
+ self.mapper.deleteRelations(self)
497
+ self.mapper.commit()
498
+ self.mapper.store(protocol)
499
+ self.mapper.commit()
500
+
501
+ def _fixProtParamsConfiguration(self, protocol=None):
502
+ """
503
+ This function fix:
504
+ 1. The old parameters configuration in the protocols.
505
+ Now, dependent protocols have a pointer to the parent protocol, and
506
+ the extended parameter has a parent output value
507
+ """
508
+ # Take the old configuration attributes and fix the pointer
509
+ oldStylePointerList = [item for key, item in
510
+ protocol.iterInputAttributes()
511
+ if not isinstance(item.getObjValue(),
512
+ pwprot.Protocol)]
513
+ if oldStylePointerList:
514
+ # Fix the protocol parameters
515
+ for pointer in oldStylePointerList:
516
+ auxPointer = pointer.getObjValue()
517
+ pointer.set(self.getRunsGraph().getNode(str(pointer.get().getObjParentId())).run)
518
+ pointer.setExtended(auxPointer.getLastName())
519
+ protocol._store()
520
+ self._storeProtocol(protocol)
521
+ self._updateProtocol(protocol)
522
+ self.mapper.commit()
523
+
524
+ def stopWorkFlow(self, activeProtList):
525
+ """
526
+ This function can stop a workflow from a selected protocol
527
+ :param initialProtocol: selected protocol
528
+ """
529
+ errorProtList = []
530
+ for protocol in activeProtList.values():
531
+ try:
532
+ self.stopProtocol(protocol)
533
+ except Exception:
534
+ errorProtList.append(protocol)
535
+ return errorProtList
536
+
537
+ def resetWorkFlow(self, workflowProtocolList):
538
+ """
539
+ This function can reset a workflow from a selected protocol
540
+ :param initialProtocol: selected protocol
541
+ """
542
+ errorProtList = []
543
+ if workflowProtocolList:
544
+ for protocol, level in workflowProtocolList.values():
545
+ if protocol.getStatus() != pwprot.STATUS_SAVED:
546
+ try:
547
+ self.resetProtocol(protocol)
548
+ except Exception:
549
+ errorProtList.append(protocol)
550
+ return errorProtList
551
+
552
+ def launchWorkflow(self, workflowProtocolList, mode=MODE_RESUME):
553
+ """
554
+ This function can launch a workflow from a selected protocol in two
555
+ modes depending on the 'mode' value (RESTART, CONTINUE)
556
+ Actions done here are:
557
+
558
+ 1. Check if the workflow has active protocols.
559
+ 2. Fix the workflow if is not properly configured
560
+ 3. Restart or Continue a workflow starting from the protocol depending
561
+ on the 'mode' value
562
+
563
+ """
564
+ errorsList = []
565
+ if mode == MODE_RESTART:
566
+ self._restartWorkflow(errorsList, workflowProtocolList)
567
+ else:
568
+ self._continueWorkflow(errorsList,workflowProtocolList)
569
+ return errorsList
570
+
571
+ def launchProtocol(self, protocol, wait=False, scheduled=False,
572
+ force=False):
573
+ """ In this function the action of launching a protocol
574
+ will be initiated. Actions done here are:
575
+
576
+ 1. Store the protocol and assign name and working dir
577
+ 2. Create the working dir and also the protocol independent db
578
+ 3. Call the launch method in protocol.job to handle submission:
579
+ mpi, thread, queue.
580
+
581
+ If the protocol has some prerequisites (other protocols that
582
+ needs to be finished first), it will be scheduled.
583
+
584
+ :param protocol: Protocol instance to launch
585
+ :param wait: Optional. If true, this method
586
+ will wait until execution is finished. Used in tests.
587
+ :param scheduled: Optional. If true, run.db and paths
588
+ already exist and are preserved.
589
+ :param force: Optional. If true, launch is forced, regardless
590
+ latter dependent executions. Used when restarting many protocols a once.
591
+
592
+ """
593
+ if protocol.getPrerequisites() and not scheduled:
594
+ return self.scheduleProtocol(protocol)
595
+
596
+ isRestart = protocol.getRunMode() == MODE_RESTART
597
+
598
+ if not force:
599
+ if (not protocol.isInteractive() and not protocol.isInStreaming()) or isRestart:
600
+ self._checkModificationAllowed([protocol],
601
+ 'Cannot RE-LAUNCH protocol')
602
+
603
+ protocol.setStatus(pwprot.STATUS_LAUNCHED)
604
+ self._setupProtocol(protocol)
605
+
606
+ # Prepare a separate db for this run if not from schedule jobs
607
+ # Scheduled protocols will load the project db from the run.db file,
608
+ # so there is no need to copy the database
609
+
610
+ if not scheduled:
611
+ protocol.makePathsAndClean() # Create working dir if necessary
612
+ # Delete the relations created by this protocol
613
+ if isRestart:
614
+ self.mapper.deleteRelations(self)
615
+ # Clean and persist execution attributes; otherwise, this would retain old job IDs and PIDs.
616
+ protocol.cleanExecutionAttributes()
617
+ protocol._store(protocol._jobId)
618
+
619
+ self.mapper.commit()
620
+
621
+ # NOTE: now we are simply copying the entire project db, this can be
622
+ # changed later to only create a subset of the db need for the run
623
+ pwutils.path.copyFile(self.dbPath, protocol.getDbPath())
624
+
625
+ # Launch the protocol; depending on the case, either the pId or the jobId will be set in this call
626
+ pwprot.launch(protocol, wait)
627
+
628
+ # Commit changes
629
+ if wait: # This is only useful for launching tests...
630
+ self._updateProtocol(protocol)
631
+ else:
632
+ self.mapper.store(protocol)
633
+ self.mapper.commit()
634
+
635
+ def scheduleProtocol(self, protocol, prerequisites=[], initialSleepTime=0):
636
+ """ Schedule a new protocol that will run when the input data
637
+ is available and the prerequisites are finished.
638
+
639
+ :param protocol: the protocol that will be scheduled.
640
+ :param prerequisites: a list with protocols ids that the scheduled
641
+ protocol will wait for.
642
+ :param initialSleepTime: number of seconds to wait before
643
+ checking input's availability
644
+
645
+ """
646
+ isRestart = protocol.getRunMode() == MODE_RESTART
647
+
648
+ protocol.setStatus(pwprot.STATUS_SCHEDULED)
649
+ protocol.addPrerequisites(*prerequisites)
650
+
651
+ self._setupProtocol(protocol)
652
+ protocol.makePathsAndClean() # Create working dir if necessary
653
+ # Delete the relations created by this protocol if any
654
+ if isRestart:
655
+ self.mapper.deleteRelations(self)
656
+ self.mapper.commit()
657
+
658
+ # Prepare a separate db for this run
659
+ # NOTE: now we are simply copying the entire project db, this can be
660
+ # changed later to only create a subset of the db need for the run
661
+ pwutils.path.copyFile(self.dbPath, protocol.getDbPath())
662
+ # Launch the protocol, the jobId should be set after this call
663
+ pwprot.schedule(protocol, initialSleepTime=initialSleepTime)
664
+ self.mapper.store(protocol)
665
+ self.mapper.commit()
666
+
667
+ def _updateProtocol(self, protocol: Protocol, tries=0, checkPid=False,
668
+ skipUpdatedProtocols=True):
669
+
670
+ # If this is read only exit
671
+ if self.openedAsReadOnly():
672
+ return pw.NOT_UPDATED_READ_ONLY
673
+
674
+ try:
675
+
676
+ # Backup the values of 'jobId', 'label' and 'comment'
677
+ # to be restored after the .copy
678
+ jobId = protocol.getJobIds().clone() # Use clone to prevent this variable from being overwritten or cleared in the latter .copy() call
679
+ label = protocol.getObjLabel()
680
+ comment = protocol.getObjComment()
681
+
682
+ if skipUpdatedProtocols:
683
+ # If we are already updated, comparing timestamps
684
+ if pwprot.isProtocolUpToDate(protocol):
685
+
686
+ # Always check for the status of the process (queue job or pid)
687
+ self.checkIsAlive(protocol)
688
+ return pw.NOT_UPDATED_UNNECESSARY
689
+
690
+
691
+ # If the protocol database has ....
692
+ # Comparing date will not work unless we have a reliable
693
+ # lastModificationDate of a protocol in the project.sqlite
694
+ prot2 = pwprot.getProtocolFromDb(self.path,
695
+ protocol.getDbPath(),
696
+ protocol.getObjId())
697
+
698
+ # Capture the db timestamp before loading.
699
+ lastUpdateTime = pwutils.getFileLastModificationDate(protocol.getDbPath())
700
+
701
+ # Copy is only working for db restored objects
702
+ protocol.setMapper(self.mapper)
703
+
704
+ localOutputs = list(protocol._outputs)
705
+ protocol.copy(prot2, copyId=False, excludeInputs=True)
706
+
707
+ # merge outputs: This is necessary when outputs are added from the GUI
708
+ # e.g.: adding coordinates from analyze result and protocol is active (interactive).
709
+ for attr in localOutputs:
710
+ if attr not in protocol._outputs:
711
+ protocol._outputs.append(attr)
712
+
713
+ # Restore backup values
714
+ if protocol.useQueueForProtocol() and jobId: # If jobId not empty then restore value as the db is empty
715
+ # Case for direct protocol launch from the GUI. Without passing through a scheduling process.
716
+ # In this case the jobid is obtained by the GUI and the job id should be preserved.
717
+ protocol.setJobIds(jobId)
718
+
719
+ # In case of scheduling a protocol, the jobid is obtained during the "scheduling job"
720
+ # and it is written in the rub.db. Therefore, it should be taken from there.
721
+
722
+ protocol.setObjLabel(label)
723
+ protocol.setObjComment(comment)
724
+ # Use the run.db timestamp instead of the system TS to prevent
725
+ # possible inconsistencies.
726
+ protocol.lastUpdateTimeStamp.set(lastUpdateTime)
727
+
728
+ # Check pid at the end, once updated
729
+ if checkPid:
730
+ self.checkIsAlive(protocol)
731
+
732
+ self.mapper.store(protocol)
733
+
734
+ # Close DB connections
735
+ prot2.getProject().closeMapper()
736
+ prot2.closeMappers()
737
+
738
+ except Exception as ex:
739
+ if tries == 3: # 3 tries have been failed
740
+ traceback.print_exc()
741
+ # If any problem happens, the protocol will be marked
742
+ # with a FAILED status
743
+ try:
744
+ protocol.setFailed(str(ex))
745
+ self.mapper.store(protocol)
746
+ except Exception:
747
+ pass
748
+ return pw.NOT_UPDATED_ERROR
749
+ else:
750
+ logger.warning("Couldn't update protocol %s(jobId=%s) from it's own database. ERROR: %s, attempt=%d"
751
+ % (protocol.getObjName(), jobId, ex, tries))
752
+ time.sleep(0.5)
753
+ self._updateProtocol(protocol, tries + 1)
754
+
755
+ return pw.PROTOCOL_UPDATED
756
+
757
+ def checkIsAlive(self, protocol):
758
+ """ Check if a protocol is alive based on its jobid or pid"""
759
+ if protocol.getPid() == 0:
760
+ self.checkJobId(protocol)
761
+ else:
762
+ self.checkPid(protocol)
763
+
764
+ def stopProtocol(self, protocol):
765
+ """ Stop a running protocol """
766
+ try:
767
+ if protocol.getStatus() in ACTIVE_STATUS:
768
+ self._updateProtocol(protocol) # update protocol to have the latest rub.db values
769
+ pwprot.stop(protocol)
770
+ except Exception as e:
771
+ logger.error("Couldn't stop the protocol: %s" % e)
772
+ raise
773
+ finally:
774
+ protocol.setAborted()
775
+ protocol.setMapper(self.createMapper(protocol.getDbPath()))
776
+ protocol._store()
777
+ self._storeProtocol(protocol)
778
+ protocol.getMapper().close()
779
+
780
+ def resetProtocol(self, protocol):
781
+ """ Stop a running protocol """
782
+ try:
783
+ if protocol.getStatus() in ACTIVE_STATUS:
784
+ pwprot.stop(protocol)
785
+ except Exception:
786
+ raise
787
+ finally:
788
+ protocol.setSaved()
789
+ protocol.runMode.set(MODE_RESTART)
790
+ protocol.makePathsAndClean() # Create working dir if necessary
791
+ # Clean jobIds, Pid and StepsDone;
792
+ protocol.cleanExecutionAttributes() # otherwise, this would retain old executions info
793
+ protocol._store()
794
+
795
+ def continueProtocol(self, protocol):
796
+ """ This function should be called
797
+ to mark a protocol that have an interactive step
798
+ waiting for approval that can continue
799
+ """
800
+ protocol.continueFromInteractive()
801
+ self.launchProtocol(protocol)
802
+
803
+ def __protocolInList(self, prot, protocols):
804
+ """ Check if a protocol is in a list comparing the ids. """
805
+ for p in protocols:
806
+ if p.getObjId() == prot.getObjId():
807
+ return True
808
+ return False
809
+
810
+ def __validDependency(self, prot, child, protocols):
811
+ """ Check if the given child is a true dependency of the protocol
812
+ in order to avoid any modification.
813
+ """
814
+ return (not self.__protocolInList(child, protocols) and
815
+ not child.isSaved() and not child.isScheduled())
816
+
817
+ def _getProtocolsDependencies(self, protocols):
818
+ error = ''
819
+ runsGraph = self.getRunsGraph()
820
+ for prot in protocols:
821
+ node = runsGraph.getNode(prot.strId())
822
+ if node:
823
+ childs = [node.run for node in node.getChilds() if
824
+ self.__validDependency(prot, node.run, protocols)]
825
+ if childs:
826
+ deps = [' ' + c.getRunName() for c in childs]
827
+ error += '\n *%s* is referenced from:\n - ' % prot.getRunName()
828
+ error += '\n - '.join(deps)
829
+ return error
830
+
831
+ def _getProtocolDescendents(self, protocol):
832
+ """Getting the descendents protocols from a given one"""
833
+ runsGraph = self.getRunsGraph()
834
+ visitedNodes = dict()
835
+ node = runsGraph.getNode(protocol.strId())
836
+ if node is None:
837
+ return visitedNodes
838
+
839
+ visitedNodes[int(node.getName())] = node
840
+
841
+ def getDescendents(rootNode):
842
+ for child in rootNode.getChilds():
843
+ if int(child.getName()) not in visitedNodes:
844
+ visitedNodes[int(child.getName())] = child
845
+ getDescendents(child)
846
+
847
+ getDescendents(node)
848
+ return visitedNodes
849
+
850
+ def getProtocolCompatibleOutputs(self, protocol, classes, condition):
851
+ """Getting the outputs compatible with an object type. The outputs of the child protocols are excluded. """
852
+ objects = []
853
+ maxNum = 200
854
+ protocolDescendents = self._getProtocolDescendents(protocol)
855
+ runs = self.getRuns(refresh=False)
856
+
857
+ for prot in runs:
858
+ # Make sure we don't include previous output of the same
859
+ # and other descendent protocols
860
+ if prot.getObjId() not in protocolDescendents:
861
+ # Check if the protocol itself is one of the desired classes
862
+ if any(issubclass(prot.getClass(), c) for c in classes):
863
+ p = pwobj.Pointer(prot)
864
+ objects.append(p)
865
+
866
+ try:
867
+ # paramName and attr must be set to None
868
+ # Otherwise, if a protocol has failed and the corresponding output object of type XX does not exist
869
+ # any other protocol that uses objects of type XX as input will not be able to choose then using
870
+ # the magnifier glass (object selector of type XX)
871
+ paramName = None
872
+ attr = None
873
+ for paramName, attr in prot.iterOutputAttributes(includePossible=True):
874
+ def _checkParam(paramName, attr):
875
+ # If attr is a subclasses of any desired one, add it to the list
876
+ # we should also check if there is a condition, the object
877
+ # must comply with the condition
878
+ p = None
879
+
880
+ match = False
881
+ cancelConditionEval = False
882
+ possibleOutput = isinstance(attr, type)
883
+
884
+ # Go through all compatible Classes coming from in pointerClass string
885
+ for c in classes:
886
+ # If attr is an instance
887
+ if isinstance(attr, c):
888
+ match = True
889
+ break
890
+ # If it is a class already: "possibleOutput" case. In this case attr is the class and not
891
+ # an instance of c. In this special case
892
+ elif possibleOutput and attr == c:
893
+ match = True
894
+ cancelConditionEval = True
895
+
896
+ # If attr matches the class
897
+ if match:
898
+ if cancelConditionEval or not condition or attr.evalCondition(condition):
899
+ p = pwobj.Pointer(prot, extended=paramName)
900
+ p._allowsSelection = True
901
+ objects.append(p)
902
+ return
903
+
904
+ # JMRT: For all sets, we don't want to include the
905
+ # subitems here for performance reasons (e.g. SetOfParticles)
906
+ # Thus, a Set class can define EXPOSE_ITEMS = True
907
+ # to enable the inclusion of its items here
908
+ if getattr(attr, 'EXPOSE_ITEMS', False) and not possibleOutput:
909
+ # If the ITEM type match any of the desired classes
910
+ # we will add some elements from the set
911
+ if (attr.ITEM_TYPE is not None and
912
+ any(issubclass(attr.ITEM_TYPE, c) for c in classes)):
913
+ if p is None: # This means the set have not be added
914
+ p = pwobj.Pointer(prot, extended=paramName)
915
+ p._allowsSelection = False
916
+ objects.append(p)
917
+ # Add each item on the set to the list of objects
918
+ try:
919
+ for i, item in enumerate(attr):
920
+ if i == maxNum: # Only load up to NUM particles
921
+ break
922
+ pi = pwobj.Pointer(prot, extended=paramName)
923
+ pi.addExtended(item.getObjId())
924
+ pi._parentObject = p
925
+ objects.append(pi)
926
+ except Exception as ex:
927
+ print("Error loading items from:")
928
+ print(" protocol: %s, attribute: %s" % (prot.getRunName(), paramName))
929
+ print(" dbfile: ", os.path.join(self.getPath(), attr.getFileName()))
930
+ print(ex)
931
+
932
+ _checkParam(paramName, attr)
933
+ # The following is a dirty fix for the RCT case where there
934
+ # are inner output, maybe we should consider extend this for
935
+ # in a more general manner
936
+ for subParam in ['_untilted', '_tilted']:
937
+ if hasattr(attr, subParam):
938
+ _checkParam('%s.%s' % (paramName, subParam),
939
+ getattr(attr, subParam))
940
+ except Exception as e:
941
+ print("Cannot read attributes for %s (%s)" % (prot.getClass(), e))
942
+
943
+ return objects
944
+
945
+ def _checkProtocolsDependencies(self, protocols, msg):
946
+ """ Check if the protocols have dependencies.
947
+ This method is used before delete or save protocols to be sure
948
+ it is not referenced from other runs. (an Exception is raised)
949
+ Params:
950
+ protocols: protocol list to be analyzed.
951
+ msg: String message to be prefixed to Exception error.
952
+ """
953
+ # Check if the protocol have any dependencies
954
+ error = self._getProtocolsDependencies(protocols)
955
+ if error:
956
+ raise ModificationNotAllowedException(msg + error)
957
+
958
+ def _checkModificationAllowed(self, protocols, msg):
959
+ """ Check if any modification operation is allowed for
960
+ this group of protocols.
961
+ """
962
+ if self.openedAsReadOnly():
963
+ raise Exception(msg + " Running in READ-ONLY mode.")
964
+
965
+ self._checkProtocolsDependencies(protocols, msg)
966
+
967
+ def _getSubworkflow(self, protocol, fixProtParam=True, getStopped=True):
968
+ """
969
+ This function get the workflow from "protocol" and determine the
970
+ protocol level into the graph. Also, checks if there are active
971
+ protocols excluding interactive protocols.
972
+ :param protocol from where to start the subworkflow (included)
973
+ :param fixProtParam fix the old parameters configuration in the protocols
974
+ :param getStopped takes into account protocols that aren't stopped
975
+ """
976
+ affectedProtocols = {}
977
+ affectedProtocolsActive = {}
978
+ auxProtList = []
979
+ # store the protocol and your level into the workflow
980
+ affectedProtocols[protocol.getObjId()] = [protocol, 0]
981
+ auxProtList.append([protocol.getObjId(), 0])
982
+ runGraph = self.getRunsGraph()
983
+
984
+ while auxProtList:
985
+ protId, level = auxProtList.pop(0)
986
+ protocol = runGraph.getNode(str(protId)).run
987
+
988
+ # Increase the level for the children
989
+ level = level + 1
990
+
991
+ if fixProtParam:
992
+ self._fixProtParamsConfiguration(protocol)
993
+
994
+ if not getStopped and protocol.isActive():
995
+ affectedProtocolsActive[protocol.getObjId()] = protocol
996
+ elif not protocol.getObjId() in affectedProtocolsActive.keys() and getStopped and \
997
+ not protocol.isSaved() and protocol.getStatus() != STATUS_INTERACTIVE:
998
+ affectedProtocolsActive[protocol.getObjId()] = protocol
999
+
1000
+ node = runGraph.getNode(protocol.strId())
1001
+ dependencies = [node.run for node in node.getChilds()]
1002
+ for dep in dependencies:
1003
+ if not dep.getObjId() in auxProtList:
1004
+ auxProtList.append([dep.getObjId(), level])
1005
+
1006
+ if not dep.getObjId() in affectedProtocols.keys():
1007
+ affectedProtocols[dep.getObjId()] = [dep, level]
1008
+ elif level > affectedProtocols[dep.getObjId()][1]:
1009
+ affectedProtocols[dep.getObjId()][1] = level
1010
+
1011
+ return affectedProtocols, affectedProtocolsActive
1012
+
1013
+ def deleteProtocol(self, *protocols):
1014
+ self._checkModificationAllowed(protocols, 'Cannot DELETE protocols')
1015
+
1016
+ for prot in protocols:
1017
+ # Delete the relations created by this protocol
1018
+ self.mapper.deleteRelations(prot)
1019
+ # Delete from protocol from database
1020
+ self.mapper.delete(prot)
1021
+ wd = prot.workingDir.get()
1022
+
1023
+ if wd.startswith(PROJECT_RUNS):
1024
+ prot.cleanWorkingDir()
1025
+ else:
1026
+ logger.info("Can't delete protocol %s. Its workingDir %s does not starts with %s " % (prot, wd, PROJECT_RUNS))
1027
+
1028
+ self.mapper.commit()
1029
+
1030
+ def deleteProtocolOutput(self, protocol, output):
1031
+ """ Delete a given object from the project.
1032
+ Usually to clean up some outputs.
1033
+ """
1034
+ node = self.getRunsGraph().getNode(protocol.strId())
1035
+ deps = []
1036
+
1037
+ for node in node.getChilds():
1038
+ for _, inputObj in node.run.iterInputAttributes():
1039
+ value = inputObj.get()
1040
+ if (value is not None and
1041
+ value.getObjId() == output.getObjId() and
1042
+ not node.run.isSaved()):
1043
+ deps.append(node.run)
1044
+
1045
+ if deps:
1046
+ error = 'Cannot DELETE Object, it is referenced from:'
1047
+ for d in deps:
1048
+ error += '\n - %s' % d.getRunName()
1049
+ raise Exception(error)
1050
+ else:
1051
+ protocol.deleteOutput(output)
1052
+ pwutils.path.copyFile(self.dbPath, protocol.getDbPath())
1053
+
1054
+ def __setProtocolLabel(self, newProt):
1055
+ """ Set a readable label to a newly created protocol.
1056
+ We will try to find another existing protocol with the default label
1057
+ and then use an incremental labeling in parenthesis (<number>++)
1058
+ """
1059
+ defaultLabel = newProt.getClassLabel()
1060
+ maxSuffix = 0
1061
+
1062
+ for prot in self.getRuns(iterate=True, refresh=False):
1063
+ otherProtLabel = prot.getObjLabel()
1064
+ m = REGEX_NUMBER_ENDING.match(otherProtLabel)
1065
+ if m and m.groupdict()['prefix'].strip() == defaultLabel:
1066
+ stringSuffix = m.groupdict()['number'].strip('(').strip(')')
1067
+ try:
1068
+ maxSuffix = max(int(stringSuffix), maxSuffix)
1069
+ except:
1070
+ logger.error("Couldn't set protocol's label. %s" % stringSuffix)
1071
+ elif otherProtLabel == defaultLabel: # When only we have the prefix,
1072
+ maxSuffix = max(1, maxSuffix) # this REGEX don't match.
1073
+
1074
+ if maxSuffix:
1075
+ protLabel = '%s (%d)' % (defaultLabel, maxSuffix+1)
1076
+ else:
1077
+ protLabel = defaultLabel
1078
+
1079
+ newProt.setObjLabel(protLabel)
1080
+
1081
+ def newProtocol(self, protocolClass, **kwargs):
1082
+ """ Create a new protocol from a given class. """
1083
+ newProt = protocolClass(project=self, **kwargs)
1084
+ # Only set a default label to the protocol if is was not
1085
+ # set through the kwargs
1086
+ if not newProt.getObjLabel():
1087
+ self.__setProtocolLabel(newProt)
1088
+
1089
+ newProt.setMapper(self.mapper)
1090
+ newProt.setProject(self)
1091
+
1092
+ return newProt
1093
+
1094
+ def __getIOMatches(self, node, childNode):
1095
+ """ Check if some output of node is used as input in childNode.
1096
+ Return the list of attribute names that matches.
1097
+ Used from self.copyProtocol
1098
+ """
1099
+ matches = []
1100
+ for iKey, iAttr in childNode.run.iterInputAttributes():
1101
+ # As this point iAttr should be always a Pointer that
1102
+ # points to the output of other protocol
1103
+ if iAttr.getObjValue() is node.run:
1104
+ oKey = iAttr.getExtended()
1105
+ matches.append((oKey, iKey))
1106
+ else:
1107
+ for oKey, oAttr in node.run.iterOutputAttributes():
1108
+ # If node output is "real" and iAttr is still just a pointer
1109
+ # the iAttr.get() will return None
1110
+ pointed = iAttr.get()
1111
+ if pointed is not None and oAttr.getObjId() == pointed.getObjId():
1112
+ matches.append((oKey, iKey))
1113
+
1114
+ return matches
1115
+
1116
+ def __cloneProtocol(self, protocol):
1117
+ """ Make a copy of the protocol parameters, not outputs.
1118
+ We will label the new protocol with the same name adding the
1119
+ parenthesis as follow -> (copy) -> (copy 2) -> (copy 3)
1120
+ """
1121
+ newProt = self.newProtocol(protocol.getClass())
1122
+ oldProtName = protocol.getRunName()
1123
+ maxSuffix = 0
1124
+
1125
+ # if '(copy...' suffix is not in the old name, we add it in the new name
1126
+ # and setting the newnumber
1127
+ mOld = REGEX_NUMBER_ENDING_CP.match(oldProtName)
1128
+ if mOld:
1129
+ newProtPrefix = mOld.groupdict()['prefix']
1130
+ if mOld.groupdict()['number'] == '':
1131
+ oldNumber = 1
1132
+ else:
1133
+ oldNumber = int(mOld.groupdict()['number'])
1134
+ else:
1135
+ newProtPrefix = oldProtName + ' (copy'
1136
+ oldNumber = 0
1137
+ newNumber = oldNumber + 1
1138
+
1139
+ # looking for "<old name> (copy" prefixes in the project and
1140
+ # setting the newNumber as the maximum+1
1141
+ for prot in self.getRuns(iterate=True, refresh=False):
1142
+ otherProtLabel = prot.getObjLabel()
1143
+ mOther = REGEX_NUMBER_ENDING_CP.match(otherProtLabel)
1144
+ if mOther and mOther.groupdict()['prefix'] == newProtPrefix:
1145
+ stringSuffix = mOther.groupdict()['number']
1146
+ if stringSuffix == '':
1147
+ stringSuffix = 1
1148
+ maxSuffix = max(maxSuffix, int(stringSuffix))
1149
+ if newNumber <= maxSuffix:
1150
+ newNumber = maxSuffix + 1
1151
+
1152
+ # building the new name
1153
+ if newNumber == 1:
1154
+ newProtLabel = newProtPrefix + ')'
1155
+ else:
1156
+ newProtLabel = '%s %d)' % (newProtPrefix, newNumber)
1157
+
1158
+ newProt.setObjLabel(newProtLabel)
1159
+ newProt.copyDefinitionAttributes(protocol)
1160
+ newProt.copyAttributes(protocol, 'hostName', '_useQueue', '_queueParams')
1161
+ newProt.runMode.set(MODE_RESTART)
1162
+ newProt.cleanExecutionAttributes() # Clean jobIds and Pid; otherwise, this would retain old job IDs and PIDs.
1163
+
1164
+ return newProt
1165
+
1166
+ def copyProtocol(self, protocol):
1167
+ """ Make a copy of the protocol,
1168
+ Return a new instance with copied values. """
1169
+ result = None
1170
+
1171
+ if isinstance(protocol, pwprot.Protocol):
1172
+ result = self.__cloneProtocol(protocol)
1173
+
1174
+ elif isinstance(protocol, list):
1175
+ # Handle the copy of a list of protocols
1176
+ # for this case we need to update the references of input/outputs
1177
+ newDict = {}
1178
+
1179
+ for prot in protocol:
1180
+ newProt = self.__cloneProtocol(prot)
1181
+ newDict[prot.getObjId()] = newProt
1182
+ self.saveProtocol(newProt)
1183
+
1184
+ g = self.getRunsGraph()
1185
+
1186
+ for prot in protocol:
1187
+ node = g.getNode(prot.strId())
1188
+ newProt = newDict[prot.getObjId()]
1189
+
1190
+ for childNode in node.getChilds():
1191
+ newChildProt = newDict.get(childNode.run.getObjId(), None)
1192
+
1193
+ if newChildProt:
1194
+ # Get the matches between outputs/inputs of
1195
+ # node and childNode
1196
+ matches = self.__getIOMatches(node, childNode)
1197
+ # For each match, set the pointer and the extend
1198
+ # attribute to reproduce the dependencies in the
1199
+ # new workflow
1200
+ for oKey, iKey in matches:
1201
+ childPointer = getattr(newChildProt, iKey)
1202
+
1203
+ # Scalar with pointer case: If is a scalar with a pointer
1204
+ if isinstance(childPointer, pwobj.Scalar) and childPointer.hasPointer():
1205
+ # In this case childPointer becomes the contained Pointer
1206
+ childPointer = childPointer.getPointer()
1207
+
1208
+ elif isinstance(childPointer, pwobj.PointerList):
1209
+ for p in childPointer:
1210
+ if p.getObjValue().getObjId() == prot.getObjId():
1211
+ childPointer = p
1212
+ childPointer.set(newProt)
1213
+ childPointer.setExtended(oKey)
1214
+ self.mapper.store(newChildProt)
1215
+
1216
+ self.mapper.commit()
1217
+ else:
1218
+ raise Exception("Project.copyProtocol: invalid input protocol ' "
1219
+ "'type '%s'." % type(protocol))
1220
+
1221
+ return result
1222
+
1223
+ def getProtocolsDict(self, protocols=None, namesOnly=False):
1224
+ """ Creates a dict with the information of the given protocols.
1225
+
1226
+ :param protocols: list of protocols or None to include all.
1227
+ :param namesOnly: the output list will contain only the protocol names.
1228
+
1229
+ """
1230
+ protocols = protocols or self.getRuns()
1231
+
1232
+ # If the nameOnly, we will simply return a json list with their names
1233
+ if namesOnly:
1234
+ return {i: prot.getClassName() for i, prot in enumerate(protocols)}
1235
+
1236
+ # Handle the copy of a list of protocols
1237
+ # for this case we need to update the references of input/outputs
1238
+ newDict = OrderedDict()
1239
+
1240
+ for prot in protocols:
1241
+ newDict[prot.getObjId()] = prot.getDefinitionDict()
1242
+
1243
+ g = self.getRunsGraph()
1244
+
1245
+ for prot in protocols:
1246
+ protId = prot.getObjId()
1247
+ node = g.getNode(prot.strId())
1248
+
1249
+ for childNode in node.getChilds():
1250
+ childId = childNode.run.getObjId()
1251
+ childProt = childNode.run
1252
+ if childId in newDict:
1253
+ childDict = newDict[childId]
1254
+ # Get the matches between outputs/inputs of
1255
+ # node and childNode
1256
+ matches = self.__getIOMatches(node, childNode)
1257
+ for oKey, iKey in matches:
1258
+ inputAttr = getattr(childProt, iKey)
1259
+ if isinstance(inputAttr, pwobj.PointerList):
1260
+ childDict[iKey] = [p.getUniqueId() for p in
1261
+ inputAttr]
1262
+ else:
1263
+ childDict[iKey] = '%s.%s' % (
1264
+ protId, oKey) # equivalent to pointer.getUniqueId
1265
+
1266
+ return newDict
1267
+
1268
+ def getProtocolsJson(self, protocols=None, namesOnly=False):
1269
+ """
1270
+ Wraps getProtocolsDict to get a json string
1271
+
1272
+ :param protocols: list of protocols or None to include all.
1273
+ :param namesOnly: the output list will contain only the protocol names.
1274
+
1275
+ """
1276
+ newDict = self.getProtocolsDict(protocols=protocols, namesOnly=namesOnly)
1277
+ return json.dumps(list(newDict.values()),
1278
+ indent=4, separators=(',', ': '))
1279
+
1280
+ def exportProtocols(self, protocols, filename):
1281
+ """ Create a text json file with the info
1282
+ to import the workflow into another project.
1283
+ This method is very similar to copyProtocol
1284
+
1285
+ :param protocols: a list of protocols to export.
1286
+ :param filename: the filename where to write the workflow.
1287
+
1288
+ """
1289
+ jsonStr = self.getProtocolsJson(protocols)
1290
+ f = open(filename, 'w')
1291
+ f.write(jsonStr)
1292
+ f.close()
1293
+
1294
+ def loadProtocols(self, filename=None, jsonStr=None):
1295
+ """ Load protocols generated in the same format as self.exportProtocols.
1296
+
1297
+ :param filename: the path of the file where to read the workflow.
1298
+ :param jsonStr:
1299
+
1300
+ Note: either filename or jsonStr should be not None.
1301
+
1302
+ """
1303
+ importDir = None
1304
+ if filename:
1305
+ with open(filename) as f:
1306
+ importDir = os.path.dirname(filename)
1307
+ protocolsList = json.load(f)
1308
+
1309
+ elif jsonStr:
1310
+ protocolsList = json.loads(jsonStr)
1311
+ else:
1312
+ logger.error("Invalid call to loadProtocols. Either filename or jsonStr has to be passed.")
1313
+ return
1314
+
1315
+ emProtocols = self._domain.getProtocols()
1316
+ newDict = OrderedDict()
1317
+
1318
+ # First iteration: create all protocols and setup parameters
1319
+ for i, protDict in enumerate(protocolsList):
1320
+ protClassName = protDict['object.className']
1321
+ protId = protDict['object.id']
1322
+ protClass = emProtocols.get(protClassName, None)
1323
+
1324
+ if protClass is None:
1325
+ logger.error("Protocol with class name '%s' not found. Are you missing its plugin?." % protClassName)
1326
+ else:
1327
+ protLabel = protDict.get('object.label', None)
1328
+ prot = self.newProtocol(protClass,
1329
+ objLabel=protLabel,
1330
+ objComment=protDict.get('object.comment', None))
1331
+ protocolsList[i] = prot.processImportDict(protDict, importDir) if importDir else protDict
1332
+
1333
+ prot._useQueue.set(protDict.get('_useQueue', pw.Config.SCIPION_USE_QUEUE))
1334
+ prot._queueParams.set(protDict.get('_queueParams', None))
1335
+ prot._prerequisites.set(protDict.get('_prerequisites', None))
1336
+ prot.forceSchedule.set(protDict.get('forceSchedule', False))
1337
+ newDict[protId] = prot
1338
+ # This saves the protocol JUST with the common attributes. Is it necessary?
1339
+ # Actually, if after this the is an error, the protocol appears.
1340
+ self.saveProtocol(prot)
1341
+
1342
+ # Second iteration: update pointers values
1343
+ def _setPointer(pointer, value):
1344
+ # Properly setup the pointer value checking if the
1345
+ # id is already present in the dictionary
1346
+ # Value to pointers could be None: Partial workflows
1347
+ if value:
1348
+ parts = value.split('.')
1349
+
1350
+ protId = parts[0]
1351
+ # Try to get the protocol holding the input form the dictionary
1352
+ target = newDict.get(protId, None)
1353
+
1354
+ if target is None:
1355
+ # Try to use existing protocol in the project
1356
+ logger.info("Protocol identifier (%s) not self contained. Looking for it in the project." % protId)
1357
+
1358
+ try:
1359
+ target = self.getProtocol(int(protId), fromRuns=True)
1360
+ except:
1361
+ # Not a protocol..
1362
+ logger.info("%s is not a protocol identifier. Probably a direct pointer created by tests. This case is not considered." % protId)
1363
+
1364
+ if target:
1365
+ logger.info("Linking %s to existing protocol in the project: %s" % (prot, target))
1366
+
1367
+ pointer.set(target)
1368
+ if not pointer.pointsNone():
1369
+ pointer.setExtendedParts(parts[1:])
1370
+
1371
+ def _setPrerequisites(prot):
1372
+ prerequisites = prot.getPrerequisites()
1373
+ if prerequisites:
1374
+ newPrerequisites = []
1375
+ for prerequisite in prerequisites:
1376
+ if prerequisite in newDict:
1377
+ newProtId = newDict[prerequisite].getObjId()
1378
+ newPrerequisites.append(newProtId)
1379
+ else:
1380
+ logger.info('"Wait for" id %s missing: ignored.' % prerequisite)
1381
+ prot._prerequisites.set(newPrerequisites)
1382
+
1383
+ for protDict in protocolsList:
1384
+ protId = protDict['object.id']
1385
+
1386
+ if protId in newDict:
1387
+ prot = newDict[protId]
1388
+ _setPrerequisites(prot)
1389
+ for paramName, attr in prot.iterDefinitionAttributes():
1390
+ if paramName in protDict:
1391
+ # If the attribute is a pointer, we should look
1392
+ # if the id is already in the dictionary and
1393
+ # set the extended property
1394
+ if attr.isPointer():
1395
+ _setPointer(attr, protDict[paramName])
1396
+ # This case is similar to Pointer, but the values
1397
+ # is a list and we will setup a pointer for each value
1398
+ elif isinstance(attr, pwobj.PointerList):
1399
+ attribute = protDict[paramName]
1400
+ if attribute is None:
1401
+ continue
1402
+ for value in attribute:
1403
+ p = pwobj.Pointer()
1404
+ _setPointer(p, value)
1405
+ attr.append(p)
1406
+ # For "normal" parameters we just set the string value
1407
+ else:
1408
+ try:
1409
+ attr.set(protDict[paramName])
1410
+ # Case for Scalars with pointers. So far this will work for Numbers. With Strings (still there are no current examples)
1411
+ # We will need something different to test if the value look like a pointer: regex? ####.text
1412
+ except ValueError as e:
1413
+ newPointer = pwobj.Pointer()
1414
+ _setPointer(newPointer, protDict[paramName])
1415
+ attr.setPointer(newPointer)
1416
+
1417
+ self.mapper.store(prot)
1418
+
1419
+ self.mapper.commit()
1420
+
1421
+ return newDict
1422
+
1423
+ def saveProtocol(self, protocol):
1424
+ self._checkModificationAllowed([protocol], 'Cannot SAVE protocol')
1425
+
1426
+ if (protocol.isRunning() or protocol.isFinished()
1427
+ or protocol.isLaunched()):
1428
+ raise ModificationNotAllowedException('Cannot SAVE a protocol that is %s. '
1429
+ 'Copy it instead.' % protocol.getStatus())
1430
+
1431
+ protocol.setStatus(pwprot.STATUS_SAVED)
1432
+ if protocol.hasObjId():
1433
+ self._storeProtocol(protocol)
1434
+ else:
1435
+ self._setupProtocol(protocol)
1436
+
1437
+ def getProtocolFromRuns(self, protId):
1438
+ """ Returns the protocol with the id=protId from the runs list (memory) or None"""
1439
+ if self.runs:
1440
+ for run in self.runs:
1441
+ if run.getObjId() == protId:
1442
+ return run
1443
+
1444
+ return None
1445
+
1446
+ def getProtocol(self, protId, fromRuns=False):
1447
+ """ Returns the protocol with the id=protId or raises an Exception
1448
+
1449
+ :param protId: integer with an existing protocol identifier
1450
+ :param fromRuns: If true, it tries to get it from the runs list (memory) avoiding querying the db."""
1451
+
1452
+ protocol = self.getProtocolFromRuns(protId) if fromRuns else None
1453
+
1454
+ if protocol is None:
1455
+ protocol = self.mapper.selectById(protId)
1456
+
1457
+ if not isinstance(protocol, pwprot.Protocol):
1458
+ raise Exception('>>> ERROR: Invalid protocol id: %d' % protId)
1459
+
1460
+ self._setProtocolMapper(protocol)
1461
+
1462
+ return protocol
1463
+
1464
+ # FIXME: this function just return if a given object exists, not
1465
+ # if it is a protocol, so it is incorrect judging by the name
1466
+ # Moreover, a more consistent name (comparing to similar methods)
1467
+ # would be: hasProtocol
1468
+ def doesProtocolExists(self, protId):
1469
+ return self.mapper.exists(protId)
1470
+
1471
+ def getProtocolsByClass(self, className):
1472
+ return self.mapper.selectByClass(className)
1473
+
1474
+ def getObject(self, objId):
1475
+ """ Retrieve an object from the db given its id. """
1476
+ return self.mapper.selectById(objId)
1477
+
1478
+ def _setHostConfig(self, protocol):
1479
+ """ Set the appropriate host config to the protocol
1480
+ give its value of 'hostname'
1481
+ """
1482
+ hostName = protocol.getHostName()
1483
+ hostConfig = self.getHostConfig(hostName)
1484
+ protocol.setHostConfig(hostConfig)
1485
+
1486
+ def _storeProtocol(self, protocol):
1487
+ # Read only mode
1488
+ if not self.openedAsReadOnly():
1489
+ self.mapper.store(protocol)
1490
+ self.mapper.commit()
1491
+
1492
+ def _setProtocolMapper(self, protocol):
1493
+ """ Set the project and mapper to the protocol. """
1494
+
1495
+ # Tolerate loading errors. For support.
1496
+ # When only having the sqlite, sometime there are exceptions here
1497
+ # due to the absence of a set.
1498
+ from pyworkflow.mapper.sqlite import SqliteFlatMapperException
1499
+ try:
1500
+
1501
+ protocol.setProject(self)
1502
+ protocol.setMapper(self.mapper)
1503
+ self._setHostConfig(protocol)
1504
+
1505
+ except SqliteFlatMapperException:
1506
+ protocol.addSummaryWarning(
1507
+ "*Protocol loading problem*: A set related to this "
1508
+ "protocol couldn't be loaded.")
1509
+
1510
+ def _setupProtocol(self, protocol):
1511
+ """Insert a new protocol instance in the database"""
1512
+
1513
+ # Read only mode
1514
+ if not self.openedAsReadOnly():
1515
+ self._storeProtocol(protocol) # Store first to get a proper id
1516
+ # Set important properties of the protocol
1517
+ workingDir = self.getProtWorkingDir(protocol)
1518
+ self._setProtocolMapper(protocol)
1519
+
1520
+ protocol.setWorkingDir(self.getPath(PROJECT_RUNS, workingDir))
1521
+ # Update with changes
1522
+ self._storeProtocol(protocol)
1523
+
1524
+ @staticmethod
1525
+ def getProtWorkingDir(protocol):
1526
+ """
1527
+ Return the protocol working directory
1528
+ """
1529
+ return "%06d_%s" % (protocol.getObjId(), protocol.getClassName())
1530
+
1531
+ def getRuns(self, iterate=False, refresh=True, checkPids=False):
1532
+ """ Return the existing protocol runs in the project.
1533
+ """
1534
+ if self.runs is None or refresh:
1535
+ # Close db open connections to db files
1536
+ if self.runs is not None:
1537
+ for r in self.runs:
1538
+ r.closeMappers()
1539
+
1540
+ # Use new selectAll Batch
1541
+ # self.runs = self.mapper.selectAll(iterate=False,
1542
+ # objectFilter=lambda o: isinstance(o, pwprot.Protocol))
1543
+ self.runs = self.mapper.selectAllBatch(objectFilter=lambda o: isinstance(o, pwprot.Protocol))
1544
+
1545
+ # Invalidate _runsGraph because the runs are updated
1546
+ self._runsGraph = None
1547
+
1548
+ for r in self.runs:
1549
+
1550
+ self._setProtocolMapper(r)
1551
+
1552
+ # Check for run warnings
1553
+ r.checkSummaryWarnings()
1554
+
1555
+ # Update nodes that are running and were not invoked
1556
+ # by other protocols
1557
+ if r.isActive():
1558
+ if not r.isChild():
1559
+ self._updateProtocol(r, checkPid=checkPids)
1560
+
1561
+ self._annotateLastRunTime(r.endTime)
1562
+
1563
+ self.mapper.commit()
1564
+
1565
+ return self.runs
1566
+
1567
+ def _annotateLastRunTime(self, protLastTS):
1568
+ """ Sets _lastRunTime for the project if it is after current _lastRunTime"""
1569
+ try:
1570
+ if protLastTS is None:
1571
+ return
1572
+
1573
+ if self._lastRunTime is None:
1574
+ self._lastRunTime = protLastTS
1575
+ elif self._lastRunTime.datetime() < protLastTS.datetime():
1576
+ self._lastRunTime = protLastTS
1577
+ except Exception as e:
1578
+ return
1579
+
1580
+ def needRefresh(self):
1581
+ """ True if any run is active and its timestamp is older than its
1582
+ corresponding runs.db
1583
+ NOTE: If an external script changes the DB this will fail. It uses
1584
+ only in memory objects."""
1585
+ for run in self.runs:
1586
+ if run.isActive():
1587
+ if not pwprot.isProtocolUpToDate(run):
1588
+ return True
1589
+ return False
1590
+
1591
+ def checkPid(self, protocol):
1592
+ """ Check if a running protocol is still alive or not.
1593
+ The check will only be done for protocols that have not been sent
1594
+ to a queue system.
1595
+ """
1596
+ from pyworkflow.protocol.launch import _runsLocally
1597
+ pid = protocol.getPid()
1598
+
1599
+ if pid == 0:
1600
+ return
1601
+
1602
+ # Include running and scheduling ones
1603
+ # Exclude interactive protocols
1604
+ # NOTE: This may be happening even with successfully finished protocols
1605
+ # which PID is gone.
1606
+ if (protocol.isActive() and not protocol.isInteractive() and _runsLocally(protocol)
1607
+ and not pwutils.isProcessAlive(pid)):
1608
+ protocol.setFailed("Process %s not found running on the machine. "
1609
+ "It probably has died or been killed without "
1610
+ "reporting the status to Scipion. Logs might "
1611
+ "have information about what happened to this "
1612
+ "process." % pid)
1613
+
1614
+ def checkJobId(self, protocol):
1615
+ """ Check if a running protocol is still alive or not.
1616
+ The check will only be done for protocols that have been sent
1617
+ to a queue system.
1618
+ """
1619
+ jobid = protocol.getJobIds()[0]
1620
+ hostConfig = protocol.getHostConfig()
1621
+
1622
+ if jobid == UNKNOWN_JOBID:
1623
+ return
1624
+
1625
+ # Include running and scheduling ones
1626
+ # Exclude interactive protocols
1627
+ # NOTE: This may be happening even with successfully finished protocols
1628
+ # which PID is gone.
1629
+ if protocol.isActive() and not protocol.isInteractive():
1630
+
1631
+ jobStatus = _checkJobStatus(hostConfig, jobid)
1632
+
1633
+ if jobStatus == STATUS_FINISHED:
1634
+ protocol.setFailed("Process %s not found running on the machine. "
1635
+ "It probably has died or been killed without "
1636
+ "reporting the status to Scipion. Logs might "
1637
+ "have information about what happened to this "
1638
+ "process." % jobid)
1639
+
1640
+ def iterSubclasses(self, classesName, objectFilter=None):
1641
+ """ Retrieve all objects from the project that are instances
1642
+ of any of the classes in classesName list.
1643
+ Params:
1644
+ classesName: String with commas separated values of classes name.
1645
+ objectFilter: a filter function to discard some of the retrieved
1646
+ objects."""
1647
+ for objClass in classesName.split(","):
1648
+ for obj in self.mapper.selectByClass(objClass.strip(), iterate=True,
1649
+ objectFilter=objectFilter):
1650
+ yield obj
1651
+
1652
+ def getRunsGraph(self, refresh=False, checkPids=False):
1653
+ """ Build a graph taking into account the dependencies between
1654
+ different runs, ie. which outputs serves as inputs of other protocols.
1655
+ """
1656
+
1657
+ if refresh or self._runsGraph is None:
1658
+ runs = [r for r in self.getRuns(refresh=refresh, checkPids=checkPids)
1659
+ if not r.isChild()]
1660
+ self._runsGraph = self.getGraphFromRuns(runs)
1661
+
1662
+ return self._runsGraph
1663
+
1664
+ def getGraphFromRuns(self, runs):
1665
+ """
1666
+ This function will build a dependencies graph from a set
1667
+ of given runs.
1668
+
1669
+ :param runs: The input runs to build the graph
1670
+ :return: The graph taking into account run dependencies
1671
+
1672
+ """
1673
+ outputDict = {} # Store the output dict
1674
+ g = pwutils.Graph(rootName=ROOT_NODE_NAME)
1675
+
1676
+ for r in runs:
1677
+ n = g.createNode(r.strId())
1678
+ n.run = r
1679
+
1680
+ # Legacy protocols do not have a plugin!!
1681
+ develTxt = ''
1682
+ plugin = r.getPlugin()
1683
+ if plugin and plugin.inDevelMode():
1684
+ develTxt = '* '
1685
+
1686
+ n.setLabel('%s%s' % (develTxt, r.getRunName()))
1687
+ outputDict[r.getObjId()] = n
1688
+ for _, attr in r.iterOutputAttributes():
1689
+ # mark this output as produced by r
1690
+ if attr is None:
1691
+ logger.warning("Output attribute %s of %s is None" % (_, r))
1692
+ else:
1693
+ outputDict[attr.getObjId()] = n
1694
+
1695
+ def _checkInputAttr(node, pointed):
1696
+ """ Check if an attr is registered as output"""
1697
+ if pointed is not None:
1698
+ pointedId = pointed.getObjId()
1699
+
1700
+ if pointedId in outputDict:
1701
+ parentNode = outputDict[pointedId]
1702
+ if parentNode is node:
1703
+ logger.warning("WARNING: Found a cyclic dependence from node %s to itself, probably a bug. " % pointedId)
1704
+ else:
1705
+ parentNode.addChild(node)
1706
+ if os.environ.get('CHECK_CYCLIC_REDUNDANCY') and self._checkCyclicRedundancy(parentNode, node):
1707
+ conflictiveNodes = set()
1708
+ for child in node.getChilds():
1709
+ if node in child._parents:
1710
+ child._parents.remove(node)
1711
+ conflictiveNodes.add(child)
1712
+ logger.warning("WARNING: Found a cyclic dependence from node %s to %s, probably a bug. "
1713
+ % (node.getLabel() + '(' + node.getName() + ')',
1714
+ child.getLabel() + '(' + child.getName() + ')'))
1715
+
1716
+ for conflictNode in conflictiveNodes:
1717
+ node._childs.remove(conflictNode)
1718
+
1719
+ return False
1720
+ return True
1721
+ return False
1722
+
1723
+ for r in runs:
1724
+ node = g.getNode(r.strId())
1725
+ for _, attr in r.iterInputAttributes():
1726
+ if attr.hasValue():
1727
+ pointed = attr.getObjValue()
1728
+ # Only checking pointed object and its parent, if more
1729
+ # levels we need to go up to get the correct dependencies
1730
+ if not _checkInputAttr(node, pointed):
1731
+ parent = self.mapper.getParent(pointed)
1732
+ _checkInputAttr(node, parent)
1733
+ rootNode = g.getRoot()
1734
+ rootNode.run = None
1735
+ rootNode.label = ROOT_NODE_NAME
1736
+
1737
+ for n in g.getNodes():
1738
+ if n.isRoot() and n is not rootNode:
1739
+ rootNode.addChild(n)
1740
+ return g
1741
+
1742
+ @staticmethod
1743
+ def _checkCyclicRedundancy(parent, child):
1744
+ visitedNodes = set()
1745
+ recursionStack = set()
1746
+
1747
+ def depthFirstSearch(node):
1748
+ visitedNodes.add(node)
1749
+ recursionStack.add(node)
1750
+ for child in node.getChilds():
1751
+ if child not in visitedNodes:
1752
+ if depthFirstSearch(child):
1753
+ return True
1754
+ elif child in recursionStack and child != parent:
1755
+ return True
1756
+
1757
+ recursionStack.remove(node)
1758
+ return False
1759
+
1760
+ return depthFirstSearch(child)
1761
+
1762
+
1763
+ def _getRelationGraph(self, relation=pwobj.RELATION_SOURCE, refresh=False):
1764
+ """ Retrieve objects produced as outputs and
1765
+ make a graph taking into account the SOURCE relation. """
1766
+ relations = self.mapper.getRelationsByName(relation)
1767
+ g = pwutils.Graph(rootName=ROOT_NODE_NAME)
1768
+ root = g.getRoot()
1769
+ root.pointer = None
1770
+ runs = self.getRuns(refresh=refresh)
1771
+
1772
+ for r in runs:
1773
+ for paramName, attr in r.iterOutputAttributes():
1774
+ p = pwobj.Pointer(r, extended=paramName)
1775
+ node = g.createNode(p.getUniqueId(), attr.getNameId())
1776
+ node.pointer = p
1777
+ # The following alias if for backward compatibility
1778
+ p2 = pwobj.Pointer(attr)
1779
+ g.aliasNode(node, p2.getUniqueId())
1780
+
1781
+ for rel in relations:
1782
+ pObj = self.getObject(rel[OBJECT_PARENT_ID])
1783
+
1784
+ # Duplicated ...
1785
+ if pObj is None:
1786
+ logger.warning("Relation seems to point to a deleted object. "
1787
+ "%s: %s" % (OBJECT_PARENT_ID, rel[OBJECT_PARENT_ID]))
1788
+ continue
1789
+
1790
+ pExt = rel['object_parent_extended']
1791
+ pp = pwobj.Pointer(pObj, extended=pExt)
1792
+
1793
+ if pObj is None or pp.get() is None:
1794
+ logger.error("project._getRelationGraph: pointer to parent is "
1795
+ "None. IGNORING IT.\n")
1796
+ for key in rel.keys():
1797
+ logger.info("%s: %s" % (key, rel[key]))
1798
+
1799
+ continue
1800
+
1801
+ pid = pp.getUniqueId()
1802
+ parent = g.getNode(pid)
1803
+
1804
+ while not parent and pp.hasExtended():
1805
+ pp.removeExtended()
1806
+ parent = g.getNode(pp.getUniqueId())
1807
+
1808
+ if not parent:
1809
+ logger.error("project._getRelationGraph: parent Node "
1810
+ "is None: %s" % pid)
1811
+ else:
1812
+ cObj = self.getObject(rel['object_child_id'])
1813
+ cExt = rel['object_child_extended']
1814
+
1815
+ if cObj is not None:
1816
+ if cObj.isPointer():
1817
+ cp = cObj
1818
+ if cExt:
1819
+ cp.setExtended(cExt)
1820
+ else:
1821
+ cp = pwobj.Pointer(cObj, extended=cExt)
1822
+ child = g.getNode(cp.getUniqueId())
1823
+
1824
+ if not child:
1825
+ logger.error("project._getRelationGraph: child Node "
1826
+ "is None: %s." % cp.getUniqueId())
1827
+ logger.error(" parent: %s" % pid)
1828
+ else:
1829
+ parent.addChild(child)
1830
+ else:
1831
+ logger.error("project._getRelationGraph: child Obj "
1832
+ "is None, id: %s " % rel['object_child_id'])
1833
+ logger.error(" parent: %s" % pid)
1834
+
1835
+ for n in g.getNodes():
1836
+ if n.isRoot() and n is not root:
1837
+ root.addChild(n)
1838
+
1839
+ return g
1840
+
1841
+ def getSourceChilds(self, obj):
1842
+ """ Return all the objects have used obj
1843
+ as a source.
1844
+ """
1845
+ return self.mapper.getRelationChilds(pwobj.RELATION_SOURCE, obj)
1846
+
1847
+ def getSourceParents(self, obj):
1848
+ """ Return all the objects that are SOURCE of this object.
1849
+ """
1850
+ return self.mapper.getRelationParents(pwobj.RELATION_SOURCE, obj)
1851
+
1852
+ def getTransformGraph(self, refresh=False):
1853
+ """ Get the graph from the TRANSFORM relation. """
1854
+ if refresh or not self._transformGraph:
1855
+ self._transformGraph = self._getRelationGraph(pwobj.RELATION_TRANSFORM,
1856
+ refresh)
1857
+
1858
+ return self._transformGraph
1859
+
1860
+ def getSourceGraph(self, refresh=False):
1861
+ """ Get the graph from the SOURCE relation. """
1862
+ if refresh or not self._sourceGraph:
1863
+ self._sourceGraph = self._getRelationGraph(pwobj.RELATION_SOURCE,
1864
+ refresh)
1865
+
1866
+ return self._sourceGraph
1867
+
1868
+ def getRelatedObjects(self, relation, obj, direction=pwobj.RELATION_CHILDS,
1869
+ refresh=False):
1870
+ """ Get all objects related to obj by a give relation.
1871
+
1872
+ :param relation: the relation name to search for.
1873
+ :param obj: object from which the relation will be search,
1874
+ actually not only this, but all other objects connected
1875
+ to this one by the pwobj.RELATION_TRANSFORM.
1876
+ :parameter direction: Not used
1877
+ :param refresh: If True, cached objects will be refreshed
1878
+
1879
+ """
1880
+
1881
+ graph = self.getTransformGraph(refresh)
1882
+ relations = self.mapper.getRelationsByName(relation)
1883
+ connection = self._getConnectedObjects(obj, graph)
1884
+
1885
+ objects = []
1886
+ objectsDict = {}
1887
+
1888
+ for rel in relations:
1889
+ pObj = self.getObject(rel[OBJECT_PARENT_ID])
1890
+
1891
+ if pObj is None:
1892
+ logger.warning("Relation seems to point to a deleted object. "
1893
+ "%s: %s" % (OBJECT_PARENT_ID, rel[OBJECT_PARENT_ID]))
1894
+ continue
1895
+ pExt = rel['object_parent_extended']
1896
+ pp = pwobj.Pointer(pObj, extended=pExt)
1897
+
1898
+ if pp.getUniqueId() in connection:
1899
+ cObj = self.getObject(rel['object_child_id'])
1900
+ cExt = rel['object_child_extended']
1901
+ cp = pwobj.Pointer(cObj, extended=cExt)
1902
+ if cp.hasValue() and cp.getUniqueId() not in objectsDict:
1903
+ objects.append(cp)
1904
+ objectsDict[cp.getUniqueId()] = True
1905
+
1906
+ return objects
1907
+
1908
+ def _getConnectedObjects(self, obj, graph):
1909
+ """ Given a TRANSFORM graph, return the elements that
1910
+ are connected to an object, either children, ancestors or siblings.
1911
+ """
1912
+ n = graph.getNode(obj.strId())
1913
+ # Get the oldest ancestor of a node, before reaching the root node
1914
+ while n is not None and not n.getParent().isRoot():
1915
+ n = n.getParent()
1916
+
1917
+ connection = {}
1918
+
1919
+ if n is not None:
1920
+ # Iterate recursively all descendants
1921
+ for node in n.iterChilds():
1922
+ connection[node.pointer.getUniqueId()] = True
1923
+ # Add also
1924
+ connection[node.pointer.get().strId()] = True
1925
+
1926
+ return connection
1927
+
1928
+ def isReadOnly(self):
1929
+ if getattr(self, 'settings', None) is None:
1930
+ return False
1931
+
1932
+ return self.settings.getReadOnly()
1933
+
1934
+ def isInReadOnlyFolder(self):
1935
+ return self._isInReadOnlyFolder
1936
+
1937
+ def openedAsReadOnly(self):
1938
+ return self.isReadOnly() or self.isInReadOnlyFolder()
1939
+
1940
+ def setReadOnly(self, value):
1941
+ self.settings.setReadOnly(value)
1942
+
1943
+ def fixLinks(self, searchDir):
1944
+ logger.info("Fixing project links. Searching at %s" % searchDir)
1945
+ runs = self.getRuns()
1946
+
1947
+ for prot in runs:
1948
+ print (prot)
1949
+ broken = False
1950
+ if isinstance(prot, ProtImportBase) or prot.getClassName() == "ProtImportMovies":
1951
+ logger.info("Import detected")
1952
+ for _, attr in prot.iterOutputAttributes():
1953
+ for f in attr.getFiles():
1954
+ if ':' in f:
1955
+ f = f.split(':')[0]
1956
+
1957
+ if not os.path.exists(f):
1958
+ if not broken:
1959
+ broken = True
1960
+ logger.info("Found broken links in run: %s" %
1961
+ pwutils.magenta(prot.getRunName()))
1962
+ logger.info(" Missing: %s" % pwutils.magenta(f))
1963
+
1964
+ if os.path.islink(f):
1965
+ sourceFile = os.path.realpath(f)
1966
+ logger.info(" -> %s" % pwutils.red(sourceFile))
1967
+
1968
+ newFile = pwutils.findFile(os.path.basename(sourceFile),
1969
+ searchDir,
1970
+ recursive=True)
1971
+ if newFile:
1972
+ logger.info(" Found file %s, creating link... %s" % (newFile,
1973
+ pwutils.green(" %s -> %s" % (f, newFile))))
1974
+ pwutils.createAbsLink(newFile, f)
1975
+
1976
+ @staticmethod
1977
+ def cleanProjectName(projectName):
1978
+ """ Cleans a project name to avoid common errors
1979
+ Use it whenever you want to get the final project name pyworkflow will end up.
1980
+ Spaces will be replaced by _ """
1981
+
1982
+ return re.sub(r"[^\w\d\-\_]", "-", projectName)
1983
+
1984
+
1985
+ class MissingProjectDbException(Exception):
1986
+ pass
1987
+
1988
+
1989
+ class ModificationNotAllowedException(Exception):
1990
+ pass