import optparse
import os
import sys
import json
import stat
sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
os.pardir, os.pardir, os.pardir, "build"))
from scripts.util import build_utils
def parse_args(args):
args = build_utils.expand_file_args(args)
parser = optparse.OptionParser()
build_utils.add_depfile_option(parser)
parser.add_option('--output', help='fixed para file')
parser.add_option('--source-file', help='source para file')
parser.add_option('--extra', action="append", type="string", dest="extra", help='extra params')
options, _ = parser.parse_args(args)
return options
def parse_params(line, contents):
line = line.strip()
pos = line.find('=')
if pos <= 0:
return
name = line[:pos]
value = line[pos + 1:]
name = name.strip()
value = value.strip()
contents[name] = value
def parse_extra_params(extras, contents):
for extra in extras:
extra = extra.strip()
parse_params(extra, contents)
def fix_para_file(options):
contents = {}
with open(options.source_file, 'r') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if line.startswith('#') or not line:
continue
parse_params(line, contents)
if options.extra:
parse_extra_params(options.extra, contents)
flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
modes = stat.S_IWUSR | stat.S_IRUSR | stat.S_IWGRP | stat.S_IRGRP
with os.fdopen(os.open(options.output, flags, modes), 'w') as f:
for key in contents:
f.write("".join([key, "=", contents[key], '\n']))
def main(args):
options = parse_args(args)
depfile_deps = ([options.source_file])
fix_para_file(options)
build_utils.write_depfile(options.depfile,
options.output, depfile_deps, add_pydeps=False)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))