Commit 82b06413 authored by Kirill Smelkov's avatar Kirill Smelkov

Switch to opening storages by URL

Previously for specifying a storage we were requiring users to create
zconfig file and put information about storage there. This is not always
convenient e.g. for quickly inspecting a file storage or a NEO instance
here and there for which address and name is already at hand.

So thanks to zodburi we can switch to specifying storages by URL without
loosing generality as there is still zconfig:// schema which allows to
configure storages zconfig way.

P.S. for convenience we allow paths without schema to be treated as
    FileStorage (i.e. file:// schema is implicitly used).

P.P.S. zodbanalyze is not affected (for now ?) as it currently
    works with FileStorage only.

[1] http://docs.pylonsproject.org/projects/zodburi/
parent 984cfe22
...@@ -19,7 +19,7 @@ setup( ...@@ -19,7 +19,7 @@ setup(
keywords = 'zodb utility tool', keywords = 'zodb utility tool',
packages = find_packages(), packages = find_packages(),
install_requires = ['ZODB'], install_requires = ['ZODB', 'zodburi'],
entry_points= {'console_scripts': ['zodb = zodbtools.zodb:main']}, entry_points= {'console_scripts': ['zodb = zodbtools.zodb:main']},
......
...@@ -20,3 +20,33 @@ from collections import OrderedDict ...@@ -20,3 +20,33 @@ from collections import OrderedDict
# topic_name -> (topic_summary, topic_help) # topic_name -> (topic_summary, topic_help)
topic_dict = OrderedDict() topic_dict = OrderedDict()
help_zurl = """\
Almost every zodb command works with a database.
A database can be specified by way of providing URL for its storage.
The most general way to specify a storage is via preparing file with
ZConfig-based storage definition, e.g.
%import neo.client
<NEOStorage>
master_nodes ...
name ...
</NEOStorage>
and using path to that file with zconfig:// schema:
zconfig://<path-to-zconfig-storage-definition>
There are also following simpler ways:
- neo://<db>@<master> for a NEO database
- zeo://<host>:<port> for a ZEO database
- /path/to/file for a FileStorage database
Please see zodburi documentation for full details:
http://docs.pylonsproject.org/projects/zodburi/
"""
topic_dict['zurl'] = "specifying database URL", help_zurl
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
# See COPYING file for full licensing terms. # See COPYING file for full licensing terms.
import hashlib import hashlib
import zodburi
def ashex(s): def ashex(s):
return s.encode('hex') return s.encode('hex')
...@@ -72,3 +73,15 @@ def parse_tidrange(tidrange): ...@@ -72,3 +73,15 @@ def parse_tidrange(tidrange):
# empty tid means -inf / +inf respectively # empty tid means -inf / +inf respectively
# ( which is None in IStorage.iterator() ) # ( which is None in IStorage.iterator() )
return (tidmin or None, tidmax or None) return (tidmin or None, tidmax or None)
# storageFromURL opens a ZODB-storage specified by url
def storageFromURL(url):
# no schema -> file://
if "://" not in url:
url = "file://" + url
stor_factory, dbkw = zodburi.resolve_uri(url)
stor = stor_factory()
return stor
...@@ -29,7 +29,8 @@ Exit status is 0 if inputs are the same, 1 if different, 2 if error. ...@@ -29,7 +29,8 @@ Exit status is 0 if inputs are the same, 1 if different, 2 if error.
""" """
from __future__ import print_function from __future__ import print_function
from zodbtools.util import ashex, inf, nextitem, txnobjv, parse_tidrange, TidRangeInvalid from zodbtools.util import ashex, inf, nextitem, txnobjv, parse_tidrange, TidRangeInvalid, \
storageFromURL
from time import time from time import time
# compare two storage transactions # compare two storage transactions
...@@ -106,7 +107,6 @@ def storcmp(stor1, stor2, tidmin, tidmax, verbose=False): ...@@ -106,7 +107,6 @@ def storcmp(stor1, stor2, tidmin, tidmax, verbose=False):
# ---------------------------------------- # ----------------------------------------
import ZODB.config
import sys, getopt import sys, getopt
import traceback import traceback
...@@ -117,13 +117,7 @@ def usage(out): ...@@ -117,13 +117,7 @@ def usage(out):
Usage: zodb cmp [OPTIONS] <storage1> <storage2> [tidmin..tidmax] Usage: zodb cmp [OPTIONS] <storage1> <storage2> [tidmin..tidmax]
Compare two ZODB databases. Compare two ZODB databases.
<storageX> is a file with ZConfig-based storage definition, e.g. <storageX> is an URL (see 'zodb help zurl') of a ZODB-storage.
%import neo.client
<NEOStorage>
master_nodes ...
name ...
</NEOStorage>
Options: Options:
...@@ -149,7 +143,7 @@ def main2(argv): ...@@ -149,7 +143,7 @@ def main2(argv):
verbose = True verbose = True
try: try:
storconf1, storconf2 = argv[0:2] storurl1, storurl2 = argv[0:2]
except ValueError: except ValueError:
usage(sys.stderr) usage(sys.stderr)
sys.exit(2) sys.exit(2)
...@@ -163,8 +157,8 @@ def main2(argv): ...@@ -163,8 +157,8 @@ def main2(argv):
print("E: invalid tidrange: %s" % e, file=sys.stderr) print("E: invalid tidrange: %s" % e, file=sys.stderr)
sys.exit(2) sys.exit(2)
stor1 = ZODB.config.storageFromFile(open(storconf1, 'r')) stor1 = storageFromURL(storurl1)
stor2 = ZODB.config.storageFromFile(open(storconf2, 'r')) stor2 = storageFromURL(storurl2)
zcmp = storcmp(stor1, stor2, tidmin, tidmax, verbose) zcmp = storcmp(stor1, stor2, tidmin, tidmax, verbose)
sys.exit(1 if zcmp else 0) sys.exit(1 if zcmp else 0)
......
...@@ -30,8 +30,8 @@ txn ... ...@@ -30,8 +30,8 @@ txn ...
""" """
from __future__ import print_function from __future__ import print_function
from zodbtools.util import ashex, sha1, txnobjv, parse_tidrange, TidRangeInvalid from zodbtools.util import ashex, sha1, txnobjv, parse_tidrange, TidRangeInvalid, \
storageFromURL
def zodbdump(stor, tidmin, tidmax, hashonly=False): def zodbdump(stor, tidmin, tidmax, hashonly=False):
first = True first = True
...@@ -67,7 +67,6 @@ def zodbdump(stor, tidmin, tidmax, hashonly=False): ...@@ -67,7 +67,6 @@ def zodbdump(stor, tidmin, tidmax, hashonly=False):
# ---------------------------------------- # ----------------------------------------
import ZODB.config
import sys, getopt import sys, getopt
summary = "dump content of a ZODB database" summary = "dump content of a ZODB database"
...@@ -77,13 +76,7 @@ def usage(out): ...@@ -77,13 +76,7 @@ def usage(out):
Usage: zodb dump [OPTIONS] <storage> [tidmin..tidmax] Usage: zodb dump [OPTIONS] <storage> [tidmin..tidmax]
Dump content of a ZODB database. Dump content of a ZODB database.
<storage> is a file with ZConfig-based storage definition, e.g. <storage> is an URL (see 'zodb help zurl') of a ZODB-storage.
%import neo.client
<NEOStorage>
master_nodes ...
name ...
</NEOStorage>
Options: Options:
...@@ -109,7 +102,7 @@ def main(argv): ...@@ -109,7 +102,7 @@ def main(argv):
hashonly = True hashonly = True
try: try:
storconf = argv[0] storurl = argv[0]
except IndexError: except IndexError:
usage(sys.stderr) usage(sys.stderr)
sys.exit(2) sys.exit(2)
...@@ -123,6 +116,6 @@ def main(argv): ...@@ -123,6 +116,6 @@ def main(argv):
print("E: invalid tidrange: %s" % e, file=sys.stderr) print("E: invalid tidrange: %s" % e, file=sys.stderr)
sys.exit(2) sys.exit(2)
stor = ZODB.config.storageFromFile(open(storconf, 'r')) stor = storageFromURL(storurl)
zodbdump(stor, tidmin, tidmax, hashonly) zodbdump(stor, tidmin, tidmax, hashonly)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment