diff options
author | David Lamparter <equinox@diac24.net> | 2020-04-23 20:01:14 +0200 |
---|---|---|
committer | David Lamparter <equinox@diac24.net> | 2020-04-27 09:30:01 +0200 |
commit | 879a9dc5cdaa4cbf70f7615935466759c82065ff (patch) | |
tree | 671f3a30661378d761d5614605fae28c909af649 /python/makevars.py | |
parent | build: use VPATH for vtysh_scan (diff) | |
download | frr-879a9dc5cdaa4cbf70f7615935466759c82065ff.tar.xz frr-879a9dc5cdaa4cbf70f7615935466759c82065ff.zip |
python: move MakeVars class into separate module
... so I can reuse it.
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'python/makevars.py')
-rw-r--r-- | python/makevars.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/python/makevars.py b/python/makevars.py new file mode 100644 index 000000000..e0e2031a0 --- /dev/null +++ b/python/makevars.py @@ -0,0 +1,50 @@ +# +# helper class to grab variables from FRR's Makefile +# + +import os +import subprocess + +class MakeVars(object): + ''' + makevars['FOO_CFLAGS'] gets you "FOO_CFLAGS" from Makefile + ''' + def __init__(self): + self._data = dict() + + def getvars(self, varlist): + ''' + get a batch list of variables from make. faster than individual calls. + ''' + rdfd, wrfd = os.pipe() + + shvars = ['shvar-%s' % s for s in varlist] + make = subprocess.Popen(['make', '-s', 'VARFD=%d' % wrfd] + shvars, pass_fds = [wrfd]) + os.close(wrfd) + data = b'' + + rdf = os.fdopen(rdfd, 'rb') + while True: + rdata = rdf.read() + if len(rdata) == 0: + break + data += rdata + + del rdf + make.wait() + + data = data.decode('US-ASCII').strip().split('\n') + for row in data: + k, v = row.split('=', 1) + v = v[1:-1] + self._data[k] = v + + def __getitem__(self, k): + if k not in self._data: + self.getvars([k]) + return self._data[k] + + def get(self, k, defval = None): + if k not in self._data: + self.getvars([k]) + return self._data[k] or defval |