Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stamping to .deb package version and description #708

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/private/deb/deb.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should follow the logic of what the others do. Where there is a stamp parameter which can be 1, 0, or -1, and we only use the stamp dectect if -1.

args += ["--stamp=@" + ctx.version_file.path]
args += ["--stamp=@" + ctx.info_file.path]
files += [ctx.version_file, ctx.info_file]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the 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:
Expand Down Expand Up @@ -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"),
Expand All @@ -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
)
15 changes: 12 additions & 3 deletions pkg/private/deb/make_deb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -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)

Expand Down
15 changes: 14 additions & 1 deletion pkg/private/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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