Skip to content

Commit

Permalink
feat: use pyman instead of shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
kauffinger committed Dec 22, 2024
1 parent 41d2189 commit c596cf2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"require": {
"php": "^8.3",
"illuminate/contracts": "^10.0||^11.0",
"kauffinger/pyman": "^0.0.1",
"spatie/laravel-package-tools": "^1.16",
"spatie/temporary-directory": "^2.2"
},
Expand Down Expand Up @@ -49,7 +50,7 @@
"scripts": {
"post-autoload-dump": "@composer run prepare",
"setup-python-env": [
"./setup-python-env.sh"
"@php vendor/bin/testbench markitdown:install"
],
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"analyse": "vendor/bin/phpstan analyse",
Expand Down
40 changes: 15 additions & 25 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Innobrain\Markitdown\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;
use Illuminate\Process\Factory;
use Kauffinger\Pyman\Exceptions\PymanException;
use Kauffinger\Pyman\PythonEnvironmentManager;

class InstallCommand extends Command
{
Expand Down Expand Up @@ -54,7 +56,7 @@ public function handle(): int
/** @var array<string, array<string, mixed>> $composer */

// Add our script to the project's composer.json scripts
$scriptPath = './vendor/innobrain/markitdown/setup-python-env.sh';
$script = '@php artisan markitdown:install';
$scriptAdded = false;

// Add to post-autoload-dump
Expand All @@ -69,8 +71,8 @@ public function handle(): int
$composer['scripts']['post-autoload-dump'] = [$composer['scripts']['post-autoload-dump']];
}

if (! in_array($scriptPath, $composer['scripts']['post-autoload-dump'], true)) {
$composer['scripts']['post-autoload-dump'][] = $scriptPath;
if (! in_array($script, $composer['scripts']['post-autoload-dump'], true)) {
$composer['scripts']['post-autoload-dump'][] = $script;
$scriptAdded = true;
}

Expand Down Expand Up @@ -101,33 +103,21 @@ public function handle(): int
$this->components->info('Added Markitdown setup script to composer.json');
}

// Run the setup script
$scriptPath = realpath(__DIR__.'/../../setup-python-env.sh');
$pythonPath = realpath(__DIR__.'/../../python');

if ($scriptPath === false || ! file_exists($scriptPath)) {
$this->components->error('Setup script not found.');
if ($pythonPath === false) {
$this->components->error('Python virtual environment not found.');

return self::FAILURE;
}

$this->components->info('Setting up Python virtual environment...');
$this->components->info('Installing or updating python environment...');
$pythonEnvironmentManager = new PythonEnvironmentManager($pythonPath, app(Factory::class));

// Make the script executable
if (! chmod($scriptPath, 0755)) {
$this->components->error('Failed to make setup script executable.');

return self::FAILURE;
}

// Run the setup script
$pendingProcess = Process::path(dirname($scriptPath))
->tty(false)
->timeout(300);

$processResult = $pendingProcess->run($scriptPath);

if (! $processResult->successful()) {
$this->components->error('Failed to set up Python virtual environment: '.$processResult->errorOutput());
try {
$pythonEnvironmentManager->setup();
} catch (PymanException $pymanException) {
$this->components->error($pymanException->getMessage());

return self::FAILURE;
}
Expand Down

0 comments on commit c596cf2

Please sign in to comment.