-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-114576: Add command-line interface for dbm module #137893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
furkanonder
wants to merge
8
commits into
python:main
Choose a base branch
from
furkanonder:gh-114576
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d3044d8
Add command-line interface for dbm module
furkanonder ddfa563
Remove unused imports from test_dbm
furkanonder b4c224a
Merge branch 'main' into gh-114576
furkanonder 31ad2c4
Change output format separator in whichdb command
furkanonder 7059a0c
update whichdb output format test
furkanonder a547468
simplify reorganize detection
furkanonder 740f5bc
Fix _dump_command to use repr() and avoid data loss
furkanonder 3295f7e
Merge branch 'main' into gh-114576
furkanonder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -528,3 +528,70 @@ | |||||||||||||
|
||||||||||||||
Synchronize the on-disk directory and data files. This method is called | ||||||||||||||
by the :meth:`shelve.Shelf.sync` method. | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
.. _dbm-commandline: | ||||||||||||||
.. program:: dbm | ||||||||||||||
|
||||||||||||||
Command-line interface | ||||||||||||||
---------------------- | ||||||||||||||
|
||||||||||||||
.. module:: dbm.__main__ | ||||||||||||||
:synopsis: A command-line interface for DBM database operations. | ||||||||||||||
|
||||||||||||||
**Source code:** :source:`Lib/dbm/__main__.py` | ||||||||||||||
|
||||||||||||||
-------------- | ||||||||||||||
|
||||||||||||||
The :mod:`dbm` module can be invoked as a script via ``python -m dbm`` | ||||||||||||||
to identify, examine, and reorganize DBM database files. | ||||||||||||||
|
||||||||||||||
Command-line options | ||||||||||||||
^^^^^^^^^^^^^^^^^^^^ | ||||||||||||||
|
||||||||||||||
.. option:: --whichdb file [file ...] | ||||||||||||||
Comment on lines
+551
to
+552
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above
Suggested change
|
||||||||||||||
|
||||||||||||||
Identify the database type for one or more database files: | ||||||||||||||
|
||||||||||||||
.. code-block:: shell-session | ||||||||||||||
|
||||||||||||||
$ python -m dbm --whichdb *.db | ||||||||||||||
dbm.gnu - database1.db | ||||||||||||||
dbm.sqlite3 - database2.db | ||||||||||||||
UNKNOWN - corrupted.db | ||||||||||||||
|
||||||||||||||
This command uses the :func:`whichdb` function to determine the type | ||||||||||||||
of each database file. Files that cannot be identified are marked as | ||||||||||||||
``UNKNOWN``. | ||||||||||||||
|
||||||||||||||
.. option:: --dump file | ||||||||||||||
|
||||||||||||||
Display the contents of a database file: | ||||||||||||||
|
||||||||||||||
.. code-block:: shell-session | ||||||||||||||
|
||||||||||||||
$ python -m dbm --dump mydb.db | ||||||||||||||
username: john_doe | ||||||||||||||
email: [email protected] | ||||||||||||||
last_login: 2024-01-15 | ||||||||||||||
|
||||||||||||||
Keys and values are displayed in ``key: value`` format. Binary data | ||||||||||||||
is decoded using UTF-8 with error replacement for display purposes. | ||||||||||||||
|
||||||||||||||
.. option:: --reorganize file | ||||||||||||||
|
||||||||||||||
Reorganize and compact a database file to reduce disk space: | ||||||||||||||
|
||||||||||||||
.. code-block:: shell-session | ||||||||||||||
|
||||||||||||||
$ python -m dbm --reorganize mydb.db | ||||||||||||||
Reorganized database 'mydb.db' | ||||||||||||||
|
||||||||||||||
This operation uses the database's native :meth:`!reorganize` method | ||||||||||||||
when available (:mod:`dbm.sqlite3`, :mod:`dbm.gnu`, :mod:`dbm.dumb`). | ||||||||||||||
For database types that don't support reorganization, an error message | ||||||||||||||
is displayed. | ||||||||||||||
|
||||||||||||||
.. option:: -h, --help | ||||||||||||||
|
||||||||||||||
Show the help message. | ||||||||||||||
Comment on lines
+594
to
+597
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we should add help flag as option because it's not a common practice.
Suggested change
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import argparse | ||
import os | ||
import sys | ||
|
||
from . import open as dbm_open, whichdb, error | ||
|
||
|
||
def _whichdb_command(filenames): | ||
exit_code = 0 | ||
|
||
for filename in filenames: | ||
if os.path.exists(filename): | ||
db_type = whichdb(filename) | ||
print(f"{db_type or 'UNKNOWN'} {filename}") | ||
else: | ||
print(f"Error: File '{filename}' not found", file=sys.stderr) | ||
exit_code = 1 | ||
|
||
return exit_code | ||
|
||
|
||
def _dump_command(filename): | ||
try: | ||
with dbm_open(filename, "r") as db: | ||
for key in db: | ||
print(f"{key!r}: {db[key]!r}") | ||
return 0 | ||
except error: | ||
print(f"Error: Database '{filename}' not found", file=sys.stderr) | ||
return 1 | ||
|
||
|
||
def _reorganize_command(filename): | ||
try: | ||
with dbm_open(filename, "c") as db: | ||
if db.hasattr("reorganize"): | ||
db.reorganize() | ||
print(f"Reorganized database: '{filename}'", file=sys.stderr) | ||
else: | ||
print("Database type doesn't support reorganize method", | ||
file=sys.stderr) | ||
return 1 | ||
return 0 | ||
except error: | ||
print(f"Error: Database '{filename}' not found or cannot be opened", | ||
file=sys.stderr) | ||
return 1 | ||
|
||
|
||
def main(): | ||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser = argparse.ArgumentParser( | ||
prog="python -m dbm", description="DBM toolkit" | ||
) | ||
group = parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument( | ||
"--whichdb", | ||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nargs="+", | ||
metavar="file", | ||
help="Identify database type for one or more files", | ||
) | ||
group.add_argument( | ||
"--dump", metavar="file", help="Display database contents" | ||
) | ||
group.add_argument( | ||
"--reorganize", | ||
metavar="file", | ||
help="Reorganize the database", | ||
) | ||
options = parser.parse_args() | ||
|
||
try: | ||
if options.whichdb: | ||
return _whichdb_command(options.whichdb) | ||
elif options.dump: | ||
return _dump_command(options.dump) | ||
elif options.reorganize: | ||
return _reorganize_command(options.reorganize) | ||
except KeyboardInterrupt: | ||
return 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. program::
directive should be placed closer to options