Created
October 25, 2018 21:19
-
-
Save invisiblefunnel/f6eb8f6559ba4b22115bcf8ac5a32716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
import os | |
import subprocess | |
import tempfile | |
@contextmanager | |
def maybe_download_remote(url_or_path, **kwargs): | |
""" | |
Adapted from: https://git.io/fxMdS | |
""" | |
if _is_url(url_or_path): | |
_, path = tempfile.mkstemp(**kwargs) | |
try: | |
args = ["curl", "-L", "-o", path, url_or_path] | |
p = subprocess.Popen(args) | |
_, err = p.communicate() | |
e = p.wait() | |
if e != 0: | |
raise Exception(err) | |
yield path | |
finally: | |
if os.access(path, os.F_OK): | |
os.unlink(path) | |
else: | |
yield url_or_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment