|
7 | 7 | import platform |
8 | 8 | import subprocess |
9 | 9 | import datetime |
10 | | -import zipfile |
11 | 10 | import stat |
12 | 11 | from urllib.parse import urlparse |
13 | 12 | from urllib.request import urlopen |
@@ -56,36 +55,41 @@ def install_azcopy(self, install_location): |
56 | 55 | install_dir = os.path.dirname(install_location) |
57 | 56 | if not os.path.exists(install_dir): |
58 | 57 | os.makedirs(install_dir) |
59 | | - base_url = 'https://azcopyvnext-awgzd8g7aagqhzhe.b02.azurefd.net/release20211027/azcopy_{}_{}_{}.{}' |
| 58 | + file_extension = 'zip' |
60 | 59 | if self.system == 'Windows': |
61 | 60 | if platform.machine().endswith('64'): |
62 | | - file_url = base_url.format('windows', 'amd64', AZCOPY_VERSION, 'zip') |
| 61 | + file_url = 'https://aka.ms/downloadazcopy-v10-windows' |
63 | 62 | if _verify_url(file_url) is None: |
64 | 63 | file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIWindowsX64') |
65 | 64 | else: |
66 | | - file_url = base_url.format('windows', '386', AZCOPY_VERSION, 'zip') |
| 65 | + file_url = 'https://aka.ms/downloadazcopy-v10-windows-32bit' |
67 | 66 | if _verify_url(file_url) is None: |
68 | 67 | file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIWindows') |
69 | 68 | elif self.system == 'Linux': |
70 | | - file_url = base_url.format('linux', 'amd64', AZCOPY_VERSION, 'tar.gz') |
| 69 | + file_extension = 'tar.gz' |
| 70 | + file_url = 'https://aka.ms/downloadazcopy-v10-linux' |
71 | 71 | if _verify_url(file_url) is None: |
72 | 72 | file_url = _verify_url('https://aka.ms/InstallAzCopyForCLILinux') |
73 | 73 | elif self.system == 'Darwin': |
74 | | - file_url = base_url.format('darwin', 'amd64', AZCOPY_VERSION, 'zip') |
| 74 | + file_url = 'https://aka.ms/downloadazcopy-v10-mac' |
75 | 75 | if _verify_url(file_url) is None: |
76 | 76 | file_url = _verify_url('https://aka.ms/InstallAzCopyForCLIDarwin') |
77 | 77 | else: |
78 | | - raise CLIError('Azcopy ({}) does not exist.'.format(self.system)) |
| 78 | + raise CLIError('Azcopy executable does not exist for {}.'.format(self.system)) |
| 79 | + azcopy_install_guide = 'https://learn.microsoft.com/azure/storage/common/storage-use-azcopy-v10' |
| 80 | + if not file_url: |
| 81 | + raise CLIError('Error while attempting to download azcopy. You could manually install the azcopy ' |
| 82 | + 'executable to {} by following the guide here: {}'.format(install_dir, |
| 83 | + azcopy_install_guide)) |
79 | 84 | try: |
80 | 85 | os.chmod(install_dir, os.stat(install_dir).st_mode | stat.S_IWUSR) |
81 | | - _urlretrieve(file_url, install_location) |
| 86 | + _urlretrieve(file_url, install_location, file_extension) |
82 | 87 | os.chmod(install_location, |
83 | 88 | os.stat(install_location).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
84 | | - except OSError as err: |
85 | | - azcopy_install_guide = 'https://learn.microsoft.com/azure/storage/common/storage-use-azcopy-v10' |
86 | | - raise CLIError('Connection error while attempting to download azcopy {}. You could also install the ' |
87 | | - 'specified azcopy version to {} manually following the guide here: {} ' |
88 | | - '({})'.format(AZCOPY_VERSION, install_dir, azcopy_install_guide, err)) |
| 89 | + except (OSError, CLIError) as err: |
| 90 | + raise CLIError('Error while attempting to download azcopy from {}. You could manually install the azcopy ' |
| 91 | + 'executable to {} by following the guide here: {} ' |
| 92 | + '({})'.format(file_url, install_dir, azcopy_install_guide, err)) |
89 | 93 |
|
90 | 94 | def check_version(self): |
91 | 95 | try: |
@@ -226,34 +230,41 @@ def _get_default_install_location(): |
226 | 230 | return install_location |
227 | 231 |
|
228 | 232 |
|
229 | | -def _urlretrieve(url, install_location): |
| 233 | +def _urlretrieve(url, install_location, file_extension): |
230 | 234 | import io |
231 | 235 | logger.warning('Downloading AzCopy from %s', url) |
232 | | - req = urlopen(url) |
233 | | - compressedFile = io.BytesIO(req.read()) |
234 | | - if url.endswith('zip'): |
235 | | - zip_file = zipfile.ZipFile(compressedFile) |
236 | | - for fileName in zip_file.namelist(): |
237 | | - if fileName.endswith('azcopy') or fileName.endswith('azcopy.exe'): |
238 | | - with open(install_location, 'wb') as f: |
239 | | - f.write(zip_file.read(fileName)) |
240 | | - elif url.endswith('gz'): |
241 | | - import tarfile |
242 | | - with tarfile.open(fileobj=compressedFile, mode="r:gz") as tar: |
243 | | - for tarinfo in tar: |
244 | | - if tarinfo.isfile() and tarinfo.name.endswith('azcopy'): |
| 236 | + res = urlopen(url) |
| 237 | + if res.status != 200: |
| 238 | + raise CLIError('Invalid downloading url {}'.format(url)) |
| 239 | + compressedFile = io.BytesIO(res.read()) |
| 240 | + if file_extension == 'zip': |
| 241 | + try: |
| 242 | + import zipfile |
| 243 | + zip_file = zipfile.ZipFile(compressedFile) |
| 244 | + for fileName in zip_file.namelist(): |
| 245 | + if fileName.endswith('azcopy') or fileName.endswith('azcopy.exe'): |
245 | 246 | with open(install_location, 'wb') as f: |
246 | | - f.write(tar.extractfile(tarinfo).read()) |
| 247 | + f.write(zip_file.read(fileName)) |
| 248 | + except Exception as ex: # pylint: disable=broad-except |
| 249 | + raise CLIError(ex) |
247 | 250 | else: |
248 | | - raise CLIError('Invalid downloading url {}'.format(url)) |
| 251 | + try: |
| 252 | + import tarfile |
| 253 | + with tarfile.open(fileobj=compressedFile, mode="r:gz") as tar: |
| 254 | + for tarinfo in tar: |
| 255 | + if tarinfo.isfile() and tarinfo.name.endswith('azcopy'): |
| 256 | + with open(install_location, 'wb') as f: |
| 257 | + f.write(tar.extractfile(tarinfo).read()) |
| 258 | + except Exception as ex: # pylint: disable=broad-except |
| 259 | + raise CLIError(ex) |
249 | 260 |
|
250 | 261 |
|
251 | 262 | def _verify_url(url): |
252 | 263 | from urllib.error import HTTPError, URLError |
253 | 264 | try: |
254 | 265 | response = urlopen(url) |
255 | 266 | if response.code == 200: |
256 | | - return response.url |
| 267 | + return url |
257 | 268 | return None |
258 | 269 | except (HTTPError, URLError): |
259 | 270 | logger.warning('There is an error downloading from the url: %s', url) |
|
0 commit comments