make.download.inc

  1. 8.0.x commands/make/make.download.inc
  2. 6.x commands/make/make.download.inc
  3. 7.x commands/make/make.download.inc
  4. 5.x commands/make/make.download.inc
  5. master commands/make/make.download.inc

Download-specific functions for Drush Make.

Functions

Namesort descending Description
make_download_bzr Checks out a Bazaar repository to the specified download location.
make_download_copy Copies a folder the specified location.
make_download_factory Downloads the given package to the destination directory.
make_download_file Downloads a file to the specified location.
make_download_file_unpack Unpacks a file to the specified download location.
make_download_get For backwards compatibility.
make_download_git Checks out a git repository to the specified download location.
make_download_pm Download project using drush's pm-download command.
make_download_svn Checks out an SVN repository to the specified download location.
_make_download_copy Wrapper to drush_download_copy().
_make_download_file Wrapper to drush_download_file().
_make_download_file_move Move a downloaded and unpacked file or directory into place.
_make_hash Calculate the hash of a string for a given algorithm.
_make_verify_checksums Test that any supplied hash values match the hash of the file content.

File

commands/make/make.download.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Download-specific functions for Drush Make.
  5. */
  6. /**
  7. * Downloads the given package to the destination directory.
  8. *
  9. * @return mixed
  10. * The destination path on success, FALSE on failure.
  11. */
  12. function make_download_factory($name, $type, $download, $download_location) {
  13. $function = 'make_download_' . $download['type'];
  14. if (function_exists($function)) {
  15. return $function($name, $type, $download, $download_location);
  16. }
  17. else {
  18. return FALSE;
  19. }
  20. }
  21. /**
  22. * Download project using drush's pm-download command.
  23. */
  24. function make_download_pm($name, $type, $download, $download_location) {
  25. $full_project_version = $name . '-' . $download['full_version'];
  26. $options = array(
  27. 'destination' => dirname($download_location),
  28. 'yes' => TRUE,
  29. 'package-handler' => 'wget',
  30. 'source' => $download['status url'],
  31. // This is only relevant for profiles, but we always want the variant to
  32. // be 'profile-only' so we don't end up with extra copies of core.
  33. 'variant' => $type == 'core' ? 'full' : 'profile-only',
  34. 'cache' => TRUE,
  35. );
  36. if ($type == 'core') {
  37. $options['drupal-project-rename'] = basename($download_location);
  38. }
  39. if (drush_get_option('no-cache', FALSE)) {
  40. unset($options['cache']);
  41. }
  42. $backend_options = array();
  43. if (!drush_get_option(array('verbose', 'debug'), FALSE)) {
  44. $backend_options['integrate'] = TRUE;
  45. $backend_options['log'] = FALSE;
  46. }
  47. // Perform actual download with `drush pm-download`.
  48. $return = drush_invoke_process('@none', 'pm-download', array($full_project_version), $options, $backend_options);
  49. if (empty($return['error_log'])) {
  50. // @todo Report the URL we used for download. See
  51. // http://drupal.org/node/1452672.
  52. drush_log(dt('@project downloaded.', array('@project' => $full_project_version)), 'ok');
  53. }
  54. }
  55. /**
  56. * Downloads a file to the specified location.
  57. *
  58. * @return mixed
  59. * The destination directory on success, FALSE on failure.
  60. */
  61. function make_download_file($name, $type, $download, $download_location, $cache_duration = DRUSH_CACHE_LIFETIME_DEFAULT) {
  62. if ($filename = _make_download_file($download['url'], $cache_duration)) {
  63. if (!drush_get_option('ignore-checksums') && !_make_verify_checksums($download, $filename)) {
  64. return FALSE;
  65. }
  66. drush_log(dt('@project downloaded from @url.', array('@project' => $name, '@url' => $download['url'])), 'ok');
  67. $download_filename = isset($download['filename']) ? $download['filename'] : '';
  68. $subtree = isset($download['subtree']) ? $download['subtree'] : NULL;
  69. return make_download_file_unpack($filename, $download_location, $download_filename, $subtree);
  70. }
  71. make_error('DOWNLOAD_ERROR', dt('Unable to download @project from @url.', array('@project' => $name, '@url' => $download['url'])));
  72. return FALSE;
  73. }
  74. /**
  75. * Wrapper to drush_download_file().
  76. *
  77. * @param string $download
  78. * The url of the file to download.
  79. * @param int $cache_duration
  80. * The time in seconds to cache the resultant download.
  81. *
  82. * @return string
  83. * The location of the downloaded file, or FALSE on failure.
  84. */
  85. function _make_download_file($download, $cache_duration = DRUSH_CACHE_LIFETIME_DEFAULT) {
  86. if (drush_get_option('no-cache', FALSE)) {
  87. $cache_duration = 0;
  88. }
  89. $tmp_path = make_tmp();
  90. // Ensure that we aren't including the querystring when generating a filename
  91. // to save our download to.
  92. $file = basename(current(explode('?', $download, 2)));
  93. return drush_download_file($download, $tmp_path . '/' . $file, $cache_duration);
  94. }
  95. /**
  96. * Unpacks a file to the specified download location.
  97. *
  98. * @return mixed
  99. * The download location on success, FALSE on failure.
  100. */
  101. function make_download_file_unpack($filename, $download_location, $name, $subtree = NULL) {
  102. $success = FALSE;
  103. if (drush_file_is_tarball($filename)) {
  104. $tmp_location = drush_tempdir();
  105. if (!drush_tarball_extract($filename, $tmp_location)) {
  106. return FALSE;
  107. }
  108. if ($subtree) {
  109. $tmp_location .= '/' . $subtree;
  110. if (!file_exists($tmp_location)) {
  111. return drush_set_error('DRUSH_MAKE_SUBTREE_NOT_FOUND', dt('Directory !subtree not found within !file', array('!subtree' => $subtree, '!file' => $filename)));
  112. }
  113. }
  114. else {
  115. $files = scandir($tmp_location);
  116. unset($files[0]); // . directory
  117. unset($files[1]); // .. directory
  118. if ((count($files) == 1) && is_dir($tmp_location . '/' . current($files))) {
  119. $tmp_location .= '/' . current($files);
  120. }
  121. }
  122. $success = drush_move_dir($tmp_location, $download_location, TRUE);
  123. // Remove the tarball.
  124. if (file_exists($filename)) {
  125. drush_delete_dir($filename, TRUE);
  126. }
  127. }
  128. else {
  129. // If this is an individual file, and no filename has been specified,
  130. // assume the original name.
  131. if (is_file($filename) && !$name) {
  132. $name = basename($filename);
  133. }
  134. // The destination directory has already been created by
  135. // findDownloadLocation().
  136. $destination = $download_location . ($name ? '/' . $name : '');
  137. $success = drush_move_dir($filename, $destination, TRUE);
  138. }
  139. return $success ? $download_location : FALSE;
  140. }
  141. /**
  142. * Move a downloaded and unpacked file or directory into place.
  143. *
  144. * TODO merge with core drush methods.
  145. */
  146. function _make_download_file_move($tmp_path, $filename, $download_location, $subtree = NULL) {
  147. $lines = drush_scan_directory($tmp_path, '/./', array('.', '..'), 0, FALSE, 'filename', 0, TRUE);
  148. $main_directory = basename($download_location);
  149. if (count($lines) == 1) {
  150. $directory = array_shift($lines);
  151. if ($directory->basename != $main_directory) {
  152. drush_move_dir($directory->filename, $tmp_path . DIRECTORY_SEPARATOR . $main_directory, TRUE);
  153. }
  154. drush_copy_dir($tmp_path . DIRECTORY_SEPARATOR . $main_directory . DIRECTORY_SEPARATOR . $subtree, $download_location, TRUE);
  155. drush_delete_dir($tmp_path, TRUE);
  156. }
  157. elseif (count($lines) > 1) {
  158. drush_delete_dir($download_location, TRUE);
  159. drush_move_dir($tmp_path . DIRECTORY_SEPARATOR . $subtree, $download_location, TRUE);
  160. }
  161. // Remove the tarball.
  162. if (file_exists($filename)) {
  163. drush_delete_dir($filename, TRUE);
  164. }
  165. if (file_exists($tmp_path)) {
  166. drush_delete_dir($tmp_path, TRUE);
  167. }
  168. return TRUE;
  169. }
  170. /**
  171. * For backwards compatibility.
  172. */
  173. function make_download_get($name, $type, $download, $download_location) {
  174. return make_download_file($name, $type, $download, $download_location);
  175. }
  176. /**
  177. * Copies a folder the specified location.
  178. *
  179. * @return mixed
  180. * The TRUE on success, FALSE on failure.
  181. */
  182. function make_download_copy($name, $type, $download, $download_location) {
  183. if ($folder = _make_download_copy($download['url'])) {
  184. drush_log(dt('@project copied from @url.', array('@project' => $name, '@url' => $download['url'])), 'ok');
  185. return drush_copy_dir($folder, $download_location, FILE_EXISTS_OVERWRITE);
  186. }
  187. make_error('COPY_ERROR', dt('Unable to copy @project from @url.', array('@project' => $name, '@url' => $download['url'])));
  188. return FALSE;
  189. }
  190. /**
  191. * Wrapper to drush_download_copy().
  192. *
  193. * @param string $folder
  194. * The location of the folder to copy.
  195. *
  196. * @return string
  197. * The location of the folder, or FALSE on failure.
  198. */
  199. function _make_download_copy($folder) {
  200. if (substr($folder, 0, 7) == 'file://') {
  201. $folder = substr($folder, 7);
  202. }
  203. if (is_dir($folder)) {
  204. return $folder;
  205. }
  206. return FALSE;
  207. }
  208. /**
  209. * Checks out a git repository to the specified download location.
  210. *
  211. * Allowed parameters in $download, in order of precedence:
  212. * - 'tag'
  213. * - 'revision'
  214. * - 'branch'
  215. *
  216. * This will also attempt to write out release information to the
  217. * .info file if the 'no-gitinfofile' option is FALSE. If
  218. * $download['full_version'] is present, this will be used, otherwise,
  219. * version will be set in this order of precedence:
  220. * - 'tag'
  221. * - 'branch'
  222. * - 'revision'
  223. *
  224. * @return mixed
  225. * The download location on success, FALSE otherwise.
  226. */
  227. function make_download_git($name, $type, $download, $download_location) {
  228. $tmp_path = make_tmp();
  229. $wc = _get_working_copy_option($download);
  230. // If no download URL specified, assume anonymous clone from git.drupal.org.
  231. $download['url'] = isset($download['url']) ? $download['url'] : "http://git.drupal.org/project/$name.git";
  232. // If no working-copy download URL specified, assume it is the same.
  233. $download['wc_url'] = isset($download['wc_url']) ? $download['wc_url'] : $download['url'];
  234. // If not a working copy, and if --no-cache has not been explicitly
  235. // declared, create a new git reference cache of the remote repository,
  236. // or update the existing cache to fetch recent changes.
  237. // @see package_handler_download_project()
  238. $cache = !$wc && !drush_get_option('no-cache', FALSE);
  239. if ($cache && ($git_cache = drush_directory_cache('git'))) {
  240. $project_cache = $git_cache . '/' . $name . '-' . md5($download['url']);
  241. // Set up a new cache, if it doesn't exist.
  242. if (!file_exists($project_cache)) {
  243. $command = 'git clone --mirror';
  244. if (drush_get_context('DRUSH_VERBOSE')) {
  245. $command .= ' --verbose --progress';
  246. }
  247. $command .= ' %s %s';
  248. drush_shell_cd_and_exec($git_cache, $command, $download['url'], $project_cache);
  249. }
  250. else {
  251. // Update the --mirror clone.
  252. drush_shell_cd_and_exec($project_cache, 'git remote update');
  253. }
  254. $git_cache = $project_cache;
  255. }
  256. // Use working-copy download URL if --working-copy specified.
  257. $url = $wc ? $download['wc_url'] : $download['url'];
  258. $tmp_location = drush_tempdir() . '/' . basename($download_location);
  259. $command = 'git clone %s %s';
  260. if (drush_get_context('DRUSH_VERBOSE')) {
  261. $command .= ' --verbose --progress';
  262. }
  263. if ($cache) {
  264. $command .= ' --reference ' . drush_escapeshellarg($git_cache);
  265. }
  266. // Before we can checkout anything, we need to clone the repository.
  267. if (!drush_shell_exec($command, $url, $tmp_location)) {
  268. make_error('DOWNLOAD_ERROR', dt('Unable to clone @project from @url.', array('@project' => $name, '@url' => $url)));
  269. return FALSE;
  270. }
  271. drush_log(dt('@project cloned from @url.', array('@project' => $name, '@url' => $url)), 'ok');
  272. // Get the current directory (so we can move back later).
  273. $cwd = getcwd();
  274. // Change into the working copy of the cloned repo.
  275. chdir($tmp_location);
  276. // We want to use the most specific target possible, so first try a refspec.
  277. if (!empty($download['refspec'])) {
  278. if (drush_shell_exec("git fetch %s %s", $url, $download['refspec'])) {
  279. drush_log(dt("Fetched refspec !refspec.", array('!refspec' => $download['refspec'])), 'ok');
  280. if (drush_shell_exec("git checkout FETCH_HEAD")) {
  281. drush_log(dt("Checked out FETCH_HEAD."), 'info');
  282. }
  283. }
  284. else {
  285. make_error('DOWNLOAD_ERROR', dt("Unable to fetch the refspec @refspec from @project.", array('@refspec' => $download['refspec'], '@project' => $name)));
  286. }
  287. }
  288. // If there wasn't a refspec, try a tag.
  289. elseif (!empty($download['tag'])) {
  290. // @TODO: change checkout to refs path.
  291. if (drush_shell_exec("git checkout %s", 'refs/tags/' . $download['tag'])) {
  292. drush_log(dt("Checked out tag @tag.", array('@tag' => $download['tag'])), 'ok');
  293. }
  294. else {
  295. make_error('DOWNLOAD_ERROR', dt("Unable to check out tag @tag.", array('@tag' => $download['tag'])));
  296. }
  297. }
  298. // If there wasn't a tag, try a specific revision hash.
  299. elseif (!empty($download['revision'])) {
  300. if (drush_shell_exec("git checkout %s", $download['revision'])) {
  301. drush_log(dt("Checked out revision @revision.", array('@revision' => $download['revision'])), 'ok');
  302. }
  303. else {
  304. make_error('DOWNLOAD_ERROR', dt("Unable to checkout revision @revision", array('@revision' => $download['revision'])));
  305. }
  306. }
  307. // If not, see if we at least have a branch.
  308. elseif (!empty($download['branch'])) {
  309. if (drush_shell_exec("git checkout %s", $download['branch']) && (trim(implode(drush_shell_exec_output())) != '')) {
  310. drush_log(dt("Checked out branch @branch.", array('@branch' => $download['branch'])), 'ok');
  311. }
  312. elseif (drush_shell_exec("git checkout -b %s %s", $download['branch'], 'origin/' . $download['branch'])) {
  313. drush_log(dt('Checked out branch origin/@branch.', array('@branch' => $download['branch'])), 'ok');
  314. }
  315. else {
  316. make_error('DOWNLOAD_ERROR', dt('Unable to check out branch @branch.', array('@branch' => $download['branch'])));
  317. }
  318. }
  319. if (!empty($download['submodule'])) {
  320. $command = 'git submodule update';
  321. foreach ($download['submodule'] as $option) {
  322. $command .= ' --%s';
  323. }
  324. if (call_user_func_array('drush_shell_exec', array_merge(array($command), $download['submodule']))) {
  325. drush_log(dt('Initialized registered submodules.'), 'ok');
  326. }
  327. else {
  328. make_error('DOWNLOAD_ERROR', dt('Unable to initialize submodules.'));
  329. }
  330. }
  331. // Move back to last current directory (first line).
  332. chdir($cwd);
  333. // Move the directory into the final resting location.
  334. drush_copy_dir($tmp_location, $download_location, TRUE);
  335. return dirname($tmp_location);
  336. }
  337. /**
  338. * Checks out a Bazaar repository to the specified download location.
  339. *
  340. * @return mixed
  341. * The download location on success, FALSE otherwise.
  342. */
  343. function make_download_bzr($name, $type, $download, $download_location) {
  344. $tmp_path = make_tmp();
  345. $tmp_location = drush_tempdir() . '/' . basename($download_location);
  346. $wc = _get_working_copy_option($download);
  347. if (!empty($download['url'])) {
  348. $args = array();
  349. $command = 'bzr';
  350. if ($wc) {
  351. $command .= ' branch --use-existing-dir';
  352. }
  353. else {
  354. $command .= ' export';
  355. }
  356. if (isset($download['revision'])) {
  357. $command .= ' -r %s';
  358. $args[] = $download['revision'];
  359. }
  360. $command .= ' %s %s';
  361. if ($wc) {
  362. $args[] = $download['url'];
  363. $args[] = $tmp_location;
  364. }
  365. else {
  366. $args[] = $tmp_location;
  367. $args[] = $download['url'];
  368. }
  369. array_unshift($args, $command);
  370. if (call_user_func_array('drush_shell_exec', $args)) {
  371. drush_log(dt('@project downloaded from @url.', array('@project' => $name, '@url' => $download['url'])), 'ok');
  372. drush_copy_dir($tmp_location, $download_location, TRUE);
  373. return dirname($download_location);
  374. }
  375. }
  376. else {
  377. $download['url'] = dt("unspecified location");
  378. }
  379. make_error('DOWNLOAD_ERROR', dt('Unable to download @project from @url.', array('@project' => $name, '@url' => $download['url'])));
  380. drush_delete_dir(dirname($tmp_location), TRUE);
  381. return FALSE;
  382. }
  383. /**
  384. * Checks out an SVN repository to the specified download location.
  385. *
  386. * @return mixed
  387. * The download location on success, FALSE otherwise.
  388. */
  389. function make_download_svn($name, $type, $download, $download_location) {
  390. $wc = _get_working_copy_option($download);
  391. if (!empty($download['url'])) {
  392. if (!empty($download['interactive'])) {
  393. $function = 'drush_shell_exec_interactive';
  394. }
  395. else {
  396. $options = ' --non-interactive';
  397. $function = 'drush_shell_exec';
  398. }
  399. if (!isset($download['force']) || $download['force']) {
  400. $options = ' --force';
  401. }
  402. if ($wc) {
  403. $command = 'svn' . $options . ' checkout';
  404. }
  405. else {
  406. $command = 'svn' . $options . ' export';
  407. }
  408. $args = array();
  409. if (isset($download['revision'])) {
  410. $command .= ' -r%s';
  411. $args[] = $download['revision'];
  412. }
  413. $command .= ' %s %s';
  414. $args[] = $download['url'];
  415. $args[] = $download_location;
  416. if (!empty($download['username'])) {
  417. $command .= ' --username %s';
  418. $args[] = $download['username'];
  419. if (!empty($download['password'])) {
  420. $command .= ' --password %s';
  421. $args[] = $download['password'];
  422. }
  423. }
  424. array_unshift($args, $command);
  425. $result = call_user_func_array($function, $args);
  426. if ($result) {
  427. $args = array(
  428. '@project' => $name,
  429. '@command' => $command,
  430. '@url' => $download['url'],
  431. );
  432. drush_log(dt('@project @command from @url.', $args), 'ok');
  433. return $download_location;
  434. }
  435. else {
  436. $download['url'] = dt("unspecified location");
  437. }
  438. }
  439. else {
  440. make_error('DOWNLOAD_ERROR', dt('Unable to download @project from @url.', array('@project' => $name, '@url' => $download['url'])));
  441. return FALSE;
  442. }
  443. }
  444. /**
  445. * Test that any supplied hash values match the hash of the file content.
  446. *
  447. * Unsupported hash algorithms are reported as failure.
  448. */
  449. function _make_verify_checksums($info, $filename) {
  450. $hash_algos = array('md5', 'sha1', 'sha256', 'sha512');
  451. // We only have something to do if a key is an
  452. // available function.
  453. if (array_intersect(array_keys($info), $hash_algos)) {
  454. $content = file_get_contents($filename);
  455. foreach ($hash_algos as $algo) {
  456. if (!empty($info[$algo])) {
  457. $hash = _make_hash($algo, $content);
  458. if ($hash !== $info[$algo]) {
  459. $args = array(
  460. '@algo' => $algo,
  461. '@file' => basename($filename),
  462. '@expected' => $info[$algo],
  463. '@hash' => $hash,
  464. );
  465. make_error('DOWNLOAD_ERROR', dt('Checksum @algo verification failed for @file. Expected @expected, received @hash.', $args));
  466. return FALSE;
  467. }
  468. }
  469. }
  470. }
  471. return TRUE;
  472. }
  473. /**
  474. * Calculate the hash of a string for a given algorithm.
  475. */
  476. function _make_hash($algo, $string) {
  477. switch ($algo) {
  478. case 'md5':
  479. return md5($string);
  480. case 'sha1':
  481. return sha1($string);
  482. default:
  483. return function_exists('hash') ? hash($algo, $string) : '';
  484. }
  485. }