[Mesa-dev] [PATCH 12/14] util/gen_xmlpool: Don't write via shell redirection
Dylan Baker
dylan at pnwbakers.com
Fri Oct 26 17:23:41 UTC 2018
This is bad for a couple of reasons, but the worst is that it gets the
shell involved. When the shell gets involved we can start running into
problems with LANG, namely LANG=C. This is particularly obnoxious for
translation files, since there is a very high likelyhood of running into
unicode in them. If we write it in python through file.write we don't
have this problem as python just shovels bits into a file, and the shell
doesn't know, and thus doesn't care.
---
src/util/Android.mk | 2 +-
src/util/xmlpool/Makefile.am | 2 +-
src/util/xmlpool/SConscript | 2 +-
src/util/xmlpool/gen_xmlpool.py | 98 ++++++++++++++-------------------
src/util/xmlpool/meson.build | 3 +-
5 files changed, 46 insertions(+), 61 deletions(-)
diff --git a/src/util/Android.mk b/src/util/Android.mk
index 993a7f179f4..d3e68a2a6fd 100644
--- a/src/util/Android.mk
+++ b/src/util/Android.mk
@@ -102,7 +102,7 @@ $(UTIL_GENERATED_SOURCES): $(intermediates)/%.c: $(LOCAL_PATH)/%.py
$(transform-generated-source)
$(MESA_DRI_OPTIONS_H): PRIVATE_CUSTOM_TOOL = $(PRIVATE_PYTHON) $< $(PRIVATE_TEMPLATE_HEADER) \
- $(PRIVATE_LOCALEDIR) $(MESA_DRI_OPTIONS_LANGS) > $@
+ $@ $(PRIVATE_LOCALEDIR) $(MESA_DRI_OPTIONS_LANGS)
$(MESA_DRI_OPTIONS_H): $(PRIVATE_SCRIPT) $(PRIVATE_TEMPLATE_HEADER) $(PRIVATE_MO_FILES)
$(transform-generated-source)
diff --git a/src/util/xmlpool/Makefile.am b/src/util/xmlpool/Makefile.am
index 9d5977f86ce..d61fc8ddf3a 100644
--- a/src/util/xmlpool/Makefile.am
+++ b/src/util/xmlpool/Makefile.am
@@ -75,7 +75,7 @@ clean-local:
# Default target options.h
LOCALEDIR := .
options.h: t_options.h $(MOS)
- $(AM_V_GEN) $(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/gen_xmlpool.py $(srcdir)/t_options.h $(LOCALEDIR) $(LANGS) > options.h
+ $(AM_V_GEN) $(PYTHON2) $(PYTHON_FLAGS) $(srcdir)/gen_xmlpool.py $(srcdir)/t_options.h options.h $(LOCALEDIR) $(LANGS)
# Update .mo files from the corresponding .po files.
%.gmo: %.po
diff --git a/src/util/xmlpool/SConscript b/src/util/xmlpool/SConscript
index fa42554d3a5..4778316fd2c 100644
--- a/src/util/xmlpool/SConscript
+++ b/src/util/xmlpool/SConscript
@@ -8,7 +8,7 @@ xmlpool_options, = env.CodeGenerate(
target = 'options.h',
script = 'gen_xmlpool.py',
source = ['t_options.h'],
- command = python_cmd + ' $SCRIPT $SOURCE ' + LOCALEDIR + ' > $TARGET'
+ command = python_cmd + ' $SCRIPT $SOURCE $TARGET ' + LOCALEDIR
)
Export('xmlpool_options')
diff --git a/src/util/xmlpool/gen_xmlpool.py b/src/util/xmlpool/gen_xmlpool.py
index 6a5dcee0a87..01e5cc60e3b 100644
--- a/src/util/xmlpool/gen_xmlpool.py
+++ b/src/util/xmlpool/gen_xmlpool.py
@@ -7,7 +7,7 @@
# `{localedir}/{language}/LC_MESSAGES/options.mo`.
#
-from __future__ import print_function
+from __future__ import print_function, unicode_literals
import argparse
import gettext
import io
@@ -114,7 +114,7 @@ def expandCString(s):
#
# DESC, DESC_BEGIN format: \1 \2=<lang> \3 \4=gettext(" \5=<text> \6=") \7
# ENUM format: \1 \2=gettext(" \3=<text> \4=") \5
-def expandMatches(matches, translations, end=None):
+def expandMatches(matches, translations, outfile, end=None):
assert len(matches) > 0
nTranslations = len(translations)
i = 0
@@ -131,12 +131,7 @@ def expandMatches(matches, translations, end=None):
matches[0].expand (r'\5'))))
text = (matches[0].expand(r'\1' + lang + r'\3"' + text + r'"\7') + suffix)
- # In Python 2, stdout expects encoded byte strings, or else it will
- # encode them with the ascii 'codec'
- if sys.version_info.major == 2:
- text = text.encode('utf-8')
-
- print(text)
+ outfile.write(text + '\n')
# Expand any subsequent enum lines
for match in matches[1:]:
@@ -144,16 +139,11 @@ def expandMatches(matches, translations, end=None):
match.expand(r'\3'))))
text = match.expand(r'\1"' + text + r'"\5')
- # In Python 2, stdout expects encoded byte strings, or else it will
- # encode them with the ascii 'codec'
- if sys.version_info.major == 2:
- text = text.encode('utf-8')
-
- print(text)
+ outfile.write(text + '\n')
# Expand description end
if end:
- print(end, end='')
+ outfile.write(end)
# Regular expressions:
reLibintl_h = re.compile(r'#\s*include\s*<libintl.h>')
@@ -166,6 +156,7 @@ reDESC_END = re.compile(r'\s*DRI_CONF_DESC_END')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('template')
+ parser.add_argument('outfile')
parser.add_argument('localedir')
parser.add_argument('languages', nargs='*')
args = parser.parse_args()
@@ -183,50 +174,45 @@ def main():
continue
translations.append((lang, trans))
- print("/***********************************************************************\n" \
- " *** THIS FILE IS GENERATED AUTOMATICALLY. DON'T EDIT! ***\n" \
- " ***********************************************************************/")
-
- # Process the options template and generate options.h with all
- # translations.
- with io.open(args.template, mode="rt", encoding='utf-8') as template:
- descMatches = []
- for line in template:
- if len(descMatches) > 0:
- matchENUM = reENUM.match(line)
- matchDESC_END = reDESC_END.match(line)
- if matchENUM:
- descMatches.append(matchENUM)
- elif matchDESC_END:
- expandMatches(descMatches, translations, line)
- descMatches = []
+ with io.open(args.outfile, mode='wt', encoding='utf-8') as output:
+ output.write("/* This is file is generated automatically. Don't edit! */\n")
+
+ # Process the options template and generate options.h with all
+ # translations.
+ with io.open(args.template, mode="rt", encoding='utf-8') as template:
+ descMatches = []
+ for line in template:
+ if len(descMatches) > 0:
+ matchENUM = reENUM.match(line)
+ matchDESC_END = reDESC_END.match(line)
+ if matchENUM:
+ descMatches.append(matchENUM)
+ elif matchDESC_END:
+ expandMatches(descMatches, translations, output, line)
+ descMatches = []
+ else:
+ print("Warning: unexpected line inside description dropped:\n",
+ line, file=sys.stderr)
+ continue
+ if reLibintl_h.search(line):
+ # Ignore (comment out) #include <libintl.h>
+ output.write("/* %s * commented out by gen_xmlpool.py */\n" % line)
+ continue
+ matchDESC = reDESC.match(line)
+ matchDESC_BEGIN = reDESC_BEGIN.match(line)
+ if matchDESC:
+ assert len(descMatches) == 0
+ expandMatches([matchDESC], translations, output)
+ elif matchDESC_BEGIN:
+ assert len(descMatches) == 0
+ descMatches = [matchDESC_BEGIN]
else:
- print("Warning: unexpected line inside description dropped:\n",
- line, file=sys.stderr)
- continue
- if reLibintl_h.search(line):
- # Ignore (comment out) #include <libintl.h>
- print("/* %s * commented out by gen_xmlpool.py */" % line)
- continue
- matchDESC = reDESC.match(line)
- matchDESC_BEGIN = reDESC_BEGIN.match(line)
- if matchDESC:
- assert len(descMatches) == 0
- expandMatches([matchDESC], translations)
- elif matchDESC_BEGIN:
- assert len(descMatches) == 0
- descMatches = [matchDESC_BEGIN]
- else:
- # In Python 2, stdout expects encoded byte strings, or else it will
- # encode them with the ascii 'codec'
- if sys.version_info.major == 2:
- line = line.encode('utf-8')
- print(line, end='')
+ output.write(line)
- if len(descMatches) > 0:
- print("Warning: unterminated description at end of file.", file=sys.stderr)
- expandMatches(descMatches, translations)
+ if len(descMatches) > 0:
+ print("Warning: unterminated description at end of file.", file=sys.stderr)
+ expandMatches(descMatches, translations, output)
if __name__ == '__main__':
diff --git a/src/util/xmlpool/meson.build b/src/util/xmlpool/meson.build
index ae7c951a097..ccf7919a0b3 100644
--- a/src/util/xmlpool/meson.build
+++ b/src/util/xmlpool/meson.build
@@ -30,9 +30,8 @@ xmlpool_options_h = custom_target(
input : ['gen_xmlpool.py', 't_options.h'],
output : 'options.h',
command : [
- prog_python, '@INPUT@', meson.current_build_dir(), _langs,
+ prog_python, '@INPUT@', '@OUTPUT@', meson.current_build_dir(), _langs,
],
- capture : true,
depend_files : _langs_po_files,
)
--
2.19.1
More information about the mesa-dev
mailing list