We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Failed to download CelebA in Colab
## 🐛 Bug

It fails to download the following files **img_align_celeba.zip**

## To Reproduce

Steps to reproduce the behavior:

1. `from torchvision import datasets`
`trainset = datasets.CelebA(root='./data/', split='train', target_type="attr", transform=transform, download=True)`

Error:
```
Traceback (most recent call last):
  File "transfer_learning.py", line 123, in <module>
    trainset = datasets.CelebA(root='./data/', split='train', transform=transform, download=True)
  File "/usr/local/lib/python3.6/dist-packages/torchvision/datasets/celeba.py", line 66, in __init__
    self.download()
  File "/usr/local/lib/python3.6/dist-packages/torchvision/datasets/celeba.py", line 120, in download
    with zipfile.ZipFile(os.path.join(self.root, self.base_folder, "img_align_celeba.zip"), "r") as f:
  File "/usr/lib/python3.6/zipfile.py", line 1131, in __init__
    self._RealGetContents()
  File "/usr/lib/python3.6/zipfile.py", line 1198, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
```

## Expected behavior

## Environment

```
PyTorch version: 1.5.0+cu101
Is debug build: No
CUDA used to build PyTorch: 10.1

OS: Ubuntu 18.04.3 LTS
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
CMake version: version 3.12.0

Python version: 3.6
Is CUDA available: No
CUDA runtime version: 10.1.243
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5

Versions of relevant libraries:
[pip3] numpy==1.18.5
[pip3] torch==1.5.0+cu101
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.3.1
[pip3] torchvision==0.6.0+cu101
[conda] Could not collect
```

## Additional context

<!-- Add any other context about the problem here. -->


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torchvision/datasets/utils.py`
Content:
```
1 import os
2 import os.path
3 import hashlib
4 import gzip
5 import errno
6 import tarfile
7 import zipfile
8 
9 import torch
10 from torch.utils.model_zoo import tqdm
11 
12 
13 def gen_bar_updater():
14     pbar = tqdm(total=None)
15 
16     def bar_update(count, block_size, total_size):
17         if pbar.total is None and total_size:
18             pbar.total = total_size
19         progress_bytes = count * block_size
20         pbar.update(progress_bytes - pbar.n)
21 
22     return bar_update
23 
24 
25 def calculate_md5(fpath, chunk_size=1024 * 1024):
26     md5 = hashlib.md5()
27     with open(fpath, 'rb') as f:
28         for chunk in iter(lambda: f.read(chunk_size), b''):
29             md5.update(chunk)
30     return md5.hexdigest()
31 
32 
33 def check_md5(fpath, md5, **kwargs):
34     return md5 == calculate_md5(fpath, **kwargs)
35 
36 
37 def check_integrity(fpath, md5=None):
38     if not os.path.isfile(fpath):
39         return False
40     if md5 is None:
41         return True
42     return check_md5(fpath, md5)
43 
44 
45 def download_url(url, root, filename=None, md5=None):
46     """Download a file from a url and place it in root.
47 
48     Args:
49         url (str): URL to download file from
50         root (str): Directory to place downloaded file in
51         filename (str, optional): Name to save the file under. If None, use the basename of the URL
52         md5 (str, optional): MD5 checksum of the download. If None, do not check
53     """
54     import urllib
55 
56     root = os.path.expanduser(root)
57     if not filename:
58         filename = os.path.basename(url)
59     fpath = os.path.join(root, filename)
60 
61     os.makedirs(root, exist_ok=True)
62 
63     # check if file is already present locally
64     if check_integrity(fpath, md5):
65         print('Using downloaded and verified file: ' + fpath)
66     else:   # download the file
67         try:
68             print('Downloading ' + url + ' to ' + fpath)
69             urllib.request.urlretrieve(
70                 url, fpath,
71                 reporthook=gen_bar_updater()
72             )
73         except (urllib.error.URLError, IOError) as e:
74             if url[:5] == 'https':
75                 url = url.replace('https:', 'http:')
76                 print('Failed download. Trying https -> http instead.'
77                       ' Downloading ' + url + ' to ' + fpath)
78                 urllib.request.urlretrieve(
79                     url, fpath,
80                     reporthook=gen_bar_updater()
81                 )
82             else:
83                 raise e
84         # check integrity of downloaded file
85         if not check_integrity(fpath, md5):
86             raise RuntimeError("File not found or corrupted.")
87 
88 
89 def list_dir(root, prefix=False):
90     """List all directories at a given root
91 
92     Args:
93         root (str): Path to directory whose folders need to be listed
94         prefix (bool, optional): If true, prepends the path to each result, otherwise
95             only returns the name of the directories found
96     """
97     root = os.path.expanduser(root)
98     directories = [p for p in os.listdir(root) if os.path.isdir(os.path.join(root, p))]
99     if prefix is True:
100         directories = [os.path.join(root, d) for d in directories]
101     return directories
102 
103 
104 def list_files(root, suffix, prefix=False):
105     """List all files ending with a suffix at a given root
106 
107     Args:
108         root (str): Path to directory whose folders need to be listed
109         suffix (str or tuple): Suffix of the files to match, e.g. '.png' or ('.jpg', '.png').
110             It uses the Python "str.endswith" method and is passed directly
111         prefix (bool, optional): If true, prepends the path to each result, otherwise
112             only returns the name of the files found
113     """
114     root = os.path.expanduser(root)
115     files = [p for p in os.listdir(root) if os.path.isfile(os.path.join(root, p)) and p.endswith(suffix)]
116     if prefix is True:
117         files = [os.path.join(root, d) for d in files]
118     return files
119 
120 
121 def download_file_from_google_drive(file_id, root, filename=None, md5=None):
122     """Download a Google Drive file from  and place it in root.
123 
124     Args:
125         file_id (str): id of file to be downloaded
126         root (str): Directory to place downloaded file in
127         filename (str, optional): Name to save the file under. If None, use the id of the file.
128         md5 (str, optional): MD5 checksum of the download. If None, do not check
129     """
130     # Based on https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url
131     import requests
132     url = "https://docs.google.com/uc?export=download"
133 
134     root = os.path.expanduser(root)
135     if not filename:
136         filename = file_id
137     fpath = os.path.join(root, filename)
138 
139     os.makedirs(root, exist_ok=True)
140 
141     if os.path.isfile(fpath) and check_integrity(fpath, md5):
142         print('Using downloaded and verified file: ' + fpath)
143     else:
144         session = requests.Session()
145 
146         response = session.get(url, params={'id': file_id}, stream=True)
147         token = _get_confirm_token(response)
148 
149         if token:
150             params = {'id': file_id, 'confirm': token}
151             response = session.get(url, params=params, stream=True)
152 
153         _save_response_content(response, fpath)
154 
155 
156 def _get_confirm_token(response):
157     for key, value in response.cookies.items():
158         if key.startswith('download_warning'):
159             return value
160 
161     return None
162 
163 
164 def _save_response_content(response, destination, chunk_size=32768):
165     with open(destination, "wb") as f:
166         pbar = tqdm(total=None)
167         progress = 0
168         for chunk in response.iter_content(chunk_size):
169             if chunk:  # filter out keep-alive new chunks
170                 f.write(chunk)
171                 progress += len(chunk)
172                 pbar.update(progress - pbar.n)
173         pbar.close()
174 
175 
176 def _is_tarxz(filename):
177     return filename.endswith(".tar.xz")
178 
179 
180 def _is_tar(filename):
181     return filename.endswith(".tar")
182 
183 
184 def _is_targz(filename):
185     return filename.endswith(".tar.gz")
186 
187 
188 def _is_tgz(filename):
189     return filename.endswith(".tgz")
190 
191 
192 def _is_gzip(filename):
193     return filename.endswith(".gz") and not filename.endswith(".tar.gz")
194 
195 
196 def _is_zip(filename):
197     return filename.endswith(".zip")
198 
199 
200 def extract_archive(from_path, to_path=None, remove_finished=False):
201     if to_path is None:
202         to_path = os.path.dirname(from_path)
203 
204     if _is_tar(from_path):
205         with tarfile.open(from_path, 'r') as tar:
206             tar.extractall(path=to_path)
207     elif _is_targz(from_path) or _is_tgz(from_path):
208         with tarfile.open(from_path, 'r:gz') as tar:
209             tar.extractall(path=to_path)
210     elif _is_tarxz(from_path):
211         with tarfile.open(from_path, 'r:xz') as tar:
212             tar.extractall(path=to_path)
213     elif _is_gzip(from_path):
214         to_path = os.path.join(to_path, os.path.splitext(os.path.basename(from_path))[0])
215         with open(to_path, "wb") as out_f, gzip.GzipFile(from_path) as zip_f:
216             out_f.write(zip_f.read())
217     elif _is_zip(from_path):
218         with zipfile.ZipFile(from_path, 'r') as z:
219             z.extractall(to_path)
220     else:
221         raise ValueError("Extraction of {} not supported".format(from_path))
222 
223     if remove_finished:
224         os.remove(from_path)
225 
226 
227 def download_and_extract_archive(url, download_root, extract_root=None, filename=None,
228                                  md5=None, remove_finished=False):
229     download_root = os.path.expanduser(download_root)
230     if extract_root is None:
231         extract_root = download_root
232     if not filename:
233         filename = os.path.basename(url)
234 
235     download_url(url, download_root, filename, md5)
236 
237     archive = os.path.join(download_root, filename)
238     print("Extracting {} to {}".format(archive, extract_root))
239     extract_archive(archive, extract_root, remove_finished)
240 
241 
242 def iterable_to_str(iterable):
243     return "'" + "', '".join([str(item) for item in iterable]) + "'"
244 
245 
246 def verify_str_arg(value, arg=None, valid_values=None, custom_msg=None):
247     if not isinstance(value, torch._six.string_classes):
248         if arg is None:
249             msg = "Expected type str, but got type {type}."
250         else:
251             msg = "Expected type str for argument {arg}, but got type {type}."
252         msg = msg.format(type=type(value), arg=arg)
253         raise ValueError(msg)
254 
255     if valid_values is None:
256         return value
257 
258     if value not in valid_values:
259         if custom_msg is not None:
260             msg = custom_msg
261         else:
262             msg = ("Unknown value '{value}' for argument {arg}. "
263                    "Valid values are {{{valid_values}}}.")
264             msg = msg.format(value=value, arg=arg,
265                              valid_values=iterable_to_str(valid_values))
266         raise ValueError(msg)
267 
268     return value
269
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torchvision/datasets/utils.py b/torchvision/datasets/utils.py
--- a/torchvision/datasets/utils.py
+++ b/torchvision/datasets/utils.py
@@ -118,6 +118,10 @@
     return files
 
 
+def _quota_exceeded(response: "requests.models.Response") -> bool:
+    return "Google Drive - Quota exceeded" in response.text
+
+
 def download_file_from_google_drive(file_id, root, filename=None, md5=None):
     """Download a Google Drive file from  and place it in root.
 
@@ -150,6 +154,14 @@
             params = {'id': file_id, 'confirm': token}
             response = session.get(url, params=params, stream=True)
 
+        if _quota_exceeded(response):
+            msg = (
+                f"The daily quota of the file {filename} is exceeded and it "
+                f"can't be downloaded. This is a limitation of Google Drive "
+                f"and can only be overcome by trying again later."
+            )
+            raise RuntimeError(msg)
+
         _save_response_content(response, fpath)

{"golden_diff": "diff --git a/torchvision/datasets/utils.py b/torchvision/datasets/utils.py\n--- a/torchvision/datasets/utils.py\n+++ b/torchvision/datasets/utils.py\n@@ -118,6 +118,10 @@\n     return files\n \n \n+def _quota_exceeded(response: \"requests.models.Response\") -> bool:\n+    return \"Google Drive - Quota exceeded\" in response.text\n+\n+\n def download_file_from_google_drive(file_id, root, filename=None, md5=None):\n     \"\"\"Download a Google Drive file from  and place it in root.\n \n@@ -150,6 +154,14 @@\n             params = {'id': file_id, 'confirm': token}\n             response = session.get(url, params=params, stream=True)\n \n+        if _quota_exceeded(response):\n+            msg = (\n+                f\"The daily quota of the file {filename} is exceeded and it \"\n+                f\"can't be downloaded. This is a limitation of Google Drive \"\n+                f\"and can only be overcome by trying again later.\"\n+            )\n+            raise RuntimeError(msg)\n+\n         _save_response_content(response, fpath)\n", "issue": "Failed to download CelebA in Colab\n## \ud83d\udc1b Bug\r\n\r\nIt fails to download the following files **img_align_celeba.zip**\r\n\r\n## To Reproduce\r\n\r\nSteps to reproduce the behavior:\r\n\r\n1. `from torchvision import datasets`\r\n`trainset = datasets.CelebA(root='./data/', split='train', target_type=\"attr\", transform=transform, download=True)`\r\n\r\nError:\r\n```\r\nTraceback (most recent call last):\r\n  File \"transfer_learning.py\", line 123, in <module>\r\n    trainset = datasets.CelebA(root='./data/', split='train', transform=transform, download=True)\r\n  File \"/usr/local/lib/python3.6/dist-packages/torchvision/datasets/celeba.py\", line 66, in __init__\r\n    self.download()\r\n  File \"/usr/local/lib/python3.6/dist-packages/torchvision/datasets/celeba.py\", line 120, in download\r\n    with zipfile.ZipFile(os.path.join(self.root, self.base_folder, \"img_align_celeba.zip\"), \"r\") as f:\r\n  File \"/usr/lib/python3.6/zipfile.py\", line 1131, in __init__\r\n    self._RealGetContents()\r\n  File \"/usr/lib/python3.6/zipfile.py\", line 1198, in _RealGetContents\r\n    raise BadZipFile(\"File is not a zip file\")\r\nzipfile.BadZipFile: File is not a zip file\r\n```\r\n\r\n## Expected behavior\r\n\r\n## Environment\r\n\r\n```\r\nPyTorch version: 1.5.0+cu101\r\nIs debug build: No\r\nCUDA used to build PyTorch: 10.1\r\n\r\nOS: Ubuntu 18.04.3 LTS\r\nGCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0\r\nCMake version: version 3.12.0\r\n\r\nPython version: 3.6\r\nIs CUDA available: No\r\nCUDA runtime version: 10.1.243\r\nGPU models and configuration: Could not collect\r\nNvidia driver version: Could not collect\r\ncuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5\r\n\r\nVersions of relevant libraries:\r\n[pip3] numpy==1.18.5\r\n[pip3] torch==1.5.0+cu101\r\n[pip3] torchsummary==1.5.1\r\n[pip3] torchtext==0.3.1\r\n[pip3] torchvision==0.6.0+cu101\r\n[conda] Could not collect\r\n```\r\n\r\n## Additional context\r\n\r\n<!-- Add any other context about the problem here. -->\r\n\n", "before_files": [{"content": "import os\nimport os.path\nimport hashlib\nimport gzip\nimport errno\nimport tarfile\nimport zipfile\n\nimport torch\nfrom torch.utils.model_zoo import tqdm\n\n\ndef gen_bar_updater():\n    pbar = tqdm(total=None)\n\n    def bar_update(count, block_size, total_size):\n        if pbar.total is None and total_size:\n            pbar.total = total_size\n        progress_bytes = count * block_size\n        pbar.update(progress_bytes - pbar.n)\n\n    return bar_update\n\n\ndef calculate_md5(fpath, chunk_size=1024 * 1024):\n    md5 = hashlib.md5()\n    with open(fpath, 'rb') as f:\n        for chunk in iter(lambda: f.read(chunk_size), b''):\n            md5.update(chunk)\n    return md5.hexdigest()\n\n\ndef check_md5(fpath, md5, **kwargs):\n    return md5 == calculate_md5(fpath, **kwargs)\n\n\ndef check_integrity(fpath, md5=None):\n    if not os.path.isfile(fpath):\n        return False\n    if md5 is None:\n        return True\n    return check_md5(fpath, md5)\n\n\ndef download_url(url, root, filename=None, md5=None):\n    \"\"\"Download a file from a url and place it in root.\n\n    Args:\n        url (str): URL to download file from\n        root (str): Directory to place downloaded file in\n        filename (str, optional): Name to save the file under. If None, use the basename of the URL\n        md5 (str, optional): MD5 checksum of the download. If None, do not check\n    \"\"\"\n    import urllib\n\n    root = os.path.expanduser(root)\n    if not filename:\n        filename = os.path.basename(url)\n    fpath = os.path.join(root, filename)\n\n    os.makedirs(root, exist_ok=True)\n\n    # check if file is already present locally\n    if check_integrity(fpath, md5):\n        print('Using downloaded and verified file: ' + fpath)\n    else:   # download the file\n        try:\n            print('Downloading ' + url + ' to ' + fpath)\n            urllib.request.urlretrieve(\n                url, fpath,\n                reporthook=gen_bar_updater()\n            )\n        except (urllib.error.URLError, IOError) as e:\n            if url[:5] == 'https':\n                url = url.replace('https:', 'http:')\n                print('Failed download. Trying https -> http instead.'\n                      ' Downloading ' + url + ' to ' + fpath)\n                urllib.request.urlretrieve(\n                    url, fpath,\n                    reporthook=gen_bar_updater()\n                )\n            else:\n                raise e\n        # check integrity of downloaded file\n        if not check_integrity(fpath, md5):\n            raise RuntimeError(\"File not found or corrupted.\")\n\n\ndef list_dir(root, prefix=False):\n    \"\"\"List all directories at a given root\n\n    Args:\n        root (str): Path to directory whose folders need to be listed\n        prefix (bool, optional): If true, prepends the path to each result, otherwise\n            only returns the name of the directories found\n    \"\"\"\n    root = os.path.expanduser(root)\n    directories = [p for p in os.listdir(root) if os.path.isdir(os.path.join(root, p))]\n    if prefix is True:\n        directories = [os.path.join(root, d) for d in directories]\n    return directories\n\n\ndef list_files(root, suffix, prefix=False):\n    \"\"\"List all files ending with a suffix at a given root\n\n    Args:\n        root (str): Path to directory whose folders need to be listed\n        suffix (str or tuple): Suffix of the files to match, e.g. '.png' or ('.jpg', '.png').\n            It uses the Python \"str.endswith\" method and is passed directly\n        prefix (bool, optional): If true, prepends the path to each result, otherwise\n            only returns the name of the files found\n    \"\"\"\n    root = os.path.expanduser(root)\n    files = [p for p in os.listdir(root) if os.path.isfile(os.path.join(root, p)) and p.endswith(suffix)]\n    if prefix is True:\n        files = [os.path.join(root, d) for d in files]\n    return files\n\n\ndef download_file_from_google_drive(file_id, root, filename=None, md5=None):\n    \"\"\"Download a Google Drive file from  and place it in root.\n\n    Args:\n        file_id (str): id of file to be downloaded\n        root (str): Directory to place downloaded file in\n        filename (str, optional): Name to save the file under. If None, use the id of the file.\n        md5 (str, optional): MD5 checksum of the download. If None, do not check\n    \"\"\"\n    # Based on https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url\n    import requests\n    url = \"https://docs.google.com/uc?export=download\"\n\n    root = os.path.expanduser(root)\n    if not filename:\n        filename = file_id\n    fpath = os.path.join(root, filename)\n\n    os.makedirs(root, exist_ok=True)\n\n    if os.path.isfile(fpath) and check_integrity(fpath, md5):\n        print('Using downloaded and verified file: ' + fpath)\n    else:\n        session = requests.Session()\n\n        response = session.get(url, params={'id': file_id}, stream=True)\n        token = _get_confirm_token(response)\n\n        if token:\n            params = {'id': file_id, 'confirm': token}\n            response = session.get(url, params=params, stream=True)\n\n        _save_response_content(response, fpath)\n\n\ndef _get_confirm_token(response):\n    for key, value in response.cookies.items():\n        if key.startswith('download_warning'):\n            return value\n\n    return None\n\n\ndef _save_response_content(response, destination, chunk_size=32768):\n    with open(destination, \"wb\") as f:\n        pbar = tqdm(total=None)\n        progress = 0\n        for chunk in response.iter_content(chunk_size):\n            if chunk:  # filter out keep-alive new chunks\n                f.write(chunk)\n                progress += len(chunk)\n                pbar.update(progress - pbar.n)\n        pbar.close()\n\n\ndef _is_tarxz(filename):\n    return filename.endswith(\".tar.xz\")\n\n\ndef _is_tar(filename):\n    return filename.endswith(\".tar\")\n\n\ndef _is_targz(filename):\n    return filename.endswith(\".tar.gz\")\n\n\ndef _is_tgz(filename):\n    return filename.endswith(\".tgz\")\n\n\ndef _is_gzip(filename):\n    return filename.endswith(\".gz\") and not filename.endswith(\".tar.gz\")\n\n\ndef _is_zip(filename):\n    return filename.endswith(\".zip\")\n\n\ndef extract_archive(from_path, to_path=None, remove_finished=False):\n    if to_path is None:\n        to_path = os.path.dirname(from_path)\n\n    if _is_tar(from_path):\n        with tarfile.open(from_path, 'r') as tar:\n            tar.extractall(path=to_path)\n    elif _is_targz(from_path) or _is_tgz(from_path):\n        with tarfile.open(from_path, 'r:gz') as tar:\n            tar.extractall(path=to_path)\n    elif _is_tarxz(from_path):\n        with tarfile.open(from_path, 'r:xz') as tar:\n            tar.extractall(path=to_path)\n    elif _is_gzip(from_path):\n        to_path = os.path.join(to_path, os.path.splitext(os.path.basename(from_path))[0])\n        with open(to_path, \"wb\") as out_f, gzip.GzipFile(from_path) as zip_f:\n            out_f.write(zip_f.read())\n    elif _is_zip(from_path):\n        with zipfile.ZipFile(from_path, 'r') as z:\n            z.extractall(to_path)\n    else:\n        raise ValueError(\"Extraction of {} not supported\".format(from_path))\n\n    if remove_finished:\n        os.remove(from_path)\n\n\ndef download_and_extract_archive(url, download_root, extract_root=None, filename=None,\n                                 md5=None, remove_finished=False):\n    download_root = os.path.expanduser(download_root)\n    if extract_root is None:\n        extract_root = download_root\n    if not filename:\n        filename = os.path.basename(url)\n\n    download_url(url, download_root, filename, md5)\n\n    archive = os.path.join(download_root, filename)\n    print(\"Extracting {} to {}\".format(archive, extract_root))\n    extract_archive(archive, extract_root, remove_finished)\n\n\ndef iterable_to_str(iterable):\n    return \"'\" + \"', '\".join([str(item) for item in iterable]) + \"'\"\n\n\ndef verify_str_arg(value, arg=None, valid_values=None, custom_msg=None):\n    if not isinstance(value, torch._six.string_classes):\n        if arg is None:\n            msg = \"Expected type str, but got type {type}.\"\n        else:\n            msg = \"Expected type str for argument {arg}, but got type {type}.\"\n        msg = msg.format(type=type(value), arg=arg)\n        raise ValueError(msg)\n\n    if valid_values is None:\n        return value\n\n    if value not in valid_values:\n        if custom_msg is not None:\n            msg = custom_msg\n        else:\n            msg = (\"Unknown value '{value}' for argument {arg}. \"\n                   \"Valid values are {{{valid_values}}}.\")\n            msg = msg.format(value=value, arg=arg,\n                             valid_values=iterable_to_str(valid_values))\n        raise ValueError(msg)\n\n    return value\n", "path": "torchvision/datasets/utils.py"}], "after_files": [{"content": "import os\nimport os.path\nimport hashlib\nimport gzip\nimport errno\nimport tarfile\nimport zipfile\n\nimport torch\nfrom torch.utils.model_zoo import tqdm\n\n\ndef gen_bar_updater():\n    pbar = tqdm(total=None)\n\n    def bar_update(count, block_size, total_size):\n        if pbar.total is None and total_size:\n            pbar.total = total_size\n        progress_bytes = count * block_size\n        pbar.update(progress_bytes - pbar.n)\n\n    return bar_update\n\n\ndef calculate_md5(fpath, chunk_size=1024 * 1024):\n    md5 = hashlib.md5()\n    with open(fpath, 'rb') as f:\n        for chunk in iter(lambda: f.read(chunk_size), b''):\n            md5.update(chunk)\n    return md5.hexdigest()\n\n\ndef check_md5(fpath, md5, **kwargs):\n    return md5 == calculate_md5(fpath, **kwargs)\n\n\ndef check_integrity(fpath, md5=None):\n    if not os.path.isfile(fpath):\n        return False\n    if md5 is None:\n        return True\n    return check_md5(fpath, md5)\n\n\ndef download_url(url, root, filename=None, md5=None):\n    \"\"\"Download a file from a url and place it in root.\n\n    Args:\n        url (str): URL to download file from\n        root (str): Directory to place downloaded file in\n        filename (str, optional): Name to save the file under. If None, use the basename of the URL\n        md5 (str, optional): MD5 checksum of the download. If None, do not check\n    \"\"\"\n    import urllib\n\n    root = os.path.expanduser(root)\n    if not filename:\n        filename = os.path.basename(url)\n    fpath = os.path.join(root, filename)\n\n    os.makedirs(root, exist_ok=True)\n\n    # check if file is already present locally\n    if check_integrity(fpath, md5):\n        print('Using downloaded and verified file: ' + fpath)\n    else:   # download the file\n        try:\n            print('Downloading ' + url + ' to ' + fpath)\n            urllib.request.urlretrieve(\n                url, fpath,\n                reporthook=gen_bar_updater()\n            )\n        except (urllib.error.URLError, IOError) as e:\n            if url[:5] == 'https':\n                url = url.replace('https:', 'http:')\n                print('Failed download. Trying https -> http instead.'\n                      ' Downloading ' + url + ' to ' + fpath)\n                urllib.request.urlretrieve(\n                    url, fpath,\n                    reporthook=gen_bar_updater()\n                )\n            else:\n                raise e\n        # check integrity of downloaded file\n        if not check_integrity(fpath, md5):\n            raise RuntimeError(\"File not found or corrupted.\")\n\n\ndef list_dir(root, prefix=False):\n    \"\"\"List all directories at a given root\n\n    Args:\n        root (str): Path to directory whose folders need to be listed\n        prefix (bool, optional): If true, prepends the path to each result, otherwise\n            only returns the name of the directories found\n    \"\"\"\n    root = os.path.expanduser(root)\n    directories = [p for p in os.listdir(root) if os.path.isdir(os.path.join(root, p))]\n    if prefix is True:\n        directories = [os.path.join(root, d) for d in directories]\n    return directories\n\n\ndef list_files(root, suffix, prefix=False):\n    \"\"\"List all files ending with a suffix at a given root\n\n    Args:\n        root (str): Path to directory whose folders need to be listed\n        suffix (str or tuple): Suffix of the files to match, e.g. '.png' or ('.jpg', '.png').\n            It uses the Python \"str.endswith\" method and is passed directly\n        prefix (bool, optional): If true, prepends the path to each result, otherwise\n            only returns the name of the files found\n    \"\"\"\n    root = os.path.expanduser(root)\n    files = [p for p in os.listdir(root) if os.path.isfile(os.path.join(root, p)) and p.endswith(suffix)]\n    if prefix is True:\n        files = [os.path.join(root, d) for d in files]\n    return files\n\n\ndef _quota_exceeded(response: \"requests.models.Response\") -> bool:\n    return \"Google Drive - Quota exceeded\" in response.text\n\n\ndef download_file_from_google_drive(file_id, root, filename=None, md5=None):\n    \"\"\"Download a Google Drive file from  and place it in root.\n\n    Args:\n        file_id (str): id of file to be downloaded\n        root (str): Directory to place downloaded file in\n        filename (str, optional): Name to save the file under. If None, use the id of the file.\n        md5 (str, optional): MD5 checksum of the download. If None, do not check\n    \"\"\"\n    # Based on https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url\n    import requests\n    url = \"https://docs.google.com/uc?export=download\"\n\n    root = os.path.expanduser(root)\n    if not filename:\n        filename = file_id\n    fpath = os.path.join(root, filename)\n\n    os.makedirs(root, exist_ok=True)\n\n    if os.path.isfile(fpath) and check_integrity(fpath, md5):\n        print('Using downloaded and verified file: ' + fpath)\n    else:\n        session = requests.Session()\n\n        response = session.get(url, params={'id': file_id}, stream=True)\n        token = _get_confirm_token(response)\n\n        if token:\n            params = {'id': file_id, 'confirm': token}\n            response = session.get(url, params=params, stream=True)\n\n        if _quota_exceeded(response):\n            msg = (\n                f\"The daily quota of the file {filename} is exceeded and it \"\n                f\"can't be downloaded. This is a limitation of Google Drive \"\n                f\"and can only be overcome by trying again later.\"\n            )\n            raise RuntimeError(msg)\n\n        _save_response_content(response, fpath)\n\n\ndef _get_confirm_token(response):\n    for key, value in response.cookies.items():\n        if key.startswith('download_warning'):\n            return value\n\n    return None\n\n\ndef _save_response_content(response, destination, chunk_size=32768):\n    with open(destination, \"wb\") as f:\n        pbar = tqdm(total=None)\n        progress = 0\n        for chunk in response.iter_content(chunk_size):\n            if chunk:  # filter out keep-alive new chunks\n                f.write(chunk)\n                progress += len(chunk)\n                pbar.update(progress - pbar.n)\n        pbar.close()\n\n\ndef _is_tarxz(filename):\n    return filename.endswith(\".tar.xz\")\n\n\ndef _is_tar(filename):\n    return filename.endswith(\".tar\")\n\n\ndef _is_targz(filename):\n    return filename.endswith(\".tar.gz\")\n\n\ndef _is_tgz(filename):\n    return filename.endswith(\".tgz\")\n\n\ndef _is_gzip(filename):\n    return filename.endswith(\".gz\") and not filename.endswith(\".tar.gz\")\n\n\ndef _is_zip(filename):\n    return filename.endswith(\".zip\")\n\n\ndef extract_archive(from_path, to_path=None, remove_finished=False):\n    if to_path is None:\n        to_path = os.path.dirname(from_path)\n\n    if _is_tar(from_path):\n        with tarfile.open(from_path, 'r') as tar:\n            tar.extractall(path=to_path)\n    elif _is_targz(from_path) or _is_tgz(from_path):\n        with tarfile.open(from_path, 'r:gz') as tar:\n            tar.extractall(path=to_path)\n    elif _is_tarxz(from_path):\n        with tarfile.open(from_path, 'r:xz') as tar:\n            tar.extractall(path=to_path)\n    elif _is_gzip(from_path):\n        to_path = os.path.join(to_path, os.path.splitext(os.path.basename(from_path))[0])\n        with open(to_path, \"wb\") as out_f, gzip.GzipFile(from_path) as zip_f:\n            out_f.write(zip_f.read())\n    elif _is_zip(from_path):\n        with zipfile.ZipFile(from_path, 'r') as z:\n            z.extractall(to_path)\n    else:\n        raise ValueError(\"Extraction of {} not supported\".format(from_path))\n\n    if remove_finished:\n        os.remove(from_path)\n\n\ndef download_and_extract_archive(url, download_root, extract_root=None, filename=None,\n                                 md5=None, remove_finished=False):\n    download_root = os.path.expanduser(download_root)\n    if extract_root is None:\n        extract_root = download_root\n    if not filename:\n        filename = os.path.basename(url)\n\n    download_url(url, download_root, filename, md5)\n\n    archive = os.path.join(download_root, filename)\n    print(\"Extracting {} to {}\".format(archive, extract_root))\n    extract_archive(archive, extract_root, remove_finished)\n\n\ndef iterable_to_str(iterable):\n    return \"'\" + \"', '\".join([str(item) for item in iterable]) + \"'\"\n\n\ndef verify_str_arg(value, arg=None, valid_values=None, custom_msg=None):\n    if not isinstance(value, torch._six.string_classes):\n        if arg is None:\n            msg = \"Expected type str, but got type {type}.\"\n        else:\n            msg = \"Expected type str for argument {arg}, but got type {type}.\"\n        msg = msg.format(type=type(value), arg=arg)\n        raise ValueError(msg)\n\n    if valid_values is None:\n        return value\n\n    if value not in valid_values:\n        if custom_msg is not None:\n            msg = custom_msg\n        else:\n            msg = (\"Unknown value '{value}' for argument {arg}. \"\n                   \"Valid values are {{{valid_values}}}.\")\n            msg = msg.format(value=value, arg=arg,\n                             valid_values=iterable_to_str(valid_values))\n        raise ValueError(msg)\n\n    return value\n", "path": "torchvision/datasets/utils.py"}]}