1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-24 01:29:19 +03:00
Files
postgres/src/tools/pgflex
Peter Eisentraut 40a96cd148 pgflex: propagate environment to flex subprocess
Python's subprocess.run docs say that if the env argument is not None,
it will be used "instead of the default behavior of inheriting the
current process’ environment".  However, the environment should be
preserved, only adding FLEX_TMP_DIR to it.

Author: Javier Maestro <jjmaestro@ieee.org>
Discussion: https://www.postgresql.org/message-id/flat/CABvji06GUpmrTqqiCr6_F9vRL2-JUSVAh8ChgWa6k47FUCvYmA%40mail.gmail.com
2025-06-30 12:24:48 +02:00

73 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Wrapper around flex that:
# - ensures lex.backup is created in a private directory
# - can error out if lex.backup is created (--no-backup)
# - works around concurrency issues with win_flex.exe:
# https://github.com/lexxmark/winflexbison/issues/86
import argparse
import os
import subprocess
import sys
from os.path import abspath
parser = argparse.ArgumentParser()
parser.add_argument('--flex', type=abspath, required=True)
parser.add_argument('--perl', type=abspath, required=True)
parser.add_argument('--builddir', type=abspath, required=True)
parser.add_argument('--srcdir', type=abspath, required=True)
parser.add_argument('--privatedir', type=abspath, required=True,
help='private directory for target')
parser.add_argument('-o', dest='output_file', type=abspath, required=True,
help='output file')
parser.add_argument('-i', dest='input_file', type=abspath, help='input file')
parser.add_argument('--no-backup', action='store_true',
help='whether no_backup is enabled or not')
parser.add_argument('flex_flags', nargs='*', help='flags passed on to flex')
args = parser.parse_args()
# Since 'lex.backup' is always named that and ninja uses the top level build
# directory as current directory for all commands, change directory to
# temporary directory to avoid conflicts between concurrent flex
# invocations. Only unreleased versions of flex have an argument to change
# lex.filename to be named differently.
if not os.path.isdir(args.privatedir):
os.mkdir(args.privatedir)
os.chdir(args.privatedir)
# win_flex.exe generates names in a racy way, sometimes leading to random
# "error deleting file" failures and sometimes to intermingled file
# contents. Set FLEX_TMP_DIR to the target private directory to avoid
# that. That environment variable isn't consulted on other platforms, so we
# don't even need to make this conditional.
os.environ['FLEX_TMP_DIR'] = args.privatedir
# build flex invocation
command = [args.flex, '-o', args.output_file]
if args.no_backup:
command += ['-b']
command += args.flex_flags
command += [args.input_file]
# create .c file from .l file
sp = subprocess.run(command)
if sp.returncode != 0:
sys.exit(sp.returncode)
# check lex.backup
if args.no_backup:
with open('lex.backup') as lex:
if len(lex.readlines()) != 1:
sys.exit('Scanner requires backup; see lex.backup.')
os.remove('lex.backup')
sys.exit(0)