Permalink
Browse files

Add progress bar

  • Loading branch information...
1 parent d04d9b0 commit f3c6bed86313c5c92cea2e274343bb8154cb1fb2 @mousavian mousavian committed Jan 26, 2016
Showing with 111 additions and 43 deletions.
  1. +1 −0 composer.json
  2. +49 −3 composer.lock
  3. +61 −40 parallel.php
View
@@ -10,6 +10,7 @@
],
"require": {
"spindle/httpclient": "1.*",
+ "guiguiboy/php-cli-progress-bar": "dev-master",
"php": ">=5.4",
"ext-json": "*",
"ext-hash": "*",
View
@@ -4,10 +4,54 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "b85d6f76b08d95ed2feb2e2cbc1d50c2",
- "content-hash": "c82d4030f1ae6e5946b50d6c7a1b32ed",
+ "hash": "2e937a4f120a0b748759af9dd8377cf2",
+ "content-hash": "325314e6a447f35b59eaeff8d1bca3c5",
"packages": [
{
+ "name": "guiguiboy/php-cli-progress-bar",
+ "version": "dev-master",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/guiguiboy/PHP-CLI-Progress-Bar.git",
+ "reference": "7d3eb61c1f0c164b9c3139af694b2d38171e4d04"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/guiguiboy/PHP-CLI-Progress-Bar/zipball/7d3eb61c1f0c164b9c3139af694b2d38171e4d04",
+ "reference": "7d3eb61c1f0c164b9c3139af694b2d38171e4d04",
+ "shasum": ""
+ },
+ "require": {
+ "ext-mbstring": "*",
+ "php": ">=5.3.0"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-0": {
+ "ProgressBar": "."
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Guillaume",
+ "email": "guillaume.bretou@gmail.com"
+ }
+ ],
+ "description": "Progress bar for PHP CLI scripts",
+ "homepage": "https://github.com/guiguiboy/PHP-CLI-Progress-Bar",
+ "keywords": [
+ "bar",
+ "cli",
+ "command-line",
+ "progress"
+ ],
+ "time": "2014-11-19 13:12:00"
+ },
+ {
"name": "spindle/httpclient",
"version": "1.0.1",
"source": {
@@ -55,7 +99,9 @@
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
- "stability-flags": [],
+ "stability-flags": {
+ "guiguiboy/php-cli-progress-bar": 20
+ },
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
View
@@ -1,6 +1,7 @@
<?php
namespace Spindle\HttpClient;
+use ProgressBar\Manager as ProgressBarManager;
use hirak\PackagistCrawler\ExpiredFileManager;
set_time_limit(0);
@@ -82,35 +83,39 @@ function downloadProviders($config, $globals)
$providers = [];
+ $numberOfProviders = count( (array)$packages->{'provider-includes'} );
+ $progressBar = new ProgressBarManager(0, $numberOfProviders);
+ $progressBar->setFormat('Downloading Providers: %current%/%max% [%bar%] %percent%%');
+
foreach ($packages->{'provider-includes'} as $tpl => $version) {
$fileurl = str_replace('%hash%', $version->sha256, $tpl);
$cachename = $cachedir . $fileurl;
$providers[] = $cachename;
- if (file_exists($cachename)) continue;
-
- $req->setOption('url', $config->packagistUrl . '/' . $fileurl);
- $res = $req->send();
-
- error_log($res->getStatusCode(). "\t". $res->getUrl());
- if (200 !== $res->getStatusCode()) {
- $globals->retry = true;
- continue;
- }
+ if (!file_exists($cachename)){
+ $req->setOption('url', $config->packagistUrl . '/' . $fileurl);
+ $res = $req->send();
- $oldcache = $cachedir . str_replace('%hash%', '*', $tpl);
- if ($glob = glob($oldcache)) {
- foreach ($glob as $old) {
- $globals->expiredManager->add($old, time());
+ if (200 === $res->getStatusCode()) {
+ $oldcache = $cachedir . str_replace('%hash%', '*', $tpl);
+ if ($glob = glob($oldcache)) {
+ foreach ($glob as $old) {
+ $globals->expiredManager->add($old, time());
+ }
+ }
+ if (!file_exists(dirname($cachename))) {
+ mkdir(dirname($cachename), 0777, true);
+ }
+ file_put_contents($cachename, $res->getBody());
+ if ($config->generateGz) {
+ file_put_contents($cachename . '.gz', gzencode($res->getBody()));
+ }
+ } else {
+ $globals->retry = true;
}
}
- if (!file_exists(dirname($cachename))) {
- mkdir(dirname($cachename), 0777, true);
- }
- file_put_contents($cachename, $res->getBody());
- if ($config->generateGz) {
- file_put_contents($cachename . '.gz', gzencode($res->getBody()));
- }
+
+ $progressBar->advance();
}
return $providers;
@@ -123,7 +128,8 @@ function downloadProviders($config, $globals)
function downloadPackages($config, $globals, $providers)
{
$cachedir = $config->cachedir;
- $i = 0;
+ $i = 1;
+ $numberOfProviders = count($providers);
$urls = [];
foreach ($providers as $providerjson) {
@@ -133,8 +139,13 @@ function downloadPackages($config, $globals, $providers)
$list = $list->providers;
$all = count((array)$list);
+ $progressBar = new ProgressBarManager(0, $all);
+ echo " - Provider {$i}/{$numberOfProviders}:\n";
+ $progressBar->setFormat(" - Package: %current%/%max% [%bar%] %percent%%");
+
$sum = 0;
foreach ($list as $packageName => $provider) {
+ $progressBar->advance();
++$sum;
$url = "$config->packagistUrl/p/$packageName\$$provider->sha256.json";
$cachefile = $cachedir . str_replace("$config->packagistUrl/", '', $url);
@@ -152,7 +163,6 @@ function downloadPackages($config, $globals, $providers)
do {
$requests = $globals->mh->getFinishedResponses(); //block
} while (0 === count($requests));
- //error_log("downloaded: $sum / $all");
foreach ($requests as $req) {
$res = $req->getResponse();
@@ -181,35 +191,42 @@ function downloadPackages($config, $globals, $providers)
}
}
}
+
+ ++$i;
}
if (0 === count($globals->mh)) return;
//残りの端数をダウンロード
$globals->mh->waitResponse();
+
+ $progressBar = new ProgressBarManager(0, count($globals->mh));
+ $progressBar->setFormat(" - Remianed packages: %current%/%max% [%bar%] %percent%%");
+
foreach ($globals->mh as $req) {
$res = $req->getResponse();
- if (200 !== $res->getStatusCode()) {
- error_log($res->getStatusCode(). "\t". $res->getUrl());
+ if (200 === $res->getStatusCode()) {
+ $cachefile = $cachedir
+ . str_replace("$config->packagistUrl/", '', $res->getUrl());
+ if ($glob = glob("{$cachedir}p/$req->packageName\$*")) {
+ foreach ($glob as $old) {
+ $globals->expiredManager->add($old, time());
+ }
+ }
+ if (!file_exists(dirname($cachefile))) {
+ mkdir(dirname($cachefile), 0777, true);
+ }
+ file_put_contents($cachefile, $res->getBody());
+ if ($config->generateGz) {
+ file_put_contents($cachefile . '.gz', gzencode($res->getBody()));
+ }
+
+ } else {
$globals->retry = true;
- continue;
}
- $cachefile = $cachedir
- . str_replace("$config->packagistUrl/", '', $res->getUrl());
- if ($glob = glob("{$cachedir}p/$req->packageName\$*")) {
- foreach ($glob as $old) {
- $globals->expiredManager->add($old, time());
- }
- }
- if (!file_exists(dirname($cachefile))) {
- mkdir(dirname($cachefile), 0777, true);
- }
- file_put_contents($cachefile, $res->getBody());
- if ($config->generateGz) {
- file_put_contents($cachefile . '.gz', gzencode($res->getBody()));
- }
+ $progressBar->advance();
}
}
@@ -262,12 +279,16 @@ function clearExpiredFiles(ExpiredFileManager $expiredManager)
{
$expiredFiles = $expiredManager->getExpiredFileList();
+ $progressBar = new ProgressBarManager(0, count($expiredFiles));
+ $progressBar->setFormat(" - Clearing Expired Files: %current%/%max% [%bar%] %percent%%");
+
foreach ($expiredFiles as $file) {
if (file_exists($file)) {
unlink($file) and $expiredManager->delete($file);
} else {
$expiredManager->delete($file);
}
+ $progressBar->advance();
}
}

0 comments on commit f3c6bed

Please sign in to comment.