Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Created October 25, 2018 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save invisiblefunnel/f6eb8f6559ba4b22115bcf8ac5a32716 to your computer and use it in GitHub Desktop.
Save invisiblefunnel/f6eb8f6559ba4b22115bcf8ac5a32716 to your computer and use it in GitHub Desktop.
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