1 |
76bdcd04
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Common drush functions for the print submodules.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Download and extract the lib.
|
10 |
|
|
*
|
11 |
|
|
* @param string $library
|
12 |
|
|
* Library to download.
|
13 |
|
|
* @param string $url
|
14 |
|
|
* URL of the file to download.
|
15 |
|
|
*/
|
16 |
|
|
function _print_drush_download_lib($library, $url) {
|
17 |
|
|
$path = drush_get_option('path');
|
18 |
|
|
if (empty($path)) {
|
19 |
|
|
$path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/sites/all/libraries/' . $library;
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
// Create the path if it does not exist.
|
23 |
|
|
if (!is_dir($path)) {
|
24 |
|
|
drush_op('mkdir', $path);
|
25 |
|
|
drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
// Chdir to the download location.
|
29 |
|
|
$olddir = getcwd();
|
30 |
|
|
drush_op('chdir', $path);
|
31 |
|
|
|
32 |
|
|
// Warn about an existing dir.
|
33 |
|
|
if (is_dir($library)) {
|
34 |
|
|
drush_log(dt('An existing @library was overwritten at @path', array('@library' => $library, '@path' => $path . '/' . $library)), 'notice');
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
if (preg_match('!api.github.com/repos/.*/releases/latest!', $url)) {
|
38 |
|
|
$url = _print_drush_github_latest_url($url);
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
// Download the archive.
|
42 |
|
|
$filename = _print_drush_download_file($url);
|
43 |
|
|
if ($filename) {
|
44 |
|
|
$extract_ret = _print_drush_download_extract($filename);
|
45 |
|
|
if ($extract_ret) {
|
46 |
|
|
// Remove the archive.
|
47 |
|
|
drush_op('unlink', $filename);
|
48 |
|
|
drush_log(dt('@file has been downloaded and extracted in @path', array('@file' => $filename, '@path' => $path)), 'success');
|
49 |
|
|
}
|
50 |
|
|
else {
|
51 |
|
|
drush_log(dt('@file has been downloaded to @path, but extract failed. Check that you have the necessary program installed, and if necessary extract it manually.',
|
52 |
|
|
array('@file' => $filename, '@path' => $path)), 'warning');
|
53 |
|
|
}
|
54 |
|
|
}
|
55 |
|
|
else {
|
56 |
|
|
drush_log(dt('Drush was unable to download @library to @path', array('@library' => $library, '@path' => $path)), 'error');
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
// Set working directory back to the previous working directory.
|
60 |
|
|
drush_op('chdir', $olddir);
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
|
|
* Get filename of latest from github.
|
65 |
|
|
*
|
66 |
|
|
* @param string $github_url
|
67 |
|
|
* The github URL to the latest project release.
|
68 |
|
|
*
|
69 |
|
|
* @return string
|
70 |
|
|
* The URL to the latest file release.
|
71 |
|
|
*/
|
72 |
|
|
function _print_drush_github_latest_url($github_url) {
|
73 |
|
|
$filename = _print_drush_download_file($github_url);
|
74 |
|
|
$contents = file_get_contents($filename);
|
75 |
|
|
drush_op('unlink', $filename);
|
76 |
|
|
$json = json_decode($contents);
|
77 |
|
|
if (isset($json->assets[0])) {
|
78 |
|
|
$download_url = $json->assets[0]->browser_download_url;
|
79 |
|
|
}
|
80 |
|
|
else {
|
81 |
|
|
// Try the zipball_url.
|
82 |
|
|
$download_url = $json->zipball_url;
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
return $download_url;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
/**
|
89 |
|
|
* Download a file using wget or curl.
|
90 |
|
|
*
|
91 |
|
|
* Adapted from a function in drush/includes/drush.inc to support 302 redirects.
|
92 |
|
|
*
|
93 |
|
|
* @param string $download_url
|
94 |
|
|
* The path to the file to download.
|
95 |
|
|
*
|
96 |
|
|
* @return string
|
97 |
|
|
* The filename that was downloaded, or NULL if the file could not be
|
98 |
|
|
* downloaded.
|
99 |
|
|
*/
|
100 |
|
|
function _print_drush_download_file($download_url) {
|
101 |
|
|
if (!drush_get_context('DRUSH_SIMULATE')) {
|
102 |
|
|
$wget_ret = drush_shell_exec("wget -nv --content-disposition %s", $download_url);
|
103 |
|
|
if ($wget_ret) {
|
104 |
|
|
// Get the filename of the saved file from the output.
|
105 |
|
|
$wget_out = explode('"', array_shift(drush_shell_exec_output()));
|
106 |
|
|
$filename = $wget_out[1];
|
107 |
|
|
}
|
108 |
|
|
else {
|
109 |
|
|
$tempnam = uniqid('print_drush_');
|
110 |
|
|
|
111 |
|
|
$curl_ret = drush_shell_exec("curl -s -L -o %s %s -w '%%{url_effective}'", $tempnam, $download_url);
|
112 |
|
|
if ($curl_ret) {
|
113 |
|
|
// File was downloaded with the temporary name.
|
114 |
|
|
// Find the effective name.
|
115 |
|
|
$filename = explode('/', array_shift(drush_shell_exec_output()));
|
116 |
|
|
$filename = array_pop($filename);
|
117 |
|
|
|
118 |
|
|
// Rename file from tempname to effective name.
|
119 |
|
|
if (!drush_op('rename', $tempnam, './' . $filename)) {
|
120 |
|
|
$filename = $tempnam;
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
else {
|
124 |
|
|
$filename = FALSE;
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
else {
|
129 |
|
|
$filename = basename($download_url);
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
return $filename;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Helper to extract the downloaded zip/tar archive.
|
137 |
|
|
*
|
138 |
|
|
* @param string $filename
|
139 |
|
|
* Filename of the file to extract.
|
140 |
|
|
*
|
141 |
|
|
* @return bool
|
142 |
|
|
* TRUE on success, FALSE on failure
|
143 |
|
|
*/
|
144 |
|
|
function _print_drush_download_extract($filename) {
|
145 |
|
|
$arch_ret = FALSE;
|
146 |
|
|
|
147 |
|
|
if (drush_op('is_file', $filename)) {
|
148 |
|
|
$mime_type = drush_op('mime_content_type', $filename);
|
149 |
|
|
switch ($mime_type) {
|
150 |
|
|
case 1:
|
151 |
|
|
$arch_ret = TRUE;
|
152 |
|
|
break;
|
153 |
|
|
|
154 |
|
|
case 'application/zip':
|
155 |
|
|
// Decompress the zip archive.
|
156 |
|
|
$arch_ret = drush_shell_exec('unzip -qq -o %s', $filename);
|
157 |
|
|
// ZIP archives usually get the access rights wrong.
|
158 |
|
|
drush_log(dt('@filename is a Zip file. Check the access permissions of the extracted files.', array('@filename' => $filename)), 'warning');
|
159 |
|
|
break;
|
160 |
|
|
|
161 |
|
|
case 'application/x-gzip':
|
162 |
|
|
// Decompress the tar gz archive.
|
163 |
|
|
$arch_ret = drush_shell_exec('tar xzf %s', $filename);
|
164 |
|
|
break;
|
165 |
|
|
|
166 |
|
|
case 'application/x-bzip2':
|
167 |
|
|
// Decompress the tar bz2 archive.
|
168 |
|
|
$arch_ret = drush_shell_exec('tar xjf %s', $filename);
|
169 |
|
|
break;
|
170 |
|
|
|
171 |
|
|
case 'application/x-xz':
|
172 |
|
|
// Decompress the tar xz archive.
|
173 |
|
|
$arch_ret = drush_shell_exec('tar xJf %s', $filename);
|
174 |
|
|
break;
|
175 |
|
|
|
176 |
|
|
default:
|
177 |
|
|
drush_log(dt('Unknown MIME type: @type', array('@type' => $mime_type)), 'error');
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
else {
|
181 |
|
|
drush_log(dt('@filename not found.', array('@filename' => $filename)), 'error');
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
return $arch_ret;
|
185 |
|
|
} |