Mercurial > cpython-lru_cache-weakref
annotate Lib/whichdb.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | bcc35a0f1a78 |
| children | e107c921b8f3 |
| rev | line source |
|---|---|
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Guess which db package to use to open a db file.""" |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
16949
09ba15ee1961
RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents:
16888
diff
changeset
|
3 import os |
|
24489
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
4 import struct |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
5 |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
6 try: |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
7 import dbm |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
8 _dbmerror = dbm.error |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
9 except ImportError: |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
10 dbm = None |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
11 # just some sort of valid exception which might be raised in the |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
12 # dbm test |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
13 _dbmerror = IOError |
|
16949
09ba15ee1961
RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents:
16888
diff
changeset
|
14 |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 def whichdb(filename): |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 """Guess which db package to use to open a db file. |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 Return values: |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 - None if the database file can't be read; |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 - empty string if the file can be read but can't be recognized |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 - the module name (e.g. "dbm" or "gdbm") if recognized. |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 Importing the given module may still fail, and opening the |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 database using that module may still fail. |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 """ |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 # Check for dbm first -- this has a .pag and a .dir file |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 try: |
|
20654
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
17055
diff
changeset
|
30 f = open(filename + os.extsep + "pag", "rb") |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
31 f.close() |
|
20654
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
17055
diff
changeset
|
32 f = open(filename + os.extsep + "dir", "rb") |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
33 f.close() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
34 return "dbm" |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 except IOError: |
|
24489
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
36 # some dbm emulations based on Berkeley DB generate a .db file |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
37 # some do not, but they should be caught by the dbhash checks |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
38 try: |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
39 f = open(filename + os.extsep + "db", "rb") |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
40 f.close() |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
41 # guarantee we can actually open the file using dbm |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
42 # kind of overkill, but since we are dealing with emulations |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
43 # it seems like a prudent step |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
44 if dbm is not None: |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
45 d = dbm.open(filename) |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
46 d.close() |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
47 return "dbm" |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
48 except (IOError, _dbmerror): |
|
bcc35a0f1a78
catch the situation where Berkeley DB is used to emulate dbm(3) library
Skip Montanaro <skip@pobox.com>
parents:
20654
diff
changeset
|
49 pass |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
13741
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
51 # Check for dumbdbm next -- this has a .dir and and a .dat file |
|
13852
c480529807ab
Add missing 'try:'. Patch by Rob W. W. Hooft, #101071 (closed.)
Thomas Wouters <thomas@python.org>
parents:
13741
diff
changeset
|
52 try: |
|
20654
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
17055
diff
changeset
|
53 f = open(filename + os.extsep + "dat", "rb") |
|
13741
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
54 f.close() |
|
20654
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
17055
diff
changeset
|
55 f = open(filename + os.extsep + "dir", "rb") |
|
13741
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
56 try: |
|
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
57 if f.read(1) in ["'", '"']: |
|
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
58 return "dumbdbm" |
|
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
59 finally: |
|
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
60 f.close() |
|
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
61 except IOError: |
|
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
62 pass |
|
f65b86738d3b
Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents:
11742
diff
changeset
|
63 |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 # See if the file exists, return None if not |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
66 f = open(filename, "rb") |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 except IOError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
68 return None |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 |
| 10933 | 70 # Read the start of the file -- the magic number |
| 71 s16 = f.read(16) | |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 f.close() |
| 10933 | 73 s = s16[0:4] |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 # Return "" if not at least 4 bytes |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 if len(s) != 4: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
77 return "" |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 # Convert to 4-byte int in native byte order -- return "" if impossible |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
81 (magic,) = struct.unpack("=l", s) |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 except struct.error: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
83 return "" |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 # Check for GNU dbm |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 if magic == 0x13579ace: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
87 return "gdbm" |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 # Check for BSD hash |
|
8172
5732ec2487c5
Support byte-swapped dbhash (bsddb) files. Found by Ben Sayer.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
90 if magic in (0x00061561, 0x61150600): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4932
diff
changeset
|
91 return "dbhash" |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 |
| 10933 | 93 # BSD hash v2 has a 12-byte NULL pad in front of the file type |
| 94 try: | |
|
11742
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
10933
diff
changeset
|
95 (magic,) = struct.unpack("=l", s16[-4:]) |
| 10933 | 96 except struct.error: |
| 97 return "" | |
| 98 | |
| 99 # Check for BSD hash | |
| 100 if magic in (0x00061561, 0x61150600): | |
| 101 return "dbhash" | |
| 102 | |
|
3825
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 # Unknown |
|
c3ec69b1fe36
Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 return "" |
