mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-13 16:11:15 +00:00
misc: replace os.path with Pathlib
also change some strings to use f-strings
This commit is contained in:
parent
d8df8cc869
commit
df93a225a8
@ -31,6 +31,7 @@ import sys
|
|||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
|
from pathlib import Path
|
||||||
from subprocess import check_output, CalledProcessError
|
from subprocess import check_output, CalledProcessError
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
@ -126,16 +127,16 @@ def readlist(filename, uniq=True):
|
|||||||
Read a list of words from the indicated file. If 'uniq' is True, filter
|
Read a list of words from the indicated file. If 'uniq' is True, filter
|
||||||
out duplicated words.
|
out duplicated words.
|
||||||
"""
|
"""
|
||||||
|
p = Path(filename)
|
||||||
|
|
||||||
if not os.path.isfile(filename):
|
if not p.is_file():
|
||||||
Logger.error('File "%s" does not exist.' % filename)
|
Logger.error(f'File {p} does not exist.')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
with open(filename) as f:
|
content = p.read_text().replace('\n', ' ').replace(',', ' ')
|
||||||
content = f.read().replace('\n', ' ').replace(',', ' ')
|
|
||||||
|
|
||||||
if not content.strip():
|
if not content.strip():
|
||||||
Logger.error('File "%s" is empty.' % filename)
|
Logger.error(f'File {p} is empty.')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
items = [item for item in content.split() if item]
|
items = [item for item in content.split() if item]
|
||||||
@ -211,7 +212,7 @@ def verify_file_checksums(pathname, checksums={}, size=0):
|
|||||||
|
|
||||||
Any failure will log an error.
|
Any failure will log an error.
|
||||||
|
|
||||||
pathname: str
|
pathname: str or Path
|
||||||
full path to file
|
full path to file
|
||||||
checksums: dict
|
checksums: dict
|
||||||
keys are alg name, values are expected checksum
|
keys are alg name, values are expected checksum
|
||||||
@ -220,30 +221,29 @@ def verify_file_checksums(pathname, checksums={}, size=0):
|
|||||||
|
|
||||||
Returns True if all checks pass, False otherwise
|
Returns True if all checks pass, False otherwise
|
||||||
"""
|
"""
|
||||||
if not os.path.isfile(pathname):
|
p = Path(pathname)
|
||||||
Logger.error('File not found: %s', pathname)
|
|
||||||
|
if not p.is_file():
|
||||||
|
Logger.error(f'File {p} not found')
|
||||||
return False
|
return False
|
||||||
filename = os.path.basename(pathname)
|
filesize = p.stat().st_size
|
||||||
if size and size != os.path.getsize(pathname):
|
if size and size != filesize:
|
||||||
Logger.error('File %s incorrect size, got %s expected %s',
|
Logger.error(f'File {p} incorrect size, got {filesize} expected {size}')
|
||||||
filename, os.path.getsize(pathname), size)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for (alg, checksum) in checksums.items():
|
for (alg, checksum) in checksums.items():
|
||||||
h = hashlib.new(alg)
|
h = hashlib.new(alg)
|
||||||
with open(pathname, 'rb') as f:
|
with p.open('rb') as f:
|
||||||
while True:
|
while True:
|
||||||
block = f.read(h.block_size)
|
block = f.read(h.block_size)
|
||||||
if len(block) == 0:
|
if len(block) == 0:
|
||||||
break
|
break
|
||||||
h.update(block)
|
h.update(block)
|
||||||
match = h.hexdigest() == checksum
|
digest = h.hexdigest()
|
||||||
if match:
|
if digest == checksum:
|
||||||
Logger.debug('File %s checksum (%s) verified: %s',
|
Logger.debug(f'File {p} checksum ({alg}) verified: {checksum}')
|
||||||
filename, alg, checksum)
|
|
||||||
else:
|
else:
|
||||||
Logger.error('File %s checksum (%s) mismatch: got %s expected %s',
|
Logger.error(f'File {p} checksum ({alg}) mismatch: got {digest} expected {checksum}')
|
||||||
filename, alg, h.hexdigest(), checksum)
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -251,7 +251,7 @@ def verify_file_checksums(pathname, checksums={}, size=0):
|
|||||||
def verify_file_checksum(pathname, alg, checksum, size=0):
|
def verify_file_checksum(pathname, alg, checksum, size=0):
|
||||||
""" verify checksum of file
|
""" verify checksum of file
|
||||||
|
|
||||||
pathname: str
|
pathname: str or Path
|
||||||
full path to file
|
full path to file
|
||||||
alg: str
|
alg: str
|
||||||
name of checksum alg
|
name of checksum alg
|
||||||
@ -268,7 +268,7 @@ 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
|
||||||
|
|
||||||
src: str
|
src: str or Path
|
||||||
Source to copy from (file path or url)
|
Source to copy from (file path or url)
|
||||||
dst: str
|
dst: str
|
||||||
Destination dir or filename
|
Destination dir or filename
|
||||||
@ -278,6 +278,7 @@ def download(src, dst, size=0):
|
|||||||
This calls urllib.request.urlopen() so it may raise the same
|
This calls urllib.request.urlopen() so it may raise the same
|
||||||
exceptions as that method (URLError or HTTPError)
|
exceptions as that method (URLError or HTTPError)
|
||||||
"""
|
"""
|
||||||
|
src = str(src)
|
||||||
if not urlparse(src).scheme:
|
if not urlparse(src).scheme:
|
||||||
src = 'file://%s' % os.path.abspath(os.path.expanduser(src))
|
src = 'file://%s' % os.path.abspath(os.path.expanduser(src))
|
||||||
dst = os.path.abspath(os.path.expanduser(dst))
|
dst = os.path.abspath(os.path.expanduser(dst))
|
||||||
@ -341,16 +342,15 @@ def download(src, dst, size=0):
|
|||||||
|
|
||||||
def _download_text(src, binary):
|
def _download_text(src, binary):
|
||||||
with tempfile.TemporaryDirectory() as d:
|
with tempfile.TemporaryDirectory() as d:
|
||||||
dst = os.path.join(d, 'dst')
|
dst = Path(d) / 'dst'
|
||||||
download(src, dst)
|
download(src, dst)
|
||||||
with open(dst, mode='rb' if binary else 'r') as f:
|
return dst.read_bytes() if binary else dst.read_text()
|
||||||
return f.read()
|
|
||||||
|
|
||||||
|
|
||||||
def download_text(src, mode=None):
|
def download_text(src, mode=None):
|
||||||
""" Return the text content of a downloaded file
|
""" Return the text content of a downloaded file
|
||||||
|
|
||||||
src: str
|
src: str or Path
|
||||||
Source to copy from (file path or url)
|
Source to copy from (file path or url)
|
||||||
mode: str
|
mode: str
|
||||||
Deprecated, ignored unless a string that contains 'b'
|
Deprecated, ignored unless a string that contains 'b'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user