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,527 @@
1
+ # **************************************************************************
2
+ # *
3
+ # * Authors: J.M. De la Rosa Trevin (delarosatrevin@scilifelab.se) [1]
4
+ # *
5
+ # * [1] SciLifeLab, Stockholm University
6
+ # *
7
+ # * This program is free software: you can redistribute it and/or modify
8
+ # * it under the terms of the GNU General Public License as published by
9
+ # * the Free Software Foundation, either version 3 of the License, or
10
+ # * (at your option) any later version.
11
+ # *
12
+ # * This program is distributed in the hope that it will be useful,
13
+ # * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # * GNU General Public License for more details.
16
+ # *
17
+ # * You should have received a copy of the GNU General Public License
18
+ # * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ # *
20
+ # * All comments concerning this program package may be sent to the
21
+ # * e-mail address 'scipion@cnb.csic.es'
22
+ # *
23
+ # **************************************************************************
24
+ """
25
+ This module contains the PATH related utilities
26
+ inside the utils module
27
+ """
28
+
29
+ import logging
30
+
31
+ from pyworkflow.utils import yellow
32
+
33
+ logger = logging.getLogger(__name__)
34
+
35
+ import os
36
+ import shutil
37
+ import sys
38
+ from glob import glob
39
+ import datetime
40
+
41
+ from pyworkflow import SCIPION_SCRATCH, DOCSITEURLS, ASCII_COLOR_2_TKINTER
42
+ from pyworkflow.exceptions import PyworkflowException
43
+ from pyworkflow.config import Config
44
+
45
+ ROOT = "/"
46
+
47
+
48
+ def findFileRecursive(filename, path):
49
+ """
50
+ Finds a file/folder in a single path recursively
51
+
52
+ :param filename: Name (myfile.txt) of the file to look for.
53
+ :param path: folder to start the search from.
54
+ :return: The absolute path to the 'filename' found or None if not found.
55
+ """
56
+ for root, dirs, files in os.walk(path):
57
+ if filename in files or filename in dirs:
58
+ return os.path.join(root, filename)
59
+ return None
60
+
61
+
62
+ def findFile(filename, *paths, recursive=False):
63
+ """
64
+ Search if a file/folder is present in any of the paths provided.
65
+
66
+ :param filename: Name (myfile.txt) of the file to look for.
67
+ :param paths: N number of folders to look at.
68
+ :param recursive: If True it will iterate into the subfolders.
69
+ :return: None if nothing is found.
70
+ """
71
+
72
+ if filename:
73
+ for p in paths:
74
+ fn = os.path.join(p, filename)
75
+ if os.path.exists(fn):
76
+ return fn
77
+ if recursive:
78
+ f = findFileRecursive(filename, p)
79
+ if f:
80
+ return f
81
+ return None
82
+
83
+
84
+ def findRootFrom(referenceFile, searchFile):
85
+ """ This method will find a path (root) from 'referenceFile'
86
+ from which the 'searchFile' os.path.exists.
87
+ A practical example of 'referenceFile' is a metadata file
88
+ and 'searchFile' is an image to be found from the metadata.
89
+ Return None if the path is not found.
90
+ """
91
+ absPath = os.path.dirname(os.path.abspath(referenceFile))
92
+
93
+ while absPath is not None and absPath != '/':
94
+ if os.path.exists(os.path.join(absPath, searchFile)):
95
+ return absPath
96
+ absPath = os.path.dirname(absPath)
97
+
98
+ return None
99
+
100
+
101
+ def getParentFolder(path):
102
+ """ Returns the absolute parent folder of a file or folder. Work for
103
+ folders that ens with "/" which dirname can't"""
104
+ return os.path.dirname(os.path.abspath(path))
105
+
106
+
107
+ def replaceExt(filename, newExt):
108
+ """ Replace the current path extension(from last .)
109
+ with a new one. The new one should not contain the ."""
110
+ return os.path.splitext(filename)[0] + '.' + newExt
111
+
112
+
113
+ def replaceBaseExt(filename, newExt):
114
+ """ Replace the current basename extension(from last .)
115
+ with a new one. The new one should not contains the .
116
+ """
117
+ return replaceExt(os.path.basename(filename), newExt)
118
+
119
+
120
+ def removeBaseExt(filename):
121
+ """Take the basename of the filename and remove extension"""
122
+ return removeExt(os.path.basename(filename))
123
+
124
+
125
+ def removeExt(filename):
126
+ """ Remove extension from basename """
127
+ return os.path.splitext(filename)[0]
128
+
129
+
130
+ def joinExt(*extensions):
131
+ """ Join several path parts with a ."""
132
+ return '.'.join(extensions)
133
+
134
+
135
+ def getExt(filePath):
136
+ """ Return the extension given a file. """
137
+ return os.path.splitext(filePath)[1]
138
+
139
+
140
+ def cleanPath(*paths):
141
+ """ Remove a list of paths, either folders or files"""
142
+ for p in paths:
143
+ if os.path.exists(p):
144
+ if os.path.isdir(p):
145
+ if os.path.islink(p):
146
+ os.remove(p)
147
+ else:
148
+ shutil.rmtree(p)
149
+ else:
150
+ os.remove(p)
151
+
152
+
153
+ def cleanPattern(pattern):
154
+ """ Remove all files that match the pattern. """
155
+ files = glob(pattern)
156
+ cleanPath(*files)
157
+
158
+
159
+ def copyPattern(pattern, destFolder):
160
+ """ Copy all files matching the pattern to the given destination folder."""
161
+ for file in glob(pattern):
162
+ copyFile(file, destFolder)
163
+
164
+
165
+ def makePath(*paths):
166
+ """ Create a list of paths if they don't os.path.exists.
167
+ Recursively create all folder needed in a path.
168
+ If a path passed is a file, only the directory will be created.
169
+ """
170
+ for p in paths:
171
+ if not os.path.exists(p) and len(p):
172
+ os.makedirs(p)
173
+
174
+
175
+ def makeTmpPath(protocol):
176
+ """
177
+ Create the scratch folder if SCIPION_SCRATCH variable is defined into the
178
+ Scipion config, i.o.c create tmp folder
179
+ """
180
+ tmpPath = protocol._getTmpPath()
181
+ if not os.path.exists(tmpPath) and len(tmpPath):
182
+ scratchPath = Config.SCIPION_SCRATCH
183
+
184
+ if scratchPath is None: # Case when SCIPION_SCRATCH doesn't exist. TMP folder is created
185
+ os.makedirs(tmpPath)
186
+ else:
187
+ try:
188
+ project = protocol.getProject()
189
+ folderId = "_".join([project.getShortName(),project.getProtWorkingDir(protocol)])
190
+ tmpScratchFolder = os.path.join(scratchPath, folderId)
191
+ if os.path.exists(tmpScratchFolder):
192
+ cleanPath(tmpScratchFolder)
193
+ os.makedirs(tmpScratchFolder) # Create scratch folder
194
+ createAbsLink(tmpScratchFolder, tmpPath) # Create a sym link
195
+
196
+ except Exception as e:
197
+ raise PyworkflowException("Couldn't create the temporary folder %s at:\n %s\nPlease, review %s variable." %
198
+ (folderId, scratchPath, SCIPION_SCRATCH), url=DOCSITEURLS.CONFIG_SECTION % "scratch-folder") from e
199
+
200
+
201
+ def makeFilePath(*files):
202
+ """ Make the path to ensure that files can be written. """
203
+ makePath(*[os.path.dirname(f) for f in files])
204
+
205
+
206
+ def missingPaths(*paths):
207
+ """ Check if the list of paths os.path.exists.
208
+ Will return the list of missing files,
209
+ if the list is empty means that all path os.path.exists
210
+ """
211
+ return [p for p in paths if not os.path.exists(p)]
212
+
213
+
214
+ def getHomePath(user=''):
215
+ """Return the home path of a give user."""
216
+ return os.path.expanduser("~" + user)
217
+
218
+
219
+ def expandPattern(pattern, vars=True, user=True):
220
+ """ Expand environment vars and user from a given pattern. """
221
+ if vars:
222
+ pattern = os.path.expandvars(pattern)
223
+ if user:
224
+ pattern = os.path.expanduser(pattern)
225
+ return pattern
226
+
227
+
228
+ def getFiles(folderPath):
229
+ """
230
+ Gets all files of given folder and it subfolders.
231
+ folderPath -- Folder path to get files.
232
+ returns -- Set with all folder files.
233
+ """
234
+ filePaths = set()
235
+ for path, dirs, files in os.walk(folderPath):
236
+ for f in files:
237
+ filePaths.add(os.path.join(path, f))
238
+ return filePaths
239
+
240
+
241
+ def copyTree(source, dest):
242
+ """
243
+ Wrapper around the shutil.copytree, but allowing
244
+ that the dest folder also os.path.exists.
245
+ """
246
+ if not os.path.exists(dest):
247
+ shutil.copytree(source, dest, symlinks=True)
248
+ else:
249
+ for f in os.listdir(source):
250
+ fnPath = os.path.join(source, f)
251
+ if os.path.isfile(fnPath):
252
+ shutil.copy(fnPath, dest)
253
+ elif os.path.isdir(fnPath):
254
+ copyTree(fnPath, os.path.join(dest, f))
255
+
256
+
257
+ def moveTree(src, dest):
258
+ copyTree(src, dest)
259
+ cleanPath(src)
260
+
261
+
262
+ def copyFile(source, dest):
263
+ """ Shortcut to shutil.copy. """
264
+ shutil.copy(source, dest)
265
+
266
+
267
+ def moveFile(source, dest):
268
+ """ Move file from source to dest. """
269
+ copyFile(source, dest)
270
+ cleanPath(source)
271
+
272
+
273
+ def createLink(source, dest):
274
+ """ Creates a relative link to a given file path.
275
+ Try to use common path for source and dest to avoid errors.
276
+ Different relative paths may exist since there are different valid paths
277
+ for a file, it depends on the current working dir path"""
278
+ if os.path.islink(dest):
279
+ os.remove(dest)
280
+
281
+ if os.path.exists(dest):
282
+ raise Exception('Destination %s os.path.exists and is not a link'
283
+ % dest)
284
+ sourcedir = getParentFolder(source)
285
+ destdir = getParentFolder(dest)
286
+ relsource = os.path.join(os.path.relpath(sourcedir, destdir),
287
+ os.path.basename(source))
288
+ os.symlink(relsource, dest)
289
+
290
+
291
+ def createAbsLink(source, dest):
292
+ """ Creates a link to a given file path"""
293
+ if os.path.islink(dest):
294
+ os.remove(dest)
295
+
296
+ if os.path.exists(dest):
297
+ raise Exception('Destination %s os.path.exists and is not a link' % dest)
298
+
299
+ os.symlink(source, dest)
300
+
301
+
302
+ def getLastFile(pattern):
303
+ """ Return the last file matching the pattern. """
304
+ files = glob(pattern)
305
+ if len(files):
306
+ files.sort()
307
+ return files[-1]
308
+ return None
309
+
310
+
311
+ def commonPath(*paths):
312
+ """ Return the common longest prefix path.
313
+ It uses the python os.path.commonprefix and
314
+ then the direname over it since the former is
315
+ implemented in char-by-char base.
316
+ """
317
+ return os.path.dirname(os.path.commonprefix(*paths))
318
+
319
+
320
+
321
+ def renderTextFile(fname, add, offset=0, lineNo=0, numberLines=True,
322
+ maxSize=400, headSize=40, tailSize=None, notifyLine=None, errors='strict'):
323
+ """
324
+ Call callback function add() on each fragment of text from file fname,
325
+ delimited by lines and/or color codes.
326
+
327
+ :param add: callback function with signature (txt, tag='normal')
328
+ :param offset: byte offset - we start reading the file from there
329
+ :param lineNo: lines will be numbered from this value on
330
+ :param numberLines: whether to prepend the line numbers
331
+
332
+ """
333
+ textfile = open(fname, encoding='utf-8', errors=errors)
334
+ size = (os.stat(fname).st_size - offset) / 1024 # in kB
335
+
336
+ for line in iterBigFile(textfile, offset, size,
337
+ maxSize, headSize, tailSize):
338
+ if line is not None:
339
+ lineNo += 1
340
+ if notifyLine is not None:
341
+ notifyLine(line)
342
+ renderLine(line, add, lineNo, numberLines)
343
+ else:
344
+ add("""\n
345
+ ==> Too much data to read (%d kB) -- %d kB omitted
346
+ ==> Click on """ % (size, size - headSize - (tailSize or headSize)))
347
+ add(fname, 'link:%s' % fname)
348
+ add(' to open it with the default viewer\n\n')
349
+ if numberLines:
350
+ add(' ==> Line numbers below are not '
351
+ 'in sync with the input data\n\n')
352
+
353
+ offset = textfile.tell() # save last position in file
354
+ textfile.close()
355
+
356
+ return offset, lineNo
357
+
358
+
359
+ def renderLine(line, add, lineNo=1, numberLines=True):
360
+ """
361
+ Find all the fragments of formatted text in line and call
362
+ add(fragment, tag) for each of them.
363
+ """
364
+ # Prepend line number
365
+ if numberLines and lineNo:
366
+ add('%05d:' % lineNo, 'cyan')
367
+ add(' ')
368
+
369
+ # iter 1\riter 2\riter 3 --> iter 3
370
+ if '\r' in line:
371
+ line = line[line.rfind('\r')+1:] # overwriting!
372
+
373
+ # Find all console escape codes and use the appropriate tag instead.
374
+ pos = 0 # current position in the line we are parsing
375
+ attribute = None
376
+ while True:
377
+ # line looks like:
378
+ # 'blah blah \x1b[{attr1};...;{attrn}mTEXT\x1b[0m blah blah'
379
+ # where {attrn} is the color code (31 is red, for example). See
380
+ # http://www.termsys.demon.co.uk/vtansi.htm#colors
381
+ start = line.find('\x1b[', pos)
382
+ if start < 0: # no more escape codes, just add the remaining text
383
+ add(line[pos:], attribute)
384
+ break
385
+
386
+ add(line[pos:start], attribute)
387
+ end = line.find('m', start+2)
388
+ if end < 0: # an escape code interrupted by newline... weird
389
+ break
390
+ code = line[start+2:end]
391
+
392
+ # See what attribute to use from now on, and update pos
393
+ if code == '0':
394
+ attribute = None
395
+ else:
396
+ attribute = ASCII_COLOR_2_TKINTER.get(code[-2:], None)
397
+ pos = end + 1 # go to the character next to "m", the closing char
398
+
399
+
400
+ def iterBigFile(textfile, offset=0, size=None,
401
+ maxSize=400, headSize=40, tailSize=None):
402
+ """
403
+ Yield lines from file textfile. If the size to read is bigger
404
+ than maxSize then yield the first lines until headSize bytes, then
405
+ yield None, then yield the last lines from tailSize bytes to the end.
406
+ """
407
+ if size is None:
408
+ # Size in kB of the part of the file that we will read
409
+ textfile.seek(0, 2)
410
+ sizeKb = (textfile.tell() - offset) / 1024
411
+ else:
412
+ sizeKb = size
413
+
414
+ headSizeB = headSize * 1024
415
+ tailSizeB = (tailSize or headSize) * 1024
416
+
417
+ textfile.seek(offset)
418
+ # If the size is bigger than the max that we want to read (in kB).
419
+ if 0 < maxSize < sizeKb:
420
+ # maxSize <= 0 means we just want to read it all and not enter here.
421
+ for line in textfile.read(headSizeB).split('\n'):
422
+ yield line + '\n'
423
+ yield None # Special result to mark omitting lines
424
+ textfile.seek(sizeKb*1024-tailSizeB) # ready to show the last bytes
425
+
426
+ # Add the remaining lines (from our last offset)
427
+ for line in textfile:
428
+ yield line
429
+
430
+
431
+ def createUniqueFileName(fn):
432
+ """
433
+ This function creates a file name that is similar to the original
434
+ by adding a unique numeric suffix. check NamedTemporaryFile
435
+ from tempfile for alternatives
436
+ """
437
+ if not os.path.os.path.exists(fn):
438
+ return fn
439
+
440
+ path, name = os.path.split(fn)
441
+ name, ext = os.path.splitext(name)
442
+
443
+ make_fn = lambda i: os.path.join(path, '%s_tmp_%d_%s' % (name, i, ext))
444
+
445
+ for i in range(2, sys.maxsize):
446
+ uni_fn = make_fn(i)
447
+ if not os.path.os.path.exists(uni_fn):
448
+ return uni_fn
449
+
450
+ return None
451
+
452
+
453
+ def getFileSize(fn):
454
+ """ Shortcut to inspect the size of a file or a folder. """
455
+
456
+ if not os.path.exists(fn):
457
+ return 0
458
+
459
+ elif os.path.isdir(fn):
460
+ total_size = 0
461
+ for dirpath, dirnames, filenames in os.walk(fn):
462
+ for f in filenames:
463
+ fp = os.path.join(dirpath, f)
464
+ if not os.path.islink(fp):
465
+ total_size += os.path.getsize(fp)
466
+ return total_size
467
+
468
+ else:
469
+ return os.path.getsize(fn)
470
+
471
+
472
+ def hasChangedSince(fn, time):
473
+ """ Returns if the file has changed since the timestamp passed as parameter. It will check
474
+ the last modified time of the file this set uses to persists.
475
+
476
+ :parameter time: timestamp to compare to the last modification time """
477
+
478
+ if time is None:
479
+ return True
480
+
481
+ # Get the last time it was modified
482
+ modTime = getFileLastModificationDate(fn)
483
+
484
+ return time < modTime
485
+
486
+ def isFileFinished(fn , duration=60):
487
+ """ Returns True if the file (fn) last modification has not changed for 60 seconds (default duration)"""
488
+
489
+ modTime = getFileLastModificationDate(fn)
490
+ fileAge = datetime.datetime.now()-modTime
491
+
492
+ return fileAge.seconds > duration
493
+
494
+
495
+ def getFileLastModificationDate(fn):
496
+ """ Returns the last modification date of a file or None
497
+ if it doesn't exist. """
498
+ if os.path.exists(fn):
499
+ ts = os.path.getmtime(fn)
500
+ return datetime.datetime.fromtimestamp(ts)
501
+ else:
502
+ logger.info(fn + " does not exist!!. Can't check last modification date.")
503
+ return None
504
+
505
+
506
+ def backup(fpath):
507
+ """
508
+ Create directory "backup" if necessary and back up the file.
509
+
510
+ :param fpath:
511
+ :return: None
512
+
513
+ """
514
+ BACKUPS="backup"
515
+
516
+ dname = os.path.dirname(fpath)
517
+
518
+ if not os.path.exists(dname):
519
+ os.makedirs(dname)
520
+
521
+ elif os.path.exists(fpath):
522
+ if not os.path.exists(os.path.join(dname, BACKUPS)):
523
+ os.makedirs(os.path.join(dname, BACKUPS))
524
+ backupFn = os.path.join(dname, BACKUPS,
525
+ '%s.%s' % (os.path.basename(fpath), datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
526
+ logger.info(yellow("* Creating backup: %s" % backupFn))
527
+ os.rename(fpath, backupFn)
@@ -0,0 +1,132 @@
1
+ # **************************************************************************
2
+ # *
3
+ # * Authors: J.M. De la Rosa Trevin (jmdelarosa@cnb.csic.es)
4
+ # * Laura del Cano (ldelcano@cnb.csic.es)
5
+ # *
6
+ # * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
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
+ """
28
+ This module handles process execution
29
+ """
30
+
31
+ import logging
32
+ logger = logging.getLogger(__name__)
33
+
34
+ import sys
35
+ from subprocess import check_call
36
+ import psutil
37
+
38
+ from .utils import greenStr
39
+ from pyworkflow import Config
40
+
41
+
42
+ # The job should be launched from the working directory!
43
+ def runJob(log, programname, params,
44
+ numberOfMpi=1, numberOfThreads=1,
45
+ hostConfig=None, env=None, cwd=None, gpuList=None, executable=None):
46
+
47
+ command = buildRunCommand(programname, params, numberOfMpi, hostConfig,
48
+ env, gpuList=gpuList)
49
+
50
+ if log is None:
51
+ log = logger
52
+
53
+ log.info("** Running command: **")
54
+ log.info(greenStr(command))
55
+
56
+ return runCommand(command, env=env, cwd=cwd, executable=executable)
57
+
58
+
59
+ def runCommand(command, env=None, cwd=None, executable=None):
60
+ """ Execute command with given environment env and directory cwd """
61
+
62
+ # First let us create core dumps if in debug mode
63
+ if Config.debugOn():
64
+ import resource
65
+ resource.setrlimit(resource.RLIMIT_CORE,
66
+ (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
67
+ # This is like "ulimit -u 99999999", so we can create core dumps
68
+
69
+ # TODO: maybe have to set PBS_NODEFILE in case it is used by "command"
70
+ # (useful for example with gnu parallel)
71
+ check_call(command, shell=True, stdout=sys.stdout, stderr=sys.stderr,
72
+ env=env, cwd=cwd, executable=executable)
73
+ # It would be nice to avoid shell=True and calling buildRunCommand()...
74
+
75
+
76
+ def buildRunCommand(programname, params, numberOfMpi, hostConfig=None,
77
+ env=None, gpuList=None):
78
+ """ Return a string with the command line to run """
79
+
80
+ # Convert our list of params to a string, with each element escaped
81
+ # with "" in case there are spaces.
82
+ if not isinstance(params, str):
83
+ params = ' '.join('"%s"' % p for p in params)
84
+
85
+ if gpuList:
86
+ params = params % {'GPU': ' '.join(str(g) for g in gpuList)}
87
+ if "CUDA_VISIBLE_DEVICES" in programname:
88
+ sep = "," if len(gpuList) > 1 else ""
89
+ programname = programname % {'GPU': sep.join(str(g) for g in gpuList)}
90
+
91
+ prepend = '' if env is None else env.getPrepend()
92
+
93
+ if numberOfMpi <= 1:
94
+ return '%s %s %s' % (prepend, programname, params)
95
+ else:
96
+ assert hostConfig is not None, 'hostConfig needed to launch MPI processes.'
97
+
98
+ if programname.startswith('xmipp') and not programname.startswith('xmipp_mpi'):
99
+ programname = programname.replace('xmipp', 'xmipp_mpi')
100
+
101
+ mpiFlags = '' if env is None else env.get('SCIPION_MPI_FLAGS', '')
102
+
103
+ mpiCmd = hostConfig.mpiCommand.get() % {
104
+ 'JOB_NODES': numberOfMpi,
105
+ 'COMMAND': "%s `which %s` %s" % (mpiFlags, programname, params),
106
+ }
107
+ return '%s %s' % (prepend, mpiCmd)
108
+
109
+
110
+ def killWithChilds(pid):
111
+ """ Kill the process with given pid and all children processes.
112
+
113
+ :param pid: the process id to terminate
114
+ """
115
+ proc = psutil.Process(pid)
116
+ for c in proc.children(recursive=True):
117
+ if c.pid is not None:
118
+ logger.info("Terminating child pid: %d" % c.pid)
119
+ c.kill()
120
+ logger.info("Terminating process pid: %s" % pid)
121
+ if pid is None:
122
+ logger.warning("Got None PID!!!")
123
+ else:
124
+ proc.kill()
125
+
126
+
127
+ def isProcessAlive(pid):
128
+ try:
129
+ psutil.Process(pid)
130
+ return True
131
+ except Exception:
132
+ return False