1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

tools: make sha256sum.py more generic and move it to 'tools'

This commit is contained in:
Bert Belder 2018-08-29 04:53:33 +02:00
parent 131a44f559
commit 542eb54254
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
3 changed files with 78 additions and 16 deletions

View file

@ -1,14 +0,0 @@
# Copyright 2018 the Deno authors. All rights reserved. MIT license.
# This script computes the sha256sum of the first command line argument, and
# writes a few hex digits of it to stdout. It is used by rust.gni to derive a
# unique string (without dots/special characters) from a crate version number.
from hashlib import sha256
import sys
if len(sys.argv) != 2:
raise Exception('Expected exactly one argument.')
hash = sha256(sys.argv[1]).hexdigest()
sys.stdout.write(hash[0:8])

View file

@ -120,8 +120,14 @@ template("run_rustc") {
if (defined(crate_version)) { if (defined(crate_version)) {
# Compute the sha256sum of the version number. See comments below. # Compute the sha256sum of the version number. See comments below.
# Note that we do this only if there are multiple versions of this crate. # Note that we do this only if there are multiple versions of this crate.
hash = hash = exec_script("//tools/sha256sum.py",
exec_script("get_version_hash.py", [ crate_version ], "trim string") [
"--input",
crate_version,
"--format",
"%.8s",
],
"trim string")
args += [ args += [
# In our build setup, all crates are built in the same directory. The # In our build setup, all crates are built in the same directory. The

70
tools/sha256sum.py Normal file
View file

@ -0,0 +1,70 @@
# Copyright 2018 the Deno authors. All rights reserved. MIT license.
"""
Computes the SHA256 hash and formats the result.
"""
import argparse
from hashlib import sha256
import os
import sys
def main():
parser = argparse.ArgumentParser(description=__doc__)
# Arguments specifying where input comes from.
# If multiple sources are specified, they are all concatenated together.
parser.add_argument(
"--input",
action="append",
dest="input",
type=str,
metavar="TEXT",
help="Hash literal text specified on the command line.")
parser.add_argument(
"--infile",
action="append",
dest="input",
type=read_file,
metavar="FILE",
help="Hash the contents of a file.")
# Arguments dealing with output.
parser.add_argument(
"--format",
type=str,
dest="format",
default="%s",
metavar="TEMPLATE",
help="Format output using Python template (default = '%%s').")
parser.add_argument(
"--outfile",
dest="outfile",
type=argparse.FileType("wb"),
default=sys.stdout,
metavar="FILE",
help="Write the formatted hash to a file (default = stdout).")
# Parse arguments. Print usage and exit if given no input.
args = parser.parse_args()
if (not args.input):
parser.print_usage()
return 1
# Compute the hash of all inputs concatenated together.
hasher = sha256()
for data in args.input:
hasher.update(data)
hash = hasher.hexdigest()
# Format and write to specified out file (or the default, stdout).
args.outfile.write(args.format % hash)
def read_file(filename):
with open(filename, "rb") as file:
return file.read()
if __name__ == '__main__':
sys.exit(main())