Skip to content

Commit

Permalink
Merge pull request #6382 from ThomasWaldmann/pyupgrade-38-master
Browse files Browse the repository at this point in the history
run pyupgrade (py38+)
  • Loading branch information
ThomasWaldmann authored Feb 27, 2022
2 parents d1bcea9 + fade3c4 commit 49de070
Show file tree
Hide file tree
Showing 39 changed files with 156 additions and 160 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

# General information about the project.
project = 'Borg - Deduplicating Archiver'
copyright = u'2010-2014 Jonas Borgström, 2015-2022 The Borg Collective (see AUTHORS file)'
copyright = '2010-2014 Jonas Borgström, 2015-2022 The Borg Collective (see AUTHORS file)'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
6 changes: 3 additions & 3 deletions scripts/glibc_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def main():
output = subprocess.check_output(objdump % filename, shell=True,
stderr=subprocess.STDOUT)
output = output.decode()
versions = set(parse_version(match.group(1))
for match in glibc_re.finditer(output))
versions = {parse_version(match.group(1))
for match in glibc_re.finditer(output)}
requires_glibc = max(versions)
overall_versions.add(requires_glibc)
if verbose:
print("%s %s" % (filename, format_version(requires_glibc)))
print(f"{filename} {format_version(requires_glibc)}")
except subprocess.CalledProcessError:
if verbose:
print("%s errored." % filename)
Expand Down
1 change: 0 additions & 1 deletion scripts/py36-blake2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
This script checks compatibility of crypto.blake2b_256 against hashlib.blake2b in CPython 3.6.
"""
Expand Down
6 changes: 3 additions & 3 deletions setup_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def long_desc_from_readme():
with open('README.rst', 'r') as fd:
with open('README.rst') as fd:
long_description = fd.read()
# remove header, but have one \n before first headline
start = long_description.find('What is BorgBackup?')
Expand All @@ -33,7 +33,7 @@ def format_metavar(option):
elif option.nargs is None:
return option.metavar
else:
raise ValueError('Can\'t format metavar %s, unknown nargs %s!' % (option.metavar, option.nargs))
raise ValueError(f'Can\'t format metavar {option.metavar}, unknown nargs {option.nargs}!')


class build_usage(Command):
Expand Down Expand Up @@ -367,7 +367,7 @@ def generate_level(self, prefix, parser, Archiver, extra_choices=None):
subparsers = [action for action in parser._actions if 'SubParsersAction' in str(action.__class__)][0]
for subcommand in subparsers.choices:
write('| borg', '[common options]', command, subcommand, '...')
self.see_also.setdefault(command, []).append('%s-%s' % (command, subcommand))
self.see_also.setdefault(command, []).append(f'{command}-{subcommand}')
else:
if command == "borgfs":
write(command, end='')
Expand Down
14 changes: 7 additions & 7 deletions src/borg/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def __init__(self, op, os_error):

def __str__(self):
if self.op:
return '%s: %s' % (self.op, self.os_error)
return f'{self.op}: {self.os_error}'
else:
return str(self.os_error)

Expand Down Expand Up @@ -464,7 +464,7 @@ def __init__(self, repository, key, manifest, name, cache=None, create=False,
raise self.AlreadyExists(name)
i = 0
while True:
self.checkpoint_name = '%s.checkpoint%s' % (name, i and ('.%d' % i) or '')
self.checkpoint_name = '{}.checkpoint{}'.format(name, i and ('.%d' % i) or '')
if self.checkpoint_name not in manifest.archives:
break
i += 1
Expand Down Expand Up @@ -1823,7 +1823,7 @@ def replacement_chunk(size):
chunks_healthy = item.chunks_healthy if has_chunks_healthy else chunks_current
if has_chunks_healthy and len(chunks_current) != len(chunks_healthy):
# should never happen, but there was issue #3218.
logger.warning('{}: {}: Invalid chunks_healthy metadata removed!'.format(archive_name, item.path))
logger.warning(f'{archive_name}: {item.path}: Invalid chunks_healthy metadata removed!')
del item.chunks_healthy
has_chunks_healthy = False
chunks_healthy = chunks_current
Expand Down Expand Up @@ -1867,7 +1867,7 @@ def replacement_chunk(size):
# if this is first repair, remember the correct chunk IDs, so we can maybe heal the file later
item.chunks_healthy = item.chunks
if has_chunks_healthy and chunk_list == chunks_healthy:
logger.info('{}: {}: Completely healed previously damaged file!'.format(archive_name, item.path))
logger.info(f'{archive_name}: {item.path}: Completely healed previously damaged file!')
del item.chunks_healthy
item.chunks = chunk_list
if 'size' in item:
Expand Down Expand Up @@ -1902,7 +1902,7 @@ def report(msg, chunk_id, chunk_no):
logger.error(msg)

def list_keys_safe(keys):
return ', '.join((k.decode(errors='replace') if isinstance(k, bytes) else str(k) for k in keys))
return ', '.join(k.decode(errors='replace') if isinstance(k, bytes) else str(k) for k in keys)

def valid_item(obj):
if not isinstance(obj, StableDict):
Expand Down Expand Up @@ -1972,7 +1972,7 @@ def valid_item(obj):
with cache_if_remote(self.repository) as repository:
for i, info in enumerate(archive_infos):
pi.show(i)
logger.info('Analyzing archive {} ({}/{})'.format(info.name, i + 1, num_archives))
logger.info(f'Analyzing archive {info.name} ({i + 1}/{num_archives})')
archive_id = info.id
if archive_id not in self.chunks:
logger.error('Archive metadata block is missing!')
Expand Down Expand Up @@ -2008,7 +2008,7 @@ def orphan_chunks_check(self):
unused = {id_ for id_, entry in self.chunks.iteritems() if entry.refcount == 0}
orphaned = unused - self.possibly_superseded
if orphaned:
logger.error('{} orphaned objects found!'.format(len(orphaned)))
logger.error(f'{len(orphaned)} orphaned objects found!')
self.error_found = True
if self.repair and unused:
logger.info('Deleting %d orphaned and %d superseded objects...' % (
Expand Down
26 changes: 13 additions & 13 deletions src/borg/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def do_key_export(self, args, repository):
else:
manager.export(args.path)
except IsADirectoryError:
self.print_error("'{}' must be a file, not a directory".format(args.path))
self.print_error(f"'{args.path}' must be a file, not a directory")
return EXIT_ERROR
return EXIT_SUCCESS

Expand Down Expand Up @@ -1191,7 +1191,7 @@ def _delete_archives(self, args, repository):
current_archive = manifest.archives.pop(archive_name)
except KeyError:
self.exit_code = EXIT_WARNING
logger.warning('Archive {} not found ({}/{}).'.format(archive_name, i, len(archive_names)))
logger.warning(f'Archive {archive_name} not found ({i}/{len(archive_names)}).')
else:
deleted = True
if self.output_list:
Expand Down Expand Up @@ -1851,12 +1851,12 @@ def list_config(config):
value = default_values.get(key)
if value is None:
raise Error('The repository config is missing the %s key which has no default value' % key)
print('%s = %s' % (key, value))
print(f'{key} = {value}')
for key in ['last_segment_checked', ]:
value = config.get('repository', key, fallback=None)
if value is None:
continue
print('%s = %s' % (key, value))
print(f'{key} = {value}')

if not args.list:
if args.name is None:
Expand Down Expand Up @@ -2059,8 +2059,8 @@ def do_debug_search_repo_objs(self, args, repository):
def print_finding(info, wanted, data, offset):
before = data[offset - context:offset]
after = data[offset + len(wanted):offset + len(wanted) + context]
print('%s: %s %s %s == %r %r %r' % (info, before.hex(), wanted.hex(), after.hex(),
before, wanted, after))
print('{}: {} {} {} == {!r} {!r} {!r}'.format(info, before.hex(), wanted.hex(), after.hex(),
before, wanted, after))

wanted = args.wanted
try:
Expand Down Expand Up @@ -5032,15 +5032,15 @@ def sig_info_handler(sig_no, stack): # pragma: no cover
total = loc['st'].st_size
except Exception:
pos, total = 0, 0
logger.info("{0} {1}/{2}".format(path, format_file_size(pos), format_file_size(total)))
logger.info(f"{path} {format_file_size(pos)}/{format_file_size(total)}")
break
if func in ('extract_item', ): # extract op
path = loc['item'].path
try:
pos = loc['fd'].tell()
except Exception:
pos = 0
logger.info("{0} {1}/???".format(path, format_file_size(pos)))
logger.info(f"{path} {format_file_size(pos)}/???")
break


Expand Down Expand Up @@ -5078,7 +5078,7 @@ def main(): # pragma: no cover
except Error as e:
msg = e.get_message()
tb_log_level = logging.ERROR if e.traceback else logging.DEBUG
tb = '%s\n%s' % (traceback.format_exc(), sysinfo())
tb = f'{traceback.format_exc()}\n{sysinfo()}'
# we might not have logging setup yet, so get out quickly
print(msg, file=sys.stderr)
if tb_log_level == logging.ERROR:
Expand All @@ -5091,7 +5091,7 @@ def main(): # pragma: no cover
msg = e.get_message()
msgid = type(e).__qualname__
tb_log_level = logging.ERROR if e.traceback else logging.DEBUG
tb = "%s\n%s" % (traceback.format_exc(), sysinfo())
tb = f"{traceback.format_exc()}\n{sysinfo()}"
exit_code = e.exit_code
except RemoteRepository.RPCError as e:
important = e.exception_class not in ('LockTimeout', ) and e.traceback
Expand All @@ -5108,18 +5108,18 @@ def main(): # pragma: no cover
msg = 'Local Exception'
msgid = 'Exception'
tb_log_level = logging.ERROR
tb = '%s\n%s' % (traceback.format_exc(), sysinfo())
tb = f'{traceback.format_exc()}\n{sysinfo()}'
exit_code = EXIT_ERROR
except KeyboardInterrupt:
msg = 'Keyboard interrupt'
tb_log_level = logging.DEBUG
tb = '%s\n%s' % (traceback.format_exc(), sysinfo())
tb = f'{traceback.format_exc()}\n{sysinfo()}'
exit_code = EXIT_SIGNAL_BASE + 2
except SigTerm:
msg = 'Received SIGTERM'
msgid = 'Signal.SIGTERM'
tb_log_level = logging.DEBUG
tb = '%s\n%s' % (traceback.format_exc(), sysinfo())
tb = f'{traceback.format_exc()}\n{sysinfo()}'
exit_code = EXIT_SIGNAL_BASE + 15
except SigHup:
msg = 'Received SIGHUP.'
Expand Down
8 changes: 4 additions & 4 deletions src/borg/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def key_matches(self, key):
if not self.known():
return False
try:
with open(self.key_type_file, 'r') as fd:
with open(self.key_type_file) as fd:
type = fd.read()
return type == str(key.TYPE)
except OSError as exc:
Expand Down Expand Up @@ -687,13 +687,13 @@ def cached_archives():
fns = os.listdir(archive_path)
# filenames with 64 hex digits == 256bit,
# or compact indices which are 64 hex digits + ".compact"
return set(unhexlify(fn) for fn in fns if len(fn) == 64) | \
set(unhexlify(fn[:64]) for fn in fns if len(fn) == 72 and fn.endswith('.compact'))
return {unhexlify(fn) for fn in fns if len(fn) == 64} | \
{unhexlify(fn[:64]) for fn in fns if len(fn) == 72 and fn.endswith('.compact')}
else:
return set()

def repo_archives():
return set(info.id for info in self.manifest.archives.list())
return {info.id for info in self.manifest.archives.list()}

def cleanup_outdated(ids):
for id in ids:
Expand Down
2 changes: 1 addition & 1 deletion src/borg/crypto/file_integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def integrity_file_path(path):
@classmethod
def read_integrity_file(cls, path):
try:
with open(cls.integrity_file_path(path), 'r') as fd:
with open(cls.integrity_file_path(path)) as fd:
return cls.parse_integrity_data(path, fd.read())
except FileNotFoundError:
logger.info('No integrity file found for %s', path)
Expand Down
8 changes: 4 additions & 4 deletions src/borg/crypto/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def decrypt(self, id, data, decompress=True):
try:
payload = self.cipher.decrypt(data)
except IntegrityError as e:
raise IntegrityError("Chunk %s: Could not decrypt [%s]" % (bin_to_hex(id), str(e)))
raise IntegrityError(f"Chunk {bin_to_hex(id)}: Could not decrypt [{str(e)}]")
if not decompress:
return payload
data = self.decompress(payload)
Expand Down Expand Up @@ -469,7 +469,7 @@ def getpass(cls, prompt):
msg = []
for env_var in 'BORG_PASSPHRASE', 'BORG_PASSCOMMAND':
env_var_set = os.environ.get(env_var) is not None
msg.append('%s is %s.' % (env_var, 'set' if env_var_set else 'not set'))
msg.append('{} is {}.'.format(env_var, 'set' if env_var_set else 'not set'))
msg.append('Interactive password query failed.')
raise NoPassphraseFailure(' '.join(msg)) from None
else:
Expand Down Expand Up @@ -760,7 +760,7 @@ def _get_new_target_in_keys_dir(self, args):
return path

def load(self, target, passphrase):
with open(target, 'r') as fd:
with open(target) as fd:
key_data = ''.join(fd.readlines()[1:])
success = self._load(key_data, passphrase)
if success:
Expand All @@ -775,7 +775,7 @@ def save(self, target, passphrase, create=False):
raise Error('Aborting because key in "%s" already exists.' % target)
key_data = self._save(passphrase)
with SaveFile(target) as fd:
fd.write('%s %s\n' % (self.FILE_ID, bin_to_hex(self.repository_id)))
fd.write(f'{self.FILE_ID} {bin_to_hex(self.repository_id)}\n')
fd.write(key_data)
fd.write('\n')
self.target = target
Expand Down
12 changes: 6 additions & 6 deletions src/borg/crypto/keymanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def load_keyblob(self):
if self.keyblob_storage == KeyBlobStorage.KEYFILE:
k = KeyfileKey(self.repository)
target = k.find_key()
with open(target, 'r') as fd:
with open(target) as fd:
self.keyblob = ''.join(fd.readlines()[1:])

elif self.keyblob_storage == KeyBlobStorage.REPO:
Expand All @@ -68,7 +68,7 @@ def store_keyblob(self, args):
self.repository.save_key(self.keyblob.encode('utf-8'))

def get_keyfile_data(self):
data = '%s %s\n' % (KeyfileKey.FILE_ID, bin_to_hex(self.repository.id))
data = f'{KeyfileKey.FILE_ID} {bin_to_hex(self.repository.id)}\n'
data += self.keyblob
if not self.keyblob.endswith('\n'):
data += '\n'
Expand Down Expand Up @@ -115,7 +115,7 @@ def grouped(s):
lines = (len(binary) + 17) // 18
repoid = bin_to_hex(self.repository.id)[:18]
complete_checksum = sha256_truncated(binary, 12)
export += 'id: {0:d} / {1} / {2} - {3}\n'.format(lines,
export += 'id: {:d} / {} / {} - {}\n'.format(lines,
grouped(repoid),
grouped(complete_checksum),
sha256_truncated((str(lines) + '/' + repoid + '/' + complete_checksum).encode('ascii'), 2))
Expand All @@ -124,7 +124,7 @@ def grouped(s):
idx += 1
binline = binary[:18]
checksum = sha256_truncated(idx.to_bytes(2, byteorder='big') + binline, 2)
export += '{0:2d}: {1} - {2}\n'.format(idx, grouped(bin_to_hex(binline)), checksum)
export += f'{idx:2d}: {grouped(bin_to_hex(binline))} - {checksum}\n'
binary = binary[18:]

with dash_open(path, 'w') as fd:
Expand Down Expand Up @@ -188,7 +188,7 @@ def import_paperkey(self, args):
idx = 1
# body line input
while True:
inline = input('{0:2d}: '.format(idx))
inline = input(f'{idx:2d}: ')
inline = inline.replace(' ', '')
if inline == '':
if yes('Abort import? [yN]:'):
Expand All @@ -204,7 +204,7 @@ def import_paperkey(self, args):
print("only characters 0-9 and a-f and '-' are valid, try again")
continue
if sha256_truncated(idx.to_bytes(2, byteorder='big') + part, 2) != checksum:
print('line checksum did not match, try line {0} again'.format(idx))
print(f'line checksum did not match, try line {idx} again')
continue
result += part
if idx == lines:
Expand Down
2 changes: 1 addition & 1 deletion src/borg/crypto/nonces.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, repository, manifest_nonce):

def get_local_free_nonce(self):
try:
with open(self.nonce_file, 'r') as fd:
with open(self.nonce_file) as fd:
return bytes_to_long(unhexlify(fd.read()))
except FileNotFoundError:
return None
Expand Down
2 changes: 1 addition & 1 deletion src/borg/fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def iter_archive_items(self, archive_item_ids, filter=None, consider_part_files=
self.write_offset = write_offset


class FuseBackend(object):
class FuseBackend:
"""Virtual filesystem based on archive(s) to provide information to fuse
"""

Expand Down
2 changes: 1 addition & 1 deletion src/borg/helpers/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def get_all_mandatory_features(self):

for operation, requirements in feature_flags.items():
if b'mandatory' in requirements:
result[operation.decode()] = set([feature.decode() for feature in requirements[b'mandatory']])
result[operation.decode()] = {feature.decode() for feature in requirements[b'mandatory']}
return result

def write(self):
Expand Down
6 changes: 3 additions & 3 deletions src/borg/helpers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ def sysinfo():
from ..fuse_impl import llfuse, BORG_FUSE_IMPL
llfuse_name = llfuse.__name__ if llfuse else 'None'
llfuse_version = (' %s' % llfuse.__version__) if llfuse else ''
llfuse_info = '%s%s [%s]' % (llfuse_name, llfuse_version, BORG_FUSE_IMPL)
llfuse_info = f'{llfuse_name}{llfuse_version} [{BORG_FUSE_IMPL}]'
info = []
if uname is not None:
info.append('Platform: %s' % (' '.join(uname), ))
info.append('Platform: {}'.format(' '.join(uname)))
if linux_distribution is not None:
info.append('Linux: %s %s %s' % linux_distribution)
info.append('Borg: %s Python: %s %s msgpack: %s fuse: %s' % (
info.append('Borg: {} Python: {} {} msgpack: {} fuse: {}'.format(
borg_version, python_implementation, python_version, msgpack_version, llfuse_info))
info.append('PID: %d CWD: %s' % (os.getpid(), os.getcwd()))
info.append('sys.argv: %r' % sys.argv)
Expand Down
Loading

0 comments on commit 49de070

Please sign in to comment.