summaryrefslogtreecommitdiff
path: root/python/makevars.py
diff options
context:
space:
mode:
authorDonatas Abraitis <donatas.abraitis@gmail.com>2020-10-08 19:38:44 +0300
committerGitHub <noreply@github.com>2020-10-08 19:38:44 +0300
commitebe39ad5d1c4787cf74836c8b453fa4464051a5e (patch)
treef6c52ae4a6c8498c9fe501d8e0170981c273350f /python/makevars.py
parent75bc27898db9347ef6ffa4734cfb9f88e25c7b85 (diff)
parent74d5f2543c89bdea7b9a3dcb43dd62c38dfd2ed7 (diff)
Merge pull request #7260 from qlyoung/reformat-python
Reformat python
Diffstat (limited to 'python/makevars.py')
-rw-r--r--python/makevars.py48
1 files changed, 29 insertions, 19 deletions
diff --git a/python/makevars.py b/python/makevars.py
index 63bf8c5eeb..951cd3438b 100644
--- a/python/makevars.py
+++ b/python/makevars.py
@@ -6,10 +6,12 @@ import os
import subprocess
import re
+
class MakeVarsBase(object):
- '''
+ """
common code between MakeVars and MakeReVars
- '''
+ """
+
def __init__(self):
self._data = dict()
@@ -18,31 +20,35 @@ class MakeVarsBase(object):
self.getvars([k])
return self._data[k]
- def get(self, k, defval = None):
+ def get(self, k, defval=None):
if k not in self._data:
self.getvars([k])
return self._data.get(k) or defval
+
class MakeVars(MakeVarsBase):
- '''
+ """
makevars['FOO_CFLAGS'] gets you "FOO_CFLAGS" from Makefile
This variant works by invoking make as a subprocess, i.e. Makefile must
be valid and working. (This is sometimes a problem if depfiles have not
been generated.)
- '''
+ """
+
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])
+ 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''
+ data = b""
- rdf = os.fdopen(rdfd, 'rb')
+ rdf = os.fdopen(rdfd, "rb")
while True:
rdata = rdf.read()
if len(rdata) == 0:
@@ -52,30 +58,34 @@ class MakeVars(MakeVarsBase):
del rdf
make.wait()
- data = data.decode('US-ASCII').strip().split('\n')
+ data = data.decode("US-ASCII").strip().split("\n")
for row in data:
- k, v = row.split('=', 1)
+ k, v = row.split("=", 1)
v = v[1:-1]
self._data[k] = v
+
class MakeReVars(MakeVarsBase):
- '''
+ """
makevars['FOO_CFLAGS'] gets you "FOO_CFLAGS" from Makefile
This variant works by regexing through Makefile. This means the Makefile
does not need to be fully working, but on the other hand it doesn't support
fancy complicated make expressions.
- '''
- var_re = re.compile(r'^([^=#\n\s]+)[ \t]*=[ \t]*([^#\n]*)(?:#.*)?$', flags=re.MULTILINE)
- repl_re = re.compile(r'\$(?:([A-Za-z])|\(([^\)]+)\))')
+ """
+
+ var_re = re.compile(
+ r"^([^=#\n\s]+)[ \t]*=[ \t]*([^#\n]*)(?:#.*)?$", flags=re.MULTILINE
+ )
+ repl_re = re.compile(r"\$(?:([A-Za-z])|\(([^\)]+)\))")
def __init__(self, maketext):
super(MakeReVars, self).__init__()
- self._vars = dict(self.var_re.findall(maketext.replace('\\\n', '')))
+ self._vars = dict(self.var_re.findall(maketext.replace("\\\n", "")))
def replacevar(self, match):
varname = match.group(1) or match.group(2)
- return self._vars.get(varname, '')
+ return self._vars.get(varname, "")
def getvars(self, varlist):
for varname in varlist: