??# export_ingest_pack_v1.0.ps1 - PS 5.1-safe # Cr?e un dossier outbox/ingest_YYYYMMDD_HHMMSS avec BootPack, KB pointer, snapshot r?f?renc?e, derniers logs, # un README_INGEST.txt, et sha256s.txt. Marque INGEST_PENDING. param([string]$Reason="manual") function Ensure-Dir([string]$p){ if($p -and !(Test-Path $p)){ New-Item -ItemType Directory -Force -Path $p | Out-Null } } function FileSHA([string]$p){ if(Test-Path $p){ (Get-FileHash -Algorithm SHA256 -LiteralPath $p).Hash } else { '' } } function ReadAll([string]$p){ if(Test-Path $p){ Get-Content -LiteralPath $p -Raw -Encoding UTF8 } else { '' } } $me=$MyInvocation.MyCommand.Definition $scriptDir=Split-Path -Parent $me $registry =Split-Path -Parent $scriptDir $bp = Join-Path $registry 'bootpack\bootpack.txt' $kb = Join-Path $registry 'bootpack\bootpack_paste.txt' if(!(Test-Path $bp)){ Write-Host "[ERR] BootPack introuvable."; exit 1 } $raw=ReadAll $bp $proj=[regex]::Match($raw,'(?mi)^\s*PROJECT:\s*(.+)$').Groups[1].Value.Trim() $man =[regex]::Match($raw,'(?ms)^\[SYNC_MANIFEST\]\s*(.*?)(?=^\[|\Z)').Groups[1].Value $mem =[regex]::Match($man,'(?mi)^\s*MEM_SYNC_ID\s*:\s*(.+)$').Groups[1].Value.Trim() $ms =[regex]::Match($man,'(?mi)^\s*MANIFEST_SHA256\s*:\s*([0-9a-f]+)\s*$').Groups[1].Value.Trim() $snap= [regex]::Match($man,'(?mi)^\s*SNAPSHOT_FILE\s*:\s*(.+)$').Groups[1].Value.Trim() $kbblk=[regex]::Match($raw,'(?ms)^\[BUG_KB_JSON_POINTER\]\s*(.*?)(?=^\[|\Z)').Groups[1].Value $kbsha=[regex]::Match($kbblk,'(?mi)^\s*SHA256\s*:\s*([0-9A-Fa-f]+)$').Groups[1].Value.Trim() $kbent=[regex]::Match($kbblk,'(?mi)^\s*Entries\s*:\s*([0-9]+)$').Groups[1].Value.Trim() $outbox=Join-Path $registry 'ingest_outbox'; Ensure-Dir $outbox $ts=Get-Date -Format 'yyyyMMdd_HHmmss' $dest=Join-Path $outbox ('ingest_'+$ts); Ensure-Dir $dest Copy-Item -LiteralPath $bp -Destination (Join-Path $dest 'bootpack.txt') -Force if(Test-Path $kb){ Copy-Item -LiteralPath $kb -Destination (Join-Path $dest 'bootpack_paste.txt') -Force } if(Test-Path $snap){ Copy-Item -LiteralPath $snap -Destination (Join-Path $dest (Split-Path $snap -Leaf)) -Force } $logs=Join-Path $registry 'logs' if(Test-Path $logs){ $last = Get-ChildItem -LiteralPath $logs -Filter 'run_*' -File | Sort-Object LastWriteTime -Descending | Select -First 3 foreach($f in $last){ Copy-Item -LiteralPath $f.FullName -Destination (Join-Path $dest $f.Name) -Force } } $memrule=(Get-ChildItem -LiteralPath $registry -Filter 'MEMORY-SYNC-RULE_*.txt' -File | Sort-Object LastWriteTime -Descending | Select -First 1) if($memrule){ Copy-Item -LiteralPath $memrule.FullName -Destination (Join-Path $dest $memrule.Name) -Force } $readme=Join-Path $dest 'README_INGEST.txt' $lines=@() $lines+="=== INGEST PACK ===" $lines+=("When : {0}" -f (Get-Date -Format s)) $lines+=("Project : {0}" -f $proj) $lines+=("Reason : {0}" -f $Reason) $lines+=("MEM_SYNC_ID : {0}" -f $mem) $lines+=("MANIFEST_SHA256: {0}" -f $ms) $lines+=("KB pointer SHA : {0}" -f $kbsha) $lines+=("KB entries : {0}" -f $kbent) $lines+=("Snapshot file : {0}" -f $snap) $lines+="" $lines+="Files:" foreach($f in (Get-ChildItem -LiteralPath $dest -File)){ $lines+=(" - "+$f.Name) } [IO.File]::WriteAllLines($readme,$lines,[Text.UTF8Encoding]::new($false)) $shaFile=Join-Path $dest 'sha256s.txt' $buf=New-Object System.Text.StringBuilder foreach($f in (Get-ChildItem -LiteralPath $dest -File)){ $sha=(Get-FileHash -Algorithm SHA256 -LiteralPath $f.FullName).Hash [void]$buf.AppendLine(($sha+" "+$f.Name)) } [IO.File]::WriteAllText($shaFile,$buf.ToString(),[Text.UTF8Encoding]::new($false)) # Flag PENDING $flag=Join-Path $dest 'INGEST_PENDING.flag' [IO.File]::WriteAllText($flag,("PENDING {0}" -f (Get-Date -Format s)),[Text.UTF8Encoding]::new($false)) [IO.File]::WriteAllText((Join-Path $outbox 'PENDING_LAST.txt'),$dest,[Text.UTF8Encoding]::new($false)) "Export OK -> "+$dest | Write-Host