Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 28097f2

Browse files
m4ns0urtrotterdylan
authored andcommitted
Add dircache module (#361)
1 parent 06d1f45 commit 28097f2

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ STDLIB_TESTS := \
9696
test/test_colorsys \
9797
test/test_datetime \
9898
test/test_dict \
99+
test/test_dircache \
99100
test/test_fpformat \
100101
test/test_genericpath \
101102
test/test_list \

third_party/stdlib/dircache.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Read and cache directory listings.
2+
3+
The listdir() routine returns a sorted list of the files in a directory,
4+
using a cache to avoid reading the directory more often than necessary.
5+
The annotate() routine appends slashes to directories."""
6+
from warnings import warnpy3k
7+
warnpy3k("the dircache module has been removed in Python 3.0", stacklevel=2)
8+
del warnpy3k
9+
10+
import os
11+
12+
__all__ = ["listdir", "opendir", "annotate", "reset"]
13+
14+
cache = {}
15+
16+
def reset():
17+
"""Reset the cache completely."""
18+
global cache
19+
cache = {}
20+
21+
def listdir(path):
22+
"""List directory contents, using cache."""
23+
try:
24+
cached_mtime, list = cache[path]
25+
del cache[path]
26+
except KeyError:
27+
cached_mtime, list = -1, []
28+
mtime = os.stat(path).st_mtime
29+
if mtime != cached_mtime:
30+
list = os.listdir(path)
31+
list.sort()
32+
cache[path] = mtime, list
33+
return list
34+
35+
opendir = listdir # XXX backward compatibility
36+
37+
def annotate(head, list):
38+
"""Add '/' suffixes to directories."""
39+
for i in range(len(list)):
40+
if os.path.isdir(os.path.join(head, list[i])):
41+
list[i] = list[i] + '/'
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Test cases for the dircache module
3+
Nick Mathewson
4+
"""
5+
6+
import unittest
7+
from test.test_support import run_unittest # , import_module
8+
# dircache = import_module('dircache', deprecated=True)
9+
import dircache
10+
import os, time, sys, tempfile
11+
12+
13+
class DircacheTests(unittest.TestCase):
14+
def setUp(self):
15+
self.tempdir = tempfile.mkdtemp()
16+
17+
def tearDown(self):
18+
for fname in os.listdir(self.tempdir):
19+
self.delTemp(fname)
20+
os.rmdir(self.tempdir)
21+
22+
def writeTemp(self, fname):
23+
f = open(os.path.join(self.tempdir, fname), 'w')
24+
f.close()
25+
26+
def mkdirTemp(self, fname):
27+
os.mkdir(os.path.join(self.tempdir, fname))
28+
29+
def delTemp(self, fname):
30+
fname = os.path.join(self.tempdir, fname)
31+
if os.path.isdir(fname):
32+
os.rmdir(fname)
33+
else:
34+
os.unlink(fname)
35+
36+
def test_listdir(self):
37+
## SUCCESSFUL CASES
38+
entries = dircache.listdir(self.tempdir)
39+
self.assertEqual(entries, [])
40+
41+
# Check that cache is actually caching, not just passing through.
42+
self.assertTrue(dircache.listdir(self.tempdir) is entries)
43+
44+
# Directories aren't "files" on Windows, and directory mtime has
45+
# nothing to do with when files under a directory get created.
46+
# That is, this test can't possibly work under Windows -- dircache
47+
# is only good for capturing a one-shot snapshot there.
48+
49+
if sys.platform[:3] not in ('win', 'os2'):
50+
# Sadly, dircache has the same granularity as stat.mtime, and so
51+
# can't notice any changes that occurred within 1 sec of the last
52+
# time it examined a directory.
53+
time.sleep(1)
54+
self.writeTemp("test1")
55+
entries = dircache.listdir(self.tempdir)
56+
self.assertEqual(entries, ['test1'])
57+
self.assertTrue(dircache.listdir(self.tempdir) is entries)
58+
59+
## UNSUCCESSFUL CASES
60+
self.assertRaises(OSError, dircache.listdir, self.tempdir+"_nonexistent")
61+
62+
def test_annotate(self):
63+
self.writeTemp("test2")
64+
self.mkdirTemp("A")
65+
lst = ['A', 'test2', 'test_nonexistent']
66+
dircache.annotate(self.tempdir, lst)
67+
self.assertEqual(lst, ['A/', 'test2', 'test_nonexistent'])
68+
69+
70+
def test_main():
71+
try:
72+
run_unittest(DircacheTests)
73+
finally:
74+
dircache.reset()
75+
76+
77+
if __name__ == "__main__":
78+
test_main()

0 commit comments

Comments
 (0)