summaryrefslogtreecommitdiff
path: root/src/buildstream/utils.py
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2020-05-11 08:44:42 +0200
committerJürg Billeter <j@bitron.ch>2020-05-27 06:18:41 +0200
commit9826e115a4bb6f44524145c33f82ca4af13847e3 (patch)
treeb4644984ab887079bdfa7705c03bc1c22b9a0498 /src/buildstream/utils.py
parent68945dbc7d4d260a4aaf7b671e6458a8b7fb3226 (diff)
downloadbuildstream-9826e115a4bb6f44524145c33f82ca4af13847e3.tar.gz
Update node property support to match proto changes
Diffstat (limited to 'src/buildstream/utils.py')
-rw-r--r--src/buildstream/utils.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py
index 29b3bf484..99c22d169 100644
--- a/src/buildstream/utils.py
+++ b/src/buildstream/utils.py
@@ -24,6 +24,7 @@ Utilities
import calendar
import errno
import hashlib
+import math
import os
import re
import shutil
@@ -39,6 +40,7 @@ from contextlib import contextmanager
from pathlib import Path
from typing import Callable, IO, Iterable, Iterator, Optional, Tuple, Union
from dateutil import parser as dateutil_parser
+from google.protobuf import timestamp_pb2
import psutil
@@ -179,6 +181,41 @@ def _parse_timestamp(timestamp: str) -> float:
raise UtilError(errmsg)
+def _make_protobuf_timestamp(timestamp: timestamp_pb2.Timestamp, timepoint: float):
+ """Obtain the Protobuf Timestamp represented by the time given in seconds.
+
+ Args:
+ timestamp: the Protobuf Timestamp to set
+ timepoint: the time since the epoch in seconds
+
+ """
+ timestamp.seconds = int(timepoint)
+ timestamp.nanos = int(math.modf(timepoint)[0] * 1e9)
+
+
+def _get_file_protobuf_mtimestamp(timestamp: timestamp_pb2.Timestamp, fullpath: str):
+ """Obtain the Protobuf Timestamp represented by the mtime of the
+ file at the given path."""
+ assert isinstance(fullpath, str), "Path to file must be a string: {}".format(str(fullpath))
+ try:
+ mtime = os.path.getmtime(fullpath)
+ except OSError:
+ raise UtilError("Failed to get mtime of file at {}".format(fullpath))
+ _make_protobuf_timestamp(timestamp, mtime)
+
+
+def _parse_protobuf_timestamp(timestamp: timestamp_pb2.Timestamp) -> float:
+ """Convert Protobuf Timestamp to seconds since epoch.
+
+ Args:
+ timestamp: the Protobuf Timestamp
+
+ Returns:
+ The time in seconds since epoch represented by the timestamp.
+ """
+ return timestamp.seconds + timestamp.nanos / 1e9
+
+
def _set_file_mtime(fullpath: str, seconds: Union[int, float]) -> None:
"""Set the access and modification times of the file at the given path
to the given time. The time of the file will be set with nanosecond