Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
+137
@@ -0,0 +1,137 @@
|
||||
#!/usr/bin/env python3
|
||||
"""核验离线归档,在搬移目录和空 Cargo home 中调用真实新版工具链。"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tarfile
|
||||
|
||||
|
||||
def sha(path):
|
||||
with path.open('rb') as stream:
|
||||
return hashlib.file_digest(stream, 'sha256').hexdigest()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('archive', type=Path)
|
||||
parser.add_argument('--work', required=True, type=Path)
|
||||
parser.add_argument('--aarch64-runner', type=Path, help='explicit QEMU executable; tests target execution under emulation')
|
||||
args = parser.parse_args()
|
||||
archive = args.archive.resolve(strict=True)
|
||||
checksum = Path(str(archive) + '.sha256').read_text().split()[0]
|
||||
assert sha(archive) == checksum, 'archive SHA-256 mismatch'
|
||||
work = args.work.absolute()
|
||||
work.mkdir()
|
||||
with tarfile.open(archive) as stream:
|
||||
stream.extractall(work, filter='data')
|
||||
bundle = work / 'dever'
|
||||
manifest = json.loads((bundle / 'manifest.json').read_text())
|
||||
assert manifest['format'] == 'dever-development-1'
|
||||
for entry in manifest['files']:
|
||||
path = bundle / entry['path']
|
||||
assert path.is_file() and not path.is_symlink(), entry['path']
|
||||
assert path.stat().st_size == entry['bytes'] and sha(path) == entry['sha256'], entry['path']
|
||||
assert os.access(path, os.X_OK) == entry['executable'], entry['path']
|
||||
moved = work / 'moved development kit'
|
||||
bundle.rename(moved)
|
||||
home = work / 'empty-home'
|
||||
home.mkdir()
|
||||
environment = {'PATH': '/usr/bin:/bin', 'HOME': str(home), 'LANG': 'C.UTF-8',
|
||||
'CARGO_BUILD_JOBS': '2', 'DEVER_HOME': str(home / '.dever')}
|
||||
compiler = moved / 'bin/dever'
|
||||
|
||||
def run(*arguments):
|
||||
return subprocess.check_output([str(compiler), *map(str, arguments)], env=environment,
|
||||
cwd=work, text=True)
|
||||
|
||||
project = work / 'hello'
|
||||
run('new', project)
|
||||
run('check', project)
|
||||
# 旧工具会跳过 .dv 并返回成功;用真实类型错误验证打包的编译器读到了文件。
|
||||
invalid = project / 'module/hello/greeting/flow/invalid.dv'
|
||||
invalid.write_text('wrong() (out: Int) {\n out = "invalid"\n}\n')
|
||||
rejected = subprocess.run([compiler, 'check', project], env=environment, cwd=work,
|
||||
capture_output=True, text=True)
|
||||
assert rejected.returncode != 0 and 'invalid.dv:2:9: C005: expected Int, got Text' in rejected.stderr, rejected.stderr
|
||||
invalid.unlink()
|
||||
result = json.loads(run('run', project, '--', 'hello.greeting.greet', '{"name":"离线"}'))
|
||||
assert result['data'] == 'Hello, 离线'
|
||||
run('inspect', project, '--links')
|
||||
run('inspect', project, '--links', '--check')
|
||||
source = project / 'module/hello/greeting/flow/greeting.dv'
|
||||
source.write_text('greet(name: Text) (message: Text) { message = text.upper(name) }\n')
|
||||
run('fmt', project)
|
||||
result = json.loads(run('run', project, '--', 'hello.greeting.greet', '{"name":"changed"}'))
|
||||
assert result['data'] == 'CHANGED'
|
||||
published = work / 'standalone-app'
|
||||
run('build', project, '--profile', 'dev', '--output', published)
|
||||
result = subprocess.check_output([str(published), 'hello.greeting.greet', '{"name":"published"}'],
|
||||
env=environment, cwd=home, text=True)
|
||||
assert json.loads(result)['data'] == 'PUBLISHED'
|
||||
if args.aarch64_runner:
|
||||
arm_project = work / "arm-hello"
|
||||
run("new", arm_project)
|
||||
domain = arm_project / "module/hello/greeting"
|
||||
(domain / "data").mkdir()
|
||||
(domain / "data/main.dever").write_text("type Greeting { name: Text }\n")
|
||||
(domain / "flow/greeting.dv").write_text(source.read_text() + """
|
||||
flow store(name: Text) (saved: data.Greeting)
|
||||
| step | kind | do |
|
||||
| saved | data | data.create({ name = name }) |
|
||||
flow reject(name: Text, accepted: Bool) ()
|
||||
| step | kind | do |
|
||||
| saved | data | data.create({ name = name }) |
|
||||
| - | check | accepted else Conflict |
|
||||
flow count() (total: Int)
|
||||
| step | kind | do |
|
||||
| total | data | data.count() |
|
||||
""")
|
||||
api = domain / "api/greeting.dv"
|
||||
api.write_text(api.read_text() + "\ncmd store = app.store\ncmd reject = app.reject\ncmd count = app.count\n")
|
||||
run("fmt", arm_project)
|
||||
arm_binary = work / 'standalone-aarch64'
|
||||
run('build', arm_project, '--profile', 'dev', '--target', 'linux-aarch64', '--output', arm_binary)
|
||||
arm_command = [
|
||||
str(args.aarch64_runner.resolve(strict=True)), '-L',
|
||||
str(moved / 'toolchain/cross/usr/aarch64-linux-gnu'), str(arm_binary),
|
||||
]
|
||||
def arm(*arguments):
|
||||
return subprocess.check_output([*arm_command, *map(str, arguments)],
|
||||
env=environment, cwd=home, text=True)
|
||||
config = arm_project / "config/setting.json"
|
||||
config.parent.mkdir(exist_ok=True)
|
||||
config.write_text(json.dumps({"database": {"default": {
|
||||
"type": "sqlite", "path": "aarch64.db"}}}))
|
||||
arm("--config", config, "migrate")
|
||||
result = arm("--config", config, 'hello.greeting.greet', '{"name":"aarch64"}')
|
||||
assert json.loads(result)['data'] == 'AARCH64'
|
||||
saved = json.loads(arm("--config", config, "hello.greeting.store", '{"name":"ARM SQLite"}'))
|
||||
assert saved["data"]["name"] == "ARM SQLite" and saved["data"]["id"] > 0
|
||||
rejected = subprocess.run([*arm_command, "--config", str(config), "hello.greeting.reject",
|
||||
'{"name":"rollback","accepted":false}'],
|
||||
env=environment, cwd=home, capture_output=True, text=True)
|
||||
assert rejected.returncode != 0 and "Conflict" in rejected.stderr, rejected
|
||||
assert json.loads(arm("--config", config, "hello.greeting.count", "{}"))["data"] == 1
|
||||
private_cargo = moved / 'toolchain/bin/cargo'
|
||||
hidden_cargo = moved / 'toolchain/bin/cargo-unavailable'
|
||||
private_cargo.rename(hidden_cargo)
|
||||
try:
|
||||
rejected = subprocess.run([str(compiler), 'run', str(project), '--',
|
||||
'hello.greeting.greet', '{"name":"missing"}'],
|
||||
env=environment, cwd=work, capture_output=True, text=True)
|
||||
assert rejected.returncode != 0, 'missing private Cargo silently used another toolchain'
|
||||
finally:
|
||||
hidden_cargo.rename(private_cargo)
|
||||
print(json.dumps({'files_verified': len(manifest['files']), 'archive_sha256': checksum,
|
||||
'moved_offline_build': True, 'changed_source': True,
|
||||
'published_binary': True, 'missing_tool_rejected': True}))
|
||||
if args.aarch64_runner:
|
||||
print(json.dumps({'aarch64_offline_build': True, 'sqlite_create_and_rollback': True,
|
||||
'execution': 'QEMU user mode, not target hardware'}))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user