From 2c59975fab5df58c9a821a2eee8399a67c51e98c Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Fri, 13 May 2022 20:00:29 +0300 Subject: [PATCH] Add stamping to .deb package version and description --- pkg/private/deb/deb.bzl | 15 +++++++++++++++ pkg/private/deb/make_deb.py | 15 ++++++++++++--- pkg/private/helpers.py | 15 ++++++++++++++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/pkg/private/deb/deb.bzl b/pkg/private/deb/deb.bzl index 2541d86b..a5b739d0 100644 --- a/pkg/private/deb/deb.bzl +++ b/pkg/private/deb/deb.bzl @@ -17,6 +17,7 @@ load("//pkg:providers.bzl", "PackageArtifactInfo", "PackageVariablesInfo") load("//pkg/private:util.bzl", "setup_output_files") _tar_filetype = [".tar", ".tar.gz", ".tgz", ".tar.bz2", "tar.xz"] +_stamp_condition = Label("//pkg/private:private_stamp_detect") def _pkg_deb_impl(ctx): """The implementation for the pkg_deb rule.""" @@ -107,6 +108,12 @@ def _pkg_deb_impl(ctx): else: fail("Neither description_file nor description attribute was specified") + # Files for stamping variables + if ctx.attr.private_stamp_detect: + args += ["--stamp=@" + ctx.version_file.path] + args += ["--stamp=@" + ctx.info_file.path] + files += [ctx.version_file, ctx.info_file] + # Built using can also be specified by a file or inlined (but is not mandatory) if ctx.attr.built_using_file: if ctx.attr.built_using: @@ -344,6 +351,10 @@ See https://www.debian.org/doc/debian-policy/ch-files.html#s-config-files.""", providers = [PackageVariablesInfo], ), + # Is --stamp set on the command line? + # TODO(https://github.com/bazelbuild/rules_pkg/issues/340): Remove this. + "private_stamp_detect": attr.bool(default = False), + # Implicit dependencies. "_make_deb": attr.label( default = Label("//pkg/private/deb:make_deb"), @@ -362,5 +373,9 @@ def pkg_deb(name, out = None, **kwargs): pkg_deb_impl( name = name, out = out, + private_stamp_detect = select({ + _stamp_condition: True, + "//conditions:default": False, + }), **kwargs ) diff --git a/pkg/private/deb/make_deb.py b/pkg/private/deb/make_deb.py index 9e2e5c5e..f3455200 100644 --- a/pkg/private/deb/make_deb.py +++ b/pkg/private/deb/make_deb.py @@ -348,6 +348,10 @@ def main(): parser.add_argument( '--triggers', help='The triggers file (prefix with @ to provide a path).') + parser.add_argument( + '--stamp', + action='append', + help='The stamp variables (prefix with @ to provide a path).') # see # https://www.debian.org/doc/manuals/debian-faq/ch-pkg_basics.en.html#s-conffile parser.add_argument( @@ -356,6 +360,11 @@ def main(): AddControlFlags(parser) options = parser.parse_args() + stamp = dict() + if options.stamp: + for stamp_vars in options.stamp: + stamp.update(helpers.ParseStamp(helpers.GetFlagValue(stamp_vars))) + CreateDeb( options.output, options.data, @@ -368,8 +377,8 @@ def main(): triggers=helpers.GetFlagValue(options.triggers, False), conffiles=GetFlagValues(options.conffile), package=options.package, - version=helpers.GetFlagValue(options.version), - description=helpers.GetFlagValue(options.description), + version=helpers.GetFlagValue(options.version, stamp=stamp), + description=helpers.GetFlagValue(options.description, stamp=stamp), maintainer=helpers.GetFlagValue(options.maintainer), section=options.section, architecture=helpers.GetFlagValue(options.architecture), @@ -393,7 +402,7 @@ def main(): architecture=options.architecture, description=helpers.GetFlagValue(options.description), maintainer=helpers.GetFlagValue(options.maintainer), package=options.package, - version=helpers.GetFlagValue(options.version), section=options.section, + version=helpers.GetFlagValue(options.version, stamp=stamp), section=options.section, priority=options.priority, distribution=options.distribution, urgency=options.urgency) diff --git a/pkg/private/helpers.py b/pkg/private/helpers.py index 5147cc2f..ad6fb2c5 100644 --- a/pkg/private/helpers.py +++ b/pkg/private/helpers.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import os +import re import sys def SplitNameValuePairAtSeparator(arg, sep): @@ -48,7 +49,7 @@ def SplitNameValuePairAtSeparator(arg, sep): # if we leave the loop, the character sep was not found unquoted return (head, '') -def GetFlagValue(flagvalue, strip=True): +def GetFlagValue(flagvalue, strip=True, stamp=None): """Converts a raw flag string to a useable value. 1. Expand @filename style flags to the content of filename. @@ -63,6 +64,7 @@ def GetFlagValue(flagvalue, strip=True): Args: flagvalue: (str) raw flag value strip: (bool) Strip white space. + stamp: (dict) Stamp variables for inline values Returns: Python2: unicode @@ -84,6 +86,17 @@ def GetFlagValue(flagvalue, strip=True): if sys.version_info[0] > 2: flagvalue = os.fsencode(flagvalue).decode('utf-8') + if stamp != None: + flagvalue = re.sub(r'\{(\w+)\}', lambda m: stamp.get(m.group(1), m.group(0)), flagvalue) + if strip: return flagvalue.strip() return flagvalue + +def ParseStamp(data): + vars = dict() + for line in data.split("\n"): + sep = line.find(' ') + if sep >= 0: + vars[line[:sep]] = line[sep+1:] + return vars