-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensorFlowBinaryInstaller.php
More file actions
250 lines (219 loc) · 7.13 KB
/
Copy pathTensorFlowBinaryInstaller.php
File metadata and controls
250 lines (219 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Installer\InstallationManager;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\Repository\WritableRepositoryInterface;
use Composer\Script\Event;
use Composer\Package\Package;
class TensorFlowBinaryInstaller
{
/**
* @var Composer $composer
*/
public static $composer;
/**
* @var IOInterface $io
*/
public static $io;
/**
* @var InstallationManager $installer
*/
public static $installer;
/**
* @var WritableRepositoryInterface $localRepo
*/
public static $localRepo;
/**
* @var array $config
*/
public static $config;
/**
* @var Package[] platform-specific packages to install
*/
public static $toInstall = array();
public static function init(Event $event)
{
self::$composer = $event->getComposer();
self::$io = $event->getIO();
self::$installer = $event->getComposer()->getInstallationManager();
self::$localRepo = $event->getComposer()->getRepositoryManager()->getLocalRepository();
$fileName = __DIR__ . '/composer-platform-specific.json';
if (!file_exists($fileName)) {
self::$io->write("<error>File $fileName not exists.</error>");
return false;
}
$content = file_get_contents($fileName);
if (!$content) {
self::$io->write("<error>Can't read $fileName file.</error>");
return false;
}
self::$config = (array)json_decode($content, true);
if (!isset(self::$config['extra']['platform-specific-packages'])) {
return false;
}
//@TODO: refactor it all to use composer.lock file, to track updated platform-specific packages
self::$toInstall = array();
$unresolved = array();
foreach (self::$config['extra']['platform-specific-packages'] as $name => $variants) {
$package = self::createPlatformSpecificPackage($name, $variants);
if ($package) {
self::$toInstall[] = $package;
} else {
$unresolved[] = $name;
}
}
if (!empty($unresolved)) {
self::$io->write(
'<error>Your requirements could not be resolved for current OS and/or processor architecture.</error>'
);
self::$io->write("\n Unresolved platform-specific packages:");
foreach ($unresolved as $name) {
self::$io->write(" - $name");
}
}
return true;
}
public static function install(Event $event)
{
if (!self::init($event)) {
return;
}
$notInstalled = 0;
if (!empty(self::$toInstall)) {
self::$io->write('<info>Installing platform-specific dependencies</info>');
foreach (self::$toInstall as $package) {
if (!self::$installer->isPackageInstalled(self::$localRepo, $package)) {
self::$installer->install(self::$localRepo, new InstallOperation($package));
self::updateBinary($package);
} else {
$notInstalled++;
}
}
}
if (empty(self::$toInstall) || $notInstalled > 0) {
self::$io->write('Nothing to install or update in platform-specific dependencies');
}
}
public static function update(Event $event)
{
//@TODO: update changed packages
self::install($event);
}
public static function updateBinary(Package $package)
{
$binaries = $package->getBinaries();
if (isset($binaries[0]) && self::getOS() !== 'windows') {
$binDir = rtrim(self::$composer->getConfig()->get('bin-dir'), '/') . '/';
@chmod($binDir . 'label_image', 0555);
}
}
/**
* @param string $packageName
* @param array $variants
* @return null|Package
*/
protected static function createPlatformSpecificPackage($packageName, $variants)
{
foreach ($variants as $variant) {
if (!empty($variant['architecture']) && $variant['architecture'] !== self::getArchitecture()) {
continue;
}
if (!empty($variant['os']) && $variant['os'] !== self::getOS()) {
continue;
}
reset($variant);
$name = key($variant);
$version = $variant[$name];
return self::createPackage($name, $version, $packageName);
}
return null;
}
/**
* @param string $name
* @param string $version
* @param string $newName
* @return null|Package
*/
protected static function createPackage($name, $version, $newName)
{
if (!isset(self::$config['repositories'])) {
return null;
}
$package = null;
foreach (self::$config['repositories'] as $cursor) {
if (isset($cursor['package']['name'], $cursor['package']['version']) &&
$cursor['package']['name'] === $name &&
($version === '*' || $cursor['package']['version'] === $version)
) {
$package = $cursor['package'];
break;
}
}
if (!$package) {
return null;
}
$new = self::bindPackageValues($newName, $package);
self::$localRepo->addPackage($new);
return $new;
}
/**
* @param string $newName
* @param array $package
* @return Package
*/
protected static function bindPackageValues($newName, array $package)
{
$new = new Package($newName, $package['version'], $package['version']);
$new->setType('dist');
if (isset($package['bin'])) {
$new->setBinaries($package['bin']);
}
if (isset($package['dist']['type'])) {
$new->setDistType($package['dist']['type']);
}
if (isset($package['dist']['url'])) {
$new->setDistUrl($package['dist']['url']);
}
if (isset($package['excludes'])) {
$new->setArchiveExcludes($package['excludes']);
}
return $new;
}
/**
* Returns the Operating System.
*
* @return string OS, e.g. macosx, freebsd, windows, linux.
*/
public static function getOS()
{
$uname = strtolower(php_uname());
if (strpos($uname, "darwin") !== false) {
return 'macosx';
} elseif (strpos($uname, "win") !== false) {
return 'windows';
} elseif (strpos($uname, "freebsd") !== false) {
return 'freebsd';
} elseif (strpos($uname, "linux") !== false) {
return 'linux';
} else {
return 'undefined';
}
}
/**
* Returns the Architecture.
*
* @return string BitSize, e.g. i386, x64.
*/
public static function getArchitecture()
{
switch (PHP_INT_SIZE) {
case 4:
return 'i386';
case 8:
return 'x64';
default:
return 'undefined';
}
}
}