1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Automation tool mixed with code generator for easier continuous development |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hidev |
7
|
|
|
* @package hidev |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hidev\base; |
13
|
|
|
|
14
|
|
|
use Yii; |
15
|
|
|
|
16
|
|
View Code Duplication |
class BinaryPython extends Binary |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string package full name, e.g. pytest |
20
|
|
|
*/ |
21
|
|
|
public $package; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string package version constraint, e.g. ^1.1 |
25
|
|
|
*/ |
26
|
|
|
public $version; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string installer URL |
30
|
|
|
*/ |
31
|
|
|
public $installer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string URL to download PHAR |
35
|
|
|
*/ |
36
|
|
|
public $download; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Detects how to run the binary. |
40
|
|
|
* Searches in this order: |
41
|
|
|
* 1. exexcutable in project's root directory |
42
|
|
|
* 2. XXX ??? vendor directory |
43
|
|
|
* 3. /home/user/.local/bin |
44
|
|
|
* |
45
|
|
|
* @param string $name |
46
|
|
|
* @return string path to the binary |
47
|
|
|
*/ |
48
|
|
|
public function detectPath($name) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$paths = [ |
51
|
|
|
Yii::getAlias("@root/$name", false), |
52
|
|
|
Yii::getAlias("@root/env/bin/$name", false), |
53
|
|
|
"$_SERVER[HOME]/.local/bin/$name", |
54
|
|
|
]; |
55
|
|
|
foreach ($paths as $path) { |
56
|
|
|
if (file_exists($path)) { |
57
|
|
|
return $path; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return parent::detectPath($name); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Detect command execution string. |
66
|
|
|
* @param mixed $path |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function detectCommand($path) |
70
|
|
|
{ |
71
|
|
|
$path = parent::detectCommand($path); |
72
|
|
|
|
73
|
|
|
return is_executable($path) ? $path : '/usr/bin/env python ' . $path; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function install() |
77
|
|
|
{ |
78
|
|
|
if ($this->installer) { |
79
|
|
|
passthru('/usr/bin/env wget ' . escapeshellarg($this->installer) . ' -O- | /usr/bin/env python', $exitcode); |
80
|
|
|
} elseif ($this->download) { |
81
|
|
|
$dest = Yii::getAlias('@root/' . $this->name, false); |
82
|
|
|
passthru('/usr/bin/env wget ' . escapeshellarg($this->download) . ' -O ' . $dest, $exitcode); |
83
|
|
|
} else { |
84
|
|
|
passthru("/usr/bin/pip install {$this->package}", $exitcode); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $exitcode; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.