summaryrefslogtreecommitdiff
path: root/morphlib/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'morphlib/util.py')
-rw-r--r--morphlib/util.py29
1 files changed, 27 insertions, 2 deletions
diff --git a/morphlib/util.py b/morphlib/util.py
index 7b610001..e1bb1ebe 100644
--- a/morphlib/util.py
+++ b/morphlib/util.py
@@ -18,8 +18,10 @@ import os
import pipes
import re
import subprocess
+import sys
import textwrap
+import cliapp
import fs.osfs
import morphlib
@@ -119,7 +121,8 @@ def get_git_resolve_cache_server(settings): # pragma: no cover
return None
-def new_artifact_caches(settings, status_cb=None): # pragma: no cover
+def new_artifact_caches(settings, status_cb=None, runcmd=cliapp.runcmd,
+ verbose=False): # pragma: no cover
'''Create new objects for local and remote artifact caches.
This includes creating the directories on disk, if missing.
@@ -135,7 +138,7 @@ def new_artifact_caches(settings, status_cb=None): # pragma: no cover
# fs.osfs.OSFS(artifact_cachedir))
lac = morphlib.ostreeartifactcache.OSTreeArtifactCache(
- artifact_cachedir, status_cb=status_cb)
+ artifact_cachedir, status_cb=status_cb, runcmd=runcmd, verbose=verbose)
rac_url = get_artifact_cache_server(settings)
rac = None
@@ -679,3 +682,25 @@ def write_from_dict(filepath, d, validate=lambda x, y: True): #pragma: no cover
os.fchown(f.fileno(), 0, 0)
os.fchmod(f.fileno(), 0644)
+
+
+def fetch_url(url, target_path, runcmd=cliapp.runcmd,
+ progress_on_stdout=False): # pragma: no cover
+ '''Fetch a remote HTTP URL to the local machine.
+
+ Unlike shutil.copyfileobj(), this method makes it easy to show progress
+ info to the user, which is important when downloading large files like
+ build artifacts.
+
+ '''
+ if progress_on_stdout:
+ verbosity_flags = []
+ kwargs = dict(stderr=sys.stdout)
+ else:
+ verbosity_flags = ['--quiet']
+ kwargs = dict()
+
+ def wget_command():
+ return ['wget'] + verbosity_flags + ['-O', target_path, url]
+
+ runcmd(wget_command(), **kwargs)