download.pm.inc

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

Functions

Namesort descending Description
drush_pm_download Command callback. Download Drupal core or any project.
drush_pm_download_validate Implementation of drush_COMMAND_validate().
pm_drush_pm_download_destination_alter Implementation of hook_drush_pm_download_destination_alter().
_pm_download_destination Returns the best destination for a particular download type we can find.
_pm_download_destination_lookup Determine a candidate destination directory for a particular site path and return it if it exists, optionally attempting to create the directory.

File

commands/pm/download.pm.inc
View source
  1. <?php
  2. /**
  3. * Implementation of drush_COMMAND_validate().
  4. */
  5. function drush_pm_download_validate() {
  6. // Accomodate --select to the values accepted by release_info_fetch().
  7. $select = drush_get_option('select', 'auto');
  8. if ($select === TRUE) {
  9. drush_set_option('select', 'always');
  10. }
  11. else if ($select === FALSE) {
  12. drush_set_option('select', 'never');
  13. }
  14. // Validate the user specified destination directory.
  15. $destination = drush_get_option('destination');
  16. if (!empty($destination)) {
  17. $destination = rtrim($destination, DIRECTORY_SEPARATOR);
  18. if (!is_dir($destination)) {
  19. drush_print(dt("The directory !destination does not exist.", array('!destination' => $destination)));
  20. if (!drush_get_context('DRUSH_SIMULATE')) {
  21. if (drush_confirm(dt('Would you like to create it?'))) {
  22. drush_mkdir($destination, TRUE);
  23. }
  24. if (!is_dir($destination)) {
  25. return drush_set_error('DRUSH_PM_NO_DESTINATION', dt('Unable to create destination directory !destination.', array('!destination' => $destination)));
  26. }
  27. }
  28. }
  29. if (!is_writable($destination)) {
  30. return drush_set_error('DRUSH_PM_NO_DESTINATION', dt('Destination directory !destination is not writable.', array('!destination' => $destination)));
  31. }
  32. // Ignore --use-site-dir, if given.
  33. if (drush_get_option('use-site-dir', FALSE)) {
  34. drush_set_option('use-site-dir', FALSE);
  35. }
  36. }
  37. // Validate --variant or enforce a sane default.
  38. $variant = drush_get_option('variant', FALSE);
  39. if ($variant) {
  40. $variants = array('full', 'projects', 'profile-only');
  41. if (!in_array($variant, $variants)) {
  42. return drush_set_error('DRUSH_PM_PROFILE_INVALID_VARIANT', dt('Invalid variant !variant. Valid values: !variants.', array('!variant' => $variant, '!variants' => implode(', ', $variants))));
  43. }
  44. }
  45. // 'full' and 'projects' variants are only valid for wget package handler.
  46. $package_handler = drush_get_option('package-handler', 'wget');
  47. if (($package_handler != 'wget') && ($variant != 'profile-only')) {
  48. $new_variant = 'profile-only';
  49. if ($variant) {
  50. drush_log(dt('Variant !variant is incompatible with !ph package-handler.', array('!variant' => $variant, '!ph' => $package_handler)), 'warning');
  51. }
  52. }
  53. // If we are working on a drupal root, full variant is not an option.
  54. else if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_ROOT) {
  55. if ((!$variant) || (($variant == 'full') && (!isset($new_variant)))) {
  56. $new_variant = 'projects';
  57. }
  58. if ($variant == 'full') {
  59. drush_log(dt('Variant full is not a valid option within a Drupal root.'), 'warning');
  60. }
  61. }
  62. if (isset($new_variant)) {
  63. drush_set_option('variant', $new_variant);
  64. if ($variant) {
  65. drush_log(dt('Switching to --variant=!variant.', array('!variant' => $new_variant)), 'ok');
  66. }
  67. }
  68. }
  69. /**
  70. * Command callback. Download Drupal core or any project.
  71. */
  72. function drush_pm_download() {
  73. if (!$requests = pm_parse_arguments(func_get_args(), FALSE)) {
  74. $requests = array('drupal');
  75. }
  76. // Parse out project name and version.
  77. $requests = pm_parse_project_version($requests);
  78. // Get release history for each request and download the project.
  79. $source = drush_get_option('source', RELEASE_INFO_DEFAULT_URL);
  80. $restrict_to = drush_get_option('dev', FALSE) ? 'dev' : '';
  81. $select = drush_get_option('select', 'auto');
  82. $all = drush_get_option('all', FALSE);
  83. foreach ($requests as $name => $request) {
  84. $request['status url'] = $source;
  85. $release = release_info_fetch($request, $restrict_to, $select, $all);
  86. if ($release == FALSE) {
  87. continue;
  88. }
  89. // Determine the name of the directory that will contain the project.
  90. // We face here all the asymetries to make it smooth for package handlers.
  91. // For Drupal core: --drupal-project-rename or drupal-x.y
  92. if (($request['project_type'] == 'core') ||
  93. (($request['project_type'] == 'profile') && (drush_get_option('variant', 'full') == 'full'))) {
  94. // Avoid downloading core into existing core.
  95. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_ROOT) {
  96. if (strpos(realpath(drush_get_option('destination')), DRUPAL_ROOT) !== FALSE) {
  97. return drush_set_error('DRUSH_PM_DOWNLOAD_TRANSLATIONS_FORBIDDEN', dt('It\'s forbidden to download !project core into an existing core.', array('!project' => $request['name'])));
  98. }
  99. }
  100. if ($rename = drush_get_option('drupal-project-rename', FALSE)) {
  101. if ($rename === TRUE) {
  102. $request['project_dir'] = $request['name'];
  103. }
  104. else {
  105. $request['project_dir'] = $rename;
  106. }
  107. }
  108. else {
  109. // Set to drupal-x.y, the expected name for .tar.gz contents.
  110. // Explicitly needed for cvs package handler.
  111. $request['project_dir'] = strtolower(strtr($release['name'], ' ', '-'));
  112. }
  113. }
  114. // For the other project types we want the project name. Including core
  115. // variant for profiles. Note those come with drupal-x.y in the .tar.gz.
  116. else {
  117. $request['project_dir'] = $request['name'];
  118. }
  119. // Download the project to a temporary location.
  120. $request['base_project_path'] = drush_tempdir();
  121. $request['full_project_path'] = $request['base_project_path'] .'/'. $request['project_dir'];
  122. drush_log(dt('Downloading project !name to !dir ...', array('!name' => $request['name'], '!dir' => $request['base_project_path'])));
  123. if (!package_handler_download_project($request, $release)) {
  124. drush_log(dt('Error downloading !name', array('!name' => $request['name']), 'error'));
  125. continue;
  126. }
  127. // Determine the install location for the project. User provided
  128. // --destination has preference.
  129. $destination = drush_get_option('destination');
  130. if (!empty($destination)) {
  131. if (!file_exists($destination)) {
  132. drush_mkdir($destination);
  133. }
  134. $request['project_install_location'] = realpath($destination);
  135. }
  136. else {
  137. $request['project_install_location'] = _pm_download_destination($request['project_type']);
  138. }
  139. // If user did not provide --destination, then call the
  140. // download-destination-alter hook to give the chance to any commandfiles
  141. // to adjust the install location or abort it.
  142. if (empty($destination)) {
  143. $result = drush_command_invoke_all_ref('drush_pm_download_destination_alter', $request, $release);
  144. if (array_search(FALSE, $result, TRUE) !== FALSE) {
  145. return FALSE;
  146. }
  147. }
  148. // Load version control engine and detect if (the parent directory of) the
  149. // project install location is under a vcs.
  150. if (!$version_control = drush_pm_include_version_control($request['project_install_location'])) {
  151. continue;
  152. }
  153. $request['project_install_location'] .= '/' . $request['project_dir'];
  154. if ($version_control->engine == 'backup') {
  155. // Check if install location already exists.
  156. if (is_dir($request['project_install_location'])) {
  157. if (drush_confirm(dt('Install location !location already exists. Do you want to overwrite it?', array('!location' => $request['project_install_location'])))) {
  158. drush_delete_dir($request['project_install_location'], TRUE);
  159. }
  160. else {
  161. drush_log(dt("Skip installation of !project to !dest.", array('!project' => $request['name'], '!dest' => $request['project_install_location'])), 'warning');
  162. continue;
  163. }
  164. }
  165. }
  166. else {
  167. // Find and unlink all files but the ones in the vcs control directories.
  168. $skip_list = array('.', '..');
  169. $skip_list = array_merge($skip_list, drush_version_control_reserved_files());
  170. drush_scan_directory($request['project_install_location'], '/.*/', $skip_list, 'unlink', TRUE, 'filename', 0, TRUE);
  171. }
  172. // Copy the project to the install location.
  173. if (drush_op('_drush_recursive_copy', $request['full_project_path'], $request['project_install_location'])) {
  174. drush_log(dt("Project !project (!version) downloaded to !dest.", array('!project' => $request['name'], '!version' => $release['version'], '!dest' => $request['project_install_location'])), 'success');
  175. $request['base_project_path'] = basename($request['project_install_location']);
  176. $request['full_project_path'] = $request['project_install_location'];
  177. if ($request['project_install_location'] == DRUSH_BASE_PATH) {
  178. drush_log(dt("Drush successfully updated to version !version.", array('!version' => $release['version'])), 'success');
  179. }
  180. // If the version control engine is a proper vcs we also need to remove
  181. // orphan directories.
  182. if ($version_control->engine != 'backup') {
  183. $empty_dirs = drush_find_empty_directories($request['full_project_path'], $version_control->reserved_files());
  184. foreach ($empty_dirs as $empty_dir) {
  185. // Some VCS files are read-only on Windows (e.g., .svn/entries).
  186. drush_delete_dir($empty_dir, TRUE);
  187. }
  188. }
  189. // Post download actions.
  190. package_handler_post_download($request, $release);
  191. drush_command_invoke_all('drush_pm_post_download', $request, $release);
  192. $version_control->post_download($request);
  193. // Print release notes if --notes option is set.
  194. if (drush_get_option('notes') && !drush_get_context('DRUSH_PIPE')) {
  195. release_info_print_releasenotes(array($name . '-' . $release['version']), FALSE);
  196. }
  197. // Inform the user about available modules a/o themes in the downloaded project.
  198. drush_pm_extensions_in_project($request);
  199. }
  200. else {
  201. // We don't `return` here in order to proceed with downloading additional projects.
  202. drush_set_error('DRUSH_PM_DOWNLOAD_FAILED', dt("Project !project (!version) could not be downloaded to !dest.", array('!project' => $request['name'], '!version' => $release['version'], '!dest' => $request['project_install_location'])));
  203. }
  204. }
  205. }
  206. /**
  207. * Implementation of hook_drush_pm_download_destination_alter().
  208. *
  209. * Built-in download-destination-alter hook. This particular version of
  210. * the hook will move modules that contain only drush commands to
  211. * /usr/share/drush/commands if it exists, or $HOME/.drush if the
  212. * site-wide location does not exist.
  213. */
  214. function pm_drush_pm_download_destination_alter(&$project, $release) {
  215. // A module is a pure drush command if it has no .module and contain
  216. // .drush.inc files. Skip this test for drush itself, though; we do
  217. // not want to download drush to the ~/.drush folder.
  218. if (($project['project_type'] == 'module') && ($project['name'] != 'drush')) {
  219. $drush_command_files = drush_scan_directory($project['full_project_path'], '/.*\.drush.inc/');
  220. if (!empty($drush_command_files)) {
  221. $module_files = drush_scan_directory($project['full_project_path'], '/.*\.module/');
  222. if (empty($module_files)) {
  223. $install_dir = drush_get_context('DRUSH_SITE_WIDE_COMMANDFILES');
  224. if (!is_dir($install_dir) || !is_writable($install_dir)) {
  225. $install_dir = drush_get_context('DRUSH_PER_USER_CONFIGURATION');
  226. }
  227. // Make the .drush dir if it does not already exist.
  228. if (!is_dir($install_dir)) {
  229. drush_mkdir($install_dir, FALSE);
  230. }
  231. // Change the location if the mkdir worked.
  232. if (is_dir($install_dir)) {
  233. $project['project_install_location'] = $install_dir;
  234. }
  235. }
  236. // We need to clear the drush commandfile cache so that
  237. // our newly-downloaded drush extension commandfiles can be found.
  238. drush_cache_clear_all();
  239. }
  240. }
  241. }
  242. /**
  243. * Determine a candidate destination directory for a particular site path and
  244. * return it if it exists, optionally attempting to create the directory.
  245. */
  246. function _pm_download_destination_lookup($type, $drupal_root, $sitepath, $create = FALSE) {
  247. switch ($type) {
  248. case 'module':
  249. // Prefer sites/all/modules/contrib if it exists.
  250. $destination = $sitepath . '/modules';
  251. $contrib = $destination . '/contrib';
  252. if (is_dir($contrib)) {
  253. $destination = $contrib;
  254. }
  255. break;
  256. case 'theme':
  257. $destination = $sitepath . '/themes';
  258. break;
  259. case 'theme engine':
  260. $destination = $sitepath . '/themes/engines';
  261. break;
  262. case 'profile':
  263. $destination = $drupal_root . '/profiles';
  264. break;
  265. }
  266. if ($create) {
  267. drush_log(dt('Attempting to create destination directory at !dir', array('!dir' => $destination)));
  268. drush_mkdir($destination, TRUE);
  269. }
  270. if (is_dir($destination)) {
  271. drush_log(dt('Using destination directory !dir', array('!dir' => $destination)));
  272. return $destination;
  273. }
  274. drush_log(dt('Could not find destination directory at !dir', array('!dir' => $destination)));
  275. return FALSE;
  276. }
  277. /**
  278. * Returns the best destination for a particular download type we can find.
  279. *
  280. * It is based on the project type and drupal and site contexts.
  281. */
  282. function _pm_download_destination($type) {
  283. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  284. $site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT', FALSE);
  285. $full_site_root = $drupal_root .'/'. $site_root;
  286. $sites_all = $drupal_root . '/sites/all';
  287. $in_site_directory = FALSE;
  288. // Check if we are running within the site directory.
  289. if (strpos(realpath(drush_cwd()), realpath($full_site_root)) !== FALSE || (drush_get_option('use-site-dir', FALSE))) {
  290. $in_site_directory = TRUE;
  291. }
  292. $destination = '';
  293. if ($type != 'core') {
  294. // Attempt 1: If we are in a specific site directory, and the destination
  295. // directory already exists, then we use that.
  296. if (empty($destination) && $site_root && $in_site_directory) {
  297. $create_dir = drush_get_option('use-site-dir', FALSE);
  298. $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, $create_dir);
  299. }
  300. // Attempt 2: If the destination directory already exists for sites/all,
  301. // then we use that.
  302. if (empty($destination) && $drupal_root) {
  303. $destination = _pm_download_destination_lookup($type, $drupal_root, $sites_all);
  304. }
  305. // Attempt 3: If a specific (non default) site directory exists and
  306. // sites/all does not exist, then we create destination in the site
  307. // specific directory.
  308. if (empty($destination) && $site_root && $site_root !== 'sites/default' && is_dir($full_site_root) && !is_dir($sites_all)) {
  309. $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, TRUE);
  310. }
  311. // Attempt 4: If sites/all exists, then we create destination in the
  312. // sites/all directory.
  313. if (empty($destination) && is_dir($sites_all)) {
  314. $destination = _pm_download_destination_lookup($type, $drupal_root, $sites_all, TRUE);
  315. }
  316. // Attempt 5: If site directory exists (even default), then we create
  317. // destination in the this directory.
  318. if (empty($destination) && $site_root && is_dir($full_site_root)) {
  319. $destination = _pm_download_destination_lookup($type, $drupal_root, $full_site_root, TRUE);
  320. }
  321. }
  322. // Attempt 6: If we didn't find a valid directory yet (or we somehow found
  323. // one that doesn't exist) we always fall back to the current directory.
  324. if (empty($destination) || !is_dir($destination)) {
  325. $destination = drush_cwd();
  326. }
  327. return $destination;
  328. }