diff mercurial/debugcommands.py @ 35562:6580cf751418

debug: add a 'debugdownload' command This command resolve and fetch and URL through the Mercurial logic. Mercurial logic add various headers (including authentication) while resolving an URL so the commands helps with building the same request Mercurial would be doing. A new test file is created because we'll add more logic regarding Mercurial download logic and it will grow to a reasonable size.
author Boris Feld <boris.feld@octobus.net>
date Fri, 15 Dec 2017 09:30:16 +0100
parents 5880318624c9
children 35fb3367f72d
line wrap: on
line diff
--- a/mercurial/debugcommands.py	Sat Apr 01 17:12:48 2017 +0900
+++ b/mercurial/debugcommands.py	Fri Dec 15 09:30:16 2017 +0100
@@ -69,6 +69,7 @@
     templater,
     treediscovery,
     upgrade,
+    url as urlmod,
     util,
     vfs as vfsmod,
 )
@@ -786,6 +787,30 @@
     localrevs = opts['rev']
     doit(localrevs, remoterevs)
 
+_chunksize = 4 << 10
+
+@command('debugdownload',
+    [
+        ('o', 'output', '', _('path')),
+    ],
+    norepo=True)
+def debugdownload(ui, url, output=None, **opts):
+    """download a resource using Mercurial logic and config
+    """
+    fh = urlmod.open(ui, url, output)
+
+    dest = ui
+    if output:
+        dest = open(output, "wb", _chunksize)
+    try:
+        data = fh.read(_chunksize)
+        while data:
+            dest.write(data)
+            data = fh.read(_chunksize)
+    finally:
+        if output:
+            dest.close()
+
 @command('debugextensions', cmdutil.formatteropts, [], norepo=True)
 def debugextensions(ui, **opts):
     '''show information about active extensions'''