Skip to content

Commit

Permalink
Platform independence pre-check (digital-asset#17955)
Browse files Browse the repository at this point in the history
* Rename //bazel_tools:match-{cabal=>golden}-file

* Generate and check hash file for contents of //compiler/damlc/tests:platform-independence.dar

* Add pre-commit hook to test //compiler/damlc/tests:platform-independence-dar-hash-file-matches

* Print info when //bazel_tools:match-golden-file fails

* platform-independence-dar-check pre-commit hook runs depending on env var

* Skip platform-independence-check if RUN_PLATFORM_INDEPENDENCE_CHECK is unset

* Document dar_hash_file rules

* move platform-independence-checks warning to .envrc

* implement generate_dar_hash_file genrule in python

in particular, this allows us to produce the same output on all platforms

* fix windows

* Update rules_daml/daml.bzl

* bytes.replace does not operate in place
  • Loading branch information
akrmn authored Dec 7, 2023
1 parent f678ad1 commit 4794454
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 11 deletions.
10 changes: 10 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ for HOOK_TYPE in ${HOOK_TYPES[@]}; do pre-commit uninstall -t "$HOOK_TYPE" > /de
if [[ -z "${DADE_NO_PRE_COMMIT:-}" ]]; then
pre-commit install -t "$DADE_PRE_COMMIT_HOOK_TYPE" > /dev/null
fi

if [ -z "${RUN_PLATFORM_INDEPENDENCE_CHECK:-}" ]; then
echo "Warning: platform-independence-checks will be skipped."
echo "To silence this warning, please set 'RUN_PLATFORM_INDEPENDENCE_CHECK'"
echo "by running the following line:"
echo " echo export RUN_PLATFORM_INDEPENDENCE_CHECK=no >> $(pwd)/.envrc.private"
echo "if you usually work on the compiler, run the following line instead:"
echo " echo export RUN_PLATFORM_INDEPENDENCE_CHECK=yes >> $(pwd)/.envrc.private"
export RUN_PLATFORM_INDEPENDENCE_CHECK=no
fi
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ repos:
pass_filenames: false
entry: "dade-copyright-headers update"
types: [text]
- id: platform-independence-check
name: platform-independence-check
language: system
pass_filenames: false
entry: "pre-commit/platform-independence-check.sh"
10 changes: 10 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ nixpkgs_cc_configure(

nixpkgs_python_configure(repository = "@nixpkgs") if not is_windows else None

dev_env_tool(
name = "python_dev_env",
nix_include = ["bogus"],
nix_paths = ["bogus"],
tools = ["python"],
win_include = ["."],
win_paths = ["python.exe"],
win_tool = "python-3.8.2",
) if is_windows else None

# Curl system dependency
nixpkgs_package(
name = "curl_nix",
Expand Down
4 changes: 2 additions & 2 deletions bazel_tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ check 'org.scala-lang:scala-reflect (in {maven_install_json})' "$$SCALA_REFLECT_
) if not is_windows else None

filegroup(
name = "match-cabal-file",
srcs = glob(["match-cabal-file.sh"]),
name = "match-golden-file",
srcs = glob(["match-golden-file.sh"]),
visibility = ["//visibility:public"],
)
12 changes: 10 additions & 2 deletions bazel_tools/haskell.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,18 @@ def generate_and_track_cabal(name, exe_name = None, src_dir = None, exclude_deps
visibility = ["//visibility:public"],
)

test_name = name + "-cabal-file-matches"

lbl = "//{package}:{target}".format(
package = native.package_name(),
target = test_name,
)

native.sh_test(
name = name + "-cabal-file-matches",
srcs = ["//bazel_tools:match-cabal-file"],
name = test_name,
srcs = ["//bazel_tools:match-golden-file"],
args = [
lbl,
"$(location :%s-generated-cabal)" % name,
"$(location :%s-golden-cabal)" % name,
"$(POSIX_DIFF)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

GENERATED=$(rlocation "$TEST_WORKSPACE/$1")
GOLDEN=$(rlocation "$TEST_WORKSPACE/$2")
DIFF=$3
ACCEPT=${4:-}
LABEL=$1
GENERATED=$(rlocation "$TEST_WORKSPACE/$2")
GOLDEN=$(rlocation "$TEST_WORKSPACE/$3")
DIFF=$4
ACCEPT=${5:-}

info() {
echo "diff ($DIFF) failed with exit status: $1"
echo "To accept the changes, run"
echo " bazel run $LABEL -- --accept"
exit $1
}

case "$ACCEPT" in
-a | --accept)
echo Accepting changes, copying "$GENERATED" to "$2"
cp "$GENERATED" "$2"
echo Accepting changes, copying "$GENERATED" to "$3"
cp "$GENERATED" "$3"
;;
*)
trap 'info $?' ERR
$DIFF $GENERATED $GOLDEN
;;
esac
9 changes: 8 additions & 1 deletion compiler/damlc/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
load("//bazel_tools:haskell.bzl", "da_haskell_binary", "da_haskell_library", "da_haskell_test")
load("@os_info//:os_info.bzl", "is_darwin", "is_windows")
load(":util.bzl", "damlc_compile_test")
load("//rules_daml:daml.bzl", "daml_compile", "daml_compile_with_dalf")
load("//rules_daml:daml.bzl", "daml_compile", "daml_compile_with_dalf", "generate_and_track_dar_hash_file")
load("@build_environment//:configuration.bzl", "ghc_version", "sdk_version")
load("//daml-lf/language:daml-lf.bzl", "COMPILER_LF_VERSIONS", "LF_MAJOR_VERSIONS", "lf_version_default_or_latest", "mangle_for_damlc")
load("//compiler/damlc:util.bzl", "ghc_pkg")
Expand Down Expand Up @@ -1190,6 +1190,13 @@ daml_compile_with_dalf(
ghc_options = [],
)

# This ensures that changes to the contents of 'platform-independence.dar'
# don't go unnoticed. This can happen after changes to daml-prim or daml-stdlib.
# This is likely to happen if you work on the compiler , so it's a good idea to
# add 'export RUN_PLATFORM_INDEPENDENCE_CHECK=yes' to your '.envrc.private'
# at the root of the repo.
generate_and_track_dar_hash_file("platform-independence")

# Desugared daml golden tests
da_haskell_test(
name = "daml-desugar",
Expand Down
34 changes: 34 additions & 0 deletions compiler/damlc/tests/platform-independence.dar-hash
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
f4cbef8a91a8767297778db25b029ac70ebba248c1a5fd10464966e75bf9c245 META-INF/MANIFEST.MF
b5c40ce08e30cf16a8cad16e2341d01d6ee49984fb8670cd12d7bea4984fae91 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/compiler/damlc/tests/PlatformIndependence.daml
0000000000000000000000000000000000000000000000000000000000000000 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/compiler/damlc/tests/PlatformIndependence.hi
0000000000000000000000000000000000000000000000000000000000000000 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/compiler/damlc/tests/PlatformIndependence.hie
dafb098481022814bddcd8796d0b5568e9795f178b3dc21c4d7ba3e88b20528d platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-041ddaabb3926f0b666d5e0a36bc1ae8dbf7191d2889afed5d1631af4172fa6b.dalf
6458feec418d71e834dfa5b56a32b640f0595d1cf9bef29c3456cf9eac1c921e platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-DA-Exception-ArithmeticError-cb0552debf219cc909f51cbb5c3b41e9981d39f8f645b1f35e2ef5be2e0b858a.dalf
ce477181ed187d31fb71e2819cd8af24b3bee3ff55b02950c23363849cf86a47 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-DA-Exception-AssertionFailed-3f4deaf145a15cdcfa762c058005e2edb9baa75bb7f95a4f8f6f937378e86415.dalf
a5dafa3128e153955c2360201cb700b09d170914847b184184e4413be848cecb platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-DA-Exception-GeneralError-86828b9843465f419db1ef8a8ee741d1eef645df02375ebf509cdc8c3ddd16cb.dalf
9ec4a0044ebf1002e47c5119b06c8650535f0bd841d8a0c7189455a3720169f3 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-DA-Exception-PreconditionFailed-f20de1e4e37b92280264c08bf15eca0be0bc5babd7a7b5e574997f154c00cb78.dalf
9a6861aa98233c2fd6a446edd4255c2891ab3db5eec355955381f35088956a23 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-DA-Internal-Erased-76bf0fd12bd945762a01f8fc5bbcdfa4d0ff20f8762af490f8f41d6237c6524f.dalf
e66c79fc41a6bd6350ab265ee3bd15c0a6d5cd1748757194577448c89ce9e6a7 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-DA-Internal-NatSyn-38e6274601b21d7202bb995bc5ec147decda5a01b68d57dda422425038772af7.dalf
608e8efb2ed17a67eae9ddb74ca235a848195da170f47bf4d18d33bc7e96cc44 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-DA-Internal-PromotedText-d58cf9939847921b2aab78eaa7b427dc4c649d25e6bee3c749ace4c3f52f5c97.dalf
42105a59d0c9c7088a8860693f6da3d60ca62da5ccac1b9cf0dde8f975b984a5 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-DA-Types-40f452260bef3f29dede136108fc08a88d5a5250310281067087da6f0baddff7.dalf
12906f0e43060e26f823f03c2bbfe4d541db833d370db4caae2c66f38acae440 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-GHC-Prim-e491352788e56ca4603acc411ffe1a49fefd76ed8b163af86cf5ee5f4c38645b.dalf
bdc8c66d0511eff0262d3a133269e655bf47ba742e98ced5bc76a3fcd0211a73 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-GHC-Tuple-6839a6d3d430c569b2425e9391717b44ca324b88ba621d597778811b2d05031d.dalf
e99c27f6b9d297aaf747b572356e08376ce8d7a994ff00242749978b8a8a9ab0 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-prim-GHC-Types-518032f41fd0175461b35ae0c9691e08b4aea55e62915f8360af2cc7a1f2ba6c.dalf
05e7e12f02e455d1efedcab3ea7c2de10807dc4d71d413f99eb885e0f8835148 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-0.0.0-71147aee3f0eeacf21152ebb2c5fe34b27462ddcfc2145e012d23dec7bfcba0d.dalf
dc4bd12915aea953a4ae6fb5e3b31fd3ec99605ae402c8b6350035ed9bbdeda7 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Action-State-Type-10e0333b52bba1ff147fc408a6b7d68465b157635ee230493bd6029b750dcb05.dalf
6d9d1309c57dab98cb34fda6b26d3c6d5b2e5fb1357333bce2c0b26c262fc372 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Date-Types-bfcd37bd6b84768e86e432f5f6c33e25d9e7724a9d42e33875ff74f6348e733f.dalf
acb729cf88297d61a52ba5852019326077f682e914052449d75142447b89b964 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Internal-Any-cc348d369011362a5190fe96dd1f0dfbc697fdfd10e382b9e9666f0da05961b7.dalf
5625ab94e607b99fd03cfb3a618c3ec8227cf35dc917cc310d12c34691d10c9d platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Internal-Down-057eed1fd48c238491b8ea06b9b5bf85a5d4c9275dd3f6183e0e6b01730cc2ba.dalf
fb97292e2989cf6b046d0c07355a86741ba327231b13596e646f6aed3c65b726 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Internal-Interface-AnyView-Types-6df2d1fd8ea994ed048a79587b2722e3a887ac7592abf31ecf46fe09ac02d689.dalf
e6817a705c974eea0b05051e919e18cab97df616f3d8c8e590b41cde1147b7bf platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Internal-Template-d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662.dalf
542ba5ef4d1707d77dbaa2c457adc06f28bad394968bae25635a982b7b1f6916 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Logic-Types-c1f1f00558799eec139fb4f4c76f95fb52fa1837a5dd29600baa1c8ed1bdccfd.dalf
a70854a646e926e98a102a6d332c67872b4bc5978700bf946ce58dfc94ba993c platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Monoid-Types-6c2c0667393c5f92f1885163068cd31800d2264eb088eb6fc740e11241b2bf06.dalf
e80bf135b35aa8a1eca973e1b57b95b2ff3c6f9b1039be8b0a8e6a1810bfc613 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-NonEmpty-Types-e22bce619ae24ca3b8e6519281cb5a33b64b3190cc763248b4c3f9ad5087a92c.dalf
77210bf43703a6883586997dfab07154c988f294b144337d056f99ba14fa6150 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Random-Types-e4cc67c3264eba4a19c080cac5ab32d87551578e0f5f58b6a9460f91c7abc254.dalf
aea033031e908d0a9b7c4701bc4a563c5f25b0f626ab3abb11725fe6525d4f79 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Semigroup-Types-8a7806365bbd98d88b4c13832ebfa305f6abaeaf32cfa2b7dd25c4fa489b79fb.dalf
41970cee0963cbf25ffb1d35601b4200631aedf794f80ab22f999416b48d8531 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Set-Types-97b883cd8a2b7f49f90d5d39c981cf6e110cf1f1c64427a28a6d58ec88c43657.dalf
e198664748ca9a14de3cf197a0bac3aec6deb3d4097db73dfbf300bb97e30b9a platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Stack-Types-5921708ce82f4255deb1b26d2c05358b548720938a5a325718dc69f381ba47ff.dalf
826cad481a81e8e1fd7151a0f9c3bf5c14cc14e2983e1abc143387dafe4b8282 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Time-Types-733e38d36a2759688a4b2c4cec69d48e7b55ecc8dedc8067b815926c917a182a.dalf
348c6f5fcd6b3b2753a8418cdb980b3a8c73fcc37ff1ffc63c0a9625159ca067 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/daml-stdlib-DA-Validation-Types-99a2705ed38c1c26cbb8fe7acf36bbf626668e167a33335de932599219e0a235.dalf
1d1014d0027a3582e15a7e23a3e96101cb2c100f1550b776bfc189e583db6c9e platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/data/platform-independence-1.0.0.conf
7c1281de1ce291c14bb08a84d1d0f990cf3ccfc541380b380352e90a76533be1 platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71/platform-independence-1.0.0-aeeeb8902241c9454cba7a1594d2ba19aae4aaa92824bf0dffd513d3f4eb1d71.dalf
9 changes: 9 additions & 0 deletions pre-commit/platform-independence-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
# Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

set -euo pipefail

if [ "$RUN_PLATFORM_INDEPENDENCE_CHECK" = "yes" ]; then
bazel test //compiler/damlc/tests:platform-independence-dar-hash-file-matches
fi
6 changes: 6 additions & 0 deletions rules_daml/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ exports_files(
["daml.bzl"],
visibility = ["//bazel_tools/docs:__pkg__"],
)

filegroup(
name = "generate-dar-hash",
srcs = glob(["generate-dar-hash.py"]),
visibility = ["//visibility:public"],
)
74 changes: 74 additions & 0 deletions rules_daml/daml.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load("@build_environment//:configuration.bzl", "ghc_version", "sdk_version")
load("//bazel_tools/sh:sh.bzl", "sh_inline_test")
load("//daml-lf/language:daml-lf.bzl", "COMPILER_LF_VERSIONS", "version_in")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@os_info//:os_info.bzl", "is_windows")

_damlc = attr.label(
default = Label("//compiler/damlc:damlc-compile-only"),
Expand Down Expand Up @@ -368,6 +369,79 @@ def daml_compile_with_dalf(
dalf = name + ".dalf",
)

def generate_and_track_dar_hash_file(name):
"""
Given a 'name', defines the following targets:
(1) ':{name}-dar-hash-file-matches',
(2) ':{name}-golden-hash'.
(3) ':{name}-generated-hash',
Target (1) is a test that checks that the files in targets (2) and (3) match.
It can also replace (2) with the output of (3) if run with the flag '--accept'.
Target (2) is an indirection to the file
':{name}.dar-hash',
which must be present in the package where this macro is used.
Target (3) is transitively defined by 'generate_dar_hash_file'.
"""
generate_dar_hash_file(name)

native.filegroup(
name = name + "-golden-hash",
srcs = native.glob([name + ".dar-hash"]),
visibility = ["//visibility:public"],
)

test_name = name + "-dar-hash-file-matches"

lbl = "//{package}:{target}".format(
package = native.package_name(),
target = test_name,
)

native.sh_test(
name = test_name,
srcs = ["//bazel_tools:match-golden-file"],
args = [
lbl,
"$(location :{}-generated-hash)".format(name),
"$(location :{}-golden-hash)".format(name),
"$(POSIX_DIFF)",
],
data = [
":{}-generated-hash".format(name),
":{}-golden-hash".format(name),
],
toolchains = [
"@rules_sh//sh/posix:make_variables",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)

def generate_dar_hash_file(name):
"""
Given a 'name', defines a target of the form
':{name}-generated-hash'.
with a single file of the form
':{name}-generated.dar-hash'.
The resulting file will contain one line per file present in
':{name}.dar',
sorted by filename and excluding *.hi and *.hie files, where each line
has the sha256sum for the file followed by the path of the file relative to
the root of the dar.
"""
native.genrule(
name = name + "-generated-hash",
srcs = ["//rules_daml:generate-dar-hash", ":{}.dar".format(name)],
tools = ["@python_dev_env//:python"] if is_windows else [],
outs = [name + "-generated.dar-hash"],
cmd = "{python} {exe} {dar} > $@".format(
python = "$(execpath @python_dev_env//:python)" if is_windows else "python",
exe = "$(location //rules_daml:generate-dar-hash)",
dar = "$(location :{}.dar)".format(name),
),
)

def daml_build_test(
name,
project_dir,
Expand Down
38 changes: 38 additions & 0 deletions rules_daml/generate-dar-hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

from argparse import ArgumentParser
from zipfile import ZipFile
from pathlib import PurePath
from hashlib import sha256


def parse_args():
parser = ArgumentParser(description="generate-dar-hash")
parser.add_argument('dar', metavar='DAR', type=str)
return parser.parse_args()


def entry(filename, bytes):
extension = PurePath(filename).suffix
if extension in ['.hi', '.hie']:
# hie/hi files may differ, so we ignore their contents.
hash = 64 * '0'
else:
if extension in ['.daml']:
# on text files, newlines will be encoded differently depending
# on platform, so we canonicalize them here
bytes = bytes.replace(b'\r\n', b'\n')
hash = sha256(bytes).hexdigest()
return f"{hash} {filename}"


def main():
args = parse_args()
with ZipFile(args.dar) as dar:
for filename in dar.namelist():
print(entry(filename, dar.read(filename)))


if __name__ == '__main__':
main()

0 comments on commit 4794454

Please sign in to comment.