-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorollary.py
executable file
·1224 lines (919 loc) · 44 KB
/
corollary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
from abc import ABC, abstractmethod
from enum import Enum
from yaml.loader import SafeLoader
import argparse
import copy
import importlib
import inspect
import logging
import os
import re
import shlex
import sys
import yaml
_NAME = 'corollary'
_VERSION = '0.5'
class Commandline:
"""Parses and clusters command-line arguments."""
def __init__(self):
"""Constructor."""
self._argument_parser = argparse.ArgumentParser(
description='Corollary - Your Simple Command Executor')
self._argument_parser.add_argument('-c', '--command_directory',
dest='commandDirectory', help='Directory of available commands',
required=True)
self._argument_parser.add_argument('-f', '--formula',
dest='formula', required=True, help='The formula to be executed')
self._argument_parser.add_argument('-t', '--target_directory',
dest='targetDirectory', required=True, help='The directory in ' \
'whose context the formula shall be executed')
def parse_arguments(self):
"""Parse the command-line arguments of the script."""
self._parsed_arguments = self._argument_parser.parse_args()
@property
def command_directory(self):
"""Passed command directory."""
return self._parsed_arguments.commandDirectory
@property
def formula(self):
"""Passed formula file."""
return self._parsed_arguments.formula
@property
def target_directory(self):
"""Passed target directory."""
return self._parsed_arguments.targetDirectory
class Commands:
"""Holds information about commands found in the command directory."""
def __init__(self, commandDirectory):
"""Constructor."""
self._load_builtin_commands()
self._load_commands_from_directory(commandDirectory)
def _load_builtin_commands(self):
"""Load built-in commands.
Built-in commands are commands that inherit from the BuiltinCommand
class.
"""
thisModule = inspect.getmodule(self)
self._builtin_commands = {}
commands = self._load_commands(thisModule, '__main__', 'BuiltinCommand')
for command in commands:
self._builtin_commands[command.get_name()] = command
def _load_commands_from_directory(self, directory):
"""Load commands from the specified directory."""
self._commands = {}
# Only those commands are loaded that are explicitly exported as
# submodules via the Python package descriptor (file "__init__.py") and
# the __all__ variable, e.g., __all__ = ['cmds']
exportedModules = importlib.import_module(directory).__all__
qualifiedSubmoduleNames = [directory + '.' + n for n in exportedModules]
loadedSubmodules = [importlib.import_module(m, package=directory)
for m in qualifiedSubmoduleNames]
# Load commands from exported submodules
for submodule in loadedSubmodules:
sys.path.append(submodule.__file__)
loadedCommands = self._load_commands(submodule)
self._validate_and_register_external_commands(loadedCommands)
def _load_commands(self, submodule, commandClassModule='corollary',
commandClassName='Command'):
"""Load commands from the passed submodule.
The commandClassModule and commandClassName determine the class, from
which valid commands need to inherit. By default, this is the Command
class contained in this module (see below).
"""
classes = self._find_command_classes(submodule, commandClassModule,
commandClassName)
return [self._init_command(c, submodule.__file__) for c in classes]
def _find_command_classes(self, submodule, superclassModule,
superclassName):
"""Find all classes that represent commands.
A class represents a command, if it directly or indirectly inherits from
the given superclassName in the given superclassModule.
"""
classes = [c for c in self._filter_abstract_classes(submodule)
if self._has_superclass(superclassModule, superclassName, c)]
return classes
def _filter_abstract_classes(self, submodule):
"""Filter abstract classes in the given submodule."""
return [c[1] for c in inspect.getmembers(submodule, inspect.isclass)
if not inspect.isabstract(c[1])]
def _has_superclass(self, superclassModule, superclassName, clazz):
"""Check if a class inherits directly or indirectly from another class.
The class to check is represented by the clazz argument. The super
class to check is represented by the superclassName argument and is
expected to be contained in the given superclassModule.
"""
superclassQualifiedName = superclassModule + '.' + superclassName
mro = inspect.getmro(clazz)
for m in mro:
# We identify an inheritance relationship between two classes by
# means of their qualified names. The qualified name consists of the
# class's module and name, separated by a dot.
mroQualifiedName = m.__module__ + '.' + m.__name__
if mroQualifiedName == superclassQualifiedName:
return True
return False
def _validate_and_register_external_commands(self, commands):
"""Validate and register "external", i.e., non-built-in, commands.
External commands usually originate from the command directory passed to
corollary.
"""
for c in commands:
# An external command must not exhibit the same name as a built-in
# command
if c.get_name() in self._builtin_commands:
raise ValueError('Error while registering command class %s ' \
'(file "%s"): Command name "%s" is reserved for built-in' \
'comamnd' % (c.get_classname(), c.get_file(), c.get_name()))
# Names of external commands must be unique
elif c.get_name() in self._commands:
raise ValueError('Error while registering command class %s ' \
'(file "%s"): Duplicate command name "%s"' %\
(c.get_classname(), c.get_file(), c.get_name()))
# Register the command. That is, store the command under its name in
# the command dictionary maintained by the Commands class.
self._commands[c.get_name()] = c
def _init_command(self, commandClass, file):
"""Initialize a command."""
# Instantiate the command
commandInstance = commandClass(file, commandClass)
# Receive initialization values as specified by the command's
# implementer, i.e., the non-abstract class that inherits from the
# super class for commands
commandInstance.init_from_implementer()
return commandInstance
def get_command(self, commandName):
"""Get command with the given name."""
if self.is_builtin_command(commandName):
return self._builtin_commands[commandName]
else:
return self._commands[commandName]
def is_builtin_command(self, commandName):
"""Check if the class with the given name is a built-in command."""
return commandName in self._builtin_commands
def get_builtin_provided_variables(self):
"""Retrieve the set of the names of all provided command variables."""
try:
return self._provided_builtin_variables
except AttributeError:
self._provided_builtin_variables = set()
for bc in self._builtin_commands.values():
self._provided_builtin_variables.update(
[v.get_name() for v in bc.get_provided_variables()]
)
return self._provided_builtin_variables
class Command(ABC):
"""Abstract baseclass for commands."""
def __init__(self, file, clazz):
"""Constructor."""
self._file = file
self._clazz = clazz
def init_from_implementer(self):
"""Initialize a Command instance with values provided by implemters.
An implementer of Command is a non-abstract class that represents a
concrete command. This method invokes the implementations of template
methods of the implementer, that provide initialization values for the
command instance.
The initialization of a concrete command with values from its
implementers is handled separately from the constructor, to increase the
performance of Command.new_instance(), which simply copies the values of
an already initialized Command instance.
"""
name = self._must_be_string(self.name(), 'command name')
maximumScope = self._must_be_enum(self.maximum_scope(),
'maximum command scope', CommandScope)
arguments = self._must_be_list_of_types(self.arguments(), Argument,
'command arguments')
for arg in arguments:
arg.init_internal(self._file, name)
arg.validate()
providedVars = self._must_be_list_of_types(self.provided_variables(),
Variable, 'provided variables')
for v in providedVars:
v.init_internal(self._file, name)
v.validate()
requiresVarsNames = self._must_be_list_of_types(
self.required_variable_names(),
str,
'required variable names'
)
self._init_from_values(name, maximumScope, arguments, providedVars,
requiresVarsNames)
def _init_from_values(self, name, maximumScope, arguments, providedVars,
requiresVarsNames):
"""Reusable helper to initialize values of the Command."""
self._classname = self._clazz.__name__
self._name = name
self._maximumScope = maximumScope
self._arguments = arguments
self._provided_variables = providedVars
self._required_variable_names = requiresVarsNames
def new_instance(self):
"""Create a new instance of a concrete Command implementation.
To create the new instance, the values determining the state of the
copy's source instance are copied. That is, the initialization methods
of the implementer need not be invoked again.
"""
newInstance = self._clazz(self._file, self._clazz)
newInstance._init_from_values(self._name, self._maximumScope,
self._arguments, self._provided_variables,
self._required_variable_names)
return newInstance
@abstractmethod
def name(self):
"""For implementers: Name of a concrete command."""
pass
def get_name(self):
"""Get a concrete command's name."""
return self._name
def get_file(self):
"""Get a concrete command's file."""
return self._file
def get_classname(self):
"""Get the name of a concrete Command implementation's class."""
return self._classname
@abstractmethod
def execute(self, argumentValues):
"""For implementers: Execution logic of a concrete command."""
pass
def set_target_directory(self, targetDirectory):
"""Pass the given target directoy to a concrete command."""
self._targetDirectory = targetDirectory
def get_target_directory(self):
"""Get the target directory."""
return self._targetDirectory
def set_scope_variables(self, scopeVariables):
"""Pass the current execution scope's variables to a command."""
self._scopeVariables = scopeVariables
def get_scope_variable_value(self, variableName):
"""Get the value of the given variable within the current scope."""
return self._scopeVariables[variableName]
def maximum_scope(self):
"""For implementers: Determine maximum scope of the command."""
return CommandScope.GLOBAL
def get_maximum_scope(self):
"""Get the maximum scope of a command."""
return self._maximumScope
def arguments(self):
"""For implementers: Determine the arguments of the command."""
return []
def get_arguments(self):
"""Get a command's argument definitions."""
return self._arguments
def provided_variables(self):
"""For implementers: Determine the variables provided by the command."""
return []
def get_provided_variables(self):
"""Get a command provided variable's definitions."""
return self._provided_variables
def required_variable_names(self):
"""For implementers: Determine the variable required by the command."""
return []
def get_required_variable_names(self):
"""Get the names of the variables required by a command."""
return self._required_variable_names
def _must_be_string(self, v, valueName, mandatory=True):
"""Check if a value is of type str.
Returns the value, if it is of type str. If the mandatory flag is set
to True, the passed str must not be empty.
"""
if not isinstance(v, str):
raise ValueError('Error while initializing command class %s ' \
'(file "%s"): Value for %s must be string' % (self._classname,
self._file, valueName))
elif mandatory and not v:
self._ensure_mandatory(v, valueName)
return v
def _ensure_mandatory(self, v, valueName):
"""Check if the passed value v is not empty.
Throws a ValueError, if v is empty.
"""
if not v:
raise ValueError('Error while initializing command class %s ' \
'(file "%s"): Value for %s must not be empty' % \
(self._classname, self._file, valueName))
def _must_be_enum(self, v, valueName, enum, mandatory=True):
"""Check if a value is of type Enum.
Returns the value, if it is of type Enum. If the mandatory flag is set
to True, the passed Enum must not be empty.
"""
if not isinstance(v, enum):
raise ValueError('Error while initializing command class %s ' \
'(file "%s"): Value for %s must be %s enum' % (self._classname,
self._file, valueName, enum.__name__))
elif mandatory and not v:
self._ensure_mandatory(v, valueName)
return v
def _must_be_list_of_types(self, v, type, valueName):
"""Check if a value is a list of instances of a given type.
Returns the value, if it is a list of the given type's instances.
"""
if not isinstance(v, list):
raise ValueError('Error while initializing command %s (class ' \
'"%s", file "%s"): Value for %s must be list' % (self._name,
self._classname, self._file, valueName))
for value in v:
if not isinstance(value, type):
raise ValueError('Error while initializing command %s ' \
'(class "%s", file "%s"): List for %s must only contain ' \
'instances of type %s' % (self._name, self._classname,
self._file, valueName, type.__name__))
return v
class CommandScope(Enum):
"""Possible command scopes."""
GLOBAL = 512
GROUP = 256
MODULE = 128
def __str__(self):
"""Convert literal to str representation."""
return self.name.lower()
class BuiltinCommand(Command):
"""Super class for built-in commands."""
pass
class _GroupCommand(BuiltinCommand):
"""Built-in group command."""
NAME = 'group'
def name(self):
"""Determine the command's name."""
return self.NAME
def arguments(self):
"""Determine the command's arguments."""
return [Argument('groupName')]
def provided_variables(self):
"""Determine the variables provided by the command."""
return [Variable('group')]
def execute(self, values):
"""Execution logic of the command."""
return {'group': values['groupName']}
class Argument:
"""Command argument."""
def __init__(self, name):
"""Constructor."""
self._name = name
def get_name(self):
"""Retrieve the argument's name."""
return self._name
def init_internal(self, commandFile, commandName):
"""Initialize an argument. To be used by corollary only."""
self.commandFile = commandFile
self.commandName = commandName
def validate(self):
"""Validate the argument's initialization."""
self._validate_is_string(self._name, 'argument name')
def _validate_is_string(self, v, valueName, mandatory=True):
"""Check if a value is of type str.
If the mandatory flag is set to True, the passed str must not be empty.
"""
if not isinstance(v, str):
raise ValueError('Error while initializing argument of command ' \
'%s (file "%s"): Value for %s must be string' % \
(self.commandName, self.commandFile, valueName))
elif mandatory and not v:
raise ValueError('Error while initializing argument of command ' \
'%s (file "%s"): Value for %s must not be empty' % \
(self.commandName, self.commandFile, valueName))
class Variable:
"""Command variable."""
def __init__(self, name):
"""Constructor."""
self._name = name
def get_name(self):
"""Get the variable's name."""
return self._name
def init_internal(self, commandFile, commandName):
"""Initialize a variable. To be used by corollary only."""
self.commandFile = commandFile
self.commandName = commandName
def validate(self):
"""Validate the variable's initialization."""
self._validate_is_string(self._name, 'argument name')
def _validate_is_string(self, v, valueName, mandatory=True):
"""Check if a value is of type str.
If the mandatory flag is set to True, the passed str must not be empty.
"""
if not isinstance(v, str):
raise ValueError('Error while initializing argument of command ' \
'%s (file "%s"): Value for %s must be string' % \
(self.commandName, self.commandFile, valueName))
elif mandatory and not v:
raise ValueError('Error while initializing argument of command ' \
'%s (file "%s"): Value for %s must not be empty' % \
(self.commandName, self.commandFile, valueName))
class _ModuleCommand(BuiltinCommand):
"""Built-in module command."""
NAME = 'module'
def name(self):
"""Determine the command's name."""
return self.NAME
def arguments(self):
"""Determine the command's arguments."""
return [Argument('moduleName')]
def provided_variables(self):
"""Determine the variables provided by the command."""
return [Variable('module')]
def execute(self, values):
"""Execution logic of the command."""
return {'module': values['moduleName']}
class Formula:
"""A corollary formula."""
def __init__(self, formulaFile, commands):
"""Constructor."""
self._formulaFile = formulaFile
self._commands = commands
with open(formulaFile, 'r') as fd:
self._unpackedEntries = \
self._unpack_yaml_entries(yaml.load(fd, Loader=YamlLineLoader))
def get_file(self):
"""Get the formula's file."""
return self._formulaFile
def _unpack_yaml_entries(self, yamlEntries):
"""Unpack YAML entries.
Unpacking means that nested YAML scalars are lifted from nested lists
and dictionaries to the "top level", i.e., they are mapped their line
numbers of the defining formula and their nesting elements are ignored.
"""
unpackedEntries = {}
entryListsTodo = [(yamlEntries, 0)]
while entryListsTodo:
currentEntryList, nestingLevel = entryListsTodo.pop()
for e in currentEntryList:
scalars, nestedLists = self._unpack_yaml_entry(e)
for lineno, scalar in scalars:
unpackedEntries[lineno] = (scalar, nestingLevel)
for l in nestedLists:
entryListsTodo.append((l, nestingLevel+1))
# YAML does not guarantee entry ordering. Sort unpacked entries by line
# numbers to circumvent that constraint.
return dict(sorted(unpackedEntries.items()))
def _unpack_yaml_entry(self, entry):
"""Unpack a YAML entry."""
if isinstance(entry, tuple):
return ([self._unpack_yaml_scalar(entry)], [])
elif isinstance(entry, dict):
scalars = [self._unpack_yaml_scalar(k) for k in entry.keys()]
nestedLists = [l for l in entry.values()]
return (scalars, nestedLists)
else:
raise ValueError('Unexpected YAML entry type: %s. Could not ' \
'unpack.' % type(entry).__name__)
def _unpack_yaml_scalar(self, scalar):
"""Unpack a YAML scalar."""
if isinstance(scalar, tuple):
yamlScalar = scalar[0]
lineno = scalar[1]
return (lineno, yamlScalar)
else:
raise Exception('YAML scalar must be tuple (was %s)' % \
type(entry).__name__)
def get_unpacked_entries(self):
"""Get unpacked YAML entries."""
return self._unpackedEntries
class YamlLineLoader(SafeLoader):
"""Implementation of a YAML loader that preserves line numbers."""
def construct_scalar(self, node):
"""Keep line number for each YAML scalar."""
scalar = super(YamlLineLoader, self).construct_scalar(node)
return (scalar, node.start_mark.line + 1)
class ExecutionPlan:
"""An execution plan derived from a formula."""
_COMMAND_REGEX = re.compile('(?P<command>\S*)(?P<argumentValues>.*)?')
_GROUP_ENTRY = 'GROUP ENTRY'
_GROUP_EXIT = 'GROUP EXIT'
_MODULE_ENTRY = 'MODULE ENTRY'
_MODULE_EXIT = 'MODULE EXIT'
def __init__(self, commands, formula, targetDirectory):
"""Constructor."""
self._commands = commands
self._formulaFile = formula.get_file()
self._targetDirectory = targetDirectory
self._executionPlan = self._parse(formula)
self._validate_scoping()
def _parse(self, formula):
"""Parse a formula."""
yamlEntries = formula.get_unpacked_entries()
executionPlan = {}
# Iterate of the formula's unpacked YAML scalars and parse commands
for self._currentLineno, entryInfo in yamlEntries.items():
(yamlScalar, nestingLevel) = entryInfo
# Parse a command, its argument values, and internal execution
# instructions
instrsBefore, command, argumentValues, instrsAfter = \
self._parse_command(yamlScalar)
# Add the command to the execution plan
if not self._currentLineno in executionPlan:
executionPlan[self._currentLineno] = \
([], command, argumentValues, [])
# The line number of the command may already be assigned in the
# execution plan, because an after execution instruction was added
# (cf. _insert_after_execution_instruction_into_plan()). In this
# case, keep existing internal execution instructions.
else:
existingBefore, _, _, existingAfter = \
executionPlan[self._currentLineno]
executionPlan[self._currentLineno] = \
(existingBefore, command, argumentValues, existingAfter)
# Add before execution instructions of the command
if instrsBefore:
self._insert_before_execution_instruction_into_plan(
executionPlan,
instrsBefore
)
# Add after execution instructions of the command
if instrsAfter:
self._insert_after_execution_instruction_into_plan(
executionPlan,
nestingLevel,
yamlEntries,
instrsAfter
)
return sorted(executionPlan.items())
def _parse_command(self, yamlScalar):
"""Parse a command from a YAML scalar."""
match = self._COMMAND_REGEX.match(yamlScalar)
if not match:
raise ValueError('Line %d: %s is not a valid command syntax ' \
'(formula "%s")' % (self._currentLineno, yamlScalar,
self._formulaFile))
commandName = match.group('command')
try:
command = self._commands.get_command(commandName)
except KeyError:
raise ValueError('Line %d: Unkown command "%s" (formula "%s")' % \
(self._currentLineno, commandName, self._formulaFile))
try:
argumentValues = shlex.split(match.group('argumentValues'))
except IndexError:
argumentValues = []
self._validate_passed_arguments(command, argumentValues)
providedVars = command.get_provided_variables()
self._validate_provided_variables(command, providedVars)
# Determine internal execution instructions of the command. These
# instructions haven nothin to do with the comman'ds execution logic,
# but represent instructions needed by corollary to execute an execution
# plan and manipulate, e.g., the variable stack.
instrBefore, instrAfter = self._internal_execution_instructions(command)
return (instrBefore, command, argumentValues, instrAfter)
def _validate_passed_arguments(self, command, passedArguments):
"""Validate the arguments passed to a command."""
expectedCount = len(command.get_arguments())
passedCount = len(passedArguments)
if passedCount != expectedCount:
raise ValueError('Line %d: Command "%s" takes %d argument(s), ' \
'got %d (formula "%s")' % (self._currentLineno,
command.get_name(), expectedCount, passedCount,
self._formulaFile))
def _validate_provided_variables(self, command, providedVariables):
"""Validate the variables provided by a command."""
if self._commands.is_builtin_command(command.get_name()):
return
# A command cannot override variables provided by a built-in command
builtinVars = self._commands.get_builtin_provided_variables()
providedVarNames = [v.get_name() for v in providedVariables]
providedBuiltins = [v for v in providedVarNames if v in builtinVars]
if providedBuiltins:
raise ValueError('Line %d: Command "%s" cannot provide built-in ' \
'variable(s) "%s" (formula "%s")' % (self._current_lineno,
command.get_name(), ', '.join(providedBuiltins),
self._formulaFile))
def _internal_execution_instructions(self, command):
"""Determine the internal execution instructions of a command.
Internal execution instructions may only originate from a built-in
command. This method returns a tuple. Its first element determines
execution instructions to be executed _before_ the command. Its second
element determines instructions to be executed _after_ the command.
"""
if not self._commands.is_builtin_command(command.get_name()):
return (None, None)
if command.get_name() == _GroupCommand.NAME:
return (self._GROUP_ENTRY, self._GROUP_EXIT)
elif command.get_name() == _ModuleCommand.NAME:
return (self._MODULE_ENTRY, self._MODULE_EXIT)
else:
return (None, None)
def _insert_before_execution_instruction_into_plan(self, executionPlan,
instrBefore):
"""Insert before execution instructions into the execution plan."""
executionPlan[self._currentLineno][0].append(instrBefore)
def _insert_after_execution_instruction_into_plan(self, executionPlan,
nestingLevel, yamlEntries, instrsAfter):
"""Insert after execution instructions into the execution plan."""
# After execution instructions are executed when the command ends, i.e.,
# at a line number whose nesting level is lesser or equal to the nesting
# level of the command. For example, after execution instructions of a
# group are inserted into the execution plan when the group is left.
nextLinenos = [l for l in sorted(yamlEntries.keys())
if l > self._currentLineno]
for nextLineno in nextLinenos:
lineLevel = yamlEntries[nextLineno][1]
if lineLevel <= nestingLevel:
# After instructions become the first instructions to be
# executed at the same or next lower nesting level
executionPlan[nextLineno] = ([instrsAfter], None, [], [])
return
# There are not line numbers following the current one, i.e., the
# command is the last one of the current formula. In this case, the
# after execution instructions become the instructions to be executed
# after the command, instead of before the command on the next nesting
# level (see above).
executionPlan[self._currentLineno][2].append(instrsAfter)
def _validate_scoping(self):
"""Validate scoping within the formula."""
self._iterate_execution_plan(ExecutionPlanScopingValidator())
def _iterate_execution_plan(self, iterator):
"""Iterate the formula's execution plan.
This is a template method, whose behavior can be influenced by
ExecutionPlanIterator implementations.
"""
self._setup_scope()
self._setup_variable_stack()
# Iterator: Pass formula file
iterator.set_formula_file(self._formulaFile)
for self._current_lineno, commandInfo in self._executionPlan:
# Iterator: Pass line number
iterator.set_lineno(self._current_lineno)
(instrsBefore, command, argumentValues, instrsAfter) = commandInfo
# For each command to be iterated, create a fresh instance. That is,
# commands are always considered stateless.
newCommandInstance = command.new_instance()
# Iterator: Pass command instance
iterator.set_command(newCommandInstance)
# Iterator: Pass target directory
iterator.set_target_directory(
os.path.realpath(self._targetDirectory)
)
# Iterator: Pass command's argument values
iterator.set_argument_values(argumentValues)
self._determine_current_scope(instrsBefore)
# Iterator: Pass current scope
iterator.after_scope_set(self._currentScope)
self._execute_instruction_on_variable_stack(instrsBefore)
# Iterator: Pass current scope's variables
iterator.after_variable_stack_preparation(
self._visibleVariables[self._currentScope]
)
# Put provided variables of a command on the current scope's
# variable stack
for v in newCommandInstance.get_provided_variables():
providedValue = iterator.get_provided_variable_value(
v.get_name()
)
self._put_value_on_variable_stack(v, providedValue)
iterator.after_provided_variables_on_stack(
self._visibleVariables[self._currentScope]
)
self._execute_instruction_on_variable_stack(instrsAfter)
self._determine_current_scope(instrsAfter)
def _setup_scope(self):
"""Setup scope stack."""
self._currentScope = CommandScope.GLOBAL
self._scopeStack = [self._currentScope]
def _setup_variable_stack(self):
"""Setup variable stack per possible scope."""
self._visibleVariables = {
CommandScope.GLOBAL: {},
CommandScope.GROUP: {},
CommandScope.MODULE: {}
}
def _determine_current_scope(self, executionInstructions):
"""Determine current scope from the given execution instructions.
This method also manipulates the scope stack depending on the given
execution instructions.
"""
for instruction in executionInstructions:
if instruction == self._GROUP_ENTRY:
self._currentScope = CommandScope.GROUP
self._scopeStack.insert(0, self._currentScope)
elif instruction == self._GROUP_EXIT:
self._currentScope = self._scopeStack.pop(0)
elif instruction == self._MODULE_ENTRY:
self._currentScope = CommandScope.MODULE
self._scopeStack.insert(0, self._currentScope)
elif instruction == self._MODULE_EXIT:
self._currentScope = self._scopeStack.pop(0)
def _execute_instruction_on_variable_stack(self, executionInstructions):
"""Execute internal execution instructions on the variable stack.
The execution instructions may impact the variable stacks of more than
one scope.
"""
globalVars = self._visibleVariables[CommandScope.GLOBAL]
for instruction in executionInstructions:
# A group was entered. Copy the variables from preceding global
# scope.
if instruction == self._GROUP_ENTRY:
self._copy_variables(globalVars.keys(), CommandScope.GLOBAL,
CommandScope.GROUP)
# A group was exited. Remove its variables from the stack.
elif instruction == self._GROUP_EXIT:
self._visibleVariables[CommandScope.GROUP] = {}
# A module. was entered. Copy the variables from preceding global
# and group scope. Group variables may overwrite global variables,
# if a module is contained in a group.
elif instruction == self._MODULE_ENTRY:
groupVars = self._visibleVariables[CommandScope.GROUP].keys()
globalNonGroup = [g for g in globalVars if g not in groupVars]
self._copy_variables(globalNonGroup, CommandScope.GLOBAL,
CommandScope.MODULE)
self._copy_variables(groupVars, CommandScope.GROUP,
CommandScope.MODULE)
# A module was exited. Remove its variables from the stack.
elif instruction == self._MODULE_EXIT:
self._visibleVariables[CommandScope.MODULE] = {}
def _copy_variables(self, variableNames, fromScope, toScope):
"""Copy variable values from a scope to another scope.
Copying is deep.
"""
scopeVars = self._visibleVariables[fromScope]
toCopy = [v for v in scopeVars if v in variableNames]
for varName in toCopy:
value = scopeVars[varName]
self._visibleVariables[toScope][varName] = copy.deepcopy(value)
def _put_value_on_variable_stack(self, variable, value):
"""Put a variable value on the current scope's variable stack."""
self._visibleVariables[self._currentScope][variable.get_name()] = value
def execute(self):
"""Execute the execution plan."""
self._iterate_execution_plan(ExecutionPlanExecutor())
class ExecutionPlanIterator(ABC):
"""Abstract baseclass for execution plan iterators."""
def after_scope_set(self, currentScope):
"""Callback: Current scope was determined."""
pass
def after_variable_stack_preparation(self, scopeVariables):
"""Callback: The variable stack for the current scope was prepared."""
pass
def get_provided_variable_value(self, variableName):
"""Get the value of a provided variable."""
return None
def after_provided_variables_on_stack(self, scopeVariables):
"""Callback: Provided variable values were put on the stack."""