From 37189921cf0df2dfd9553a98aa5440374e82c864 Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Tue, 6 Dec 2022 17:20:43 -0800 Subject: [PATCH] WIP initial working stamping rule --- stamping/BUILD.bazel | 14 ++++++++++++++ stamping/stamping.bzl | 35 +++++++++++++++++++++++++++++++++++ stamping/stamping.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 stamping/BUILD.bazel create mode 100644 stamping/stamping.bzl create mode 100755 stamping/stamping.py diff --git a/stamping/BUILD.bazel b/stamping/BUILD.bazel new file mode 100644 index 00000000..138c1b08 --- /dev/null +++ b/stamping/BUILD.bazel @@ -0,0 +1,14 @@ +# Copyright (C) 2022 Swift Navigation Inc. +# Contact: Swift Navigation +# +# This source is subject to the license found in the file 'LICENSE' which must +# be be distributed together with this source. All other rights reserved. +# +# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, +# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + +exports_files( + glob(["*.bzl"]) + ["stamping.py"], + visibility = ["//visibility:public"], +) diff --git a/stamping/stamping.bzl b/stamping/stamping.bzl new file mode 100644 index 00000000..7d05f7d2 --- /dev/null +++ b/stamping/stamping.bzl @@ -0,0 +1,35 @@ +def _stamping_impl(ctx): + file = ctx.version_file + tpl = ctx.file.template + out = ctx.actions.declare_file(ctx.attr.out) + + args = ctx.actions.args() + args.add(file) + args.add(tpl) + args.add(out) + + ctx.actions.run( + inputs = [file, tpl], + executable = ctx.executable.exec, + arguments = [args], + outputs = [out], + ) + + return [DefaultInfo(files = depset([out]))] + +stamping = rule( + implementation = _stamping_impl, + attrs = { + "out": attr.string(), + "template": attr.label( + allow_single_file = [".in"], + mandatory = True, + ), + "exec": attr.label( + executable = True, + cfg = "exec", + allow_files = True, + default = "//stamping:stamping.py", + ), + }, +) diff --git a/stamping/stamping.py b/stamping/stamping.py new file mode 100755 index 00000000..cce8a960 --- /dev/null +++ b/stamping/stamping.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +""" +Script for lulz. +""" + +from string import Template + +import argparse + + +def init() -> argparse.Namespace: + parser = argparse.ArgumentParser() + parser.add_argument("input") + parser.add_argument("template") + parser.add_argument("output") + return parser.parse_args() + + +def main() -> None: + args = init() + with open(args.input) as input, open(args.template) as tpl, open( + args.output, "w" + ) as out: + subs = dict( + (line.split(" ")[0], line.split(" ")[1]) for line in input.readlines() + ) + s = Template(tpl.read()) + out.write(s.substitute(subs)) + + +if __name__ == "__main__": + main()