Skip to content

Commit

Permalink
migrate os.listdir() to os.scandir() to increase performance
Browse files Browse the repository at this point in the history
As recommended in https://peps.python.org/pep-0471/

scandir() has significantly better performance. On some places it does
not have sense, but I was doing it everywhere, because it was super
easy. I done it, because I recently done the same thing in
scancode-toolkit where it matters a lot.

Closes: #3601
  • Loading branch information
xsuchy authored and praiskup committed Jan 27, 2025
1 parent 7136605 commit 348ef2f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions dist-git/run/prune-dist-git.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,22 @@ def main():
if not os.path.isdir(args.repos):
print("{0} is not a directory.".format(args.repos), file=sys.stderr)

for username in os.listdir(args.repos):
for username_entry in os.scandir(args.repos):
username = username_entry.name
repos_user_path = os.path.join(args.repos, username)

if not os.path.isdir(repos_user_path):
continue

for project_dirname in os.listdir(repos_user_path):
for project_dirname_entry in os.scandir(repos_user_path):
project_dirname = project_dirname_entry.name
repos_project_path = os.path.join(repos_user_path, project_dirname)

# this is only an optimization, if the modified time of the package
# directory has not changed in the last 90 days, then we perform an API check
modified_time = 0
for package in os.listdir(repos_project_path):
mt = os.path.getmtime(os.path.join(repos_project_path, package))
for package in os.scandir(repos_project_path):
mt = package.stat().st_mtime
modified_time = max(modified_time, mt)

if (datetime.datetime.today() - datetime.datetime.fromtimestamp(modified_time)).days < 90:
Expand Down
2 changes: 1 addition & 1 deletion dist-git/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
url=__url__,
license='GPLv2+',
packages=find_packages(exclude=('tests*',)),
scripts=[os.path.join("run", p) for p in os.listdir("run")],
scripts=[os.path.join("run", p.name) for p in os.scandir("run")],
include_package_data=True,
zip_safe=False,
)
3 changes: 2 additions & 1 deletion frontend/coprs_frontend/coprs/logic/complex_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,8 @@ def _logo(cls, mock_chroot):
"""
logoset = set()
logodir = app.static_folder + "/chroot_logodir"
for logo in os.listdir(logodir):
for entry in os.scandir(logodir):
logo = entry.name
# glob.glob() uses listdir() and fnmatch anyways
if fnmatch.fnmatch(logo, "*.png"):
logoset.add(logo[:-4])
Expand Down
3 changes: 2 additions & 1 deletion rpmbuild/copr_rpmbuild/automation/rpm_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def find_results_nevras_dicts(self):
as `dicts`
"""
nevras = []
for result in os.listdir(self.resultdir):
for entry in os.scandir(self.resultdir):
result = entry.name
if not result.endswith(".rpm"):
continue
package = os.path.join(self.resultdir, result)
Expand Down
2 changes: 1 addition & 1 deletion rpmbuild/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def build_srpm(args, config):

resultdir = config.get("main", "resultdir")
log.info("Output: {0}".format(
os.listdir(resultdir)))
list(entry.name for entry in os.scandir(resultdir))))

# extract spec file from SRPM
cmd = f"rpm2archive -n < {locate_srpm(resultdir)} | tar xf - '*.spec'"
Expand Down

0 comments on commit 348ef2f

Please sign in to comment.