misc: move logic into download() to handle plain file paths, and src == dst

Update download() function to handle src of plain path, by prepending
'file://' to it.  Also handle the case of src and dst pointing to the same
file.

Signed-off-by: Dan Streetman <ddstreet@canonical.com>
This commit is contained in:
Dan Streetman 2020-03-25 16:34:45 -04:00
parent fed562405d
commit a26a154bc9
2 changed files with 12 additions and 10 deletions

View File

@ -34,7 +34,6 @@ import codecs
import json import json
import os.path import os.path
import re import re
import shutil
import subprocess import subprocess
import sys import sys
@ -377,15 +376,7 @@ class SourcePackage(object):
Logger.info('Using existing file %s', filename) Logger.info('Using existing file %s', filename)
return True return True
if urlparse(url).scheme in ["", "file"]: download(url, pathname, size)
frompath = os.path.abspath(urlparse(url).path)
if frompath == pathname:
Logger.info("Using %s" % pathname)
else:
Logger.info("Copying %s from %s" % (filename, frompath))
shutil.copyfile(frompath, pathname)
else:
download(url, pathname, size)
return self._verify_file(pathname, dscverify, sha1sum, sha256sum, size) return self._verify_file(pathname, dscverify, sha1sum, sha256sum, size)

View File

@ -228,11 +228,22 @@ def verify_file_checksum(pathname, alg, checksum, size=0):
def download(src, dst, size=0): def download(src, dst, size=0):
""" download/copy a file/url to local file """ """ download/copy a file/url to local file """
if not urlparse(src).scheme:
src = 'file://%s' % os.path.abspath(os.path.expanduser(src))
dst = os.path.abspath(os.path.expanduser(dst))
filename = os.path.basename(urlparse(src).path) filename = os.path.basename(urlparse(src).path)
if os.path.isdir(dst): if os.path.isdir(dst):
dst = os.path.join(dst, filename) dst = os.path.join(dst, filename)
if urlparse(src).scheme == 'file':
srcfile = urlparse(src).path
if os.path.exists(srcfile) and os.path.exists(dst):
if os.path.samefile(srcfile, dst):
Logger.info(f"Using existing file {dst}")
return
with urlopen(src) as fsrc, open(dst, 'wb') as fdst: with urlopen(src) as fsrc, open(dst, 'wb') as fdst:
url = fsrc.geturl() url = fsrc.geturl()
Logger.debug(f"Using URL: {url}") Logger.debug(f"Using URL: {url}")