Commit af34e00a authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 15b948d7
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package main package main
import ( import (
"../../zodbtools"
) )
...@@ -41,6 +42,10 @@ Usage: ...@@ -41,6 +42,10 @@ Usage:
The commands are: The commands are:
`) `)
// TODO print commands // TODO print commands
for _, cmd := range zodbtools.AllCommands() {
fmt.Fprintf(w, "\t%s\t%s\n", cmd.Name, cmd.Summary)
}
fmt.Fprintf(w, fmt.Fprintf(w,
` `
......
...@@ -158,3 +158,11 @@ type IStorageRecordIterator interface { // XXX naming -> IRecordIterator ...@@ -158,3 +158,11 @@ type IStorageRecordIterator interface { // XXX naming -> IRecordIterator
// end of iteration is indicated with io.EOF // end of iteration is indicated with io.EOF
NextData() (*StorageRecordInformation, error) NextData() (*StorageRecordInformation, error)
} }
// TODO readonly
func Open(storageURL string) (IStorage, error) {
// TODO
return nil, nil
}
// Copyright (C) 2017 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Open Source Initiative approved licenses and Convey
// the resulting work. Corresponding source of such a combination shall include
// the source code for all other software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
package zodbtools
import (
"io"
)
// Command describes one zodb subcommand
type Command struct {
Name string
Summary string
Usage func (w io.Writer)
Main func (argv []string)
}
// registry of all commands
var cmdv = []Command{
// NOTE the order commands are listed here is the order how they will appear in help
// TODO analyze ?
// TODO cmp
{"dump", dumpSummary, dumpUsage, dumpMain},
{"info", infoSummary, infoUsage, infoMain},
}
// LookupCommand returns Command with corresponding name or nil
func LookupCommand(command string) *Command {
for i := range cmdv {
if cmdv[i].Name == command {
return &cmdv[i]
}
}
return nil
}
// AllCommands returns list of all zodbtools commands
func AllCommands() []Command {
return cmdv
}
...@@ -47,7 +47,7 @@ import ( ...@@ -47,7 +47,7 @@ import (
"lab.nexedi.com/kirr/go123/xfmt" "lab.nexedi.com/kirr/go123/xfmt"
"../../zodb" "../../zodb"
"../../storage/fs1" // "../../storage/fs1"
) )
...@@ -202,10 +202,13 @@ func Dump(w io.Writer, stor zodb.IStorage, tidMin, tidMax zodb.Tid, hashOnly boo ...@@ -202,10 +202,13 @@ func Dump(w io.Writer, stor zodb.IStorage, tidMin, tidMax zodb.Tid, hashOnly boo
return d.Dump(stor, tidMin, tidMax) return d.Dump(stor, tidMin, tidMax)
} }
// ----------------------------------------
const dumpSummary = "dump content of a ZODB database"
func dumpUsage(w io.Writer) { func dumpUsage(w io.Writer) {
fmt.Fprintf(w, fmt.Fprintf(w,
`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 an URL (see 'zodb help zurl') of a ZODB-storage. <storage> is an URL (see 'zodb help zurl') of a ZODB-storage.
...@@ -217,7 +220,7 @@ Options: ...@@ -217,7 +220,7 @@ Options:
`) `)
} }
func DumpMain(argv []string) { func dumpMain(argv []string) {
hashOnly := false hashOnly := false
tidRange := ".." // (0, +inf) tidRange := ".." // (0, +inf)
...@@ -226,9 +229,9 @@ func DumpMain(argv []string) { ...@@ -226,9 +229,9 @@ func DumpMain(argv []string) {
flags.BoolVar(&hashOnly, "hashonly", hashOnly, "dump only hashes of objects") flags.BoolVar(&hashOnly, "hashonly", hashOnly, "dump only hashes of objects")
flags.Parse(argv) flags.Parse(argv)
argv := flag.Args() argv = flags.Args()
if len(argv) < 1 { if len(argv) < 1 {
usage() flags.Usage()
os.Exit(2) os.Exit(2)
} }
storUrl := argv[0] storUrl := argv[0]
...@@ -243,7 +246,7 @@ func DumpMain(argv []string) { ...@@ -243,7 +246,7 @@ func DumpMain(argv []string) {
log.Fatal(err) // XXX recheck log.Fatal(err) // XXX recheck
} }
stor, err := fs1.Open(storUrl) // TODO read-only stor, err := zodb.Open(storUrl) // TODO read-only
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
// //
// See COPYING file for full licensing terms. // See COPYING file for full licensing terms.
package main package zodbtools
//go:generate sh -c "python2 -m zodbtools.zodb dump ../../../storage/fs1/testdata/1.fs >testdata/1.zdump.pyok" //go:generate sh -c "python2 -m zodbtools.zodb dump ../../../storage/fs1/testdata/1.fs >testdata/1.zdump.pyok"
......
...@@ -19,40 +19,73 @@ ...@@ -19,40 +19,73 @@
package zodbtools package zodbtools
# {} parameter_name -> get_parameter(stor) import (
infoDict = OrderedDict([ "flag"
("name", lambda stor: stor.getName()), "fmt"
("size", lambda stor: stor.getSize()), "io"
("last_tid", lambda stor: ashex(stor.lastTransaction())), "log"
]) "os"
def zodbinfo(stor, parameterv): "../../zodb"
wantnames = False )
if not parameterv:
parameterv = infoDict.keys() // paramFunc is a function to retrieve 1 storage parameter
wantnames = True type paramFunc func(stor zodb.IStorage) (string, error)
for parameter in parameterv: var infov = []struct {name string; getParam paramFunc} {
get_parameter = infoDict.get(parameter) // XXX e.g. stor.LastTid() should return err itself
if get_parameter is None: {"name", func(stor zodb.IStorage) (string, error) { return stor.StorageName(), nil }},
print("invalid parameter: %s" % parameter, file=sys.stderr) // {"size", func(stor zodb.IStorage) (string, error) { return stor.StorageSize(), nil }},
sys.exit(1) {"last_tid", func(stor zodb.IStorage) (string, error) {return stor.LastTid().String(), nil }},
}
out = ""
if wantnames: // {} parameter_name -> get_parameter(stor)
out += parameter + "=" var infoDict map[string]paramFunc
out += "%s" % (get_parameter(stor),)
print(out) func init() {
for _, info := range infov {
infoDict[info.name] = info.getParam
# ---------------------------------------- }
import getopt }
summary = "print general information about a ZODB database" // Info prints general information about a ZODB storage
func Info(w io.Writer, stor zodb.IStorage, parameterv []string) error {
def usage(out): wantnames := false
print("""\ if len(parameterv) == 0 {
Usage: zodb info [OPTIONS] <storage> [parameter ...] for _, info := range infov {
parameterv = append(parameterv, info.name)
}
wantnames = true
}
for _, parameter := range parameterv {
getParam, ok := infoDict[parameter]
if !ok {
return fmt.Errorf("invalid parameter: %s", parameter)
}
out := ""
if wantnames {
out += parameter + "="
}
value, err := getParam(stor)
if err != nil {
return fmt.Errorf("getting %s: %v", parameter, err)
}
out += value
fmt.Fprintf(w, "%s\n", out)
}
return nil
}
// ----------------------------------------
const infoSummary = "print general information about a ZODB database"
func infoUsage(w io.Writer) {
fmt.Fprintf(w,
`Usage: zodb info [OPTIONS] <storage> [parameter ...]
Print general information about a ZODB database. Print general information about a ZODB database.
<storage> is an URL (see 'zodb help zurl') of a ZODB-storage. <storage> is an URL (see 'zodb help zurl') of a ZODB-storage.
...@@ -64,27 +97,28 @@ named parameter on its own line. ...@@ -64,27 +97,28 @@ named parameter on its own line.
Options: Options:
-h --help show this help -h --help show this help
""", file=out) `)
}
def main(argv):
try: func infoMain(argv []string) {
optv, argv = getopt.getopt(argv[1:], "h", ["help"]) flags := flag.FlagSet{Usage: func() { infoUsage(os.Stderr) }}
except getopt.GetoptError as e: flags.Init("", flag.ExitOnError)
print(e, file=sys.stderr) flags.Parse(argv)
usage(sys.stderr)
sys.exit(2) argv = flags.Args()
if len(argv) < 1 {
for opt, _ in optv: flags.Usage()
if opt in ("-h", "--help"): os.Exit(2)
usage(sys.stdout) }
sys.exit(0) storUrl := argv[0]
try: stor, err := zodb.Open(storUrl) // TODO read-only
storurl = argv[0] if err != nil {
except IndexError: log.Fatal(err)
usage(sys.stderr) }
sys.exit(2)
err = Info(os.Stdout, stor, argv[1:])
stor = storageFromURL(storurl, read_only=True) if err != nil {
log.Fatal(err)
zodbinfo(stor, argv[1:]) }
}
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