We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Use Ruff for linting and formatting
Proposal to use[ Ruff ](https://docs.astral.sh/ruff/) for linting and code quality. Due to following reasons:

-  It's faster 
-  Can fix code automatically 


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `scripts/past_bugs_by_unit.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 # You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 import argparse
7 import json
8 import logging
9 from collections import defaultdict
10 
11 from tqdm import tqdm
12 
13 from bugbug import bugzilla, db, repository
14 from bugbug.models.regressor import BUG_FIXING_COMMITS_DB
15 from bugbug.utils import zstd_compress
16 
17 logging.basicConfig(level=logging.INFO)
18 logger = logging.getLogger(__name__)
19 
20 
21 class PastBugsCollector(object):
22     def __init__(self) -> None:
23         logger.info("Downloading commits database...")
24         assert db.download(repository.COMMITS_DB)
25 
26         logger.info("Downloading bugs database...")
27         assert db.download(bugzilla.BUGS_DB)
28 
29         logger.info("Download commit classifications...")
30         assert db.download(BUG_FIXING_COMMITS_DB)
31 
32     def go(self) -> None:
33         logger.info(
34             "Generate map of bug ID -> bug data for all bugs which were defects"
35         )
36         bug_fixing_commits = list(db.read(BUG_FIXING_COMMITS_DB))
37 
38         bug_fixing_commits_nodes = set(
39             bug_fixing_commit["rev"]
40             for bug_fixing_commit in bug_fixing_commits
41             if bug_fixing_commit["type"] in ("d", "r")
42         )
43 
44         logger.info("%d bug-fixing commits to analyze", len(bug_fixing_commits_nodes))
45 
46         all_bug_ids = set(commit["bug_id"] for commit in repository.get_commits())
47 
48         bug_map = {
49             bug["id"]: bug for bug in bugzilla.get_bugs() if bug["id"] in all_bug_ids
50         }
51 
52         logger.info(
53             "Generate a map from files/functions to the bugs which were fixed/introduced by touching them"
54         )
55 
56         # TODO: Support "moving" past bugs between files when they are renamed and between functions when they are
57         # moved across files.
58 
59         by_dimensions = ["file", "directory", "component"]
60 
61         def dimension_to_field(dimension: str) -> str:
62             return f"{dimension}s" if dimension != "directory" else "directories"
63 
64         past_regressions_by: dict[str, dict[str, list[int]]] = defaultdict(
65             lambda: defaultdict(list)
66         )
67         past_fixed_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(
68             lambda: defaultdict(list)
69         )
70         past_regression_blocked_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(
71             lambda: defaultdict(list)
72         )
73         past_fixed_bug_blocked_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(
74             lambda: defaultdict(list)
75         )
76         past_regressions_by_function: dict[str, dict[str, list[int]]] = defaultdict(
77             lambda: defaultdict(list)
78         )
79         past_fixed_bugs_by_function: dict[str, dict[str, list[int]]] = defaultdict(
80             lambda: defaultdict(list)
81         )
82         past_regression_blocked_bugs_by_function: dict[
83             str, dict[str, list[int]]
84         ] = defaultdict(lambda: defaultdict(list))
85         past_fixed_bug_blocked_bugs_by_function: dict[
86             str, dict[str, list[int]]
87         ] = defaultdict(lambda: defaultdict(list))
88 
89         for commit in tqdm(repository.get_commits()):
90             if commit["bug_id"] not in bug_map:
91                 continue
92 
93             if commit["backedoutby"]:
94                 continue
95 
96             bug = bug_map[commit["bug_id"]]
97 
98             if len(bug["regressions"]) > 0:
99                 for dimension in by_dimensions:
100                     for path in commit[dimension_to_field(dimension)]:
101                         past_regressions_by[dimension][path].extend(
102                             bug_id for bug_id in bug["regressions"] if bug_id in bug_map
103                         )
104 
105                         past_regression_blocked_bugs_by[dimension][path].extend(
106                             bugzilla.find_blocked_by(bug_map, bug)
107                         )
108 
109                 for path, f_group in commit["functions"].items():
110                     for f in f_group:
111                         past_regressions_by_function[path][f["name"]].extend(
112                             bug_id for bug_id in bug["regressions"] if bug_id in bug_map
113                         )
114 
115                         past_regression_blocked_bugs_by_function[path][
116                             f["name"]
117                         ].extend(bugzilla.find_blocked_by(bug_map, bug))
118 
119             if commit["node"] in bug_fixing_commits_nodes:
120                 for dimension in by_dimensions:
121                     for path in commit[dimension_to_field(dimension)]:
122                         past_fixed_bugs_by[dimension][path].append(bug["id"])
123 
124                         past_fixed_bug_blocked_bugs_by[dimension][path].extend(
125                             bugzilla.find_blocked_by(bug_map, bug)
126                         )
127 
128                 for path, f_group in commit["functions"].items():
129                     for f in f_group:
130                         past_fixed_bugs_by_function[path][f["name"]].append(bug["id"])
131 
132                         past_fixed_bug_blocked_bugs_by_function[path][f["name"]].extend(
133                             bugzilla.find_blocked_by(bug_map, bug)
134                         )
135 
136         def _transform(bug_ids: list[int]) -> list[dict]:
137             seen = set()
138             results = []
139             for bug_id in bug_ids:
140                 if bug_id in seen:
141                     continue
142                 seen.add(bug_id)
143 
144                 bug = bug_map[bug_id]
145                 results.append(
146                     {
147                         "id": bug_id,
148                         "summary": bug["summary"],
149                         "component": "{}::{}".format(bug["product"], bug["component"]),
150                     }
151                 )
152 
153             return results
154 
155         def past_bug_ids_to_summaries(
156             past_bugs_by: dict[str, list[int]]
157         ) -> dict[str, list[dict]]:
158             return {path: _transform(bug_ids) for path, bug_ids in past_bugs_by.items()}
159 
160         for dimension in by_dimensions:
161             with open(f"data/past_regressions_by_{dimension}.json", "w") as f:
162                 json.dump(past_bug_ids_to_summaries(past_regressions_by[dimension]), f)
163             zstd_compress(f"data/past_regressions_by_{dimension}.json")
164 
165             with open(f"data/past_fixed_bugs_by_{dimension}.json", "w") as f:
166                 json.dump(past_bug_ids_to_summaries(past_fixed_bugs_by[dimension]), f)
167             zstd_compress(f"data/past_fixed_bugs_by_{dimension}.json")
168 
169             with open(
170                 f"data/past_regression_blocked_bugs_by_{dimension}.json", "w"
171             ) as f:
172                 json.dump(
173                     past_bug_ids_to_summaries(
174                         past_regression_blocked_bugs_by[dimension]
175                     ),
176                     f,
177                 )
178             zstd_compress(f"data/past_regression_blocked_bugs_by_{dimension}.json")
179 
180             with open(
181                 f"data/past_fixed_bug_blocked_bugs_by_{dimension}.json", "w"
182             ) as f:
183                 json.dump(
184                     past_bug_ids_to_summaries(
185                         past_fixed_bug_blocked_bugs_by[dimension]
186                     ),
187                     f,
188                 )
189             zstd_compress(f"data/past_fixed_bug_blocked_bugs_by_{dimension}.json")
190 
191         def past_function_bug_ids_to_summaries(
192             past_bugs: dict[str, dict[str, list[int]]]
193         ) -> dict[str, dict[str, list[dict]]]:
194             return {
195                 path: {
196                     func: _transform(bug_ids) for func, bug_ids in funcs_bugs.items()
197                 }
198                 for path, funcs_bugs in past_bugs.items()
199             }
200 
201         with open("data/past_regressions_by_function.json", "w") as f:
202             json.dump(
203                 past_function_bug_ids_to_summaries(past_regressions_by_function), f
204             )
205         zstd_compress("data/past_regressions_by_function.json")
206 
207         with open("data/past_fixed_bugs_by_function.json", "w") as f:
208             json.dump(
209                 past_function_bug_ids_to_summaries(past_fixed_bugs_by_function), f
210             )
211         zstd_compress("data/past_fixed_bugs_by_function.json")
212 
213         with open("data/past_regression_blocked_bugs_by_function.json", "w") as f:
214             json.dump(
215                 past_function_bug_ids_to_summaries(
216                     past_regression_blocked_bugs_by_function
217                 ),
218                 f,
219             )
220         zstd_compress("data/past_regression_blocked_bugs_by_function.json")
221 
222         with open("data/past_fixed_bug_blocked_bugs_by_function.json", "w") as f:
223             json.dump(
224                 past_function_bug_ids_to_summaries(
225                     past_fixed_bug_blocked_bugs_by_function
226                 ),
227                 f,
228             )
229         zstd_compress("data/past_fixed_bug_blocked_bugs_by_function.json")
230 
231 
232 def main() -> None:
233     description = "Find past bugs linked to given units of source code"
234     parser = argparse.ArgumentParser(description=description)
235     parser.parse_args()
236 
237     past_bugs_collector = PastBugsCollector()
238     past_bugs_collector.go()
239 
240 
241 if __name__ == "__main__":
242     main()
243
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/scripts/past_bugs_by_unit.py b/scripts/past_bugs_by_unit.py
--- a/scripts/past_bugs_by_unit.py
+++ b/scripts/past_bugs_by_unit.py
@@ -153,7 +153,7 @@
             return results
 
         def past_bug_ids_to_summaries(
-            past_bugs_by: dict[str, list[int]]
+            past_bugs_by: dict[str, list[int]],
         ) -> dict[str, list[dict]]:
             return {path: _transform(bug_ids) for path, bug_ids in past_bugs_by.items()}
 
@@ -189,7 +189,7 @@
             zstd_compress(f"data/past_fixed_bug_blocked_bugs_by_{dimension}.json")
 
         def past_function_bug_ids_to_summaries(
-            past_bugs: dict[str, dict[str, list[int]]]
+            past_bugs: dict[str, dict[str, list[int]]],
         ) -> dict[str, dict[str, list[dict]]]:
             return {
                 path: {

{"golden_diff": "diff --git a/scripts/past_bugs_by_unit.py b/scripts/past_bugs_by_unit.py\n--- a/scripts/past_bugs_by_unit.py\n+++ b/scripts/past_bugs_by_unit.py\n@@ -153,7 +153,7 @@\n             return results\n \n         def past_bug_ids_to_summaries(\n-            past_bugs_by: dict[str, list[int]]\n+            past_bugs_by: dict[str, list[int]],\n         ) -> dict[str, list[dict]]:\n             return {path: _transform(bug_ids) for path, bug_ids in past_bugs_by.items()}\n \n@@ -189,7 +189,7 @@\n             zstd_compress(f\"data/past_fixed_bug_blocked_bugs_by_{dimension}.json\")\n \n         def past_function_bug_ids_to_summaries(\n-            past_bugs: dict[str, dict[str, list[int]]]\n+            past_bugs: dict[str, dict[str, list[int]]],\n         ) -> dict[str, dict[str, list[dict]]]:\n             return {\n                 path: {\n", "issue": "Use Ruff for linting and formatting\nProposal to use[ Ruff ](https://docs.astral.sh/ruff/) for linting and code quality. Due to following reasons:\r\n\r\n-  It's faster \r\n-  Can fix code automatically \r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n# This Source Code Form is subject to the terms of the Mozilla Public\n# License, v. 2.0. If a copy of the MPL was not distributed with this file,\n# You can obtain one at http://mozilla.org/MPL/2.0/.\n\nimport argparse\nimport json\nimport logging\nfrom collections import defaultdict\n\nfrom tqdm import tqdm\n\nfrom bugbug import bugzilla, db, repository\nfrom bugbug.models.regressor import BUG_FIXING_COMMITS_DB\nfrom bugbug.utils import zstd_compress\n\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(__name__)\n\n\nclass PastBugsCollector(object):\n    def __init__(self) -> None:\n        logger.info(\"Downloading commits database...\")\n        assert db.download(repository.COMMITS_DB)\n\n        logger.info(\"Downloading bugs database...\")\n        assert db.download(bugzilla.BUGS_DB)\n\n        logger.info(\"Download commit classifications...\")\n        assert db.download(BUG_FIXING_COMMITS_DB)\n\n    def go(self) -> None:\n        logger.info(\n            \"Generate map of bug ID -> bug data for all bugs which were defects\"\n        )\n        bug_fixing_commits = list(db.read(BUG_FIXING_COMMITS_DB))\n\n        bug_fixing_commits_nodes = set(\n            bug_fixing_commit[\"rev\"]\n            for bug_fixing_commit in bug_fixing_commits\n            if bug_fixing_commit[\"type\"] in (\"d\", \"r\")\n        )\n\n        logger.info(\"%d bug-fixing commits to analyze\", len(bug_fixing_commits_nodes))\n\n        all_bug_ids = set(commit[\"bug_id\"] for commit in repository.get_commits())\n\n        bug_map = {\n            bug[\"id\"]: bug for bug in bugzilla.get_bugs() if bug[\"id\"] in all_bug_ids\n        }\n\n        logger.info(\n            \"Generate a map from files/functions to the bugs which were fixed/introduced by touching them\"\n        )\n\n        # TODO: Support \"moving\" past bugs between files when they are renamed and between functions when they are\n        # moved across files.\n\n        by_dimensions = [\"file\", \"directory\", \"component\"]\n\n        def dimension_to_field(dimension: str) -> str:\n            return f\"{dimension}s\" if dimension != \"directory\" else \"directories\"\n\n        past_regressions_by: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_fixed_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_regression_blocked_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_fixed_bug_blocked_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_regressions_by_function: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_fixed_bugs_by_function: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_regression_blocked_bugs_by_function: dict[\n            str, dict[str, list[int]]\n        ] = defaultdict(lambda: defaultdict(list))\n        past_fixed_bug_blocked_bugs_by_function: dict[\n            str, dict[str, list[int]]\n        ] = defaultdict(lambda: defaultdict(list))\n\n        for commit in tqdm(repository.get_commits()):\n            if commit[\"bug_id\"] not in bug_map:\n                continue\n\n            if commit[\"backedoutby\"]:\n                continue\n\n            bug = bug_map[commit[\"bug_id\"]]\n\n            if len(bug[\"regressions\"]) > 0:\n                for dimension in by_dimensions:\n                    for path in commit[dimension_to_field(dimension)]:\n                        past_regressions_by[dimension][path].extend(\n                            bug_id for bug_id in bug[\"regressions\"] if bug_id in bug_map\n                        )\n\n                        past_regression_blocked_bugs_by[dimension][path].extend(\n                            bugzilla.find_blocked_by(bug_map, bug)\n                        )\n\n                for path, f_group in commit[\"functions\"].items():\n                    for f in f_group:\n                        past_regressions_by_function[path][f[\"name\"]].extend(\n                            bug_id for bug_id in bug[\"regressions\"] if bug_id in bug_map\n                        )\n\n                        past_regression_blocked_bugs_by_function[path][\n                            f[\"name\"]\n                        ].extend(bugzilla.find_blocked_by(bug_map, bug))\n\n            if commit[\"node\"] in bug_fixing_commits_nodes:\n                for dimension in by_dimensions:\n                    for path in commit[dimension_to_field(dimension)]:\n                        past_fixed_bugs_by[dimension][path].append(bug[\"id\"])\n\n                        past_fixed_bug_blocked_bugs_by[dimension][path].extend(\n                            bugzilla.find_blocked_by(bug_map, bug)\n                        )\n\n                for path, f_group in commit[\"functions\"].items():\n                    for f in f_group:\n                        past_fixed_bugs_by_function[path][f[\"name\"]].append(bug[\"id\"])\n\n                        past_fixed_bug_blocked_bugs_by_function[path][f[\"name\"]].extend(\n                            bugzilla.find_blocked_by(bug_map, bug)\n                        )\n\n        def _transform(bug_ids: list[int]) -> list[dict]:\n            seen = set()\n            results = []\n            for bug_id in bug_ids:\n                if bug_id in seen:\n                    continue\n                seen.add(bug_id)\n\n                bug = bug_map[bug_id]\n                results.append(\n                    {\n                        \"id\": bug_id,\n                        \"summary\": bug[\"summary\"],\n                        \"component\": \"{}::{}\".format(bug[\"product\"], bug[\"component\"]),\n                    }\n                )\n\n            return results\n\n        def past_bug_ids_to_summaries(\n            past_bugs_by: dict[str, list[int]]\n        ) -> dict[str, list[dict]]:\n            return {path: _transform(bug_ids) for path, bug_ids in past_bugs_by.items()}\n\n        for dimension in by_dimensions:\n            with open(f\"data/past_regressions_by_{dimension}.json\", \"w\") as f:\n                json.dump(past_bug_ids_to_summaries(past_regressions_by[dimension]), f)\n            zstd_compress(f\"data/past_regressions_by_{dimension}.json\")\n\n            with open(f\"data/past_fixed_bugs_by_{dimension}.json\", \"w\") as f:\n                json.dump(past_bug_ids_to_summaries(past_fixed_bugs_by[dimension]), f)\n            zstd_compress(f\"data/past_fixed_bugs_by_{dimension}.json\")\n\n            with open(\n                f\"data/past_regression_blocked_bugs_by_{dimension}.json\", \"w\"\n            ) as f:\n                json.dump(\n                    past_bug_ids_to_summaries(\n                        past_regression_blocked_bugs_by[dimension]\n                    ),\n                    f,\n                )\n            zstd_compress(f\"data/past_regression_blocked_bugs_by_{dimension}.json\")\n\n            with open(\n                f\"data/past_fixed_bug_blocked_bugs_by_{dimension}.json\", \"w\"\n            ) as f:\n                json.dump(\n                    past_bug_ids_to_summaries(\n                        past_fixed_bug_blocked_bugs_by[dimension]\n                    ),\n                    f,\n                )\n            zstd_compress(f\"data/past_fixed_bug_blocked_bugs_by_{dimension}.json\")\n\n        def past_function_bug_ids_to_summaries(\n            past_bugs: dict[str, dict[str, list[int]]]\n        ) -> dict[str, dict[str, list[dict]]]:\n            return {\n                path: {\n                    func: _transform(bug_ids) for func, bug_ids in funcs_bugs.items()\n                }\n                for path, funcs_bugs in past_bugs.items()\n            }\n\n        with open(\"data/past_regressions_by_function.json\", \"w\") as f:\n            json.dump(\n                past_function_bug_ids_to_summaries(past_regressions_by_function), f\n            )\n        zstd_compress(\"data/past_regressions_by_function.json\")\n\n        with open(\"data/past_fixed_bugs_by_function.json\", \"w\") as f:\n            json.dump(\n                past_function_bug_ids_to_summaries(past_fixed_bugs_by_function), f\n            )\n        zstd_compress(\"data/past_fixed_bugs_by_function.json\")\n\n        with open(\"data/past_regression_blocked_bugs_by_function.json\", \"w\") as f:\n            json.dump(\n                past_function_bug_ids_to_summaries(\n                    past_regression_blocked_bugs_by_function\n                ),\n                f,\n            )\n        zstd_compress(\"data/past_regression_blocked_bugs_by_function.json\")\n\n        with open(\"data/past_fixed_bug_blocked_bugs_by_function.json\", \"w\") as f:\n            json.dump(\n                past_function_bug_ids_to_summaries(\n                    past_fixed_bug_blocked_bugs_by_function\n                ),\n                f,\n            )\n        zstd_compress(\"data/past_fixed_bug_blocked_bugs_by_function.json\")\n\n\ndef main() -> None:\n    description = \"Find past bugs linked to given units of source code\"\n    parser = argparse.ArgumentParser(description=description)\n    parser.parse_args()\n\n    past_bugs_collector = PastBugsCollector()\n    past_bugs_collector.go()\n\n\nif __name__ == \"__main__\":\n    main()\n", "path": "scripts/past_bugs_by_unit.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n# This Source Code Form is subject to the terms of the Mozilla Public\n# License, v. 2.0. If a copy of the MPL was not distributed with this file,\n# You can obtain one at http://mozilla.org/MPL/2.0/.\n\nimport argparse\nimport json\nimport logging\nfrom collections import defaultdict\n\nfrom tqdm import tqdm\n\nfrom bugbug import bugzilla, db, repository\nfrom bugbug.models.regressor import BUG_FIXING_COMMITS_DB\nfrom bugbug.utils import zstd_compress\n\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(__name__)\n\n\nclass PastBugsCollector(object):\n    def __init__(self) -> None:\n        logger.info(\"Downloading commits database...\")\n        assert db.download(repository.COMMITS_DB)\n\n        logger.info(\"Downloading bugs database...\")\n        assert db.download(bugzilla.BUGS_DB)\n\n        logger.info(\"Download commit classifications...\")\n        assert db.download(BUG_FIXING_COMMITS_DB)\n\n    def go(self) -> None:\n        logger.info(\n            \"Generate map of bug ID -> bug data for all bugs which were defects\"\n        )\n        bug_fixing_commits = list(db.read(BUG_FIXING_COMMITS_DB))\n\n        bug_fixing_commits_nodes = set(\n            bug_fixing_commit[\"rev\"]\n            for bug_fixing_commit in bug_fixing_commits\n            if bug_fixing_commit[\"type\"] in (\"d\", \"r\")\n        )\n\n        logger.info(\"%d bug-fixing commits to analyze\", len(bug_fixing_commits_nodes))\n\n        all_bug_ids = set(commit[\"bug_id\"] for commit in repository.get_commits())\n\n        bug_map = {\n            bug[\"id\"]: bug for bug in bugzilla.get_bugs() if bug[\"id\"] in all_bug_ids\n        }\n\n        logger.info(\n            \"Generate a map from files/functions to the bugs which were fixed/introduced by touching them\"\n        )\n\n        # TODO: Support \"moving\" past bugs between files when they are renamed and between functions when they are\n        # moved across files.\n\n        by_dimensions = [\"file\", \"directory\", \"component\"]\n\n        def dimension_to_field(dimension: str) -> str:\n            return f\"{dimension}s\" if dimension != \"directory\" else \"directories\"\n\n        past_regressions_by: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_fixed_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_regression_blocked_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_fixed_bug_blocked_bugs_by: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_regressions_by_function: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_fixed_bugs_by_function: dict[str, dict[str, list[int]]] = defaultdict(\n            lambda: defaultdict(list)\n        )\n        past_regression_blocked_bugs_by_function: dict[\n            str, dict[str, list[int]]\n        ] = defaultdict(lambda: defaultdict(list))\n        past_fixed_bug_blocked_bugs_by_function: dict[\n            str, dict[str, list[int]]\n        ] = defaultdict(lambda: defaultdict(list))\n\n        for commit in tqdm(repository.get_commits()):\n            if commit[\"bug_id\"] not in bug_map:\n                continue\n\n            if commit[\"backedoutby\"]:\n                continue\n\n            bug = bug_map[commit[\"bug_id\"]]\n\n            if len(bug[\"regressions\"]) > 0:\n                for dimension in by_dimensions:\n                    for path in commit[dimension_to_field(dimension)]:\n                        past_regressions_by[dimension][path].extend(\n                            bug_id for bug_id in bug[\"regressions\"] if bug_id in bug_map\n                        )\n\n                        past_regression_blocked_bugs_by[dimension][path].extend(\n                            bugzilla.find_blocked_by(bug_map, bug)\n                        )\n\n                for path, f_group in commit[\"functions\"].items():\n                    for f in f_group:\n                        past_regressions_by_function[path][f[\"name\"]].extend(\n                            bug_id for bug_id in bug[\"regressions\"] if bug_id in bug_map\n                        )\n\n                        past_regression_blocked_bugs_by_function[path][\n                            f[\"name\"]\n                        ].extend(bugzilla.find_blocked_by(bug_map, bug))\n\n            if commit[\"node\"] in bug_fixing_commits_nodes:\n                for dimension in by_dimensions:\n                    for path in commit[dimension_to_field(dimension)]:\n                        past_fixed_bugs_by[dimension][path].append(bug[\"id\"])\n\n                        past_fixed_bug_blocked_bugs_by[dimension][path].extend(\n                            bugzilla.find_blocked_by(bug_map, bug)\n                        )\n\n                for path, f_group in commit[\"functions\"].items():\n                    for f in f_group:\n                        past_fixed_bugs_by_function[path][f[\"name\"]].append(bug[\"id\"])\n\n                        past_fixed_bug_blocked_bugs_by_function[path][f[\"name\"]].extend(\n                            bugzilla.find_blocked_by(bug_map, bug)\n                        )\n\n        def _transform(bug_ids: list[int]) -> list[dict]:\n            seen = set()\n            results = []\n            for bug_id in bug_ids:\n                if bug_id in seen:\n                    continue\n                seen.add(bug_id)\n\n                bug = bug_map[bug_id]\n                results.append(\n                    {\n                        \"id\": bug_id,\n                        \"summary\": bug[\"summary\"],\n                        \"component\": \"{}::{}\".format(bug[\"product\"], bug[\"component\"]),\n                    }\n                )\n\n            return results\n\n        def past_bug_ids_to_summaries(\n            past_bugs_by: dict[str, list[int]],\n        ) -> dict[str, list[dict]]:\n            return {path: _transform(bug_ids) for path, bug_ids in past_bugs_by.items()}\n\n        for dimension in by_dimensions:\n            with open(f\"data/past_regressions_by_{dimension}.json\", \"w\") as f:\n                json.dump(past_bug_ids_to_summaries(past_regressions_by[dimension]), f)\n            zstd_compress(f\"data/past_regressions_by_{dimension}.json\")\n\n            with open(f\"data/past_fixed_bugs_by_{dimension}.json\", \"w\") as f:\n                json.dump(past_bug_ids_to_summaries(past_fixed_bugs_by[dimension]), f)\n            zstd_compress(f\"data/past_fixed_bugs_by_{dimension}.json\")\n\n            with open(\n                f\"data/past_regression_blocked_bugs_by_{dimension}.json\", \"w\"\n            ) as f:\n                json.dump(\n                    past_bug_ids_to_summaries(\n                        past_regression_blocked_bugs_by[dimension]\n                    ),\n                    f,\n                )\n            zstd_compress(f\"data/past_regression_blocked_bugs_by_{dimension}.json\")\n\n            with open(\n                f\"data/past_fixed_bug_blocked_bugs_by_{dimension}.json\", \"w\"\n            ) as f:\n                json.dump(\n                    past_bug_ids_to_summaries(\n                        past_fixed_bug_blocked_bugs_by[dimension]\n                    ),\n                    f,\n                )\n            zstd_compress(f\"data/past_fixed_bug_blocked_bugs_by_{dimension}.json\")\n\n        def past_function_bug_ids_to_summaries(\n            past_bugs: dict[str, dict[str, list[int]]],\n        ) -> dict[str, dict[str, list[dict]]]:\n            return {\n                path: {\n                    func: _transform(bug_ids) for func, bug_ids in funcs_bugs.items()\n                }\n                for path, funcs_bugs in past_bugs.items()\n            }\n\n        with open(\"data/past_regressions_by_function.json\", \"w\") as f:\n            json.dump(\n                past_function_bug_ids_to_summaries(past_regressions_by_function), f\n            )\n        zstd_compress(\"data/past_regressions_by_function.json\")\n\n        with open(\"data/past_fixed_bugs_by_function.json\", \"w\") as f:\n            json.dump(\n                past_function_bug_ids_to_summaries(past_fixed_bugs_by_function), f\n            )\n        zstd_compress(\"data/past_fixed_bugs_by_function.json\")\n\n        with open(\"data/past_regression_blocked_bugs_by_function.json\", \"w\") as f:\n            json.dump(\n                past_function_bug_ids_to_summaries(\n                    past_regression_blocked_bugs_by_function\n                ),\n                f,\n            )\n        zstd_compress(\"data/past_regression_blocked_bugs_by_function.json\")\n\n        with open(\"data/past_fixed_bug_blocked_bugs_by_function.json\", \"w\") as f:\n            json.dump(\n                past_function_bug_ids_to_summaries(\n                    past_fixed_bug_blocked_bugs_by_function\n                ),\n                f,\n            )\n        zstd_compress(\"data/past_fixed_bug_blocked_bugs_by_function.json\")\n\n\ndef main() -> None:\n    description = \"Find past bugs linked to given units of source code\"\n    parser = argparse.ArgumentParser(description=description)\n    parser.parse_args()\n\n    past_bugs_collector = PastBugsCollector()\n    past_bugs_collector.go()\n\n\nif __name__ == \"__main__\":\n    main()\n", "path": "scripts/past_bugs_by_unit.py"}]}