-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocalize.py
executable file
·1231 lines (1035 loc) · 47.5 KB
/
localize.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 python
# When run with the -s flag, localize.py configures the SUMS-server component of NetDRMS.
import sys
import getopt
import re
import os
import stat
import filecmp
from subprocess import check_output, CalledProcessError
import shlex
# Constants
VERS_FILE = 'jsoc_version.h'
SDP_CFG = 'configsdp.txt'
NET_CFG = 'config.local'
NET_CFGMAP = 'config.local.map'
RET_SUCCESS = 0
RET_NOTDRMS = 1
PREFIX = """# This file was auto-generated by localize.py. Please do not edit it directly (running
# configure will run localize.py, which will then overwrite any edits manually performed).
"""
C_PREFIX = """/* This file was auto-generated by localize.py. Please do not edit it directly (running
* configure will run localize.py, which will then overwrite any edits manually performed). */
"""
PERL_BINPATH = '#!/usr/bin/perl\n'
PERL_INTIAL = """package drmsparams;
use warnings;
use strict;
"""
PERL_FXNS_A = """sub new
{
my($clname) = shift;
my($self) =
{
_paramsH => undef
};
bless($self, $clname);
$self->{_paramsH} = {};
$self->initialize();
return $self;
}
sub DESTROY
{
my($self) = shift;
}
"""
PERL_FXNS_B = """sub get
{
my($self) = shift;
my($name) = shift;
my($rv);
if (exists($self->{_paramsH}->{$name}))
{
return $self->{_paramsH}->{$name};
}
else
{
return undef;
}
}
1;"""
PY_BINPATH = '#!/usr/bin/env python3\n'
PY_ALL = f"__all__ = [ 'DPError', 'DPMissingParameterError', 'DRMSParams' ]"
PY_ERROR_CLASSES = """
class DPError(Exception):
def __init__(self):
self._msg = 'DRMS Parameter Error: '
class DPMissingParameterError(DPError):
def __init__(self, parameter):
super().__init__()
self._msg += 'missing DRMS parameter ' + parameter
def __str__(self):
return self._msg
"""
PY_FXNS_A = """
class DRMSParams(object):
def __init__(self):
self.params = {}
self.initialize()
def __del__(self):
del self.params
def initialize(self):
"""
PY_FXNS_B = """ def get(self, name):
if name in self.params:
return self.params[name]
else:
return None
"""
PY_FXNS_C = """ def get_required(self, name):
try:
value = self.params[name]
except:
raise DPMissingParameterError(name)
return value
def getBool(self, name):
if name in self.params:
return bool(self.params[name] == '1')
else:
return None
def __getattr__(self, name):
# only called if object.__getattribute__(self, name) raises; and if that is true, then we want
# to look in self.params for it, and set the instance attribute if it does exist in self.params
if name in self.params:
attr = self.params[name]
self.__setattr__(name, attr)
else:
attr = None
return attr
def __setattr__(self, name, value):
# call neither __setattr__ nor __getattr__
try:
params = object.__getattr__(self, 'params')
# put into self.params dict, overwriting if necessary
params[name] = value
except:
pass
# store in instance dict as well
object.__setattr__(self, name, value)
"""
SH_BINPATH = '#!/bin/bash\n'
SUMRM_COMMENT = """# This is the configuration file for the sum_rm program. It was auto-generated by the DRMS master configure script.
# It controls the behavior of the sum_rm program, and is loaded each time sum_rm runs. To change the
# parameter values in this configuration file, modify config.local, then re-run configure. This configuration
# file will be updated only if parameters affecting it are modified. If such changes are made to config.local,
# please make sure that the sum_rm service is disabled while configure in running.
"""
SUMRM_DOC = """# sum_rm removes end-of-life SUMS data files to prevent disk-partitions from becomming 100% full.
# sum_svc, the main SUMS service, starts this daemon when it is launched. Within an infinite loop, sum_rm sleeps
# for SLEEP number of seconds (defined below). When it awakes, it loads the sum_rm configuration file. For each SU,
# it then examines the 'effective_date' column within the sum_partn_alloc table of the SUMS database. It does so by
# sorting all SUs by effective date (this date is actually an expiration date - the SU is good until the end of that day),
# and then, starting with the SU with the oldest effective date, it deletes the SUs. It continues deleting SUs until one
# of three conditions is met:
#
# (a) The disk has at least PART_PERCENT_FREE percent free space.
# (b) All SUs have effective_date values in the future.
# (c) At least 600 SUs have been deleted (this is defined in the LIMIT statement in the file SUMLIB_RmDoX.pgc).
#
# After sum_rm stops deleteing SUs, it then sleeps for SLEEP seconds, completing the first iteration of the
# infinite loop.
"""
SUMRM_PARTN_PERCENT_FREE = """
# This is the percentage at which all disk partitions are to be kept free.
# If not specified, this defaults to 3. For example, setting PART_PERCENT_FREE = 5 will allow all partitions to
# fill to 95% full. Dividing the number of unused blocks by the total number of blocks, and rounding up,
# will result in the number specified by PART_PERCENT_FREE.
#
# NOTE : This behavior was previously controlled by the MAX_FREE_{n} family of parameters. {n} referred to the
# disk-partition number and the value of parameter MAX_FREE_{n} was the MINIMUM number of free MB in the
# partition [No clue why the word MAX was used]. As of NetDRMS 7.0, MAX_FREE_{n} has no effect."""
SUMRM_SLEEP = """
# The value is the number of seconds to sleep between iterations of the main loop in sum_rm."""
SUMRM_LOG = """
# The value is the log file (opened only at sum_rm startup; the sum_rm pid is appended to this file name)."""
SUMRM_MAIL = """
# The value is the email address of the recipient to be notified in case of a problem."""
SUMRM_NOOP = """
# If the value is set to anything other than 0, then sum_rm is rendered inactive. Otherwise, sum_rm is active."""
SUMRM_USER = """
# The value designates the linux user who is allowed to run sum_rm."""
SUMRM_NORUN = """
# This pair of paramters defines a window of time, during which sum_rm will become torpid - in this
# window, sum_rm will not scan for SUs to delete, not will it delete any SUs. Each value is an integer, 0-23, that
# represents an hour of the day. For example, if NORUN_START=8 and NORUN_STOP=10, then between 8am and 10am
# local time, sum_rm will be dormant. To disable this behavior, set both parameters to the same value."""
RULESPREFIX = """# Standard things
sp := $(sp).x
dirstack_$(sp) := $(d)
d := $(dir)
"""
RULESSUFFIX = """# Standard things
d := $(dirstack_$(sp))
sp := $(basename $(sp))
"""
ICC_MAJOR = 9
ICC_MINOR = 0
GCC_MAJOR = 3
GCC_MINOR = 0
IFORT_MAJOR = 9
IFORT_MINOR = 0
GFORT_MAJOR = 4
GFORT_MINOR = 2
# Read arguments
# d - localization directory
# b - base name of all parameter files (e.g., -b drmsparams --> drmsparams.h, drmsparams.mk, drmsparams.pm, etc.)
# s - configure a NetDRMS server (i.e., SUMS server). Otherwise, do client configuration only. A server
# configuration will modify something that only the production user has access to. An example is the sum_rm.cfg
# file. To modify that file, the user running configure must be running configure on the SUMS-server host, and
# must have write permission on sum_rm.cfg.
def GetArgs(args):
rv = bool(0)
optD = {}
try:
opts, remainder = getopt.getopt(args, "hd:b:s",["dir=", "base="])
except getopt.GetoptError:
print('Usage:')
print('localize.py [-h] -d <localization directory> -b <parameter file base>')
rv = bool(1)
if rv == bool(0):
for opt, arg in opts:
if opt == '-h':
print('localize.py [-h] -d <localization directory> -b <parameter file base>')
elif opt in ("-d", "--dir"):
regexp = re.compile(r"(\S+)/?")
matchobj = regexp.match(arg)
if matchobj is None:
rv = bool(1)
else:
optD['dir'] = matchobj.group(1)
elif opt in ("-b", "--base"):
optD['base'] = arg
elif opt in ("-s"):
optD['server'] = ""
else:
optD[opt] = arg
return optD
def createMacroStr(key, val, keyColLen, status):
if keyColLen < len(key):
status = bool(1)
return None
else:
nsp = keyColLen - len(key)
spaces = str()
for isp in range(nsp):
spaces += ' '
status = bool(0)
return '#define ' + key + spaces + val + '\n'
def createPerlConst(key, val, keyColLen, status):
if keyColLen < len(key):
status = bool(1)
return None
else:
nsp = keyColLen - len(key)
spaces = str()
for isp in range(nsp):
spaces += ' '
status = bool(0)
return 'use constant ' + key + ' => ' + spaces + val + ';\n'
def createPyConst(key, val, keyColLen, status):
if keyColLen < len(key):
status = bool(1)
return None
else:
nsp = keyColLen - len(key)
spaces = str()
for isp in range(nsp):
spaces += ' '
status = bool(0)
return key + ' = ' + spaces + val + '\n'
def createShConst(key, val, status):
status = bool(0)
return key + '=' + val + '\n'
def isSupportedPlat(plat):
regexp = re.compile(r"\s*(^x86_64|^ia32|^ia64|^avx)", re.IGNORECASE)
matchobj = regexp.match(plat);
if not matchobj is None:
return bool(1);
else:
return bool(0);
def processMakeParam(mDefs, key, val, platDict, machDict):
varMach = None
regexp = re.compile(r"(\S+):(\S+)")
matchobj = regexp.match(key)
if not matchobj is None:
varName = matchobj.group(1)
varMach = matchobj.group(2)
else:
varName = key
varValu = val
if varMach is None:
mDefs.extend(list('\n' + varName + ' = ' + varValu))
else:
if isSupportedPlat(varMach):
# The guard will compare varValu to $JSOC_MACHINE.
if not varMach in platDict:
platDict[varMach] = {}
platDict[varMach][varName] = varValu
else:
# The guard will compare varValu to $MACHINETYPE (this is just the hostname of the machine on which localize.py is running).
if not varMach in machDict:
machDict[varMach] = {}
machDict[varMach][varName] = varValu
def processParam(cfgfile, line, regexpQuote, regexp, keymap, defs, cDefs, mDefsGen, mDefsMake, perlConstSection, perlInitSection, pyConstSection, pyInitSection, shConstSection, platDict, machDict, section):
status = 0
parameter_added = {}
keyCfgSp = None
val = None
if ''.join(section) == 'defs' or not cfgfile:
if type(line) is str:
matchobj = regexp.match(line)
if not matchobj is None:
# We have a key-value line
keyCfgSp = matchobj.group(1)
val = matchobj.group(2)
else:
keyCfgSp, val = list(line.items())
if keyCfgSp is not None and val is not None:
# We have a key-value line
keyCfgSp = matchobj.group(1)
val = matchobj.group(2)
# Must map the indirect name to the actual name
if keymap:
# Map to actual name only if the keymap is not empty (which signifies NA).
if keyCfgSp in keymap:
key = keymap[keyCfgSp]
elif keyCfgSp == 'LOCAL_CONFIG_SET' or keyCfgSp == 'DRMS_SAMPLE_NAMESPACE':
# Ignore parameters that are not useful and shouldn't have been there in the first place. But
# they have been released to the world, so we have to account for them.
return None
elif not cfgfile:
# Should not be doing mapping for addenda
key = keyCfgSp
else:
raise Exception('badKeyMapKey', keyCfgSp)
else:
key = keyCfgSp
matchobj = regexpQuote.match(key)
if not matchobj is None:
quote = matchobj.group(1)
key = matchobj.group(2)
# master defs dictionary
defs[key] = val
parameter_added[key] = val
# C header file
if quote == "q":
# Add double-quotes
cDefs.extend(list(createMacroStr(key, '"' + val + '"', 40, status)))
elif quote == "p":
# Add parentheses
cDefs.extend(list(createMacroStr(key, '(' + val + ')', 40, status)))
elif quote == "a":
# Leave as-is
cDefs.extend(list(createMacroStr(key, val, 40, status)))
else:
# Unknown quote type
raise Exception('badQuoteQual', key)
if status:
raise Exception('paramNameTooLong', key)
# Make file - val should never be quoted; just use as is
mDefsGen.extend(list('\n' + key + ' = ' + val))
# Perl file - val should ALWAYS be single-quote quoted
# Save const info to a string
perlConstSection.extend(list(createPerlConst(key, "'" + val + "'", 40, status)))
# Python file
pyConstSection.extend(list(createPyConst(key, "'" + val + "'", 40, status)))
# Shell source file
shConstSection.extend(list(createShConst(key, "'" + val + "'", status)))
if status:
raise Exception('paramNameTooLong', key)
# Save initialization information as a string. Now that we've defined
# constants (the names of which are the parameter names)
# we can refer to those in the init section. The key variable holds the
# name of the constant.
perlInitSection.extend(list("\n $self->{_paramsH}->{'" + key + "'} = " + key + ';'))
# The amount of indenting matters! This is Python.
pyInitSection.extend(list(" self.params['" + key + "'] = " + key + '\n'))
else:
# No quote qualifier
raise Exception('missingQuoteQual', key)
elif ''.join(section) == 'make' and cfgfile:
# Configure the remaining make variables defined in the __MAKE__ section of the configuration file. Third-party
# library make variables are specified in the __MAKE__ section.
matchobj = regexp.match(line)
if not matchobj is None:
# We have a key-value line
key = matchobj.group(1)
val = matchobj.group(2)
# This information is for making make variables only. We do not need to worry about quoting any values
defs[key] = val
processMakeParam(mDefsMake, key, val, platDict, machDict)
return parameter_added
# We have some extraneous line or a newline - ignore.
def process_project_repos(project_includes):
print(f'[ process_project_repos ]')
error = False
ordered_files = []
if os.path.exists('proj'):
# iterate through all proj subdirectories
for subdirectory in os.listdir('proj'):
stripped_subdirectory = subdirectory.strip()
path = os.path.join('proj', stripped_subdirectory)
# skip any dir in proj that does not have a Rules.mk file
if os.path.isfile(os.path.join(path, 'Rules.mk')):
if os.path.isdir(path):
ordered_files.append(stripped_subdirectory)
ordered_files.sort()
for stripped_subdirectory in ordered_files:
project_includes.append(f'dir := $(d)/{stripped_subdirectory}')
return error
def determineSection(line, regexpStyle, regexpDefs, regexpMake):
matchobj = regexpStyle.match(line)
if matchobj:
return 'style'
matchobj = regexpDefs.match(line)
if not matchobj is None:
return 'defs'
matchobj = regexpMake.match(line)
if not matchobj is None:
return 'make'
return None
# defs is a dictionary containing all parameters (should they be needed in this script)
def parseConfig(fin, keymap, addenda, defs, cDefs, mDefsGen, mDefsMake, project_includes, perlConstSection, perlInitSection, pyConstSection, pyInitSection, shConstSection):
error = False
# Open required config file (config.local)
try:
# Examine each line, looking for key=value pairs.
regexpStyle = re.compile(r"^__STYLE__")
regexpDefs = re.compile(r"^__DEFS__")
regexpMake = re.compile(r"^__MAKE__")
regexpComm = re.compile(r"^\s*#")
regexpSp = re.compile(r"^s*$")
regexpQuote = re.compile(r"^\s*(\w):(.+)")
regexpCustMkBeg = re.compile(r"^_CUST_")
regexpCustMkEnd = re.compile(r"^_ENDCUST_")
regexpDiv = re.compile(r"^__")
regexp = re.compile(r"^\s*(\S+)\s+(\S.*)")
ignoreKeymap = False
platDict = {}
machDict = {}
# Process the parameters in the configuration file
if not fin is None:
for line in fin:
matchobj = regexpComm.match(line)
if not matchobj is None:
# Skip comment line
continue
matchobj = regexpSp.match(line)
if not matchobj is None:
# Skip whitespace line
continue
newSection = determineSection(line, regexpStyle, regexpDefs, regexpMake)
if not newSection is None:
section = newSection
if not section:
raise Exception('invalidConfigFile', 'line ' + line.strip() + ' is not in any section')
if section == 'style':
# if the config.local file has new in the __STYLE__ section, then ignore the keymap and treat config.local like configsdp.txt;
# do not map from NetDRMS config.local parameter names to configsdp.txt names
for line in fin:
matchobj = regexpDiv.match(line)
if matchobj:
break;
if line.strip(' \n').lower() == 'new' and keymap:
ignoreKeymap = True
newSection = determineSection(line, regexpStyle, regexpDefs, regexpMake)
if not newSection is None:
section = newSection
continue
elif section == 'make':
# There are some blocks of lines in the __MAKE__ section that must be copied ver batim to the output make file.
# The blocks are defined by _CUST_/_ENDCUST_ tags.
matchobj = regexpCustMkBeg.match(line)
if not matchobj is None:
mDefsMake.extend(list('\n'))
for line in fin:
matchobj = regexpCustMkEnd.match(line)
if not matchobj is None:
break;
mDefsMake.extend(list(line))
newSection = determineSection(line, regexpStyle, regexpDefs, regexpMake)
if not newSection is None:
section = newSection
continue
# Intentional fall through to next if statement
if section == 'defs' or section == 'make':
iscfg = bool(1)
if ignoreKeymap:
keymapActual = None
else:
keymapActual = keymap
ppRet = processParam(iscfg, line, regexpQuote, regexp, keymapActual, defs, cDefs, mDefsGen, mDefsMake, perlConstSection, perlInitSection, pyConstSection, pyInitSection, shConstSection, platDict, machDict, section)
else:
# Unknown section
raise Exception('unknownSection', section)
except Exception as exc:
if len(exc.args) >= 2:
msg = exc.args[0]
else:
# re-raise the exception
raise
if msg == 'invalidConfigFile':
violator = exc.args[1]
print(violator, file=sys.stderr)
error = True
elif msg == 'badKeyMapKey':
# If we are here, then there was a non-empty keymap, and the parameter came from
# the configuration file.
violator = exc.args[1]
print('Unknown parameter name ' + "'" + violator + "'" + ' in ' + fin.name + '.', file=sys.stderr)
error = True
elif msg == 'badQuoteQual':
# The bad quote qualifier came from the configuration file, not the addenda, since
# we will have fixed any bad qualifiers in the addenda (which is populated by code).
violator = exc.args[1]
print('Unknown quote qualifier ' + "'" + violator + "'" + ' in ' + fin.name + '.', file=sys.stderr)
error = True
elif msg == 'missingQuoteQual':
violator = exc.args[1]
print('Missing quote qualifier for parameter ' + "'" + violator + "'" + ' in ' + fin.name + '.', file=sys.stderr)
error = True
elif msg == 'paramNameTooLong':
violator = exc.args[1]
print('Macro name ' + "'" + violator + "' is too long.", file=sys.stderr)
error = True
elif msg == 'unknownSection':
violator = exc.args[1]
print('Unknown section ' + "'" + violator + "' in configuration file.", file=sys.stderr)
error = True
else:
# re-raise the exception
raise
if not error:
if project_includes is not None:
error = process_project_repos(project_includes)
# Process addenda - these are parameters that are not configurable and must be set in the
# NetDRMS build.
if not error:
iscfg = bool(0)
for key, val in addenda.items():
item = f'{key} {val}'
if ignoreKeymap:
keymapActual = None
else:
keymapActual = keymap
ppRet = processParam(iscfg, item, regexpQuote, regexp, keymapActual, defs, cDefs, mDefsGen, mDefsMake, perlConstSection, perlInitSection, pyConstSection, pyInitSection, shConstSection, platDict, machDict, 'defs')
# Put information collected in platDict and machDict into mDefs. Must do this here, and not in processParam, since
# we need to parse all platform-specific make variables before grouping them into platform categories.
if not error:
for plat in platDict:
mDefsMake.extend(list('\nifeq ($(JSOC_MACHINE), linux_' + plat.lower() + ')'))
for var in platDict[plat]:
mDefsMake.extend(list('\n' + var + ' = ' + platDict[plat][var]))
mDefsMake.extend(list('\nendif\n'))
if not error:
for mach in machDict:
mDefsMake.extend(list('\nifeq ($(MACHTYPE), ' + mach + ')'))
for var in machDict[mach]:
mDefsMake.extend(list('\n' + var + ' = ' + machDict[mach][var]))
mDefsMake.extend(list('\nendif\n'))
return error
def getMgrUIDLine(sums_manager, uidParam):
error = False
if sums_manager and len(sums_manager) > 0:
cmd = [ 'id', '-u', shlex.quote(sums_manager) ]
try:
ret = check_output(cmd)
uidParam['q:SUMS_MANAGER_UID'] = ret.decode("utf-8")
except ValueError:
print('Unable to run cmd: ' + cmd + '.')
error = True
except CalledProcessError:
print('Command ' + "'" + cmd + "'" + ' ran improperly.')
error = True
return error
def isVersion(maj, min, majDef, minDef):
res = 0
if maj > majDef or (maj == majDef and min >= minDef):
res = 1
return res
def configureComps(defs, mDefs):
rv = bool(0)
autoConfig = bool(1)
if 'AUTOSELCOMP' in defs:
autoConfig = (not defs['AUTOSELCOMP'] == '0')
if autoConfig:
hasicc = bool(0)
hasgcc = bool(0)
hasifort = bool(0)
hasgfort = bool(0)
# Try icc.
cmd = 'icc --version 2>&1'
try:
ret = check_output(cmd, shell=True)
ret = ret.decode("utf-8")
except CalledProcessError:
print('Command ' + "'" + cmd + "'" + ' ran improperly.')
rv = bool(1)
if not rv:
regexp = re.compile(r"\s*\S+\s+\S+\s+(\d+)[.](\d+)", re.DOTALL)
matchobj = regexp.match(ret)
if matchobj is None:
raise Exception('unexpectedIccRet', ret)
else:
major = matchobj.group(1)
minor = matchobj.group(2)
if isVersion(int(major), int(minor), ICC_MAJOR, ICC_MINOR):
hasicc = bool(1)
# Try gcc.
if not hasicc:
rv = bool(0)
cmd = 'gcc -v 2>&1'
try:
ret = check_output(cmd, shell=True)
ret = ret.decode("utf-8")
except CalledProcessError:
print('Command ' + "'" + cmd + "'" + ' ran improperly.')
rv = bool(1)
if not rv:
regexp = re.compile(r".+gcc\s+version\s+(\d+)\.(\d+)", re.DOTALL)
matchobj = regexp.match(ret)
if matchobj is None:
raise Exception('unexpectedGccRet', ret)
else:
major = matchobj.group(1)
minor = matchobj.group(2)
if isVersion(int(major), int(minor), GCC_MAJOR, GCC_MINOR):
hasgcc = bool(1)
# Try ifort.
rv = bool(0)
cmd = 'ifort --version 2>&1'
try:
ret = check_output(cmd, shell=True)
ret = ret.decode("utf-8")
except CalledProcessError:
print('Command ' + "'" + cmd + "'" + ' ran improperly.')
rv = bool(1)
if not rv:
regexp = re.compile(r"\s*\S+\s+\S+\s+(\d+)\.(\d+)", re.DOTALL)
matchobj = regexp.match(ret)
if matchobj is None:
raise Exception('unexpectedIfortRet', ret)
else:
major = matchobj.group(1)
minor = matchobj.group(2)
if isVersion(int(major), int(minor), IFORT_MAJOR, IFORT_MINOR):
hasifort = bool(1)
# Try gfortran
if not hasifort:
rv = bool(0)
cmd = 'gfortran -v 2>&1'
try:
ret = check_output(cmd, shell=True)
ret = ret.decode("utf-8")
except CalledProcessError:
print('Command ' + "'" + cmd + "'" + ' ran improperly.')
rv = bool(1)
if not rv:
regexp = re.compile(r".+gcc\s+version\s+(\d+)\.(\d+)", re.DOTALL)
matchobj = regexp.match(ret)
if matchobj is None:
raise Exception('unexpectedGfortranRet', ret)
else:
major = matchobj.group(1)
minor = matchobj.group(2)
if isVersion(int(major), int(minor), GFORT_MAJOR, GFORT_MINOR):
hasgfort = bool(1)
# Append the compiler make variables to the make file
rv = bool(0)
if not hasicc and not hasgcc:
print('Fatal error: Acceptable C compiler not found! You will be unable to build the DRMS library.', file=sys.stderr)
rv = bool(0) # Art - don't bail, we might be using drmsparams.py and not care about the rest of DRMS
elif hasicc:
mDefs.extend(list('\nCOMPILER = icc'))
# mDefs.extend(list('\nICC_VERSION = blah'))
else:
mDefs.extend(list('\nCOMPILER = gcc'))
if not hasifort and not hasgfort:
print('Warning: Acceptable Fortran compiler not found! Fortran interface will not be built, and you will be unable to build Fortran modules.', file=sys.stderr)
elif hasifort:
mDefs.extend(list('\nFCOMPILER = ifort'))
else:
mDefs.extend(list('\nFCOMPILER = gfortran'))
# Environment overrides. These get written, regardless of the disposition of auto-configuration.
mDefs.extend(list('\nifneq ($(JSOC_COMPILER),)\n COMPILER = $(JSOC_COMPILER)\nendif'))
mDefs.extend(list('\nifneq ($(JSOC_FCOMPILER),)\n FCOMPILER = $(JSOC_FCOMPILER)\nendif'))
return rv
def writeParamsFiles(base, cfile, mfile, pfile, pyfile, shfile, cDefs, mDefsGen, mDefsMake, mDefsComps, perlConstSection, perlInitSection, pyConstSection, pyInitSection, shConstSection):
rv = bool(0)
# Merge mDefsGen, mDefsMake, and mDefsComps into a single string with compiler configuration first, general parameters next, then
# make-specific make variables (e.g., third-party library information) last.
mDefs = '\n# Compiler Selection\n' + ''.join(mDefsComps) + '\n\n# General Parameters\n' + ''.join(mDefsGen) + '\n\n# Parameters to Configure make\n' + ''.join(mDefsMake)
try:
with open(cfile, 'w') as cout, open(mfile, 'w') as mout, open(pfile, 'w') as pout, open(pyfile, 'w') as pyout, open(shfile, 'w') as shout:
# C file of macros
print(C_PREFIX, file=cout)
print('/* This file contains a set of preprocessor macros - one for each configuration parameter. */\n', file=cout)
buf = '__' + base.upper() + '_H'
print('#ifndef ' + buf, file=cout)
print('#define ' + buf, file=cout)
print(''.join(cDefs), file=cout)
print('#endif', file=cout)
# Make file of make variables
print(PREFIX, file=mout)
print('# This file contains a set of make-variable values. The first section contains compiler-selection variables, the second contains general configuration variables, and the third section contains variables that configure how make is run.', file=mout)
print(mDefs, file=mout)
# Perl module
print(PERL_BINPATH, file=pout)
print(PREFIX, file=pout)
print('# This file contains a set of constants - one for each configuration parameter.\n', file=pout)
print(PERL_INTIAL, file=pout)
print(''.join(perlConstSection), file=pout)
print(PERL_FXNS_A, file=pout)
print('sub initialize', file=pout)
print('{', file=pout)
print(' my($self) = shift;', file=pout, end='')
print('', file=pout)
print(''.join(perlInitSection), file=pout)
print('}\n', file=pout)
print(PERL_FXNS_B, file=pout)
# Python module
print(PY_BINPATH, file=pyout)
print(PREFIX, file=pyout)
print('# This file contains a set of constants - one for each configuration parameter.\n', file=pyout)
print(PY_ALL, file=pyout)
print(''.join(pyConstSection), file=pyout)
print(PY_ERROR_CLASSES, file=pyout)
print(PY_FXNS_A, file=pyout, end='')
print(''.join(pyInitSection), file=pyout)
print(PY_FXNS_B, file=pyout)
print(PY_FXNS_C, file=pyout)
# Shell (bash) source file
print(SH_BINPATH, file=shout)
print(PREFIX, file=shout)
print('# This file contains a set of variable assignments - one for each configuration parameter.\n', file=shout)
print(''.join(shConstSection), file=shout)
except IOError as exc:
type, value, traceback = sys.exc_info()
print(exc.strerror, file=sys.stderr)
print('Unable to open ' + "'" + value.filename + "'.", file=sys.stderr)
rv = bool(1)
return rv
def write_project_includes(project_includes_file, project_includes):
error = False
if project_includes_file is not None:
try:
with open(project_includes_file, 'w') as file_out:
# Rules.mk
print(PREFIX, file=file_out)
print(RULESPREFIX, file=file_out)
for dir_variable in project_includes:
print(dir_variable, file=file_out)
print(f'-include $(SRCDIR)/$(dir)/Rules.mk', file=file_out)
print('', file=file_out)
print(RULESSUFFIX, file=file_out)
except IOError as exc:
print(f'unable to open {project_includes_file} for writing ({str(exc)})', file=sys.stderr)
error = True
return error
def generateSumRmCfg(defs):
rv = bool(0)
# ACK! Remember that Rick renamed these parameters. The ones in config.local are the aliases - do not use those.
# Use the ones that those map to (defined in config.local.map).
cFileTmp = defs['SUMLOG_BASEDIR'] + '/' + '.sum_rm.cfg.tmp'
cFile = defs['SUMLOG_BASEDIR'] + '/' + 'sum_rm.cfg'
# Write a temporary file sum_rm configuration file.
try:
with open(cFileTmp, 'w') as fout:
# Print comment at the top of the configuration file.
print(SUMRM_COMMENT, file=fout)
print(SUMRM_DOC, file=fout)
print(SUMRM_PARTN_PERCENT_FREE, file=fout)
if 'SUMRM_PART_PERCENT_FREE' in defs:
print('PART_PERCENT_FREE=' + defs['SUMRM_PART_PERCENT_FREE'], file=fout)
else:
print('PART_PERCENT_FREE=3', file=fout)
print(SUMRM_SLEEP, file=fout)
if 'SUMRM_SLEEP' in defs:
print('SLEEP=' + defs['SUMRM_SLEEP'], file=fout)
else:
print('SLEEP=300', file=fout)
print(SUMRM_LOG, file=fout)
if 'SUMRM_LOG' in defs:
print('LOG=' + defs['SUMRM_LOG'], file=fout)
else:
print('LOG=/tmp/sum_rm.log', file=fout)
print(SUMRM_MAIL, file=fout)
# No default for mail - don't send nothing to nobody unless the operator has asked for notifications.
if 'SUMRM_MAIL' in defs:
print('MAIL=' + defs['SUMRM_MAIL'], file=fout)
else:
print('# [email protected]', file=fout)
print(SUMRM_NOOP, file=fout)
if 'SUMRM_NOOP' in defs:
print('NOOP=' + defs['SUMRM_NOOP'], file=fout)
else:
print('NOOP=0', file=fout)
print(SUMRM_USER, file=fout)
if 'SUMRM_USER' in defs:
print('USER=' + defs['SUMRM_USER'], file=fout)
else:
print('USER=production', file=fout)
print(SUMRM_NORUN, file=fout)
# Default norun window is to have no such window. This can be accomplished by simply not providing either argument.
if 'SUMRM_NORUN_START' in defs or 'SUMRM_NORUN_STOP' in defs:
if 'SUMRM_NORUN_START' in defs:
print('NORUN_START=' + defs['SUMRM_NORUN_START'], file=fout)
else:
print('NORUN_START=0', file=fout)
if 'SUMRM_NORUN_STOP' in defs:
print('NORUN_STOP=' + defs['SUMRM_NORUN_STOP'], file=fout)
else:
print('NORUN_STOP=0', file=fout)
else:
print('# NORUN_START=0', file=fout)
print('# NORUN_STOP=0', file=fout)
except OSError:
print('Unable to open sum_rm temporary configuration file ' + cFileTmp + 'for writing.', file=sys.stderr)
rv = bool(1)
# If the content of the temporary file differs from the content of the existing configuration file, then overwrite
# the original file. Otherwise, delete the temporary file
if not rv:
try:
if filecmp.cmp(cFile, cFileTmp):
# Files identical - delete temporary file
try:
os.remove(cFileTmp)
except OSError as exc:
print('Unable to remove temporary file ' + exc.filename + '.', file=sys.stderr)
print(exc.strerr, file=sys.stderr)
else:
# Replace original with temporary file
try:
os.rename(cFileTmp, cFile)
except OSError as exc:
print('Unable to update sum_rm configuration file ' + cFile + '.', file=sys.stderr)
print(exc.strerr, file=sys.stderr)
rv = bool(1)
except OSError as exc:
# One of the files doesn't exist.
if exc.filename == cFile:
# We are ok - there might be no configuration file yet.
# Replace original with temporary file
try:
os.rename(cFileTmp, cFile)
except OSError as exc:
print('Unable to update sum_rm configuration file ' + cFile + '.', file=sys.stderr)
print(exc.strerr, file=sys.stderr)
rv = bool(1)
else:
# There is a problem with the temp file - bail.
print('Unable to update sum_rm configuration file ' + cFile + '.', file=sys.stderr)
print(exc.strerr, file=sys.stderr)
rv = bool(1)
return rv
def configureNet(cfgfile, cfile, mfile, pfile, pyfile, shfile, project_includes_file, base, keymap, createSumRmCfg):
error = False
defs = {}
cDefs = list()
mDefsGen = list()