]>
git.proxmox.com Git - mirror_frr.git/blob - python/makevars.py
2 # helper class to grab variables from FRR's Makefile
10 class MakeVarsBase(object):
12 common code between MakeVars and MakeReVars
18 def __getitem__(self
, k
):
19 if k
not in self
._data
:
23 def get(self
, k
, defval
=None):
24 if k
not in self
._data
:
26 return self
._data
.get(k
) or defval
29 class MakeVars(MakeVarsBase
):
31 makevars['FOO_CFLAGS'] gets you "FOO_CFLAGS" from Makefile
33 This variant works by invoking make as a subprocess, i.e. Makefile must
34 be valid and working. (This is sometimes a problem if depfiles have not
38 def getvars(self
, varlist
):
40 get a batch list of variables from make. faster than individual calls.
42 rdfd
, wrfd
= os
.pipe()
44 shvars
= ["shvar-%s" % s
for s
in varlist
]
45 make
= subprocess
.Popen(
46 ["make", "-s", "VARFD=%d" % wrfd
] + shvars
, pass_fds
=[wrfd
]
51 rdf
= os
.fdopen(rdfd
, "rb")
61 data
= data
.decode("US-ASCII").strip().split("\n")
63 k
, v
= row
.split("=", 1)
68 class MakeReVars(MakeVarsBase
):
70 makevars['FOO_CFLAGS'] gets you "FOO_CFLAGS" from Makefile
72 This variant works by regexing through Makefile. This means the Makefile
73 does not need to be fully working, but on the other hand it doesn't support
74 fancy complicated make expressions.
78 r
"^([^=#\n\s]+)[ \t]*=[ \t]*([^#\n]*)(?:#.*)?$", flags
=re
.MULTILINE
80 repl_re
= re
.compile(r
"\$(?:([A-Za-z])|\(([^\)]+)\))")
82 def __init__(self
, maketext
):
83 super(MakeReVars
, self
).__init
__()
84 self
._vars
= dict(self
.var_re
.findall(maketext
.replace("\\\n", "")))
86 def replacevar(self
, match
):
87 varname
= match
.group(1) or match
.group(2)
88 return self
._vars
.get(varname
, "")
90 def getvars(self
, varlist
):
91 for varname
in varlist
:
92 if varname
not in self
._vars
:
95 val
, prevval
= self
._vars
[varname
], None
98 val
= self
.repl_re
.sub(self
.replacevar
, val
)
100 self
._data
[varname
] = val