From a26a154bc96c50f949599ef929f27daf5c9009a7 Mon Sep 17 00:00:00 2001 From: Dan Streetman Date: Wed, 25 Mar 2020 16:34:45 -0400 Subject: [PATCH] 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 --- ubuntutools/archive.py | 11 +---------- ubuntutools/misc.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ubuntutools/archive.py b/ubuntutools/archive.py index cfb0da3..54a7ae7 100644 --- a/ubuntutools/archive.py +++ b/ubuntutools/archive.py @@ -34,7 +34,6 @@ import codecs import json import os.path import re -import shutil import subprocess import sys @@ -377,15 +376,7 @@ class SourcePackage(object): Logger.info('Using existing file %s', filename) return True - if urlparse(url).scheme in ["", "file"]: - 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) + download(url, pathname, size) return self._verify_file(pathname, dscverify, sha1sum, sha256sum, size) diff --git a/ubuntutools/misc.py b/ubuntutools/misc.py index c0d23fe..b856455 100644 --- a/ubuntutools/misc.py +++ b/ubuntutools/misc.py @@ -228,11 +228,22 @@ def verify_file_checksum(pathname, alg, checksum, size=0): def download(src, dst, size=0): """ 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) if os.path.isdir(dst): 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: url = fsrc.geturl() Logger.debug(f"Using URL: {url}")