Repository navigation
Expand file tree
/
Copy pathfreevideo.ps1
More file actions
170 lines (170 loc) · 10.4 KB
/
Copy pathfreevideo.ps1
File metadata and controls
170 lines (170 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Windows PowerShell 5.1 / PowerShell 7. No activation or machine-wide PATH changes.
$ErrorActionPreference = 'Stop'
$freevideoArguments = @($args)
$freevideoExit = 1
$freevideoConsoleEncoding = [Console]::OutputEncoding
$freevideoSavedEnvironment = @{}
foreach ($name in @('PYTHONPATH', 'PYTHONUTF8', 'PYTHONIOENCODING', 'UV_PYTHON_INSTALL_DIR',
'UV_PYTHON_INSTALL_MIRROR', 'UV_HTTP_TIMEOUT', 'UV_HTTP_RETRIES', 'UV_NO_CONFIG',
'FREEVIDEO_HOME', 'FREEVIDEO_BOOTSTRAP_ROOT', 'FREEVIDEO_PROXY_MODE')) {
$freevideoSavedEnvironment[$name] = [Environment]::GetEnvironmentVariable($name, 'Process')
}
try {
if ($env:OS -ne 'Windows_NT' -or -not [Environment]::Is64BitOperatingSystem) {
throw 'Use native Windows x64, or the Linux ./freevideo entry point.'
}
. (Join-Path $PSScriptRoot 'freevideo_engine\windows_bootstrap.ps1')
$env:PYTHONPATH = $PSScriptRoot
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding
$env:PYTHONUTF8 = '1'
$env:PYTHONIOENCODING = 'utf-8'
$freevideoRoot = if ($env:FREEVIDEO_HOME) { $env:FREEVIDEO_HOME } elseif (Test-Path -LiteralPath (Join-Path $PSScriptRoot 'launcher-source.json')) {
Join-Path $env:LOCALAPPDATA 'FreeVideo'
} else { $PSScriptRoot }
for ($i = 0; $i -lt $freevideoArguments.Count; $i++) {
if ($freevideoArguments[$i] -eq '--root' -and $i + 1 -lt $freevideoArguments.Count) {
$freevideoRoot = $freevideoArguments[$i + 1]
} elseif ($freevideoArguments[$i].StartsWith('--root=')) {
$freevideoRoot = $freevideoArguments[$i].Substring(7)
}
}
$env:FREEVIDEO_HOME = $freevideoRoot
$downloadSettings = Join-Path $freevideoRoot 'download-settings.json'
if (Test-Path -LiteralPath $downloadSettings) {
$preferences = Get-Content -LiteralPath $downloadSettings -Raw -Encoding UTF8 | ConvertFrom-Json
if ($preferences.proxy_mode) { $env:FREEVIDEO_PROXY_MODE = $preferences.proxy_mode }
}
if ($env:FREEVIDEO_PROXY_MODE -and $env:FREEVIDEO_PROXY_MODE -notin @('auto', 'proxy', 'direct')) {
throw 'Invalid download connection mode.'
}
$bootstrapRoot = if ($env:FREEVIDEO_BOOTSTRAP_ROOT) { $env:FREEVIDEO_BOOTSTRAP_ROOT } else { Join-Path $freevideoRoot '.freevideo\bootstrap' }
$env:FREEVIDEO_BOOTSTRAP_ROOT = $bootstrapRoot
$freevideoPython = $null
$freevideoPythonPrefix = @()
$freevideoCandidates = @()
if ($env:FREEVIDEO_PYTHON) { $freevideoCandidates += ,@($env:FREEVIDEO_PYTHON) }
$freevideoCandidates += ,@((Join-Path $freevideoRoot 'envs\unified\Scripts\python.exe'))
$pyCommand = Get-Command py.exe -ErrorAction SilentlyContinue
if ($pyCommand) { $freevideoCandidates += ,@($pyCommand.Source, '-3') }
foreach ($name in @('python.exe', 'python3.exe')) {
$command = Get-Command $name -ErrorAction SilentlyContinue
if ($command -and $command.Source -notlike '*\Microsoft\WindowsApps\*') {
$freevideoCandidates += ,@($command.Source)
}
}
if ($env:FREEVIDEO_FORCE_BOOTSTRAP -ne '1') {
foreach ($candidate in $freevideoCandidates) {
if (-not (Test-Path -LiteralPath $candidate[0] -PathType Leaf)) { continue }
$prefix = @($candidate | Select-Object -Skip 1)
try {
& $candidate[0] @prefix -B -X utf8 -c 'import sys; sys.exit(0 if sys.version_info >= (3, 9) else 1)' 2>$null
if ($LASTEXITCODE -eq 0) {
$freevideoPython = $candidate[0]
$freevideoPythonPrefix = $prefix
break
}
} catch { continue }
}
}
if (-not $freevideoPython) {
# Keep the bootstrap inside the selected installation, including source clones.
# The Python setup still reviews hardware, resources and licenses before
# creating an environment or downloading any models.
[IO.Directory]::CreateDirectory($bootstrapRoot) | Out-Null
$bootstrapLog = Join-Path $bootstrapRoot 'bootstrap.log'
Write-Host 'Preparing isolated Python for setup. Model/environment installation requires the setup confirmation.'
Write-Host "Bootstrap cache and log: $bootstrapRoot"
$bootstrapLock = [IO.File]::Open((Join-Path $bootstrapRoot 'bootstrap.lock'), 'OpenOrCreate', 'ReadWrite', 'None')
$patientDownloadNotice = $false
try {
$versions = Get-Content -LiteralPath (Join-Path $PSScriptRoot 'freevideo_engine\bootstrap_versions.json') -Raw -Encoding UTF8 | ConvertFrom-Json
$uvSpec = $versions.windows.uv
$officialOnly = $freevideoArguments -contains '--network=official'
for ($i = 0; $i -lt $freevideoArguments.Count - 1; $i++) {
if ($freevideoArguments[$i] -eq '--network' -and $freevideoArguments[$i + 1] -eq 'official') { $officialOnly = $true }
}
$routes = @('https://github.com')
if (-not $officialOnly) { $routes += 'https://ghfast.top/https://github.com' }
$proxyRoutes = @('inherited')
if ($env:FREEVIDEO_PROXY_MODE -eq 'direct') { $proxyRoutes = @('direct') }
elseif ($env:FREEVIDEO_PROXY_MODE -ne 'proxy' -and ($env:HTTP_PROXY -or $env:HTTPS_PROXY -or $env:ALL_PROXY)) { $proxyRoutes += 'direct' }
$uvCatalog = Get-Content -LiteralPath (Join-Path $PSScriptRoot 'freevideo_engine\uv_downloads.json') -Raw -Encoding UTF8 | ConvertFrom-Json
$uv = Get-FreeVideoUv -Spec $uvSpec -Catalog $uvCatalog -BootstrapRoot $bootstrapRoot `
-EngineRoot $freevideoRoot -LogPath $bootstrapLog -GithubSources $routes `
-ProxyRoutes $proxyRoutes -OfficialOnly:$officialOnly
$env:UV_PYTHON_INSTALL_DIR = Join-Path $bootstrapRoot 'python'
$env:UV_NO_CONFIG = '1'
$installed = $false
$pythonRoutes = @($routes | ForEach-Object { "$_/astral-sh/python-build-standalone/releases/download" })
if (-not $officialOnly -and $freevideoSavedEnvironment['UV_PYTHON_INSTALL_MIRROR']) {
$pythonRoutes = @($freevideoSavedEnvironment['UV_PYTHON_INSTALL_MIRROR']) + $pythonRoutes
}
$attempt = 0
$allNetworkFailures = $true
# Keep these defaults and the notice in sync with network.py (CPU tests).
$pythonRetryProfiles = @(
@{Mode='fast'; UV_HTTP_TIMEOUT='20'; UV_HTTP_RETRIES='1'},
@{Mode='patient'; UV_HTTP_TIMEOUT='120'; UV_HTTP_RETRIES='5'}
)
foreach ($profile in $pythonRetryProfiles) {
if ($installed) { break }
if ($profile.Mode -eq 'patient') {
if (-not $allNetworkFailures) { break }
Write-Host 'The network is slow. Downloading again; this may take a while.'
$patientDownloadNotice = $true
if ($env:FREEVIDEO_UI_EVENTS -eq '1') {
Write-Host '{"event":"freevideo_ui","kind":"progress","key":"download-patient-python","label":"Download","detail":"The network is slow. Downloading again; this may take a while."}'
}
}
foreach ($name in @('UV_HTTP_TIMEOUT', 'UV_HTTP_RETRIES')) {
$value = $freevideoSavedEnvironment[$name]
if ($null -eq $value) { $value = $profile[$name] }
[Environment]::SetEnvironmentVariable($name, $value, 'Process')
}
foreach ($route in $pythonRoutes) {
foreach ($proxyRoute in $proxyRoutes) {
if ($installed) { break }
$attempt++
$env:UV_PYTHON_INSTALL_MIRROR = $route
Write-Progress -Activity 'FreeVideo bootstrap' -Status ('Install isolated Python ' + $versions.python)
$pythonOutput = ''
$pythonLabel = "Install Python $($versions.python), attempt $attempt ($proxyRoute connection)"
if ($profile.Mode -eq 'patient') { $pythonLabel += ", $($profile.Mode)" }
$pythonExit = Invoke-FreeVideoBootstrapCommand -Executable $uv `
-Arguments @('python', 'install', '--no-bin', '--no-registry', '--no-progress', '--color', 'never', $versions.python) `
-LogPath $bootstrapLog -Label $pythonLabel `
-Direct:($proxyRoute -eq 'direct') -OutputTail ([ref]$pythonOutput)
if ($pythonExit -eq 0) { $installed = $true; break }
if (-not (Test-FreeVideoSlowDownloadFailure $pythonOutput)) { $allNetworkFailures = $false }
Write-Host "Python install source $attempt returned exit code $pythonExit. Details retained in $bootstrapLog"
}
}
}
if (-not $installed) { throw "Python installation failed (uv exit $pythonExit). Check your network or connection mode in Downloads. See $bootstrapLog and retry." }
$freevideoPython = (& $uv python find --managed-python $versions.python | Select-Object -Last 1).Trim()
if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $freevideoPython)) { throw 'Managed Python could not be located.' }
} finally {
if ($patientDownloadNotice -and $env:FREEVIDEO_UI_EVENTS -eq '1') {
Write-Host '{"event":"freevideo_ui","kind":"progress","key":"download-patient-python","label":"Download","detail":"","state":"complete"}'
}
$bootstrapLock.Dispose()
Write-Progress -Activity 'FreeVideo bootstrap' -Completed
}
}
# Restore only our download defaults before Python determines user overrides.
# A null saved value removes the variable from the child environment.
foreach ($name in @('UV_HTTP_TIMEOUT', 'UV_HTTP_RETRIES')) {
[Environment]::SetEnvironmentVariable($name, $freevideoSavedEnvironment[$name], 'Process')
}
& $freevideoPython @freevideoPythonPrefix -B -X utf8 -m freevideo_engine.managed @freevideoArguments
$freevideoExit = $LASTEXITCODE
} catch {
Write-Host ("FreeVideo could not start: " + $_.Exception.Message) -ForegroundColor Red
Write-Host 'Existing files are retained. Retry the same command after resolving the reported issue.'
} finally {
[Console]::OutputEncoding = $freevideoConsoleEncoding
foreach ($name in $freevideoSavedEnvironment.Keys) {
[Environment]::SetEnvironmentVariable($name, $freevideoSavedEnvironment[$name], 'Process')
}
}
exit $freevideoExit