Mercurial > p > roundup > code
comparison roundup/instance.py @ 8088:1045425c23b2
refactor!: replace os.listdir() with os.scandir()
In many places we did a listdir() then a stat to see if it's a file or
directory. This change removes the need for the stat call. Also for
larger directories, scandir() is an iterator, so less memory use.
There is one remnant of listdir used in an error handler. That
requires a stat on each element in the directory, so there is no
benefit to using scandir() other than a slight memory saving on a
rarely used piece of code.
BREAKING CHANGE:
Python 2 requires installation of scandir pip package after this
commit.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 16 Jul 2024 01:05:49 -0400 |
| parents | 771d7c43c76f |
| children | 586f76eb33e8 |
comparison
equal
deleted
inserted
replaced
| 8087:0cb81ee2e572 | 8088:1045425c23b2 |
|---|---|
| 186 """ | 186 """ |
| 187 extensions = [] | 187 extensions = [] |
| 188 dirpath = os.path.join(self.tracker_home, dirname) | 188 dirpath = os.path.join(self.tracker_home, dirname) |
| 189 if os.path.isdir(dirpath): | 189 if os.path.isdir(dirpath): |
| 190 sys.path.insert(1, dirpath) | 190 sys.path.insert(1, dirpath) |
| 191 for name in os.listdir(dirpath): | 191 for dir_entry in os.scandir(dirpath): |
| 192 name = dir_entry.name | |
| 192 if not name.endswith('.py'): | 193 if not name.endswith('.py'): |
| 193 continue | 194 continue |
| 194 env = {} | 195 env = {} |
| 195 self._execfile(os.path.join(dirname, name), env) | 196 self._execfile(os.path.join(dirname, name), env) |
| 196 extensions.append(env['init']) | 197 extensions.append(env['init']) |
