Skip to content

Commit

Permalink
added BinaryPython
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Oct 15, 2016
1 parent 452c33f commit 6c78455
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/base/BinaryPython.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* Automation tool mixed with code generator for easier continuous development
*
* @link https://github.com/hiqdev/hidev
* @package hidev
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
*/

namespace hidev\base;

use Yii;

class BinaryPython extends Binary
{
/**
* @var string package full name, e.g. pytest
*/
public $package;

/**
* @var string package version constraint, e.g. ^1.1
*/
public $version;

/**
* @var string installer URL
*/
public $installer;

/**
* @var string URL to download PHAR
*/
public $download;

/**
* Detects how to run the binary.
* Searches in this order:
* 1. exexcutable in project's root directory
* 2. XXX ??? vendor directory
* 3. /home/user/.local/bin
*
* @param string $name
* @return string path to the binary
*/
public function detectPath($name)
{
$paths = [
Yii::getAlias("@root/$name", false),
Yii::getAlias("@root/env/bin/$name", false),
"$_SERVER[HOME]/.local/bin/$name",
];
foreach ($paths as $path) {
if (file_exists($path)) {
return $path;
}
}

return parent::detectPath($name);
}

/**
* Detect command execution string.
* @param mixed $path
* @return string
*/
public function detectCommand($path)
{
$path = parent::detectCommand($path);

return is_executable($path) ? $path : '/usr/bin/env python ' . $path;
}

public function install()
{
if ($this->installer) {
passthru('/usr/bin/env wget ' . escapeshellarg($this->installer) . ' -O- | /usr/bin/env python', $exitcode);
} elseif ($this->download) {
$dest = Yii::getAlias('@root/' . $this->name, false);
passthru('/usr/bin/env wget ' . escapeshellarg($this->download) . ' -O ' . $dest, $exitcode);
} else {
passthru("/usr/bin/pip install {$this->package}", $exitcode);
}

return $exitcode;
}
}

0 comments on commit 6c78455

Please sign in to comment.