80 lines
2.1 KiB
Python
Executable File
80 lines
2.1 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
# This file is part of Cockpit.
|
|
#
|
|
# Copyright © 2022 Red Hat, Inc.
|
|
#
|
|
# Cockpit is free software; you can redistribute it and/or modify it
|
|
# under the terms of the GNU Lesser General Public License as published by
|
|
# the Free Software Foundation; either version 2.1 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Cockpit is distributed in the hope that it will be useful, but
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
# Lesser General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import argparse
|
|
import contextlib
|
|
import fileinput
|
|
import os.path
|
|
import re
|
|
import sys
|
|
|
|
|
|
BASE_DIR = os.path.realpath(f'{__file__}/../..')
|
|
|
|
|
|
def wip_version(args):
|
|
return 'g' in args.version
|
|
|
|
|
|
def substitute(match, args, package):
|
|
variable = match.group(1)
|
|
|
|
if variable == 'VERSION':
|
|
return args.version
|
|
|
|
else:
|
|
raise ValueError(f'Unsupported substitution @{variable}@')
|
|
|
|
|
|
def replace_fields(output, lines, args):
|
|
variable_re = re.compile(r'@([A-Z_]+)@')
|
|
package_re = re.compile(r'%package (-n cockpit-)?([a-z]*)')
|
|
package = None
|
|
|
|
for line in lines:
|
|
if match := package_re.fullmatch(line.strip()):
|
|
package = match.group(2)
|
|
line = variable_re.sub(lambda m: substitute(m, args, package), line)
|
|
output.write(line)
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def open_output(filename):
|
|
if filename:
|
|
fp = open(filename, 'w+')
|
|
yield fp
|
|
fp.close()
|
|
else:
|
|
yield sys.stdout
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--version', '-v', required=True, help='The version number')
|
|
parser.add_argument('--output', '-o', help='The output file (or stdout)')
|
|
parser.add_argument('template', nargs='*', help='The template (or stdin)')
|
|
args = parser.parse_args()
|
|
|
|
with open_output(args.output) as output:
|
|
replace_fields(output, fileinput.input(args.template), args)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|