Skip to content

Commit 4ccc7e7

Browse files
committed
Add multithreading support to reduce the time taken
Change-Id: I070495bbc505ea020ecded9420fdd3b6628cbdd3
1 parent ec9bd08 commit 4ccc7e7

1 file changed

Lines changed: 25 additions & 18 deletions

File tree

linux-version-finder

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import os.path
88
import re
99
import subprocess
1010
import sys
11+
from concurrent.futures import ThreadPoolExecutor
1112
from typing import List
1213

1314
import typer
@@ -67,36 +68,41 @@ def version_key(version: str) -> int:
6768

6869

6970
def find_commits_versions(
70-
commits: list[str], all: bool, single_script_path: str
71+
commits: list[str], all: bool, threads: int, single_script_path: str
7172
) -> tuple[list[str], dict[str, list]]:
7273
"""Find all the versions associated to the commits
7374
7475
This is the first half of the tool and the part the require almost all the cpu/mem"""
7576
minor_versions = get_minor_versions()
7677
versions_count = collections.defaultdict(list)
77-
for commit in tqdm(commits):
78-
minor_version = ""
79-
versions = linux_version_finder_single(single_script_path, commit)
80-
for version in versions:
81-
minor_version = get_minor_version(version)
82-
if all:
78+
with ThreadPoolExecutor(max_workers=threads) as executor:
79+
future_to_commit_versions = {
80+
c: executor.submit(linux_version_finder_single, single_script_path, c)
81+
for c in commits
82+
}
83+
for commit in tqdm(commits):
84+
minor_version = ""
85+
versions = future_to_commit_versions[commit].result()
86+
for version in versions:
87+
minor_version = get_minor_version(version)
88+
if all:
8389
# If the --no-all option isn't used add the version
8490
# because we need to know the version where the commit
8591
# was merged.
86-
versions_count[minor_version].append(version)
87-
else:
92+
versions_count[minor_version].append(version)
93+
else:
8894
# If the --no-all option is used add the sha1 of the
8995
# commit because we need to know which commit was
9096
# merged/backported.
91-
versions_count[minor_version].append(commit)
92-
if minor_version:
93-
start = minor_versions.index(minor_version) + 1
94-
for i in range(start, len(minor_versions)):
95-
minor_version = minor_versions[i]
96-
if all:
97-
versions_count[minor_version].append(minor_version + "-rc0")
98-
else:
9997
versions_count[minor_version].append(commit)
98+
if minor_version:
99+
start = minor_versions.index(minor_version) + 1
100+
for i in range(start, len(minor_versions)):
101+
minor_version = minor_versions[i]
102+
if all:
103+
versions_count[minor_version].append(minor_version + "-rc0")
104+
else:
105+
versions_count[minor_version].append(commit)
100106
return minor_versions, versions_count
101107

102108

@@ -154,13 +160,14 @@ def print_versions_without_all_commits(
154160
def main(
155161
commits: List[str],
156162
all: bool = typer.Option(True, help="Print the versions with all commits."),
163+
threads: int = typer.Option(1, "-t", "--threads", help="Number of threads to use."),
157164
debug: bool = typer.Option(False, help="Print debug info."),
158165
):
159166
if debug:
160167
logging.basicConfig(level=logging.DEBUG)
161168
single_script_path = os.path.dirname(sys.argv[0]) + "/linux-version-finder-single"
162169
minor_versions, versions_count = find_commits_versions(
163-
commits, all, single_script_path
170+
commits, all, threads, single_script_path
164171
)
165172
logging.debug(versions_count)
166173

0 commit comments

Comments
 (0)