Files
Investidor-V2/tools/demography-runtime-repair.php
T
2026-11-21 20:58:19 +02:00

57 lines
4.4 KiB
PHP

<?php
// Repair only interpreter resolution; never bypass authentication or write demographic data.
use App\Services\DemographicInstallation;
use App\Support\CheckedFile;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\File;
use Symfony\Component\Process\Process;
require dirname(__DIR__).'/vendor/autoload.php';
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
throw_unless(app(DemographicInstallation::class)->activeTarget() && ! $app->configurationIsCached(), RuntimeException::class, 'Explicit uncached operational target required.');
$proofFile = storage_path('app/private/demography-deployment/readiness.json');
$proof = json_decode(CheckedFile::read($proofFile), true, 512, JSON_THROW_ON_ERROR);
throw_unless(CheckedFile::matches(base_path('.env'), $proof['environment_sha256']) && CheckedFile::matches(public_path('build/manifest.json'), $proof['release_manifest_sha256']), RuntimeException::class, 'Runtime changed; reconcile before repair.');
$python = 'C:/Users/macbook/AppData/Local/Python/pythoncore-3.15-arm64/python.exe';
throw_unless(is_file($python), RuntimeException::class, 'Verified installed interpreter unavailable.');
if (in_array('--refresh-proof', $argv, true)) {
throw_unless(config('demography.python') === $python && CheckedFile::matches($python, $proof['runtime_repair']['python_sha256'] ?? ''), RuntimeException::class, 'Configured interpreter changed.');
$probe = new Process([$python, '-c', 'import sys; assert sys.version_info >= (3, 9)'], base_path(), timeout: 15);
$probe->mustRun();
$proof['code_sha256'] = app(DemographicInstallation::class)->fingerprint();
$proof['verified_at'] = gmdate(DATE_ATOM);
File::replace($proofFile, json_encode($proof, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
echo json_encode(['runtime_ready' => true, 'code_sha256' => $proof['code_sha256'], 'operational_data_applied' => false], JSON_THROW_ON_ERROR).PHP_EOL;
exit(0);
}
$directory = storage_path('app/private/demography-deployment');
$source = 'C:/Mac/Home/Documents/TEMPO/Portal Tempo/Servicos Tempo/portal do investidor/demografia/Provincias/Manica.xlsx';
throw_unless(CheckedFile::matches($source, DemographicInstallation::MANICA_SHA256), RuntimeException::class);
$started = microtime(true);
$output = $directory.'/runtime-probe.json';
$process = new Process([$python, base_path('tools/demography_workbook.py'), $source, '--output', $output], base_path(), timeout: 45);
$process->mustRun();
$report = json_decode(CheckedFile::read($output), true, 512, JSON_THROW_ON_ERROR);
throw_unless($report['record_count'] === 71604 && $report['sha256'] === DemographicInstallation::MANICA_SHA256, RuntimeException::class, 'Runtime probe differs from approved source.');
$envFile = base_path('.env');
$bytes = CheckedFile::read($envFile);
throw_unless(preg_match('/^DEMOGRAPHY_PYTHON=/m', $bytes) !== 1, RuntimeException::class, 'Existing interpreter setting must be reconciled, not overwritten.');
$before = $directory.'/environment-before-python-'.gmdate('Ymd_His').'.env';
File::replace($before, $bytes);
$newline = str_contains($bytes, "\r\n") ? "\r\n" : "\n";
File::replace($envFile, rtrim($bytes, "\r\n").$newline.'DEMOGRAPHY_PYTHON="'.$python.'"'.$newline);
$proof['environment_before_python_sha256'] = $proof['environment_sha256'];
$proof['environment_sha256'] = CheckedFile::sha256($envFile);
$proof['code_sha256'] = app(DemographicInstallation::class)->fingerprint();
$proof['verified_at'] = gmdate(DATE_ATOM);
$proof['runtime_repair'] = ['cause' => 'WindowsApps python execution alias in web process', 'python_executable' => $python,
'python_sha256' => CheckedFile::sha256($python), 'probe_records' => 71604, 'probe_seconds' => round(microtime(true) - $started, 3),
'environment_backup' => $before, 'authentication_changed' => false, 'operational_data_applied' => false];
File::replace($proofFile, json_encode($proof, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
$receipt = ['interpreter_probe_passed' => true, 'probe_records' => 71604, 'probe_seconds' => $proof['runtime_repair']['probe_seconds'],
'code_sha256' => $proof['code_sha256'], 'authentication_changed' => false, 'operational_data_applied' => false, 'verified_at' => $proof['verified_at']];
File::replace(base_path('outputs/demography-20261113/runtime-repair.json'), json_encode($receipt, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
echo json_encode($receipt, JSON_THROW_ON_ERROR).PHP_EOL;