Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions electron/main/python-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ import { BrowserWindow, app } from 'electron'
import { existsSync, readFileSync, writeFileSync, readdirSync } from 'fs'
import { join } from 'path'
import { spawn, execSync } from 'child_process'
import { createHash } from 'crypto'

const SETUP_VERSION = 2
const TOTAL_PACKAGES = 20

interface SetupJson {
version: number
appVersion?: string
requirementsHash?: string
}

function getRequirementsPath(): string {
return app.isPackaged
? join(process.resourcesPath, 'api', 'requirements.txt')
: join(app.getAppPath(), 'api', 'requirements.txt')
}

function hashRequirements(): string {
try {
const content = readFileSync(getRequirementsPath(), 'utf-8')
return createHash('sha256').update(content).digest('hex')
} catch {
return ''
}
}

// ─── Public helpers ──────────────────────────────────────────────────────────
Expand All @@ -19,8 +35,7 @@ export function checkSetupNeeded(userData: string): boolean {
try {
const data = JSON.parse(readFileSync(jsonPath, 'utf-8')) as SetupJson
if (data.version < SETUP_VERSION) return true
// Re-run setup if the app was updated (new python-embed is fresh, no packages)
if (data.appVersion !== app.getVersion()) return true
if (data.requirementsHash !== hashRequirements()) return true
} catch {
return true
}
Expand All @@ -35,7 +50,7 @@ export function markSetupDone(userData: string): void {
const jsonPath = join(userData, 'python_setup.json')
writeFileSync(
jsonPath,
JSON.stringify({ version: SETUP_VERSION, appVersion: app.getVersion() }),
JSON.stringify({ version: SETUP_VERSION, requirementsHash: hashRequirements() }),
'utf-8'
)
}
Expand Down Expand Up @@ -188,9 +203,7 @@ function createVenv(python3: string, venvDir: string, win: BrowserWindow): Promi

export async function runFullSetup(win: BrowserWindow, userData: string): Promise<void> {
try {
const requirementsPath = app.isPackaged
? join(process.resourcesPath, 'api', 'requirements.txt')
: join(app.getAppPath(), 'api', 'requirements.txt')
const requirementsPath = getRequirementsPath()

if (process.platform === 'win32') {
// Windows: use embedded Python bundled with the app
Expand Down