Skip to content

Commit

Permalink
Merge branch 'main' into dev-kendal-status
Browse files Browse the repository at this point in the history
  • Loading branch information
KenCorma authored Sep 8, 2024
2 parents 8167e26 + 2411e18 commit 2e8300c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .github/actions/build/linux/app/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ runs:
run: |
set -x
pip install comfy-cli
yarn make:assets:nvidia
yarn make:assets:cpu
yarn clean:assets:dev
shell: bash
- name: Make app
shell: bash
Expand Down
9 changes: 4 additions & 5 deletions .github/actions/build/windows/app/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ runs:
python-version: '3.12'
- name: Install ComfyUI and create standalone package
run: |
set -x
pip install comfy-cli
cd assets
comfy --skip-prompt --workspace ./ComfyUI install --fast-deps --nvidia --cuda-version 12.1
comfy --workspace ./ComfyUI standalone --platform windows --proc x86_64
rm -rf python cpython*.tar.gz
shell: cmd
yarn make:assets:cpu
yarn clean:assets:dev
shell: bash
- name: Make app
shell: powershell
env:
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/debug_linux.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name: Build App Linux Debug

on:
workflow_dispatch:
workflow_call:
push:
branches:
- fix-python-server-startup # just for testing
- main
pull_request:
branches:
- main

jobs:
build-linux-debug:
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/debug_windows.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name: Build Windows Debug

on:
workflow_dispatch:
workflow_call:
push:
branches:
- fix-python-server-startup # just for testing
- main
pull_request:
branches:
- main

jobs:
build-windows-debug:
Expand All @@ -27,4 +28,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: comfyui-electron-debug-win32-${{env.sha_short}}
path: out/make/zip/win32/x64/*.zip
path: out/make/zip/win32/x64/*.zip
2 changes: 1 addition & 1 deletion forge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const config: ForgeConfig = {
teamId: process.env.APPLE_TEAM_ID
},
},
extraResource: ['./assets/UI', './assets/ComfyUI', './assets/python.tgz'],
extraResource: ['./assets'],

},
rebuildConfig: {},
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"packageManager": "[email protected]",
"scripts": {
"clean": "rimraf .vite dist out",
"clean:assets": "rimraf assets/ComfyUI assets/python assets/override.txt assets/python.tgz || rimraf assets/cpython*.tar.gz || rimraf assets/requirements.*",
"clean:assets": "rimraf assets/.env assets/ComfyUI assets/python.tgz && yarn run clean:assets:dev",
"clean:assets:dev": "rimraf assets/python assets/override.txt | rimraf assets/cpython*.tar.gz | rimraf assets/requirements.*",
"clean:slate": "yarn run clean && yarn run clean:assets && rimraf node_modules",
"lint": "eslint --ext .ts,.tsx .",
"lint:fix": "eslint --fix --ext .ts,.tsx .",
"make": "electron-forge make",
"make:assets:amd": "cd assets && comfy-cli --skip-prompt --here install --fast-deps --amd && comfy-cli --here standalone",
"make:assets:cpu": "cd assets && comfy-cli --skip-prompt --here install --fast-deps --cpu && comfy-cli --here standalone",
"make:assets:cpu": "cd assets && comfy-cli --skip-prompt --here install --fast-deps --cpu && comfy-cli --here standalone && node ../scripts/env.mjs",
"make:assets:nvidia": "cd assets && comfy-cli --skip-prompt --here install --fast-deps --nvidia && comfy-cli --here standalone",
"notarize": "node debug/notarize.js",
"package": "electron-forge package",
Expand Down
6 changes: 0 additions & 6 deletions scripts/assets.sh

This file was deleted.

7 changes: 7 additions & 0 deletions scripts/env.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as fs from "node:fs/promises";

const envContent = `# env vars picked up by the ComfyUI executable on startup
COMFYUI_CPU_ONLY=true
`

fs.writeFile(".env", envContent);
82 changes: 32 additions & 50 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn, ChildProcess } from 'node:child_process';
import { access, mkdir, readdir, rm } from 'node:fs/promises';
import * as fs from 'node:fs/promises';
import net from 'node:net';
import path from 'node:path';

Expand Down Expand Up @@ -82,7 +82,9 @@ const maxFailWait: number = 10 * 2000; // 10seconds
let currentWaitTime: number = 0;
let spawnServerTimeout: NodeJS.Timeout = null;

const launchPythonServer = async () => {
const launchPythonServer = async (args: {userResourcesPath: string, appResourcesPath: string}) => {
const {userResourcesPath, appResourcesPath} = args;

const isServerRunning = await isPortInUse(host, port);
if (isServerRunning) {
console.log('Python server is already running');
Expand All @@ -100,37 +102,14 @@ const launchPythonServer = async () => {
console.log('Launching Python server...');

return new Promise<void>(async (resolve, reject) => {
const {userResourcesPath, appResourcesPath} = app.isPackaged ? {
// production: install python to per-user application data dir
userResourcesPath: app.getPath('appData'),
appResourcesPath: process.resourcesPath,
} : {
// development: install python to in-tree assets dir
userResourcesPath: path.join(app.getAppPath(), 'assets'),
appResourcesPath: path.join(app.getAppPath(), 'assets'),
}

try {
await mkdir(userResourcesPath);
} catch {
null;
}
console.log(`userResourcesPath: ${userResourcesPath}`);
console.log(`appResourcesPath: ${appResourcesPath}`);

const {pythonPath, scriptPath} = process.platform==='win32' ? {
pythonPath: path.join(userResourcesPath, 'python', 'python.exe'),
scriptPath: path.join(appResourcesPath, 'ComfyUI', 'main.py'),
} : {
pythonPath: path.join(userResourcesPath, 'python', 'bin', 'python'),
scriptPath: path.join(appResourcesPath, 'ComfyUI', 'main.py'),
};

console.log('Python Path:', pythonPath);
console.log('Script Path:', scriptPath);

access(pythonPath).then(async () => {
pythonProcess = spawn(pythonPath, [scriptPath], {
const pythonRootPath = path.join(userResourcesPath, 'python');
const pythonInterpreterPath = process.platform==='win32' ? path.join(pythonRootPath, 'python.exe') : path.join(pythonRootPath, 'bin', 'python');
const pythonRecordPath = path.join(pythonRootPath, "INSTALLER");
const scriptPath = path.join(appResourcesPath, 'ComfyUI', 'main.py');
const comfyMainCmd = [scriptPath, ...(process.env.COMFYUI_CPU_ONLY === "true" ? ["--cpu"] : [])];

const spawnPython = async () => {
pythonProcess = spawn(pythonInterpreterPath, comfyMainCmd, {
cwd: path.dirname(scriptPath)
});

Expand All @@ -140,37 +119,41 @@ const launchPythonServer = async () => {
pythonProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
}).catch(async () => {
}

try {
// check for existence of both interpreter and INSTALLER record to ensure a correctly installed python env
await Promise.all([fs.access(pythonInterpreterPath), fs.access(pythonRecordPath)]);
spawnPython();
} catch {
console.log('Running one-time python installation on first startup...');
// clean up any possible existing non-functional python env
try {
await fs.rm(pythonRootPath, {recursive: true});
} catch {null;}

const pythonTarPath = path.join(appResourcesPath, 'python.tgz');
await tar.extract({file: pythonTarPath, cwd: userResourcesPath, strict: true});

const pythonRootPath = path.join(userResourcesPath, 'python');
const wheelsPath = path.join(pythonRootPath, 'wheels');
const rehydrateCmd = ['-m', 'uv', 'pip', 'install', '--no-index', '--no-deps', ...(await readdir(wheelsPath)).map(x => path.join(wheelsPath, x))];
const rehydrateProc = spawn(pythonPath, rehydrateCmd, {cwd: wheelsPath});
const rehydrateCmd = ['-m', 'uv', 'pip', 'install', '--no-index', '--no-deps', ...(await fs.readdir(wheelsPath)).map(x => path.join(wheelsPath, x))];
const rehydrateProc = spawn(pythonInterpreterPath, rehydrateCmd, {cwd: wheelsPath});

rehydrateProc.on("exit", code => {
// write an INSTALLER record on sucessful completion of rehydration
fs.writeFile(pythonRecordPath, "ComfyUI");

if (code===0) {
// remove the now installed wheels
rm(wheelsPath, {recursive: true});
fs.rm(wheelsPath, {recursive: true});
console.log(`Python successfully installed to ${pythonRootPath}`);

pythonProcess = spawn(pythonPath, [scriptPath], {
cwd: path.dirname(scriptPath)
});

pythonProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
pythonProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
spawnPython();
} else {
console.log(`Rehydration of python bundle exited with code ${code}`);
}
});
});
}

const checkInterval = 1000; // Check every 1 second

Expand Down Expand Up @@ -205,7 +188,6 @@ const launchPythonServer = async () => {
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {

const {userResourcesPath, appResourcesPath} = app.isPackaged ? {
// production: install python to per-user application data dir
userResourcesPath: app.getPath('appData'),
Expand Down
11 changes: 0 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2657,17 +2657,6 @@ __metadata:
languageName: node
linkType: hard

"ds-store@npm:^0.1.5":
version: 0.1.6
resolution: "ds-store@npm:0.1.6"
dependencies:
bplist-creator: "npm:~0.0.3"
macos-alias: "npm:~0.2.5"
tn1150: "npm:^0.1.0"
checksum: 10c0/40293e925e27fa6df7f043320a2e277c5901368c49a7e420ecfc510dd843a6878b92875b126b841b53a70be7a6ab67338582b7f7ffd4ace3e581cf15aea0a1e2
languageName: node
linkType: hard

"eastasianwidth@npm:^0.2.0":
version: 0.2.0
resolution: "eastasianwidth@npm:0.2.0"
Expand Down

0 comments on commit 2e8300c

Please sign in to comment.