File: abstract_flow.py

package info (click to toggle)
openmolcas 25.02-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 170,204 kB
  • sloc: f90: 498,088; fortran: 139,779; python: 13,587; ansic: 5,745; sh: 745; javascript: 660; pascal: 460; perl: 325; makefile: 17
file content (515 lines) | stat: -rw-r--r-- 17,991 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# -*- coding: utf-8 -*-

#***********************************************************************
# This file is part of OpenMolcas.                                     *
#                                                                      *
# OpenMolcas is free software; you can redistribute it and/or modify   *
# it under the terms of the GNU Lesser General Public License, v. 2.1. *
# OpenMolcas is distributed in the hope that it will be useful, but it *
# is provided "as is" and without any express or implied warranties.   *
# For more details see the full text of the license in the file        *
# LICENSE or in <http://www.gnu.org/licenses/>.                        *
#                                                                      *
# Copyright (C) 2015-2018, Ignacio Fdez. Galván                        *
#***********************************************************************

from __future__ import (unicode_literals, division, absolute_import, print_function)
try:
  from builtins import super
except ImportError:
  from future.builtins import super
try:
  from six import text_type, python_2_unicode_compatible
except ImportError:
  text_type = str
  def python_2_unicode_compatible(orig):
    return orig

from os.path import isfile
from re import match
from io import BytesIO

from molcas_aux import *
from tee import teed_call
from simpleeval import simple_eval, SimpleEval

#===============================================================================
# Patch SimpleEval to use the "decimal" module for better precision handling

import ast
import decimal

def _pymolcas_eval(self, expr):
  self.expr = expr
  return text_type(self._eval(ast.parse(expr.strip()).body[0].value))

@staticmethod
def _pymolcas_eval_num(node):
  return decimal.Decimal(text_type(node.n))

SimpleEval.eval = _pymolcas_eval
SimpleEval._eval_num = _pymolcas_eval_num

#===============================================================================

def env_print(env, string):
  if (env.echo):
    print(string)

def check_trap(rc_name=None):
  # this codes should not be affected by MOLCAS_TRAP
  if (rc_name in ['_RC_ALL_IS_WELL_', '_RC_CHECK_ERROR_']):
    return True
  elif (get_utf8('MOLCAS_TRAP', default='on').lower() == 'off'):
    print('*********************************************************')
    print('**                                                     **')
    print('** Non-zero return code, and MOLCAS_TRAP is set to OFF **')
    print('**                                                     **')
    print('*********************************************************')
    return False
  else:
    return True

@python_2_unicode_compatible
class Expression(object):
  def __init__(self, var, op, val):
    self.var = var
    self.op = op
    self.val = val
  def __bool__(self):
    if (self.op == 'eq'):
      return (expandvars(self.var) == expandvars(self.val))
    if (self.op == 'ne'):
      return (expandvars(self.var) != expandvars(self.val))
    if (self.op == 'file'):
      return self.env.file_exists(self.val)
  def __str__(self):
    var = self.var
    if (var == '$MOLCAS_ITER'):
      var = 'ITER'
    if (self.op == 'eq'):
      return '{0} == {1}'.format(var, self.val)
    if (self.op == 'ne'):
      return '{0} != {1}'.format(var, self.val)
    if (self.op == 'file'):
      return '-FILE {0}'.format(self.val)
  #python2
  __nonzero__ = __bool__

class Statement(object):
  def __init__(self, string=None):
    self.rc = None
    self.level = 0
    self.indent = '  '
    if (string is not None):
      self.string = string
  def run(self, env):
    if (env._goto):
      return None
    print('Unsupported statement: {0}'.format(self.string))
    return self.rc

@python_2_unicode_compatible
class Group(Statement):
  def __init__(self, contents, condition=True, rerun=False, var=None, val=None):
    super().__init__()
    self.contents = contents
    self.condition = condition
    self.rerun = rerun
    self.var = var
    self.val = val
    self.maxiter = 1
    self.grouptype = self._find_type()
  def _find_type(self):
    if (isinstance(self.condition, Expression) and (self.rerun is False)):
      return 'if'
    elif ((self.condition is True) and (self.rerun is True)):
      if (self.var is not None):
        return 'foreach'
      else:
        return 'do'
    elif ((self.condition is True) and (self.rerun is False)):
      return 'group'
    else:
      return 'unknown'
  def __str__(self):
    if (self.grouptype == 'group'):
      init = ''
      end = ''
    elif (self.grouptype == 'foreach'):
      init = self.level*self.indent + '>>> FOREACH {0} IN ({1})\n\n'.format(self.var, self.val)
      end = '\n\n' + self.level*self.indent + '>>> END FOREACH'
    elif (self.grouptype == 'do'):
      init = self.level*self.indent + '>>> DO WHILE\n\n'
      end = '\n\n' + self.level*self.indent + '>>> END DO'
    elif (self.grouptype == 'if'):
      if ((len(self.contents) == 1) and isinstance(self.contents[0], Jump)):
        init = self.level*self.indent + '>>> IF ({0}) GOTO {1}'.format(self.condition, self.contents[0].name)
        return init
      init = self.level*self.indent + '>>> IF ({0})\n\n'.format(self.condition)
      end = '\n\n' + self.level*self.indent + '>>> END IF'
    else:
      init = '('
      end = ')'
    return '{0}{1}{2}'.format(init, '\n\n'.join(map(text_type, self.contents)), end)
  def run(self, env):
    if (env._goto):
      return None
    if (self.grouptype == 'foreach'):
      self.values = expandvars(self.val, default='UNKNOWN_VARIABLE')
      re_match = match(r'(-?\d+)\s*\.\.\s*(-?\d+)', self.values)
      if (re_match):
        ini = int(re_match.group(1))
        fin = int(re_match.group(2))
        sign = -1 if (fin < ini) else 1
        self.values = range(ini, fin + sign, sign)
        self.values = list(map(text_type, self.values))
      else:
        self.values = [x.strip() for x in self.values.split(',')]
      self.maxiter = len(self.values)
      env.enter_loop('foreach')
    elif (self.grouptype == 'do'):
      self.previter = get_utf8('MOLCAS_ITER', default='0')
      self.maxiter = int(get_utf8('MOLCAS_MAXITER', default='50'))
      env.enter_loop('do')
    self.thisiter = 1
    if (isinstance(self.condition, Expression)):
      self.condition.env = env
    if (isinstance(self.rerun, Expression)):
      self.rerun.env = env
    while (self.thisiter <= self.maxiter):
      if (self.grouptype == 'if'):
        env_print(env, '\n>>> IF ({0})'.format(self.condition))
      itercontents = self.contents[:]
      if (self.condition):
        i = 0
        if (self.grouptype == 'foreach'):
          Assignment(self.var, self.values[self.thisiter-1]).run(env)
        elif (self.grouptype == 'do'):
          set_utf8('MOLCAS_ITER', self.thisiter)
        no_break = True
        if (self.grouptype == 'do'):
          rc_name = '_RC_CONTINUE_LOOP_'
        else:
          rc_name = '_RC_ALL_IS_WELL_'
        while ((i < len(itercontents)) and (no_break)):
          item = itercontents[i]
          item.group = itercontents
          rc = item.run(env)
          # Transparent return: only update return code if not None
          if (rc is not None):
            self.rc = rc
            rc_name = env.rc_to_name(self.rc)
          no_break = rc_name in ['_RC_ALL_IS_WELL_', '_RC_CONTINUE_LOOP_', '_RC_CONTINUE_UNIX_LOOP_']
          # check MOLCAS_TRAP here only if it's not the last element in the group
          if (i+1 < len(itercontents)):
            if ((not no_break) and (not check_trap(rc_name))):
              no_break = True
              rc_name = '_RC_ALL_IS_WELL_'
          set_utf8('EMIL_RETURNCODE', 0 if self.rc is None else self.rc)
          i += 1
      else:
        env_print(env, '(Skipped)')
      # Termination conditions
      # TODO: should MOLCAS_TRAP be checked here too?
      # TODO: run EMIL commands after a module fails (see FS#37)
      if (env._goto):
        break
      # this seems to be needed (here) for compatibility with old driver
      if (self.grouptype in ['do', 'foreach']):
        env._check_count += 1
      if (not self.rerun):
        break
      # in a DO loop, "all is well" means the loop terminates
      if (self.grouptype == 'do'):
        no_break = rc_name in ['_RC_CONTINUE_LOOP_', '_RC_CONTINUE_UNIX_LOOP_']
      elif (self.grouptype == 'foreach'):
        no_break = rc_name in ['_RC_ALL_IS_WELL_', '_RC_CONTINUE_LOOP_', '_RC_CONTINUE_UNIX_LOOP_']
      else:
        no_break = rc_name in ['_RC_ALL_IS_WELL_']
      # no-trap and loops are tricky
      if ((not no_break) and (not check_trap(rc_name))):
        if ((rc_name not in ['_RC_NOT_CONVERGED_']) or (self.grouptype != 'do')):
          no_break = True
          rc_name = '_RC_ALL_IS_WELL_'
      if (self.grouptype == 'do'):
        env_print(env, '\n>>> END DO')
      elif (self.grouptype == 'foreach'):
        env_print(env, '\n>>> END FOREACH')
      if (not no_break):
        break
      self.thisiter += 1
    if (self.grouptype == 'do'):
      if (not env._goto):
        if (rc_name in ['_RC_CONTINUE_LOOP_', '_RC_CONTINUE_UNIX_LOOP_']):
          self.rc = '_RC_NOT_CONVERGED_'
      set_utf8('MOLCAS_ITER', self.previter)
      env.exit_loop()
    if (self.grouptype == 'foreach'):
      env.exit_loop()
    set_utf8('EMIL_RETURNCODE', self.rc)
    return self.rc

@python_2_unicode_compatible
class Assignment(Statement):
  def __init__(self, var, val, literal=True):
    super().__init__()
    self.literal = literal
    self.var = var
    self.val = val
  def __str__(self):
    if (self.literal):
      return self.level*self.indent + '>>> EXPORT {0} = {1}'.format(self.var, self.val)
    else:
      return self.level*self.indent + '>>> EVAL {0} = {1}'.format(self.var, self.val)
  def run(self, env):
    if (env._goto):
      return None
    if (self.literal):
      val = expandvars(self.val, default='UNKNOWN_VARIABLE')
      env_print(env, '\n>>> EXPORT {0} = {1}'.format(self.var, val))
      set_utf8(self.var, val)
    else:
      val = expandvars(self.val, default='0')
      try:
        eval_val = simple_eval(val)
      except:
        eval_val = ''
      env_print(env, '\n>>> EVAL {0} = {1} = {2}'.format(self.var, val, eval_val))
      set_utf8(self.var, eval_val)
    return self.rc

@python_2_unicode_compatible
class Break(Statement):
  def __init__(self, rc):
    super().__init__()
    self.rc = rc
  def __str__(self):
    num = ' {0}'.format(self.rc)
    if (self.rc is None):
      num = ''
    return self.level*self.indent + '>>> EXIT{0}'.format(num)
  def run(self, env):
    if (env._goto):
      return None
    try:
      num = expandvars(self.rc)
      rc = int(num)
    except:
      rc = None
    if (rc is None):
      rc = '_RC_EXIT_'
    if (rc == '0'):
      rc = '_RC_EXIT_EXPECTED_'
    env_print(env, '\n>>> EXIT {0}'.format(env.rc_to_name(rc)))
    return rc

@python_2_unicode_compatible
class Include(Statement):
  def __init__(self, filename):
    super().__init__()
    self.filename = filename
  def __str__(self):
    return self.level*self.indent + '>>> INCLUDE {0}'.format(self.filename)
  def run(self, env):
    if (env._goto):
      return None
    # NOTE: circular dependency here
    import emil_parse
    pos = self.group.index(self)+1
    fname = expandvars(self.filename, default='UNKNOWN_VARIABLE')
    env_print(env, '\n>>> INCLUDE {0}'.format(fname))
    if (isfile(fname)):
      blocks, files = emil_parse.EMIL_Parse(fname)
      env.add_files(files)
      self.group.insert(pos, blocks)
    else:
      self.rc = '_RC_INPUT_EMIL_ERROR_'
    return self.rc

@python_2_unicode_compatible
class Program(Statement):
  def __init__(self, lines):
    super().__init__()
    self.lines = lines
  def __str__(self):
    name = '&' + self.lines[1].upper()
    code = ('\n' + self.level*self.indent).join(self.lines[2:])
    if (code != ''):
      code = '\n' + self.level*self.indent + code
    return self.level*self.indent + name + code
  def run(self, env):
    if (env._goto):
      return None
    name = self.lines[1]
    inp = expandvars('\n'.join(self.lines[2:]), default='UNKNOWN_VARIABLE')
    rc = env.run_module(name, inp)
    if (rc is not None):
      self.rc = rc
    return self.rc

# TODO: convert to ParTask?
@python_2_unicode_compatible
class System(Statement):
  def __init__(self, commandline, parallel=False):
    super().__init__()
    self.commandline = commandline
    self.parallel = parallel
  @property
  def expanded_commandline(self):
    return expandvars(self.commandline, skip_escaped=True)
  @property
  def name(self):
    if (self.parallel):
      return "EXEC"
    else:
      return "SHELL"
  def __str__(self):
    return self.level*self.indent + '>>> {0} {1}'.format(self.name, self.commandline)
  def run(self, env):
    if (env._goto):
      return None
    output = BytesIO()
    error = BytesIO()
    env_print(env, '\n>>> {0} {1}'.format(self.name, self.expanded_commandline))
    if (env.allow_shell):
      if (self.parallel):
        task = ['!'] + self.expanded_commandline.split()
        self.rc = env.parallel_task(task)
      else:
        self.rc = teed_call(self.expanded_commandline, shell=True, cwd=env.scratch, stdout=output, stderr=error)
    else:
      env_print(env, '(Shell commands disabled)')
    # Successful return is transparent: it inherits the previous return code
    if (self.rc == 0):
      self.rc = None
    return self.rc

@python_2_unicode_compatible
class Setting(Statement):
  def __init__(self, var, val):
    super().__init__()
    self.var = var
    self.val = val
  def __str__(self):
    if (self.var == 'echo'):
      val = self.val
      if (val in ['on', 'off']):
        val = val.upper()
      return self.level*self.indent + '>>> ECHO {0}'.format(val)
  def run(self, env):
    if (env._goto):
      return None
    if (self.var == 'echo'):
      if (self.val == 'on'):
        env.echo = True
      elif (self.val == 'off'):
        env.echo = False
      else:
        print(self.val)
    return self.rc

@python_2_unicode_compatible
class Label(Statement):
  def __init__(self, name):
    super().__init__()
    self.name = name
  def __str__(self):
    return self.level*self.indent + '>>> LABEL {0}'.format(self.name)
  def run(self, env):
    if (env._goto):
      if (env._goto == self.name):
        env._goto = False
    return self.rc

@python_2_unicode_compatible
class Jump(Statement):
  def __init__(self, name):
    super().__init__()
    self.name = name
  def __str__(self):
    return self.level*self.indent + '>>> GOTO {0}'.format(self.name)
  def run(self, env):
    if (env._goto):
      return None
    env_print(env, '\n>>> GOTO {0}'.format(self.name))
    env._goto = self.name
    return self.rc

@python_2_unicode_compatible
class ParTask(Statement):
  def __init__(self, action, force, args):
    super().__init__()
    self.action = action
    self.force = force
    self.args = args
  def __str__(self):
    f = '-FORCE ' if self.force else ''
    if (self.action == 'rm'):
      return self.level*self.indent + '>>> RM {0}{1}'.format(f, self.args[0])
    if (self.action == 'copy'):
      return self.level*self.indent + '>>> COPY {0}{1} {2}'.format(f, self.args[0], self.args[1])
    if (self.action == 'save'):
      return self.level*self.indent + '>>> SAVE {0}{1} {2}'.format(f, self.args[0], self.args[1])
    if (self.action == 'clone'):
      return self.level*self.indent + '>>> CLONE {0}{1} {2}'.format(f, self.args[0], self.args[1])
    if (self.action == 'collect'):
      return self.level*self.indent + '>>> COLLECT {0}{1} {2}'.format(f, self.args[0], self.args[1])
  def run(self, env):
    if (env._goto):
      return None
    f = '-FORCE ' if self.force else ''
    if (self.action == 'rm'):
      fname = expandvars(self.args[0], default='UNKNOWN_VARIABLE')
      env_print(env, '\n>>> RM {0}{1}'.format(f, fname))
      task = ['x', fname]
    if (self.action == 'copy'):
      f_in = expandvars(self.args[0], default='UNKNOWN_VARIABLE')
      f_out = expandvars(self.args[1], default='UNKNOWN_VARIABLE')
      env_print(env, '\n>>> COPY {0}{1} {2}'.format(f, f_in, f_out))
      task = ['c', '1', f_in, f_out]
    if (self.action == 'save'):
      f_in = expandvars(self.args[0], default='UNKNOWN_VARIABLE')
      f_out = expandvars(self.args[1], default='UNKNOWN_VARIABLE')
      env_print(env, '\n>>> SAVE {0}{1} {2}'.format(f, f_in, f_out))
      task = ['c', '0', f_in, f_out]
    if (self.action == 'clone'):
      f_in = expandvars(self.args[0], default='UNKNOWN_VARIABLE')
      f_out = expandvars(self.args[1], default='UNKNOWN_VARIABLE')
      env_print(env, '\n>>> CLONE {0}{1} {2}'.format(f, f_in, f_out))
      task = ['c', '3', f_in, f_out]
    if (self.action == 'collect'):
      f_in = expandvars(self.args[0], default='UNKNOWN_VARIABLE')
      f_out = expandvars(self.args[1], default='UNKNOWN_VARIABLE')
      env_print(env, '\n>>> COLLECT {0}{1} {2}'.format(f, f_in, f_out))
      task = ['c', '2', f_in, f_out]
    self.rc = env.parallel_task(task, force=self.force)
    if (self.force):
      self.rc = 0
    # Successful return is transparent: it inherits the previous return code
    if (self.rc == 0):
      self.rc = None
    else:
      self.rc = '_RC_INPUT_EMIL_ERROR_'
    return self.rc

@python_2_unicode_compatible
class Python(Statement):
  '''
  This is not really part of the "abstract flow" family, but it's included
  for consistency. Intended to be used with arbitrary python code, instead
  of some specific input language (like EMIL).
  '''
  def __init__(self, filename):
    self.rc = None
    with utf8_open(filename) as f_in:
      self.text = f_in.read()
      # If there's an error in the code SyntaxError will be raised
      # and catched upstream
      self.code = compile(self.text, filename, 'exec')
  def __str__(self):
    return self.text.rstrip()
  def run(self, env):
    exec(self.code, {'MOLCAS': env})
    self.rc = 0
    return self.rc