1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-17 19:00:58 +00:00

fix(common): set consistent timestamps on template-generated files

this helps the build system keep track of whether anything has actually changed
This commit is contained in:
matejcik 2023-10-18 12:53:23 +02:00 committed by matejcik
parent 6331076444
commit 1c244648c5

View File

@ -3,7 +3,6 @@ from __future__ import annotations
import datetime import datetime
import fnmatch import fnmatch
import glob
import json import json
import logging import logging
import os import os
@ -11,6 +10,7 @@ import re
import sys import sys
from collections import defaultdict from collections import defaultdict
from hashlib import sha256 from hashlib import sha256
from pathlib import Path
from typing import Any, Callable, Iterator, TextIO, cast from typing import Any, Callable, Iterator, TextIO, cast
import click import click
@ -133,13 +133,13 @@ MAKO_FILTERS = {
def render_file( def render_file(
src: str, dst: TextIO, coins: CoinsInfo, support_info: SupportInfo src: Path, dst: Path, coins: CoinsInfo, support_info: SupportInfo
) -> None: ) -> None:
"""Renders `src` template into `dst`. """Renders `src` template into `dst`.
`src` is a filename, `dst` is an open file object. `src` is a filename, `dst` is an open file object.
""" """
template = mako.template.Template(filename=src) template = mako.template.Template(filename=str(src.resolve()))
eth_defs_date = datetime.datetime.fromisoformat( eth_defs_date = datetime.datetime.fromisoformat(
DEFINITIONS_TIMESTAMP_PATH.read_text().strip() DEFINITIONS_TIMESTAMP_PATH.read_text().strip()
) )
@ -150,7 +150,9 @@ def render_file(
**coins, **coins,
**MAKO_FILTERS, **MAKO_FILTERS,
) )
dst.write(result) dst.write_text(str(result))
src_stat = src.stat()
os.utime(dst, ns=(src_stat.st_atime_ns, src_stat.st_mtime_ns))
# ====== validation functions ====== # ====== validation functions ======
@ -840,13 +842,13 @@ def dump(
@cli.command() @cli.command()
# fmt: off # fmt: off
@click.argument("paths", metavar="[path]...", nargs=-1) @click.argument("paths", type=click.Path(path_type=Path), metavar="[path]...", nargs=-1)
@click.option("-o", "--outfile", type=click.File("w"), help="Alternate output file") @click.option("-o", "--outfile", type=click.Path(dir_okay=False, writable=True, path_type=Path), help="Alternate output file")
@click.option("-v", "--verbose", is_flag=True, help="Print rendered file names") @click.option("-v", "--verbose", is_flag=True, help="Print rendered file names")
@click.option("-b", "--bitcoin-only", is_flag=True, help="Accept only Bitcoin coins") @click.option("-b", "--bitcoin-only", is_flag=True, help="Accept only Bitcoin coins")
# fmt: on # fmt: on
def render( def render(
paths: tuple[str, ...], outfile: TextIO, verbose: bool, bitcoin_only: bool paths: tuple[Path, ...], outfile: Path, verbose: bool, bitcoin_only: bool
) -> None: ) -> None:
"""Generate source code from Mako templates. """Generate source code from Mako templates.
@ -882,7 +884,7 @@ def render(
for key, value in support_info.items(): for key, value in support_info.items():
support_info[key] = Munch(value) support_info[key] = Munch(value)
def do_render(src: str, dst: TextIO) -> None: def do_render(src: Path, dst: Path) -> None:
if verbose: if verbose:
click.echo(f"Rendering {src} => {dst.name}") click.echo(f"Rendering {src} => {dst.name}")
render_file(src, dst, defs, support_info) render_file(src, dst, defs, support_info)
@ -894,25 +896,23 @@ def render(
# find files in directories # find files in directories
if not paths: if not paths:
paths = (".",) paths = (Path(),)
files: list[str] = [] files: list[Path] = []
for path in paths: for path in paths:
if not os.path.exists(path): if not path.exists():
click.echo(f"Path {path} does not exist") click.echo(f"Path {path} does not exist")
elif os.path.isdir(path): elif path.is_dir():
files += glob.glob(os.path.join(path, "*.mako")) files.extend(path.glob("*.mako"))
else: else:
files.append(path) files.append(path)
# render each file # render each file
for file in files: for file in files:
if not file.endswith(".mako"): if not file.suffix == ".mako":
click.echo(f"File {file} does not end with .mako") click.echo(f"File {file} does not end with .mako")
else: else:
target = file[: -len(".mako")] do_render(file, file.parent / file.stem)
with open(target, "w") as dst:
do_render(file, dst)
@cli.command() @cli.command()