Skip to content
Snippets Groups Projects
Commit 9ab27012 authored by Jared Hancock's avatar Jared Hancock
Browse files

Fixup Unpacker::unpackage()

- Force destination folder to always have a trailing slash
- Log if verbosity is set to stdout
- Use MD5 to see if copy is necessary
parent 962fb661
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,9 @@ class Unpacker extends Module { ...@@ -21,6 +21,9 @@ class Unpacker extends Module {
code in that folder. The folder will be automatically created if code in that folder. The folder will be automatically created if
it doesn't already exist." it doesn't already exist."
), ),
'verbose' => array('-v','--verbose', 'default'=>false, 'nargs'=>0,
'action'=>'store_true', 'help'=>
"Move verbose logging to stdout"),
); );
var $arguments = array( var $arguments = array(
...@@ -45,9 +48,9 @@ class Unpacker extends Module { ...@@ -45,9 +48,9 @@ class Unpacker extends Module {
# Read the main.inc.php script # Read the main.inc.php script
$main_inc_php = $this->destination . '/main.inc.php'; $main_inc_php = $this->destination . '/main.inc.php';
$lines = explode("\n", file_get_contents($main_inc_php)); $lines = explode("\n", file_get_contents($main_inc_php));
# Try and use ROOT_PATH # Try and use ROOT_DIR
if (strpos($include_path, $this->destination) === 0) if (strpos($include_path, $this->destination) === 0)
$include_path = "ROOT_PATH . '" . $include_path = "ROOT_DIR . '" .
str_replace($this->destination, '', $include_path) . "'"; str_replace($this->destination, '', $include_path) . "'";
else else
$include_path = "'$include_path'"; $include_path = "'$include_path'";
...@@ -78,14 +81,37 @@ class Unpacker extends Module { ...@@ -78,14 +81,37 @@ class Unpacker extends Module {
return false; return false;
} }
function unpackage($folder, $destination, $recurse=true, $exclude=false) { /**
* Copy from source to desination, perhaps recursing up to n folders.
* Exclusions are also permitted. If any files match an MD5 sum, they
* will be excluded from the copy operation.
*
* Parameters:
* folder - (string) source folder root
* destination - (string) destination folder root
* recurse - (int) recuse up to this many folders. Use 0 or false to
* disable recursion, and -1 to recurse infinite folders.
* exclude - (string | array<string>) patterns that will be matched
* using the PHP `fnmatch` function. If any file or folder matches,
* it will be excluded from the copy procedure. Omit or use false
* to disable exclusions
*/
function unpackage($folder, $destination, $recurse=0, $exclude=false) {
$verbose = $this->getOption('verbose');
if (substr($destination, -1) !== '/')
$destination .= '/';
foreach (glob($folder, GLOB_BRACE|GLOB_NOSORT) as $file) { foreach (glob($folder, GLOB_BRACE|GLOB_NOSORT) as $file) {
if ($this->exclude($exclude, $file)) if ($this->exclude($exclude, $file))
continue; continue;
if (is_file($file)) { if (is_file($file)) {
if (!is_dir($destination)) if (!is_dir($destination))
mkdir($destination, 0751, true); mkdir($destination, 0751, true);
copy($file, $destination . '/' . basename($file)); $target = $destination . basename($file);
if (is_file($target) && md5_file($target) == md5_file($file))
continue;
if ($verbose)
$this->stdout->write($target."\n");
copy($file, $target);
} }
} }
if ($recurse) { if ($recurse) {
...@@ -94,7 +120,7 @@ class Unpacker extends Module { ...@@ -94,7 +120,7 @@ class Unpacker extends Module {
continue; continue;
$this->unpackage( $this->unpackage(
dirname($folder).'/'.basename($dir).'/'.basename($folder), dirname($folder).'/'.basename($dir).'/'.basename($folder),
$destination.'/'.basename($dir), $destination.basename($dir),
$recurse - 1, $exclude); $recurse - 1, $exclude);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment