Mercurial > p > roundup > code
comparison roundup/scripts/roundup_gettext.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 | 2943140f5286 |
| children | f2b049b49fca |
comparison
equal
deleted
inserted
replaced
| 8087:0cb81ee2e572 | 8088:1045425c23b2 |
|---|---|
| 62 # name of message template file. | 62 # name of message template file. |
| 63 # i don't think this will ever need to be changed, but still... | 63 # i don't think this will ever need to be changed, but still... |
| 64 TEMPLATE_FILE = "messages.pot" | 64 TEMPLATE_FILE = "messages.pot" |
| 65 | 65 |
| 66 | 66 |
| 67 # FIXME: html directory can be specified in config.ini. Should be | |
| 68 # a parameter, or config.ini should be loaded. | |
| 67 def run(): | 69 def run(): |
| 68 # return unless command line arguments contain single directory path | 70 # return unless command line arguments contain single directory path |
| 69 if (len(sys.argv) != 2) or (sys.argv[1] in ("-h", "--help")): | 71 if (len(sys.argv) != 2) or (sys.argv[1] in ("-h", "--help")): |
| 70 print(_("Usage: %(program)s <tracker home>") % | 72 print(_("Usage: %(program)s <tracker home>") % |
| 71 {"program": sys.argv[0]}) | 73 {"program": sys.argv[0]}) |
| 74 home = os.path.abspath(sys.argv[1]) | 76 home = os.path.abspath(sys.argv[1]) |
| 75 htmldir = os.path.join(home, "html") | 77 htmldir = os.path.join(home, "html") |
| 76 if os.path.isdir(htmldir): | 78 if os.path.isdir(htmldir): |
| 77 # glob is not used because i want to match file names | 79 # glob is not used because i want to match file names |
| 78 # without case sensitivity, and that is easier done this way. | 80 # without case sensitivity, and that is easier done this way. |
| 79 htmlfiles = [filename for filename in os.listdir(htmldir) | 81 htmlfiles = [e.name for e in os.scandir(htmldir) |
| 80 if os.path.isfile(os.path.join(htmldir, filename)) | 82 if e.is_file() |
| 81 and filename.lower().endswith(".html")] | 83 and e.name.lower().endswith(".html")] |
| 82 else: | 84 else: |
| 83 htmlfiles = [] | 85 htmlfiles = [] |
| 84 # return if no html files found | 86 # return if no html files found |
| 85 if not htmlfiles: | 87 if not htmlfiles: |
| 86 print(_("No tracker templates found in directory %s") % home) | 88 print(_("No tracker templates found in directory %s") % home) |
