-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsole.php
184 lines (160 loc) · 6.58 KB
/
console.php
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
<?php
/**
*
*
*
*
*/
require 'vendor/autoload.php';
use Symfony\Component\Console\Application;
$console = new Application();
$config = require __DIR__ . '/config.php';
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
$convert = function ($size) {
$unit = array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size, 1024)))), 2).$unit[$i];
};
$console
->register('debug')
->setDefinition(array(
))
->setDescription('placeholder command to test code')
->setCode(function (InputInterface $input, OutputInterface $output) {
$client = new \Cotya\SignatureChainer\Client();
$client->processUrl(
'https://api.github.com/repos/Adyen/magento/zipball/b4377e4ff360eddb46f9f431214e3807fea542dd'
);
$output->writeln('command finished');
});
$console
->register('example')
->setDefinition(array(
new InputArgument('dir', InputArgument::REQUIRED, 'Directory name'),
))
->setDescription('Displays the files in the given directory')
->setCode(function (InputInterface $input, OutputInterface $output) {
$dir = $input->getArgument('dir');
$output->writeln(sprintf('Dir listing for <info>%s</info>', $dir));
});
$console
->register('process:csv')
->setDefinition(array(
new InputArgument('dir', InputArgument::REQUIRED, 'Directory to put the signature files into'),
new InputArgument('csvFile', InputArgument::REQUIRED, 'csvFile to import'),
new InputArgument('start', InputArgument::OPTIONAL, 'csvFile to import'),
new InputArgument('limit', InputArgument::OPTIONAL, 'csvFile to import'),
))
->setDescription('process')
->setCode(function (InputInterface $input, OutputInterface $output) use ($convert, $config) {
$time_start = microtime(true);
$dir = $input->getArgument('dir');
$csvFile = $input->getArgument('csvFile');
$start = $input->getArgument('start')?:0;
$limit = $input->getArgument('limit')?:2000;
$csvFileHandle = fopen($csvFile, "r");
$client = new \Cotya\SignatureChainer\Client($config['userAgent'], $config['githubApiToken']);
$storage = new \Cotya\SignatureChainer\Storage($dir.'/signatures');
//$limit = 5;
$count = 0;
while (($data = fgetcsv($csvFileHandle)) !== false) {
if ($count<$start) {
$count++;
continue;
}
if ($count>=$start+$limit) {
$output->writeln("Limit($limit) reached, started at $start and reached $count");
break;
}
try {
$signatureStruct = $client->processUrl($data[2]);
$signatureStruct->setPackageName($data[0]);
$signatureStruct->setPackageVersion($data[1]);
$storage->addEntry($signatureStruct, $config['storageKey']);
} catch (GuzzleHttp\Exception\ClientException $exception) {
echo PHP_EOL;
echo $exception->getMessage();
echo PHP_EOL;
if ($exception->getResponse()->getStatusCode() == 404) {
continue;
}
echo $exception->getResponse()->getBody();
echo PHP_EOL;
throw $exception;
}
$count++;
$output->write('.');
if ($count%100 == 0) {
$time_end = microtime(true);
$time = $time_end - $time_start;
$output->writeln("\n##$count## \tMemory Usage: ".
$convert(memory_get_usage(true)).'/'.$convert(memory_get_peak_usage(true)).
" Runtime: {$time}s");
}
//$limit--;
//if ($limit < 0) {
// $output->writeln('break');
// break;
//}
};
$output->writeln('command finished');
});
$console
->register('composer-repo:parse2csv')
->setDefinition(array(
new InputArgument('file', InputArgument::REQUIRED, 'file path name'),
new InputArgument('output', InputArgument::OPTIONAL, 'file path name'),
))
->setDescription('parses a composer packages file to a simpler csv list')
->setCode(function (InputInterface $input, OutputInterface $output) {
$file = $input->getArgument('file');
$outputFile = $input->getArgument('output');
$packages = json_decode(file_get_contents($file), true);
if ($outputFile) {
$csvFileHandle = fopen($outputFile, 'w');
} else {
$csvFileHandle = fopen(__DIR__.'/packages.csv', 'w');
};
foreach ($packages['packages'] as $packageName => $versionList) {
foreach ($versionList as $version => $package) {
if (!isset($package['dist']['url'])) {
continue;
}
if (!is_numeric(mb_substr($package['version'], 0, 1, 'utf-8'))
|| mb_stripos($package['version'], '-dev') !== false
) {
continue;
}
$url = $package['dist']['url'];
$entry = [
$packageName,
$package['version'],
$url,
];
fputcsv($csvFileHandle, $entry);
}
}
$output->writeln('command finished');
});
$console
->register('validate-package')
->setDefinition(array(
new InputArgument('dir', InputArgument::REQUIRED, 'Directory name'),
new InputArgument('name', InputArgument::REQUIRED, 'package name'),
new InputArgument('version', InputArgument::REQUIRED, 'package version'),
))
->setDescription('Displays the files in the given directory')
->setCode(function (InputInterface $input, OutputInterface $output) use ($config) {
$dir = $input->getArgument('dir');
$name = $input->getArgument('name');
$version = $input->getArgument('version');
$client = new \Cotya\SignatureChainer\Client($config['userAgent'], $config['githubApiToken']);
$storage = new \Cotya\SignatureChainer\Storage($dir.'/signatures');
$signatures = $storage->getSignaturesForPackageByNameAndVersion($name, $version);
$client->downloadPackageWithValidation($signatures[0]);
$output->writeln('command finished');
});
require __DIR__ . '/console/packagist.php';
$console->run();