From 92e8245876cda06ca023c9275582b061da32d68b Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Wed, 2 Mar 2011 13:34:28 +0100 Subject: [PATCH 1/4] drush_make.download.inc: cleanup _drush_make_download_file_move() X-Face: z*RaLf`X<@C75u6Ig9}{oW$H;1_\2t5)({*|jhM/Vb;]yA5\I~93>J<_`<4)A{':UrE Make _drush_make_download_file_move() more readable grouping common operations, and bring out some tasks which are not its responsibility. Details: * Don't remove temporary stuff in _drush_make_download_file_move(). As a general rule I like removing stuff in the function that created it, this is more symmetric, and helps handling error paths properly. For example, this also helped to spot and fix an issue in drush_make_download_file(): when the checksum verification failed, $filename was not deleted in the exit code path because _file_unpack (and hence _file_move, were removal should be supposed to happen) was never called; * Use 'rm -f' so we don't need to check for file existence before removing; * Now that $filename is not removed anymore by _drush_make_download() we can change its signature to a more logical form with just source and destination parameters; * In _drush_make_download_file_move() just adjust $tmp_path for the case where there is only a main directory to move, the actual "mv" will be performed outside of the if as the difference about the "type of move" to perform (one main dir, opposed to multiple dirs) is now taken care by $tmp_path itself. Signed-off-by: Antonio Ospite --- drush_make.download.inc | 35 +++++++++++++++-------------------- 1 files changed, 15 insertions(+), 20 deletions(-) diff --git a/drush_make.download.inc b/drush_make.download.inc index 83d4876..228b3cc 100644 --- a/drush_make.download.inc +++ b/drush_make.download.inc @@ -65,11 +65,14 @@ function drush_make_download_file($name, $download, $download_location) { if ($filename = _drush_make_download_file($download)) { if (isset($download['md5'])) { if (_drush_make_verify_checksum($filename, $download['md5']) === FALSE) { + drush_shell_exec('rm -f %s', $filename); return FALSE; } } drush_log(dt('%project downloaded from %url.', array('%project' => $name, '%url' => $download['url'])), 'ok'); - return drush_make_download_file_unpack($filename, $download_location, (isset($download['filename']) ? $download['filename'] : '')); + $ret = drush_make_download_file_unpack($filename, $download_location, (isset($download['filename']) ? $download['filename'] : '')); + drush_shell_exec('rm -f %s', $filename); + return $ret; } drush_make_error('DOWNLOAD_ERROR', dt('Unable to download %project from %url.', array('%project' => $name, '%url' => $download['url']))); return FALSE; @@ -263,7 +266,8 @@ function drush_make_download_file_unpack_tar($filename, $download_location) { drush_make_mkdir($tmp_path . '/__unzip__'); drush_shell_exec('tar -x -C %s -f %s', $tmp_path . '/__unzip__', $filename); - _drush_make_download_file_move($tmp_path, $filename, $download_location); + _drush_make_download_file_move($tmp_path, $download_location); + drush_shell_exec('rm -rf %s', $tmp_path . '/__unzip__'); } function drush_make_download_file_unpack_gzip($filename, $download_location) { @@ -297,34 +301,25 @@ function drush_make_download_file_unpack_zip($filename, $download_location) { drush_make_mkdir($tmp_path . '/__unzip__'); drush_shell_exec("unzip %s -d %s", $filename, $tmp_path . '/__unzip__'); - _drush_make_download_file_move($tmp_path, $filename, $download_location); + _drush_make_download_file_move($tmp_path, $download_location); + drush_shell_exec('rm -rf %s', $tmp_path . '/__unzip__'); } -function _drush_make_download_file_move($tmp_path, $filename, $download_location) { - drush_shell_exec('ls %s', $tmp_path . '/__unzip__'); +function _drush_make_download_file_move($src_path, $download_location) { + $tmp_path = $src_path . '/__unzip__'; + drush_shell_exec('ls %s', $tmp_path); $lines = drush_shell_exec_output(); $main_directory = basename($download_location); if (count($lines) == 1) { $directory = array_shift($lines); if ($directory != $main_directory) { - drush_shell_exec('mv %s %s', $tmp_path . '/__unzip__/' . $directory, $tmp_path . '/__unzip__/' . $main_directory); + drush_shell_exec('mv %s %s', $tmp_path . '/' . $directory, $tmp_path . '/' . $main_directory); } - drush_shell_exec('cp -Rf %s %s', $tmp_path . '/__unzip__/' . $main_directory, dirname($download_location)); - drush_shell_exec('rm -rf %s', $tmp_path . '/__unzip__'); - } - elseif (count($lines) > 1) { - drush_shell_exec('rm -rf %s', $download_location); - drush_shell_exec('mv %s %s', $tmp_path . '/__unzip__', $download_location); - } - - // Remove the tarball. - if (file_exists($filename)) { - drush_shell_exec('rm %s', $filename); + $tmp_path = $tmp_path . '/' . $main_directory; } - if (file_exists($tmp_path . '/__unzip__')) { - drush_shell_exec('rm -rf %s', $tmp_path . '/__unzip__'); - } + drush_shell_exec('rm -rf %s', $download_location); + drush_shell_exec('mv %s %s', $tmp_path, $download_location); } -- 1.7.4.1 From bed7dfe8f1fc17f740256837fa95f4a3b976359f Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Thu, 3 Mar 2011 12:14:12 +0100 Subject: [PATCH 2/4] drush_make.download.inc: cleanup the use of '/__unzip__' suffix X-Face: z*RaLf`X<@C75u6Ig9}{oW$H;1_\2t5)({*|jhM/Vb;]yA5\I~93>J<_`<4)A{':UrE Move the handling of the '/__unzip__' suffix out from _drush_make_download_file_move(), as this does not really belong there; this change makes the function more generic and reusable in other scenarios. As a bonus, now we can also use the more logical '/__untar__' suffix when unpacking tar archives, this is merely cosmetic, but still nice. Signed-off-by: Antonio Ospite --- drush_make.download.inc | 18 +++++++++--------- 1 files changed, 9 insertions(+), 9 deletions(-) diff --git a/drush_make.download.inc b/drush_make.download.inc index 228b3cc..68e0afa 100644 --- a/drush_make.download.inc +++ b/drush_make.download.inc @@ -259,15 +259,15 @@ function drush_make_download_file_unpack($filename, $download_location, $name) { } function drush_make_download_file_unpack_tar($filename, $download_location) { - $tmp_path = drush_make_tmp(); + $tmp_path = drush_make_tmp() . '/__untar__'; list($main_directory) = array_reverse(explode('/', $download_location)); - drush_make_mkdir($tmp_path . '/__unzip__'); - drush_shell_exec('tar -x -C %s -f %s', $tmp_path . '/__unzip__', $filename); + drush_make_mkdir($tmp_path); + drush_shell_exec('tar -x -C %s -f %s', $tmp_path, $filename); _drush_make_download_file_move($tmp_path, $download_location); - drush_shell_exec('rm -rf %s', $tmp_path . '/__unzip__'); + drush_shell_exec('rm -rf %s', $tmp_path); } function drush_make_download_file_unpack_gzip($filename, $download_location) { @@ -294,19 +294,19 @@ function drush_make_download_file_unpack_gzip($filename, $download_location) { } function drush_make_download_file_unpack_zip($filename, $download_location) { - $tmp_path = drush_make_tmp(); + $tmp_path = drush_make_tmp() . '/__unzip__'; list($main_directory) = array_reverse(explode('/', $download_location)); - drush_make_mkdir($tmp_path . '/__unzip__'); - drush_shell_exec("unzip %s -d %s", $filename, $tmp_path . '/__unzip__'); + drush_make_mkdir($tmp_path); + drush_shell_exec("unzip %s -d %s", $filename, $tmp_path); _drush_make_download_file_move($tmp_path, $download_location); - drush_shell_exec('rm -rf %s', $tmp_path . '/__unzip__'); + drush_shell_exec('rm -rf %s', $tmp_path); } function _drush_make_download_file_move($src_path, $download_location) { - $tmp_path = $src_path . '/__unzip__'; + $tmp_path = $src_path; drush_shell_exec('ls %s', $tmp_path); $lines = drush_shell_exec_output(); $main_directory = basename($download_location); -- 1.7.4.1 From 6518019ad7df620f4ff33e9ec079fe213fbd0c1e Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Thu, 3 Mar 2011 12:20:10 +0100 Subject: [PATCH 3/4] drush_make.download.inc: Remove unused $main_directory variables X-Face: z*RaLf`X<@C75u6Ig9}{oW$H;1_\2t5)({*|jhM/Vb;]yA5\I~93>J<_`<4)A{':UrE Signed-off-by: Antonio Ospite --- drush_make.download.inc | 4 ---- 1 files changed, 0 insertions(+), 4 deletions(-) diff --git a/drush_make.download.inc b/drush_make.download.inc index 68e0afa..b05becc 100644 --- a/drush_make.download.inc +++ b/drush_make.download.inc @@ -261,8 +261,6 @@ function drush_make_download_file_unpack($filename, $download_location, $name) { function drush_make_download_file_unpack_tar($filename, $download_location) { $tmp_path = drush_make_tmp() . '/__untar__'; - list($main_directory) = array_reverse(explode('/', $download_location)); - drush_make_mkdir($tmp_path); drush_shell_exec('tar -x -C %s -f %s', $tmp_path, $filename); @@ -296,8 +294,6 @@ function drush_make_download_file_unpack_gzip($filename, $download_location) { function drush_make_download_file_unpack_zip($filename, $download_location) { $tmp_path = drush_make_tmp() . '/__unzip__'; - list($main_directory) = array_reverse(explode('/', $download_location)); - drush_make_mkdir($tmp_path); drush_shell_exec("unzip %s -d %s", $filename, $tmp_path); -- 1.7.4.1 From 17e3d0eaf82d2a280e72741005e09b6263926dd3 Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Thu, 3 Mar 2011 12:38:08 +0100 Subject: [PATCH 4/4] Issue #919224 by Frando, ao2: drush_make.download.inc: Implement the [download][subtree] option X-Face: z*RaLf`X<@C75u6Ig9}{oW$H;1_\2t5)({*|jhM/Vb;]yA5\I~93>J<_`<4)A{':UrE Some modules require you to copy not the whole archive contents, but just a subtree from within the archive to the module/library directory. For example the HTMLPurifier module requires you to copy only the content of the 'library' subdirectory of the upstream archive, in order to get this layout: sites/all/libraries/htmlpurifier/ |-- HTMLPurifier/ |-- HTMLPurifier.autoload.php |-- HTMLPurifier.auto.php |-- HTMLPurifier.func.php |-- HTMLPurifier.includes.php |-- HTMLPurifier.kses.php |-- HTMLPurifier.path.php |-- HTMLPurifier.php `-- HTMLPurifier.safe-includes.php this can be obtained with this snippet in the .make file: ; Libraries libraries[htmlpurifier][download][type] = file libraries[htmlpurifier][download][url] = http://htmlpurifier.org/releases/htmlpurifier-4.2.0-lite.tar.gz libraries[htmlpurifier][download][subtree] = htmlpurifier-4.2.0-lite/library Signed-off-by: Antonio Ospite --- README.txt | 3 +++ drush_make.download.inc | 30 ++++++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.txt b/README.txt index 6ff108a..cf3ff2b 100644 --- a/README.txt +++ b/README.txt @@ -231,6 +231,9 @@ Do not use both types of declarations for a single project in your makefile. valid URL query string. Required. `filename` - What to name the file, if it's not an archive. Optional. + + `subtree` - if the download is an archive, only this subtree within the + archive will be copied to the target destination. Optional. - `download[type] = bzr` diff --git a/drush_make.download.inc b/drush_make.download.inc index b05becc..2f39b4d 100644 --- a/drush_make.download.inc +++ b/drush_make.download.inc @@ -70,7 +70,9 @@ function drush_make_download_file($name, $download, $download_location) { } } drush_log(dt('%project downloaded from %url.', array('%project' => $name, '%url' => $download['url'])), 'ok'); - $ret = drush_make_download_file_unpack($filename, $download_location, (isset($download['filename']) ? $download['filename'] : '')); + $dl_fname = isset($download['filename']) ? $download['filename'] : ''; + $subtree = isset($download['subtree']) ? $download['subtree'] : NULL; + $ret = drush_make_download_file_unpack($filename, $download_location, $dl_fname, $subtree); drush_shell_exec('rm -f %s', $filename); return $ret; } @@ -239,36 +241,38 @@ function _drush_make_download_file($download) { return FALSE; } -function drush_make_download_file_unpack($filename, $download_location, $name) { +function drush_make_download_file_unpack($filename, $download_location, $name, $subtree = NULL) { $extension = array_pop(explode('.', $filename)); switch ($extension) { case 'gz': case 'tgz': // I'd like to just use tar -z, but apparently it breaks on windoze. Why do they always have to ruin everything? - drush_make_download_file_unpack_gzip($filename, $download_location); + drush_make_download_file_unpack_gzip($filename, $download_location, $subtree); break; case 'tar': - drush_make_download_file_unpack_tar($filename, $download_location); + drush_make_download_file_unpack_tar($filename, $download_location, $subtree); break; case 'zip': - drush_make_download_file_unpack_zip($filename, $download_location); + drush_make_download_file_unpack_zip($filename, $download_location, $subtree); break; default: drush_shell_exec('mv %s %s', $filename, $download_location . ($name ? '/' . $name : '')); } } -function drush_make_download_file_unpack_tar($filename, $download_location) { +function drush_make_download_file_unpack_tar($filename, $download_location, $subtree = NULL) { $tmp_path = drush_make_tmp() . '/__untar__'; drush_make_mkdir($tmp_path); drush_shell_exec('tar -x -C %s -f %s', $tmp_path, $filename); - - _drush_make_download_file_move($tmp_path, $download_location); + + $src_path = isset($subtree) ? $tmp_path . '/' . $subtree : $tmp_path; + _drush_make_download_file_move($src_path, $download_location); + drush_shell_exec('rm -rf %s', $tmp_path); } -function drush_make_download_file_unpack_gzip($filename, $download_location) { +function drush_make_download_file_unpack_gzip($filename, $download_location, $subtree = NULL) { // Find out where contents will end up. Retrieve last column of output using awk. drush_shell_exec("gzip --list %s", $filename); $info = drush_shell_exec_output(); @@ -284,20 +288,22 @@ function drush_make_download_file_unpack_gzip($filename, $download_location) { if (isset($file)) { // Unzip it and then delete the tar file. drush_shell_exec('gzip -d %s', $filename); - return drush_make_download_file_unpack_tar($file, $download_location); + return drush_make_download_file_unpack_tar($file, $download_location, $subtree); } } drush_make_error('PACKAGE_ERROR', dt('Could not retrieve package information for %filename.', array('%filename' => $filename))); return; } -function drush_make_download_file_unpack_zip($filename, $download_location) { +function drush_make_download_file_unpack_zip($filename, $download_location, $subtree = NULL) { $tmp_path = drush_make_tmp() . '/__unzip__'; drush_make_mkdir($tmp_path); drush_shell_exec("unzip %s -d %s", $filename, $tmp_path); - _drush_make_download_file_move($tmp_path, $download_location); + $src_path = isset($subtree) ? $tmp_path . '/' . $subtree : $tmp_path; + _drush_make_download_file_move($src_path, $download_location); + drush_shell_exec('rm -rf %s', $tmp_path); } -- 1.7.4.1