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