8000 Add environment variable to set timestamp by Spacetown · Pull Request #729 · gcovr/gcovr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add environment variable to set timestamp #729

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

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ New features and notable changes:
- Allow annotations for never executed branches. (:issue:`711`)
- Add function merge mode for same function defined in different lines. (:issue:`700`)
- Update link to gcovr documentation in HTML report to point to the documentation of the used version. (:issue:`723`)
- Add environment `SOURCE_DATE_EPOCH <https://reproducible-builds.org/docs/source-date-epoch>`_ to set default for :option:`--timestamp`. (:issue:`729`)

Bug fixes and small improvements:

Expand Down
4 changes: 4 additions & 0 deletions doc/examples/example_timestamps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ set -x # trace all commands
gcovr --timestamp 1640606727
#END simple epoch

#BEGIN source date epoch
SOURCE_DATE_EPOCH=1640606727 gcovr
#END source date epoch

#BEGIN simple RFC 3339
gcovr --timestamp '2021-12-27 13:05:27'
#END simple RFC 3339
Expand Down
35 changes: 31 additions & 4 deletions doc/source/guide/timestamps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ Reproducible Timestamps

In some cases, it may be desirable to list a specific timestamp in the report.
Timestamps are shown in
the :ref:`html_output`,
:ref:`coveralls_output`,
and the :ref:`cobertura_output`.
This can be achieved via the :option:`--timestamp <gcovr --timestamp>` option.
the :ref:`html_output`, :ref:`coveralls_output`, and the :ref:`cobertura_output`.
This can be achieved via the :option:`--timestamp <gcovr --timestamp>` option
or via :ref:`Using SOURCE_DATE_EPOCH` environment variable.
This option does not affect the modification times or other filesystem metadata.

.. versionadded:: NEXT

Respect environment variable `SOURCE_DATE_EPOCH`_ for default of :option:`gcovr --timestamp`.

.. versionadded:: 5.1

The :option:`gcovr --timestamp` option.
Expand Down Expand Up @@ -112,3 +115,27 @@ The supported settings are:
``--date=iso8601-strict``,
``--date=iso-strict-local``,
``--date=iso8601-strict-local``

.. _Using SOURCE_DATE_EPOCH:

Using SOURCE_DATE_EPOCH
-----------------------

The Reproducible Builds project defines the ``SOURCE_DATE_EPOCH`` variable.
Gcovr will use this variable as a default timestamp
if no explicit :option:`--timestamp <gcovr --timestamp>` is set.

The contents of this variable *must* be an UTC epoch, without any prefix.
No other format is supported.
Example usage:

.. include:: ../../examples/example_timestamps.sh
:code: bash
:start-after: #BEGIN source date epoch
:end-before: #END source date epoch

For more information on setting and using this variable,
see the `Reproducible Builds documentation on SOURCE_DATE_EPOCH
<SOURCE_DATE_EPOCH_>`_.

.. _SOURCE_DATE_EPOCH: https://reproducible-builds.org/docs/source-date-epoch/
53 changes: 51 additions & 2 deletions gcovr/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from argparse import ArgumentParser, ArgumentTypeError, SUPPRESS
from inspect import isclass
from locale import getpreferredencoding
import logging
from multiprocessing import cpu_count
from typing import Iterable, Any, List, Optional, Union, Callable, TextIO, Dict
from dataclasses import dataclass
Expand All @@ -31,6 +32,8 @@
from .utils import FilterOption, force_unix_separator
from .writer.html import CssRenderer

logger = logging.getLogger("gcovr")


def check_percentage(value: str) -> float:
r"""
Expand Down Expand Up @@ -94,6 +97,50 @@ def timestamp(value: str) -> datetime.datetime:
raise ArgumentTypeError(f"{ex}: {value!r}") from None


def source_date_epoch() -> Optional[datetime.datetime]:
"""
Load time from SOURCE_DATE_EPOCH, if it exists.
See: <https://reproducible-builds.org/docs/source-date-epoch/>

Examples:
>>> monkeypatch = getfixture("monkeypatch")
>>> caplog = getfixture("caplog")

Example: can be empty
>>> with monkeypatch.context() as mp:
... mp.delenv("SOURCE_DATE_EPOCH", raising=False)
... print(source_date_epoch())
None

Example: can contain timestamp
>>> with monkeypatch.context() as mp:
... mp.setenv("SOURCE_DATE_EPOCH", "1677067226")
... print(source_date_epoch())
2023-02-22 12:00:26+00:00

Example: can contain invalid timestamp
>>> with monkeypatch.context() as mp:
... mp.setenv("SOURCE_DATE_EPOCH", "not a timestamp")
... print(source_date_epoch())
None
>>> for m in caplog.messages: print(m)
Ignoring invalid environment variable SOURCE_DATE_EPOCH='not a timestamp'
"""

ts = os.environ.get("SOURCE_DATE_EPOCH")

if ts:
try:
return datetime.datetime.fromtimestamp(int(ts), datetime.timezone.utc)
except Exception:
logger.warning(
"Ignoring invalid environment variable SOURCE_DATE_EPOCH=%r",
ts,
)

return None


class OutputOrDefault:
"""An output path that may be empty.

Expand Down Expand Up @@ -1160,10 +1207,12 @@ def merge_options_and_set_defaults(
"Override current time for reproducible reports. "
"Can use `YYYY-MM-DD hh:mm:ss` or epoch notation. "
"Used by HTML, Coveralls, and Cobertura reports. "
"Default: current time."
"Default: Environment variable SOURCE_DATE_EPOCH "
"(see https://reproducible-builds.org/docs/source-date-epoch) "
"or current time."
),
type=timestamp,
default=datetime.datetime.now(),
default=source_date_epoch() or datetime.datetime.now(),
),
GcovrConfigOption(
"filter",
Expand Down
0