| [3225] | 1 | # !/usr/bin/env python
|
|---|
| 2 | """Guess which db package to use to open a db file."""
|
|---|
| 3 |
|
|---|
| 4 | import os
|
|---|
| 5 | import struct
|
|---|
| 6 | import sys
|
|---|
| 7 |
|
|---|
| 8 | try:
|
|---|
| 9 | import dbm
|
|---|
| 10 | _dbmerror = dbm.error
|
|---|
| 11 | except ImportError:
|
|---|
| 12 | dbm = None
|
|---|
| 13 | # just some sort of valid exception which might be raised in the
|
|---|
| 14 | # dbm test
|
|---|
| 15 | _dbmerror = IOError
|
|---|
| 16 |
|
|---|
| 17 | def whichdb(filename):
|
|---|
| 18 | """Guess which db package to use to open a db file.
|
|---|
| 19 |
|
|---|
| 20 | Return values:
|
|---|
| 21 |
|
|---|
| 22 | - None if the database file can't be read;
|
|---|
| 23 | - empty string if the file can be read but can't be recognized
|
|---|
| 24 | - the module name (e.g. "dbm" or "gdbm") if recognized.
|
|---|
| 25 |
|
|---|
| 26 | Importing the given module may still fail, and opening the
|
|---|
| 27 | database using that module may still fail.
|
|---|
| 28 | """
|
|---|
| 29 |
|
|---|
| 30 | # Check for dbm first -- this has a .pag and a .dir file
|
|---|
| 31 | try:
|
|---|
| 32 | f = open(filename + os.extsep + "pag", "rb")
|
|---|
| 33 | f.close()
|
|---|
| 34 | # dbm linked with gdbm on OS/2 doesn't have .dir file
|
|---|
| 35 | if not (dbm.library == "GNU gdbm" and sys.platform == "os2emx"):
|
|---|
| 36 | f = open(filename + os.extsep + "dir", "rb")
|
|---|
| 37 | f.close()
|
|---|
| 38 | return "dbm"
|
|---|
| 39 | except IOError:
|
|---|
| 40 | # some dbm emulations based on Berkeley DB generate a .db file
|
|---|
| 41 | # some do not, but they should be caught by the dbhash checks
|
|---|
| 42 | try:
|
|---|
| |
|---|