| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #!/usr/bin/env python3
- """Copy an explicit release directory to Windows and compare every delivered SHA256."""
- from __future__ import annotations
- import argparse
- import base64
- import json
- from pathlib import Path
- import subprocess
- def ps(text: str) -> str:
- return "powershell -NoProfile -NonInteractive -EncodedCommand " + base64.b64encode(text.encode("utf-16le")).decode()
- def quote(text: str) -> str:
- return "'" + text.replace("'", "''") + "'"
- def main(argv=None) -> int:
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument("--source-dir", type=Path, required=True)
- parser.add_argument("--destination", required=True, help="new absolute Windows directory")
- parser.add_argument("--host", default="administrator@192.168.2.24")
- args = parser.parse_args(argv)
- source = args.source_dir.resolve()
- manifest = source / "DELIVERY-SHA256.json"
- if not manifest.is_file() or not json.loads(manifest.read_text()):
- parser.error("source-dir needs the nonempty packaging checksum manifest")
- log = source.parent / (source.name + "-delivery.log")
- receipt = source.parent / (source.name + "-delivery-receipt.json")
- def run(command):
- with log.open("a") as stream:
- subprocess.run(command, stdout=stream, stderr=subprocess.STDOUT, check=True)
- destination = args.destination.rstrip("/\\")
- scp_destination = destination.replace("\\", "/")
- run(["ssh", "-o", "ConnectTimeout=10", args.host,
- ps("$ErrorActionPreference='Stop'; $p=" + quote(destination)
- + "; if (Test-Path $p) {throw 'Use a new release directory'}; New-Item -ItemType Directory -Path $p | Out-Null")])
- for child in sorted(source.iterdir()):
- run(["scp", "-r", str(child), args.host + ":" + scp_destination + "/"])
- verify = r'''
- $ErrorActionPreference='Stop'
- $ProgressPreference='SilentlyContinue'
- $root=ROOT_VALUE
- $items=Get-Content -LiteralPath (Join-Path $root 'DELIVERY-SHA256.json') -Raw -Encoding UTF8 | ConvertFrom-Json
- $verified=@()
- foreach ($item in $items) {
- $path=Join-Path $root $item.path
- $actual=(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()
- $size=(Get-Item -LiteralPath $path).Length
- if ($actual -ne $item.sha256 -or $size -ne $item.size_bytes) {throw ('SHA or size mismatch: '+$item.path)}
- $verified+=@{path=$item.path;sha256=$actual;size_bytes=$size}
- }
- $result=@{status='PASS';scope='file transfer and SHA256 comparison';host=$env:COMPUTERNAME;destination=$root;
- verified_files=$verified.Count;files=$verified;hardware_validation='NOT_RUN_BY_DELIVERY';utc=[DateTime]::UtcNow.ToString('o')}
- $result | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Join-Path $root 'DELIVERY-RECEIPT.json') -Encoding UTF8
- Write-Output ('DELIVERY_SHA256_PASS '+$verified.Count)
- '''.replace("ROOT_VALUE", quote(destination))
- run(["ssh", "-o", "ConnectTimeout=10", args.host, ps(verify)])
- run(["scp", args.host + ":" + scp_destination + "/DELIVERY-RECEIPT.json", str(receipt)])
- result = json.loads(receipt.read_text(encoding="utf-8-sig"))
- if result.get("status") != "PASS":
- raise ValueError("remote delivery verification failed")
- print(json.dumps({key: result[key] for key in ("status", "host", "destination", "verified_files")}, ensure_ascii=False))
- print(str(receipt))
- return 0
- if __name__ == "__main__":
- raise SystemExit(main())
|