phamdangdangild commited on
Commit
499afa2
·
verified ·
1 Parent(s): 972120a

Finalize FoundryManager Studio 2.1.0

Browse files
FOUNDRY_STUDIO_INSTALL.bat CHANGED
@@ -1,255 +1,255 @@
1
- @echo off
2
- setlocal
3
- chcp 65001 >nul
4
- title FoundryManager Studio - Install and Run
5
- set "FM_BOOTSTRAP_SELF=%~f0"
6
- powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "$raw=[IO.File]::ReadAllText($env:FM_BOOTSTRAP_SELF);$mark='#==POWERSHELL==';$at=$raw.LastIndexOf($mark);if($at -lt 0){throw 'Bootstrap payload not found'};& ([scriptblock]::Create($raw.Substring($at)))"
7
- set "FM_EXIT=%ERRORLEVEL%"
8
- if not "%FM_EXIT%"=="0" (
9
- echo.
10
- echo [ERROR] FoundryManager Studio could not start. See the message above.
11
- pause
12
- )
13
- exit /b %FM_EXIT%
14
-
15
- #==POWERSHELL==
16
- $ErrorActionPreference = "Stop"
17
- [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
18
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
19
-
20
- $Product = "FoundryManager Studio"
21
- $Root = Join-Path $env:LOCALAPPDATA "FoundryManagerStudio"
22
- $DataDir = Join-Path $Root "data"
23
- $ProfilesDir = Join-Path $DataDir "profiles"
24
- $ReleasesDir = Join-Path $Root "releases"
25
- $VenvDir = Join-Path $Root "venv"
26
- $CurrentFile = Join-Path $Root "current.txt"
27
- $RequirementStamp = Join-Path $Root "requirements.sha256"
28
- $ManifestUrls = @(
29
- "https://huggingface.co/datasets/DangPhamPham/foundrymanager-studio/resolve/main/manifest.json",
30
- "https://huggingface.co/datasets/phamdangdangild/foundrymanager-studio/resolve/main/manifest.json"
31
- )
32
- $AllowedReleasePrefixes = @(
33
- "https://huggingface.co/datasets/DangPhamPham/foundrymanager-studio/resolve/",
34
- "https://huggingface.co/datasets/phamdangdangild/foundrymanager-studio/resolve/"
35
- )
36
-
37
- function Step([string]$Message) { Write-Host ("[*] " + $Message) -ForegroundColor Cyan }
38
- function Good([string]$Message) { Write-Host ("[OK] " + $Message) -ForegroundColor Green }
39
- function Warn([string]$Message) { Write-Host ("[!] " + $Message) -ForegroundColor Yellow }
40
-
41
- function Test-Administrator {
42
- $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
43
- $principal = [Security.Principal.WindowsPrincipal]::new($identity)
44
- return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
45
- }
46
-
47
- function Get-WorkingPython {
48
- $candidates = [System.Collections.Generic.List[string]]::new()
49
- $managed = Join-Path $Root "runtime\python\python.exe"
50
- if (Test-Path -LiteralPath $managed -PathType Leaf) { $candidates.Add($managed) }
51
- try {
52
- $py = Get-Command py.exe -ErrorAction Stop
53
- $resolved = (& $py.Source -3 -c "import sys;print(sys.executable)" 2>$null | Select-Object -First 1)
54
- if ($LASTEXITCODE -eq 0 -and $resolved) { $candidates.Add($resolved.Trim()) }
55
- } catch {}
56
- foreach ($command in @("python.exe", "python3.exe")) {
57
- try { $candidates.Add((Get-Command $command -ErrorAction Stop).Source) } catch {}
58
- }
59
- foreach ($candidate in ($candidates | Select-Object -Unique)) {
60
- try {
61
- $valid = & $candidate -c "import sys;raise SystemExit(0 if sys.version_info >= (3,10) else 1)" 2>$null
62
- if ($LASTEXITCODE -eq 0) { return $candidate }
63
- } catch {}
64
- }
65
- return $null
66
- }
67
-
68
- function Install-ManagedPython([string]$SessionDir) {
69
- Step "Python 3.10+ chưa có; cài Python riêng cho user hiện tại"
70
- $version = "3.12.10"
71
- $arch = if ([Environment]::Is64BitOperatingSystem) { "amd64" } else { "win32" }
72
- if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { $arch = "arm64" }
73
- $fileName = if ($arch -eq "win32") { "python-$version.exe" } else { "python-$version-$arch.exe" }
74
- $url = "https://www.python.org/ftp/python/$version/$fileName"
75
- $installer = Join-Path $SessionDir $fileName
76
- Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $installer -TimeoutSec 180
77
- $signature = Get-AuthenticodeSignature -LiteralPath $installer
78
- if ($signature.Status -ne "Valid" -or $signature.SignerCertificate.Subject -notmatch "Python Software Foundation") {
79
- throw "Chữ ký bộ cài Python không hợp lệ; đã dừng để bảo vệ VPS."
80
- }
81
- $target = Join-Path $Root "runtime\python"
82
- New-Item -ItemType Directory -Force -Path (Split-Path $target -Parent) | Out-Null
83
- $arguments = "/quiet InstallAllUsers=0 Include_launcher=0 Include_pip=1 Include_test=0 Shortcuts=0 PrependPath=0 TargetDir=`"$target`""
84
- $process = Start-Process -FilePath $installer -ArgumentList $arguments -Wait -PassThru -WindowStyle Hidden
85
- if ($process.ExitCode -ne 0 -or -not (Test-Path -LiteralPath (Join-Path $target "python.exe"))) {
86
- throw "Cài Python thất bại (exit $($process.ExitCode))."
87
- }
88
- return (Join-Path $target "python.exe")
89
- }
90
-
91
- function Get-Manifest {
92
- foreach ($url in $ManifestUrls) {
93
- try {
94
- Step "Kiểm tra bản phát hành từ Hugging Face"
95
- $cacheBust = if ($url.Contains("?")) { "&" } else { "?" }
96
- $response = Invoke-WebRequest -UseBasicParsing -Uri ($url + $cacheBust + "t=" + [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()) -TimeoutSec 45
97
- $manifest = $response.Content | ConvertFrom-Json
98
- if ($manifest.version -notmatch '^\d+\.\d+\.\d+$') { throw "version không hợp lệ" }
99
- if ($manifest.sha256 -notmatch '^[a-fA-F0-9]{64}$') { throw "SHA256 không hợp lệ" }
100
- foreach ($releaseUrl in @([string]$manifest.zip_url) + @($manifest.mirrors)) {
101
- $allowed = $false
102
- foreach ($prefix in $AllowedReleasePrefixes) {
103
- if ($releaseUrl -like ($prefix + "*")) { $allowed = $true; break }
104
- }
105
- if (-not $allowed) { throw "release URL ngoài repo cho phép" }
106
- }
107
- return $manifest
108
- } catch {
109
- Warn ("Không đọc được " + $url + ": " + $_.Exception.Message)
110
- }
111
- }
112
- return $null
113
- }
114
-
115
- function Expand-VerifiedZip([string]$ZipPath, [string]$Destination) {
116
- Add-Type -AssemblyName System.IO.Compression.FileSystem
117
- $archive = [IO.Compression.ZipFile]::OpenRead($ZipPath)
118
- try {
119
- $rootPath = [IO.Path]::GetFullPath($Destination + [IO.Path]::DirectorySeparatorChar)
120
- foreach ($entry in $archive.Entries) {
121
- $target = [IO.Path]::GetFullPath((Join-Path $Destination $entry.FullName))
122
- if (-not $target.StartsWith($rootPath, [StringComparison]::OrdinalIgnoreCase)) {
123
- throw "ZIP chứa đường dẫn không an toàn: $($entry.FullName)"
124
- }
125
- }
126
- } finally { $archive.Dispose() }
127
- [IO.Compression.ZipFile]::ExtractToDirectory($ZipPath, $Destination)
128
- }
129
-
130
- function Test-StudioServer {
131
- try { return Invoke-RestMethod -Uri "http://127.0.0.1:8799/api/health" -TimeoutSec 2 }
132
- catch { return $null }
133
- }
134
-
135
- Write-Host "============================================================" -ForegroundColor DarkCyan
136
- Write-Host " FoundryManager Studio - one-file installer / updater" -ForegroundColor White
137
- Write-Host "============================================================" -ForegroundColor DarkCyan
138
- if (Test-Administrator) {
139
- Warn "Đang chạy quyền Administrator. Tool vẫn chạy, nhưng profile Azure/browser sẽ thuộc user Administrator. Nên chạy BAT bằng double-click bình thường."
140
- }
141
-
142
- New-Item -ItemType Directory -Force -Path $Root, $DataDir, $ProfilesDir, $ReleasesDir | Out-Null
143
- $session = Join-Path $env:TEMP ("foundry-studio-" + [guid]::NewGuid().ToString("N"))
144
- New-Item -ItemType Directory -Path $session | Out-Null
145
-
146
- try {
147
- $manifest = Get-Manifest
148
- $version = $null
149
- $releaseDir = $null
150
- if ($manifest) {
151
- $version = [string]$manifest.version
152
- $releaseDir = Join-Path $ReleasesDir $version
153
- $releaseStamp = Join-Path $releaseDir ".release.sha256"
154
- $installedHash = if (Test-Path -LiteralPath $releaseStamp) { (Get-Content -LiteralPath $releaseStamp -Raw).Trim() } else { "" }
155
- if ($installedHash -ne ([string]$manifest.sha256).ToLowerInvariant()) {
156
- Step "Tải FoundryManager Studio $version"
157
- $zipPath = Join-Path $session "release.zip"
158
- $actualHash = ""; $downloadErrors = @()
159
- foreach ($releaseUrl in @([string]$manifest.zip_url) + @($manifest.mirrors)) {
160
- try {
161
- Invoke-WebRequest -UseBasicParsing -Uri $releaseUrl -OutFile $zipPath -TimeoutSec 300
162
- $actualHash = (Get-FileHash -Algorithm SHA256 -LiteralPath $zipPath).Hash.ToLowerInvariant()
163
- if ($actualHash -ne ([string]$manifest.sha256).ToLowerInvariant()) { throw "SHA256 không khớp" }
164
- break
165
- } catch { $downloadErrors += ($releaseUrl + ": " + $_.Exception.Message); $actualHash = "" }
166
- }
167
- if (-not $actualHash) { throw ("Không tải được release đã xác minh. " + ($downloadErrors -join " | ")) }
168
- $stage = Join-Path $session "app"
169
- New-Item -ItemType Directory -Path $stage | Out-Null
170
- Expand-VerifiedZip $zipPath $stage
171
- foreach ($required in @("app.py", "arm.py", "foundry.py", "requirements.lock", "VERSION", "static\studio.html")) {
172
- if (-not (Test-Path -LiteralPath (Join-Path $stage $required) -PathType Leaf)) { throw "Release thiếu $required" }
173
- }
174
- if (Test-Path -LiteralPath $releaseDir) {
175
- $backup = $releaseDir + ".replaced-" + [DateTime]::UtcNow.ToString("yyyyMMddHHmmss")
176
- Move-Item -LiteralPath $releaseDir -Destination $backup
177
- }
178
- Move-Item -LiteralPath $stage -Destination $releaseDir
179
- Set-Content -LiteralPath $releaseStamp -Value $actualHash -Encoding ascii
180
- Good "Đã xác minh SHA256 và cài release $version"
181
- } else { Good "Release $version đã có và đúng checksum" }
182
- Set-Content -LiteralPath $CurrentFile -Value $version -Encoding ascii
183
- } elseif (Test-Path -LiteralPath $CurrentFile) {
184
- $version = (Get-Content -LiteralPath $CurrentFile -Raw).Trim()
185
- $releaseDir = Join-Path $ReleasesDir $version
186
- Warn "Không kết nối được Hugging Face; dùng bản đã cài $version."
187
- } else {
188
- throw "Không tải được manifest và máy chưa có bản Studio để chạy offline."
189
- }
190
-
191
- if (-not (Test-Path -LiteralPath (Join-Path $releaseDir "app.py") -PathType Leaf)) { throw "Release hiện tại bị thiếu app.py" }
192
- $python = Get-WorkingPython
193
- if (-not $python) { $python = Install-ManagedPython $session }
194
- Good ("Python: " + (& $python --version 2>&1))
195
-
196
- $venvPython = Join-Path $VenvDir "Scripts\python.exe"
197
- $venvReady = $false
198
- if (Test-Path -LiteralPath $venvPython -PathType Leaf) {
199
- try { & $venvPython -c "import sys" 2>$null; $venvReady = ($LASTEXITCODE -eq 0) } catch {}
200
- }
201
- if (-not $venvReady) {
202
- if (Test-Path -LiteralPath $VenvDir) {
203
- $venvBackup = $VenvDir + ".replaced-" + [DateTime]::UtcNow.ToString("yyyyMMddHHmmss")
204
- Move-Item -LiteralPath $VenvDir -Destination $venvBackup
205
- }
206
- Step "Tạo môi trường Python riêng"
207
- & $python -m venv $VenvDir
208
- if ($LASTEXITCODE -ne 0) { throw "Không tạo được Python venv." }
209
- }
210
- $requirements = Join-Path $releaseDir "requirements.lock"
211
- $requirementsHash = (Get-FileHash -Algorithm SHA256 -LiteralPath $requirements).Hash.ToLowerInvariant()
212
- $oldRequirementsHash = if (Test-Path -LiteralPath $RequirementStamp) { (Get-Content -LiteralPath $RequirementStamp -Raw).Trim() } else { "" }
213
- $dependenciesReady = $false
214
- try { & $venvPython -c "import flask" 2>$null; $dependenciesReady = ($LASTEXITCODE -eq 0) } catch {}
215
- if ($oldRequirementsHash -ne $requirementsHash -or -not $dependenciesReady) {
216
- Step "Cài thư viện đã khóa phiên bản"
217
- & $venvPython -m pip install --disable-pip-version-check --quiet -r $requirements
218
- if ($LASTEXITCODE -ne 0) { throw "pip không cài được dependencies." }
219
- Set-Content -LiteralPath $RequirementStamp -Value $requirementsHash -Encoding ascii
220
- }
221
- & $venvPython -c "import flask; print(flask.__version__ if hasattr(flask,'__version__') else 'Flask ready')" 2>$null | Out-Null
222
- if ($LASTEXITCODE -ne 0) { throw "Môi trường Flask chưa sẵn sàng." }
223
-
224
- if ($env:FM_INSTALL_ONLY -eq "1") {
225
- Good "Install-only hoàn tất; chưa khởi động local server."
226
- exit 0
227
- }
228
-
229
- $server = Test-StudioServer
230
- if ($server) {
231
- Good ("Studio đang chạy (version " + $server.version + "); mở thêm tab mới.")
232
- if ([string]$server.version -ne $version) { Warn "Bản mới đã cài. Đóng cửa sổ Studio cũ rồi chạy BAT lại để kích hoạt." }
233
- Start-Process "http://127.0.0.1:8799/"
234
- exit 0
235
- }
236
-
237
- $env:FM_PROFILES_DIR = $ProfilesDir
238
- $env:FM_HOST = "127.0.0.1"
239
- $env:FM_PORT = "8799"
240
- if (-not $env:FM_OPEN_BROWSER) { $env:FM_OPEN_BROWSER = "1" }
241
- Good "Profile được lưu riêng tại $ProfilesDir"
242
- Good "Mở Studio ở tab mới: http://127.0.0.1:8799/"
243
- Set-Location $releaseDir
244
- & $venvPython (Join-Path $releaseDir "app.py")
245
- exit $LASTEXITCODE
246
- } finally {
247
- if ($session -and (Test-Path -LiteralPath $session)) {
248
- $tempRoot = [IO.Path]::GetFullPath($env:TEMP + [IO.Path]::DirectorySeparatorChar)
249
- $sessionFull = [IO.Path]::GetFullPath($session)
250
- if ($sessionFull.StartsWith($tempRoot, [StringComparison]::OrdinalIgnoreCase) -and
251
- (Split-Path $sessionFull -Leaf) -like "foundry-studio-*") {
252
- Remove-Item -LiteralPath $sessionFull -Recurse -Force -ErrorAction SilentlyContinue
253
- }
254
- }
255
- }
 
1
+ @echo off
2
+ setlocal
3
+ chcp 65001 >nul
4
+ title FoundryManager Studio - Install and Run
5
+ set "FM_BOOTSTRAP_SELF=%~f0"
6
+ powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "$raw=[IO.File]::ReadAllText($env:FM_BOOTSTRAP_SELF);$mark='#==POWERSHELL==';$at=$raw.LastIndexOf($mark);if($at -lt 0){throw 'Bootstrap payload not found'};& ([scriptblock]::Create($raw.Substring($at)))"
7
+ set "FM_EXIT=%ERRORLEVEL%"
8
+ if not "%FM_EXIT%"=="0" (
9
+ echo.
10
+ echo [ERROR] FoundryManager Studio could not start. See the message above.
11
+ pause
12
+ )
13
+ exit /b %FM_EXIT%
14
+
15
+ #==POWERSHELL==
16
+ $ErrorActionPreference = "Stop"
17
+ [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
18
+ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
19
+
20
+ $Product = "FoundryManager Studio"
21
+ $Root = Join-Path $env:LOCALAPPDATA "FoundryManagerStudio"
22
+ $DataDir = Join-Path $Root "data"
23
+ $ProfilesDir = Join-Path $DataDir "profiles"
24
+ $ReleasesDir = Join-Path $Root "releases"
25
+ $VenvDir = Join-Path $Root "venv"
26
+ $CurrentFile = Join-Path $Root "current.txt"
27
+ $RequirementStamp = Join-Path $Root "requirements.sha256"
28
+ $ManifestUrls = @(
29
+ "https://huggingface.co/datasets/DangPhamPham/foundrymanager-studio/resolve/main/manifest.json",
30
+ "https://huggingface.co/datasets/phamdangdangild/foundrymanager-studio/resolve/main/manifest.json"
31
+ )
32
+ $AllowedReleasePrefixes = @(
33
+ "https://huggingface.co/datasets/DangPhamPham/foundrymanager-studio/resolve/",
34
+ "https://huggingface.co/datasets/phamdangdangild/foundrymanager-studio/resolve/"
35
+ )
36
+
37
+ function Step([string]$Message) { Write-Host ("[*] " + $Message) -ForegroundColor Cyan }
38
+ function Good([string]$Message) { Write-Host ("[OK] " + $Message) -ForegroundColor Green }
39
+ function Warn([string]$Message) { Write-Host ("[!] " + $Message) -ForegroundColor Yellow }
40
+
41
+ function Test-Administrator {
42
+ $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
43
+ $principal = [Security.Principal.WindowsPrincipal]::new($identity)
44
+ return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
45
+ }
46
+
47
+ function Get-WorkingPython {
48
+ $candidates = [System.Collections.Generic.List[string]]::new()
49
+ $managed = Join-Path $Root "runtime\python\python.exe"
50
+ if (Test-Path -LiteralPath $managed -PathType Leaf) { $candidates.Add($managed) }
51
+ try {
52
+ $py = Get-Command py.exe -ErrorAction Stop
53
+ $resolved = (& $py.Source -3 -c "import sys;print(sys.executable)" 2>$null | Select-Object -First 1)
54
+ if ($LASTEXITCODE -eq 0 -and $resolved) { $candidates.Add($resolved.Trim()) }
55
+ } catch {}
56
+ foreach ($command in @("python.exe", "python3.exe")) {
57
+ try { $candidates.Add((Get-Command $command -ErrorAction Stop).Source) } catch {}
58
+ }
59
+ foreach ($candidate in ($candidates | Select-Object -Unique)) {
60
+ try {
61
+ $valid = & $candidate -c "import sys;raise SystemExit(0 if sys.version_info >= (3,10) else 1)" 2>$null
62
+ if ($LASTEXITCODE -eq 0) { return $candidate }
63
+ } catch {}
64
+ }
65
+ return $null
66
+ }
67
+
68
+ function Install-ManagedPython([string]$SessionDir) {
69
+ Step "Python 3.10+ chưa có; cài Python riêng cho user hiện tại"
70
+ $version = "3.12.10"
71
+ $arch = if ([Environment]::Is64BitOperatingSystem) { "amd64" } else { "win32" }
72
+ if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { $arch = "arm64" }
73
+ $fileName = if ($arch -eq "win32") { "python-$version.exe" } else { "python-$version-$arch.exe" }
74
+ $url = "https://www.python.org/ftp/python/$version/$fileName"
75
+ $installer = Join-Path $SessionDir $fileName
76
+ Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $installer -TimeoutSec 180
77
+ $signature = Get-AuthenticodeSignature -LiteralPath $installer
78
+ if ($signature.Status -ne "Valid" -or $signature.SignerCertificate.Subject -notmatch "Python Software Foundation") {
79
+ throw "Chữ ký bộ cài Python không hợp lệ; đã dừng để bảo vệ VPS."
80
+ }
81
+ $target = Join-Path $Root "runtime\python"
82
+ New-Item -ItemType Directory -Force -Path (Split-Path $target -Parent) | Out-Null
83
+ $arguments = "/quiet InstallAllUsers=0 Include_launcher=0 Include_pip=1 Include_test=0 Shortcuts=0 PrependPath=0 TargetDir=`"$target`""
84
+ $process = Start-Process -FilePath $installer -ArgumentList $arguments -Wait -PassThru -WindowStyle Hidden
85
+ if ($process.ExitCode -ne 0 -or -not (Test-Path -LiteralPath (Join-Path $target "python.exe"))) {
86
+ throw "Cài Python thất bại (exit $($process.ExitCode))."
87
+ }
88
+ return (Join-Path $target "python.exe")
89
+ }
90
+
91
+ function Get-Manifest {
92
+ foreach ($url in $ManifestUrls) {
93
+ try {
94
+ Step "Kiểm tra bản phát hành từ Hugging Face"
95
+ $cacheBust = if ($url.Contains("?")) { "&" } else { "?" }
96
+ $response = Invoke-WebRequest -UseBasicParsing -Uri ($url + $cacheBust + "t=" + [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()) -TimeoutSec 45
97
+ $manifest = $response.Content | ConvertFrom-Json
98
+ if ($manifest.version -notmatch '^\d+\.\d+\.\d+$') { throw "version không hợp lệ" }
99
+ if ($manifest.sha256 -notmatch '^[a-fA-F0-9]{64}$') { throw "SHA256 không hợp lệ" }
100
+ foreach ($releaseUrl in @([string]$manifest.zip_url) + @($manifest.mirrors)) {
101
+ $allowed = $false
102
+ foreach ($prefix in $AllowedReleasePrefixes) {
103
+ if ($releaseUrl -like ($prefix + "*")) { $allowed = $true; break }
104
+ }
105
+ if (-not $allowed) { throw "release URL ngoài repo cho phép" }
106
+ }
107
+ return $manifest
108
+ } catch {
109
+ Warn ("Không đọc được " + $url + ": " + $_.Exception.Message)
110
+ }
111
+ }
112
+ return $null
113
+ }
114
+
115
+ function Expand-VerifiedZip([string]$ZipPath, [string]$Destination) {
116
+ Add-Type -AssemblyName System.IO.Compression.FileSystem
117
+ $archive = [IO.Compression.ZipFile]::OpenRead($ZipPath)
118
+ try {
119
+ $rootPath = [IO.Path]::GetFullPath($Destination + [IO.Path]::DirectorySeparatorChar)
120
+ foreach ($entry in $archive.Entries) {
121
+ $target = [IO.Path]::GetFullPath((Join-Path $Destination $entry.FullName))
122
+ if (-not $target.StartsWith($rootPath, [StringComparison]::OrdinalIgnoreCase)) {
123
+ throw "ZIP chứa đường dẫn không an toàn: $($entry.FullName)"
124
+ }
125
+ }
126
+ } finally { $archive.Dispose() }
127
+ [IO.Compression.ZipFile]::ExtractToDirectory($ZipPath, $Destination)
128
+ }
129
+
130
+ function Test-StudioServer {
131
+ try { return Invoke-RestMethod -Uri "http://127.0.0.1:8799/api/health" -TimeoutSec 2 }
132
+ catch { return $null }
133
+ }
134
+
135
+ Write-Host "============================================================" -ForegroundColor DarkCyan
136
+ Write-Host " FoundryManager Studio - one-file installer / updater" -ForegroundColor White
137
+ Write-Host "============================================================" -ForegroundColor DarkCyan
138
+ if (Test-Administrator) {
139
+ Warn "Đang chạy quyền Administrator. Tool vẫn chạy, nhưng profile Azure/browser sẽ thuộc user Administrator. Nên chạy BAT bằng double-click bình thường."
140
+ }
141
+
142
+ New-Item -ItemType Directory -Force -Path $Root, $DataDir, $ProfilesDir, $ReleasesDir | Out-Null
143
+ $session = Join-Path $env:TEMP ("foundry-studio-" + [guid]::NewGuid().ToString("N"))
144
+ New-Item -ItemType Directory -Path $session | Out-Null
145
+
146
+ try {
147
+ $manifest = Get-Manifest
148
+ $version = $null
149
+ $releaseDir = $null
150
+ if ($manifest) {
151
+ $version = [string]$manifest.version
152
+ $releaseDir = Join-Path $ReleasesDir $version
153
+ $releaseStamp = Join-Path $releaseDir ".release.sha256"
154
+ $installedHash = if (Test-Path -LiteralPath $releaseStamp) { (Get-Content -LiteralPath $releaseStamp -Raw).Trim() } else { "" }
155
+ if ($installedHash -ne ([string]$manifest.sha256).ToLowerInvariant()) {
156
+ Step "Tải FoundryManager Studio $version"
157
+ $zipPath = Join-Path $session "release.zip"
158
+ $actualHash = ""; $downloadErrors = @()
159
+ foreach ($releaseUrl in @([string]$manifest.zip_url) + @($manifest.mirrors)) {
160
+ try {
161
+ Invoke-WebRequest -UseBasicParsing -Uri $releaseUrl -OutFile $zipPath -TimeoutSec 300
162
+ $actualHash = (Get-FileHash -Algorithm SHA256 -LiteralPath $zipPath).Hash.ToLowerInvariant()
163
+ if ($actualHash -ne ([string]$manifest.sha256).ToLowerInvariant()) { throw "SHA256 không khớp" }
164
+ break
165
+ } catch { $downloadErrors += ($releaseUrl + ": " + $_.Exception.Message); $actualHash = "" }
166
+ }
167
+ if (-not $actualHash) { throw ("Không tải được release đã xác minh. " + ($downloadErrors -join " | ")) }
168
+ $stage = Join-Path $session "app"
169
+ New-Item -ItemType Directory -Path $stage | Out-Null
170
+ Expand-VerifiedZip $zipPath $stage
171
+ foreach ($required in @("app.py", "arm.py", "foundry.py", "requirements.lock", "VERSION", "static\studio.html")) {
172
+ if (-not (Test-Path -LiteralPath (Join-Path $stage $required) -PathType Leaf)) { throw "Release thiếu $required" }
173
+ }
174
+ if (Test-Path -LiteralPath $releaseDir) {
175
+ $backup = $releaseDir + ".replaced-" + [DateTime]::UtcNow.ToString("yyyyMMddHHmmss")
176
+ Move-Item -LiteralPath $releaseDir -Destination $backup
177
+ }
178
+ Move-Item -LiteralPath $stage -Destination $releaseDir
179
+ Set-Content -LiteralPath $releaseStamp -Value $actualHash -Encoding ascii
180
+ Good "Đã xác minh SHA256 và cài release $version"
181
+ } else { Good "Release $version đã có và đúng checksum" }
182
+ Set-Content -LiteralPath $CurrentFile -Value $version -Encoding ascii
183
+ } elseif (Test-Path -LiteralPath $CurrentFile) {
184
+ $version = (Get-Content -LiteralPath $CurrentFile -Raw).Trim()
185
+ $releaseDir = Join-Path $ReleasesDir $version
186
+ Warn "Không kết nối được Hugging Face; dùng bản đã cài $version."
187
+ } else {
188
+ throw "Không tải được manifest và máy chưa có bản Studio để chạy offline."
189
+ }
190
+
191
+ if (-not (Test-Path -LiteralPath (Join-Path $releaseDir "app.py") -PathType Leaf)) { throw "Release hiện tại bị thiếu app.py" }
192
+ $python = Get-WorkingPython
193
+ if (-not $python) { $python = Install-ManagedPython $session }
194
+ Good ("Python: " + (& $python --version 2>&1))
195
+
196
+ $venvPython = Join-Path $VenvDir "Scripts\python.exe"
197
+ $venvReady = $false
198
+ if (Test-Path -LiteralPath $venvPython -PathType Leaf) {
199
+ try { & $venvPython -c "import sys" 2>$null; $venvReady = ($LASTEXITCODE -eq 0) } catch {}
200
+ }
201
+ if (-not $venvReady) {
202
+ if (Test-Path -LiteralPath $VenvDir) {
203
+ $venvBackup = $VenvDir + ".replaced-" + [DateTime]::UtcNow.ToString("yyyyMMddHHmmss")
204
+ Move-Item -LiteralPath $VenvDir -Destination $venvBackup
205
+ }
206
+ Step "Tạo môi trường Python riêng"
207
+ & $python -m venv $VenvDir
208
+ if ($LASTEXITCODE -ne 0) { throw "Không tạo được Python venv." }
209
+ }
210
+ $requirements = Join-Path $releaseDir "requirements.lock"
211
+ $requirementsHash = (Get-FileHash -Algorithm SHA256 -LiteralPath $requirements).Hash.ToLowerInvariant()
212
+ $oldRequirementsHash = if (Test-Path -LiteralPath $RequirementStamp) { (Get-Content -LiteralPath $RequirementStamp -Raw).Trim() } else { "" }
213
+ $dependenciesReady = $false
214
+ try { & $venvPython -c "import flask" 2>$null; $dependenciesReady = ($LASTEXITCODE -eq 0) } catch {}
215
+ if ($oldRequirementsHash -ne $requirementsHash -or -not $dependenciesReady) {
216
+ Step "Cài thư viện đã khóa phiên bản"
217
+ & $venvPython -m pip install --disable-pip-version-check --quiet -r $requirements
218
+ if ($LASTEXITCODE -ne 0) { throw "pip không cài được dependencies." }
219
+ Set-Content -LiteralPath $RequirementStamp -Value $requirementsHash -Encoding ascii
220
+ }
221
+ & $venvPython -c "import flask" 2>$null
222
+ if ($LASTEXITCODE -ne 0) { throw "Môi trường Flask chưa sẵn sàng." }
223
+
224
+ if ($env:FM_INSTALL_ONLY -eq "1") {
225
+ Good "Install-only hoàn tất; chưa khởi động local server."
226
+ exit 0
227
+ }
228
+
229
+ $server = Test-StudioServer
230
+ if ($server) {
231
+ Good ("Studio đang chạy (version " + $server.version + "); mở thêm tab mới.")
232
+ if ([string]$server.version -ne $version) { Warn "Bản mới đã cài. Đóng cửa sổ Studio cũ rồi chạy BAT lại để kích hoạt." }
233
+ Start-Process "http://127.0.0.1:8799/"
234
+ exit 0
235
+ }
236
+
237
+ $env:FM_PROFILES_DIR = $ProfilesDir
238
+ $env:FM_HOST = "127.0.0.1"
239
+ $env:FM_PORT = "8799"
240
+ if (-not $env:FM_OPEN_BROWSER) { $env:FM_OPEN_BROWSER = "1" }
241
+ Good "Profile được lưu riêng tại $ProfilesDir"
242
+ Good "Mở Studio ở tab mới: http://127.0.0.1:8799/"
243
+ Set-Location $releaseDir
244
+ & $venvPython (Join-Path $releaseDir "app.py")
245
+ exit $LASTEXITCODE
246
+ } finally {
247
+ if ($session -and (Test-Path -LiteralPath $session)) {
248
+ $tempRoot = [IO.Path]::GetFullPath($env:TEMP + [IO.Path]::DirectorySeparatorChar)
249
+ $sessionFull = [IO.Path]::GetFullPath($session)
250
+ if ($sessionFull.StartsWith($tempRoot, [StringComparison]::OrdinalIgnoreCase) -and
251
+ (Split-Path $sessionFull -Leaf) -like "foundry-studio-*") {
252
+ Remove-Item -LiteralPath $sessionFull -Recurse -Force -ErrorAction SilentlyContinue
253
+ }
254
+ }
255
+ }
checksums.txt CHANGED
@@ -1 +1 @@
1
- 46b5003dfb960c5528f9764bd2d6cb9c80de99805665f1f4ffa8db67c4f0b09a releases/foundrymanager-studio-2.1.0.zip
 
1
+ bc40cc5526662a93370e99f72e25ac2ab26ddcb491ac52d1ecb7ffef3682d346 releases/foundrymanager-studio-2.1.0.zip
manifest.json CHANGED
@@ -2,13 +2,13 @@
2
  "schema": 1,
3
  "product": "FoundryManager Studio",
4
  "version": "2.1.0",
5
- "published_at": "2026-08-05T12:15:17Z",
6
  "minimum_python": "3.10",
7
  "zip_url": "https://huggingface.co/datasets/DangPhamPham/foundrymanager-studio/resolve/main/releases/foundrymanager-studio-2.1.0.zip",
8
  "mirrors": [
9
  "https://huggingface.co/datasets/phamdangdangild/foundrymanager-studio/resolve/main/releases/foundrymanager-studio-2.1.0.zip"
10
  ],
11
- "sha256": "46b5003dfb960c5528f9764bd2d6cb9c80de99805665f1f4ffa8db67c4f0b09a",
12
- "size_bytes": 92759,
13
  "entrypoint": "app.py"
14
  }
 
2
  "schema": 1,
3
  "product": "FoundryManager Studio",
4
  "version": "2.1.0",
5
+ "published_at": "2026-08-05T12:22:22Z",
6
  "minimum_python": "3.10",
7
  "zip_url": "https://huggingface.co/datasets/DangPhamPham/foundrymanager-studio/resolve/main/releases/foundrymanager-studio-2.1.0.zip",
8
  "mirrors": [
9
  "https://huggingface.co/datasets/phamdangdangild/foundrymanager-studio/resolve/main/releases/foundrymanager-studio-2.1.0.zip"
10
  ],
11
+ "sha256": "bc40cc5526662a93370e99f72e25ac2ab26ddcb491ac52d1ecb7ffef3682d346",
12
+ "size_bytes": 94279,
13
  "entrypoint": "app.py"
14
  }
releases/foundrymanager-studio-2.1.0.zip CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:46b5003dfb960c5528f9764bd2d6cb9c80de99805665f1f4ffa8db67c4f0b09a
3
- size 92759
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bc40cc5526662a93370e99f72e25ac2ab26ddcb491ac52d1ecb7ffef3682d346
3
+ size 94279