From d2aee05ae2c7b452b99fa29001fe75dfe13fee20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Sun, 18 Feb 2024 12:34:57 +0000 Subject: [PATCH 1/2] Update to new ruff --- Makefile | 2 ++ breezy/bzr/inventory.py | 2 +- breezy/bzr/remote.py | 5 ++++- breezy/bzr/tests/test_knit.py | 4 +--- breezy/bzr/tests/test_weave.py | 4 +--- breezy/bzr/vf_repository.py | 5 ++--- breezy/bzr/weave.py | 10 ++-------- breezy/plugins/stats/cmds.py | 10 ++++------ breezy/plugins/weave_fmt/bzrdir.py | 4 +--- breezy/tests/test_http.py | 8 ++------ breezy/transport/__init__.py | 4 +--- breezy/ui/text.py | 4 +--- crates/bazaar-py/src/inventory.rs | 30 ++++++++++++++---------------- crates/graph-py/src/lib.rs | 9 +++------ crates/transport-py/src/lib.rs | 2 +- crates/transport-py/src/sftp.rs | 1 - pyproject.toml | 10 +++++----- 17 files changed, 45 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index d7b2f28289..f40ebca97e 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,7 @@ all: extensions extensions: @echo "building extension modules." $(PYTHON) setup.py build_ext -i $(PYTHON_BUILDFLAGS) + $(PYTHON) setup.py build_rust -i $(PYTHON_BUILDFLAGS) check:: docs check-nodocs @@ -238,6 +239,7 @@ exe: @echo *** Make brz.exe $(PYTHON) tools/win32/ostools.py remove breezy/*.pyd $(PYTHON) setup.py build_ext -i -f $(PYTHON_BUILDFLAGS) + $(PYTHON) setup.py build_rust -i -f $(PYTHON_BUILDFLAGS) $(PYTHON) setup.py py2exe > py2exe.log $(PYTHON) tools/win32/ostools.py copytodir tools/win32/start_brz.bat win32_brz.exe $(PYTHON) tools/win32/ostools.py copytodir tools/win32/breezy.url win32_brz.exe diff --git a/breezy/bzr/inventory.py b/breezy/bzr/inventory.py index 24857369b0..7b0aec2a97 100644 --- a/breezy/bzr/inventory.py +++ b/breezy/bzr/inventory.py @@ -765,7 +765,7 @@ def deserialise(klass, chk_store, lines, expected_revision_id): revision_id = info[b"revision_id"] root_id = info[b"root_id"] search_key_name = info.get(b"search_key_name", b"plain") - parent_id_basename_to_file_id = info.get(b"parent_id_basename_to_file_id", None) + parent_id_basename_to_file_id = info.get(b"parent_id_basename_to_file_id") if not parent_id_basename_to_file_id.startswith(b"sha1:"): raise ValueError( "parent_id_basename_to_file_id should be a sha1" diff --git a/breezy/bzr/remote.py b/breezy/bzr/remote.py index 096def56b3..348a325d9f 100644 --- a/breezy/bzr/remote.py +++ b/breezy/bzr/remote.py @@ -2823,7 +2823,10 @@ def _copy_repository_tarball(self, to_bzrdir, revision_id=None): with tarfile.open( "repository", fileobj=tar_file, mode="r|bz2" ) as tar, osutils.TemporaryDirectory() as tmpdir: - tar.extractall(tmpdir) + members = tar.getmembers() + if any(m.name.startswith("/") or ".." in m.name for m in members): + raise AssertionError("Tarball contains absolute paths") + tar.extractall(tmpdir, members=members) # noqa: S202 tmp_bzrdir = _mod_bzrdir.BzrDir.open(tmpdir) tmp_repo = tmp_bzrdir.open_repository() tmp_repo.copy_content_into(destination, revision_id) diff --git a/breezy/bzr/tests/test_knit.py b/breezy/bzr/tests/test_knit.py index 1def0ad93b..3bed33c32c 100644 --- a/breezy/bzr/tests/test_knit.py +++ b/breezy/bzr/tests/test_knit.py @@ -290,9 +290,7 @@ class MockReadvFailingTransport(MockTransport): """ def readv(self, relpath, offsets): - count = 0 - for result in MockTransport.readv(self, relpath, offsets): - count += 1 + for count, result in enumerate(MockTransport.readv(self, relpath, offsets)): # we use 2 because the first offset is the pack header, the second # is the first actual content requset if count > 2: diff --git a/breezy/bzr/tests/test_weave.py b/breezy/bzr/tests/test_weave.py index b8f0fd4040..6a3107c533 100644 --- a/breezy/bzr/tests/test_weave.py +++ b/breezy/bzr/tests/test_weave.py @@ -660,11 +660,9 @@ def test_multi_line_merge(self): k = Weave() parents = set() - i = 0 - for t in texts: + for i, t in enumerate(texts): k.add_lines(b"text%d" % i, list(parents), t) parents.add(b"text%d" % i) - i += 1 self.log("k._weave=" + pformat(k._weave)) diff --git a/breezy/bzr/vf_repository.py b/breezy/bzr/vf_repository.py index ff524b599d..22733fab00 100644 --- a/breezy/bzr/vf_repository.py +++ b/breezy/bzr/vf_repository.py @@ -465,7 +465,7 @@ def record_iter_changes(self, tree, basis_revision_id, iter_changes): carried_over = False if len(heads) == 1: # Could be a carry-over situation: - parent_entry_revs = parent_entries.get(file_id, None) + parent_entry_revs = parent_entries.get(file_id) if parent_entry_revs: parent_entry = parent_entry_revs.get(heads[0], None) else: @@ -1508,10 +1508,9 @@ def _find_file_keys_to_fetch(self, revision_ids, pb): file_ids = self.fileids_altered_by_revision_ids(revision_ids, inv_w) count = 0 num_file_ids = len(file_ids) - for file_id, altered_versions in file_ids.items(): + for count, (file_id, altered_versions) in enumerate(file_ids.items()): if pb is not None: pb.update(gettext("Fetch texts"), count, num_file_ids) - count += 1 yield ("file", file_id, altered_versions) def _find_non_file_keys_to_fetch(self, revision_ids): diff --git a/breezy/bzr/weave.py b/breezy/bzr/weave.py index 0b59201e30..4a8a386207 100755 --- a/breezy/bzr/weave.py +++ b/breezy/bzr/weave.py @@ -651,9 +651,7 @@ def _walk_internal(self, version_ids=None): istack = [] dset = set() - lineno = 0 # line of weave, 0-based - - for l in self._weave: + for lineno, l in enumerate(self._weave): if l.__class__ == tuple: c, v = l if c == b"{": @@ -668,7 +666,6 @@ def _walk_internal(self, version_ids=None): raise WeaveFormatError(f"unexpected instruction {v!r}") else: yield lineno, istack[-1], frozenset(dset), l - lineno += 1 if istack: raise WeaveFormatError( @@ -740,8 +737,6 @@ def _extract(self, versions): iset = set() dset = set() - lineno = 0 # line of weave, 0-based - isactive = None result = [] @@ -770,7 +765,7 @@ def _extract(self, versions): # 'in' test could dominate, so I'm leaving this change in place - when # its fast enough to consider profiling big datasets we can review. - for l in self._weave: + for lineno, l in enumerate(self._weave): if l.__class__ == tuple: c, v = l isactive = None @@ -792,7 +787,6 @@ def _extract(self, versions): isactive = (not dset) and istack and (istack[-1] in included) if isactive: result.append((istack[-1], lineno, l)) - lineno += 1 if istack: raise WeaveFormatError( "unclosed insertion blocks " "at end of weave: %s" % istack diff --git a/breezy/plugins/stats/cmds.py b/breezy/plugins/stats/cmds.py index 61609e95b1..d060fec217 100644 --- a/breezy/plugins/stats/cmds.py +++ b/breezy/plugins/stats/cmds.py @@ -291,10 +291,10 @@ def run(self, location="."): with a_branch.lock_read(): graph = a_branch.repository.get_graph() revno = 0 - cur_parents = 0 sorted_graph = tsort.merge_sort(graph.iter_ancestry([last_rev]), last_rev) - for _num, _node_name, depth, _isend in reversed(sorted_graph): - cur_parents += 1 + for cur_parents, (_num, _node_name, depth, _isend) in enumerate( + reversed(sorted_graph), 1 + ): if depth == 0: revno += 1 self.outf.write("%4d, %4d\n" % (revno, cur_parents)) @@ -304,15 +304,13 @@ def gather_class_stats(repository, revs): ret = {} total = 0 with ui.ui_factory.nested_progress_bar() as pb, repository.lock_read(): - i = 0 - for delta in repository.get_revision_deltas(revs): + for i, delta in enumerate(repository.get_revision_deltas(revs)): pb.update("classifying commits", i, len(revs)) for c in classify_delta(delta): if c not in ret: ret[c] = 0 ret[c] += 1 total += 1 - i += 1 return ret, total diff --git a/breezy/plugins/weave_fmt/bzrdir.py b/breezy/plugins/weave_fmt/bzrdir.py index 66b2490a75..f34d31bc51 100644 --- a/breezy/plugins/weave_fmt/bzrdir.py +++ b/breezy/plugins/weave_fmt/bzrdir.py @@ -357,11 +357,9 @@ def _write_all_weaves(self): transaction = WriteTransaction() try: - i = 0 - for file_id, file_weave in self.text_weaves.items(): + for i, (file_id, file_weave) in enumerate(self.text_weaves.items()): self.pb.update(gettext("writing weave"), i, len(self.text_weaves)) weaves._put_weave(file_id, file_weave, transaction) - i += 1 self.pb.update(gettext("inventory"), 0, 1) controlweaves._put_weave(b"inventory", self.inv_weave, transaction) self.pb.update(gettext("inventory"), 1, 1) diff --git a/breezy/tests/test_http.py b/breezy/tests/test_http.py index 6ad26ab5a5..d8bf90ca5f 100644 --- a/breezy/tests/test_http.py +++ b/breezy/tests/test_http.py @@ -963,8 +963,7 @@ def get_multiple_ranges(self, file, file_size, ranges): self.end_headers() # Send the multipart body - cur = 0 - for start, end in ranges: + for cur, (start, end) in enumerate(ranges): self.wfile.write(boundary_line) self.send_header("Content-type", "application/octet-stream") self.send_header( @@ -976,7 +975,6 @@ def get_multiple_ranges(self, file, file_size, ranges): self.close_connection = 1 return self.send_range_content(file, start, end - start + 1) - cur += 1 # Final boundary self.wfile.write(boundary_line) @@ -1038,8 +1036,7 @@ def get_multiple_ranges(self, file, file_size, ranges): self.end_headers() # Send the multipart body - cur = 0 - for start, end in ranges: + for cur, (start, end) in enumerate(ranges): if cur + self._truncated_ranges >= len(ranges): # Abruptly ends the response and close the connection self.close_connection = 1 @@ -1051,7 +1048,6 @@ def get_multiple_ranges(self, file, file_size, ranges): ) self.end_headers() self.send_range_content(file, start, end - start + 1) - cur += 1 # Final boundary self.wfile.write(boundary_line) diff --git a/breezy/transport/__init__.py b/breezy/transport/__init__.py index cd6ae3fe27..d71832eb93 100644 --- a/breezy/transport/__init__.py +++ b/breezy/transport/__init__.py @@ -552,14 +552,12 @@ def _iterate_over(self, multi, func, pb, msg, expand=True): """ total = self._get_total(multi) result = [] - count = 0 - for entry in multi: + for count, entry in enumerate(multi): self._update_pb(pb, msg, count, total) if expand: result.append(func(*entry)) else: result.append(func(entry)) - count += 1 return tuple(result) def abspath(self, relpath): diff --git a/breezy/ui/text.py b/breezy/ui/text.py index 84cf18569e..98fb3a418b 100644 --- a/breezy/ui/text.py +++ b/breezy/ui/text.py @@ -72,13 +72,12 @@ def _build_alternatives(self, msg, choices, default): Setup final prompt and the lists of choices and associated shortcuts. """ - index = 0 help_list = [] self.alternatives = {} choices = choices.split("\n") if default is not None and default not in range(0, len(choices)): raise ValueError("invalid default index") - for c in choices: + for index, c in enumerate(choices): name = c.replace("&", "").lower() choice = (name, index) if name in self.alternatives: @@ -103,7 +102,6 @@ def _build_alternatives(self, msg, choices, default): self.alternatives[""] = choice self.alternatives["\r"] = choice help_list.append(help) - index += 1 self.prompt = f"{msg} ({', '.join(help_list)}): " diff --git a/crates/bazaar-py/src/inventory.rs b/crates/bazaar-py/src/inventory.rs index e66002fe20..e804c3e4d9 100644 --- a/crates/bazaar-py/src/inventory.rs +++ b/crates/bazaar-py/src/inventory.rs @@ -252,23 +252,21 @@ impl InventoryEntry { // * there was a bug in revision updates with executable bit support let mut candidate = candidate.extract::>(py)?; - match (&mut candidate.0, &mut entry.0) { - ( - Entry::File { - executable: candidate_executable, - .. - }, - Entry::File { - executable: entry_executable, - .. - }, - ) => { - if candidate_executable != entry_executable { - *entry_executable = false; - *candidate_executable = false; - } + if let ( + Entry::File { + executable: candidate_executable, + .. + }, + Entry::File { + executable: entry_executable, + .. + }, + ) = (&mut candidate.0, &mut entry.0) + { + if candidate_executable != entry_executable { + *entry_executable = false; + *candidate_executable = false; } - _ => {} } } else { // add this revision as a candidate. diff --git a/crates/graph-py/src/lib.rs b/crates/graph-py/src/lib.rs index f716d62eae..ef831b1e42 100644 --- a/crates/graph-py/src/lib.rs +++ b/crates/graph-py/src/lib.rs @@ -301,13 +301,10 @@ impl MergeSorter { }; // The null: revision doesn't exist in the graph, so don't attempt to remove it - match branch_tip { - Some(ref mut tip_obj) => { - if branch_tip_is_null(py, tip_obj.clone_ref(py)) { - branch_tip = None; - } + if let Some(ref mut tip_obj) = branch_tip { + if branch_tip_is_null(py, tip_obj.clone_ref(py)) { + branch_tip = None; } - None => (), } let sorter = breezy_graph::tsort::MergeSorter::::new( diff --git a/crates/transport-py/src/lib.rs b/crates/transport-py/src/lib.rs index 7c69bcc572..1c5ec78fd4 100644 --- a/crates/transport-py/src/lib.rs +++ b/crates/transport-py/src/lib.rs @@ -1,5 +1,5 @@ use breezy_transport::lock::{FileLock, Lock as LockTrait, LockError}; -use breezy_transport::{Error, ReadStream, Transport as TransportTrait, UrlFragment, WriteStream}; +use breezy_transport::{Error, ReadStream, Transport as _, UrlFragment, WriteStream}; use log::debug; use pyo3::exceptions::{PyRuntimeError, PyValueError}; use pyo3::import_exception; diff --git a/crates/transport-py/src/sftp.rs b/crates/transport-py/src/sftp.rs index aa25661cff..d7e9d7793d 100644 --- a/crates/transport-py/src/sftp.rs +++ b/crates/transport-py/src/sftp.rs @@ -5,7 +5,6 @@ use pyo3::prelude::*; use pyo3::types::{PyBytes, PyType}; use std::collections::VecDeque; -use std::io::{Read, Seek}; use std::sync::Arc; create_exception!(breezy._transport_rs, SFTPError, PyException); diff --git a/pyproject.toml b/pyproject.toml index 401b214d99..e7784ca33c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,6 +97,9 @@ version = {attr = "breezy.__version__"} ignore_missing_imports = true [tool.ruff] +extend-exclude = ["lib", "bin"] + +[tool.ruff.lint] select = [ "ANN", # annotations "D", # pydocstyle @@ -173,15 +176,12 @@ ignore = [ ] # These are actually fine, but they make mypy more strict and then it fails. unfixable = ["ANN204"] -extend-exclude = ["lib", "bin"] - -[tool.ruff.isort] -[tool.ruff.extend-per-file-ignores] +[tool.ruff.lint.extend-per-file-ignores] "breezy/plugins/po_merge/tests/test_po_merge.py" = ["RUF001"] -[tool.ruff.pydocstyle] +[tool.ruff.lint.pydocstyle] convention = "google" [tool.setuptools-gettext] From 6e216fadae3ffd6e5e0a3fc4e9798a9698c8e1ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Sun, 18 Feb 2024 14:24:20 +0000 Subject: [PATCH 2/2] Fix test --- breezy/bzr/tests/test_knit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/breezy/bzr/tests/test_knit.py b/breezy/bzr/tests/test_knit.py index 3bed33c32c..afe19cc860 100644 --- a/breezy/bzr/tests/test_knit.py +++ b/breezy/bzr/tests/test_knit.py @@ -290,7 +290,7 @@ class MockReadvFailingTransport(MockTransport): """ def readv(self, relpath, offsets): - for count, result in enumerate(MockTransport.readv(self, relpath, offsets)): + for count, result in enumerate(MockTransport.readv(self, relpath, offsets), 1): # we use 2 because the first offset is the pack header, the second # is the first actual content requset if count > 2: