driver.py 4.23 KB
Newer Older
Pedro TOME's avatar
Pedro TOME committed
1
2
3
4
5
6
7
8
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

"""Bob Database Driver entry-point for the UTFVP
"""

import os
import sys
Pedro TOME's avatar
Pedro TOME committed
9
from bob.db.base.driver import Interface as BaseInterface
Pedro TOME's avatar
Pedro TOME committed
10
11
12
13
14
15
16
17


def dumplist(args):
  """Dumps lists of files based on your criteria"""

  from .query import Database
  db = Database()

18
19
20
21
  model_ids = None
  if args.models is not None:
    if isinstance(args.models, (list, tuple)): model_ids = args.models
    else: model_ids = (args.models,)
Pedro TOME's avatar
Pedro TOME committed
22
23
24
  r = db.objects(
      protocol=args.protocol,
      purposes=args.purpose,
25
      model_ids=model_ids,
Pedro TOME's avatar
Pedro TOME committed
26
27
28
29
30
31
      groups=args.group,
      classes=args.sclass
  )

  output = sys.stdout
  if args.selftest:
Pedro TOME's avatar
Pedro TOME committed
32
    from bob.db.base.utils import null
Pedro TOME's avatar
Pedro TOME committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    output = null()

  for f in r:
    output.write('%s\n' % (f.make_path(args.directory, args.extension),))

  return 0


def checkfiles(args):
  """Checks existence of files based on your criteria"""

  from .query import Database
  db = Database()

  r = db.objects()

  # go through all files, check if they are available on the filesystem
  good = []
  bad = []
  for f in r:
    if os.path.exists(f.make_path(args.directory, args.extension)):
      good.append(f)
    else:
      bad.append(f)

  # report
  output = sys.stdout
  if args.selftest:
Pedro TOME's avatar
Pedro TOME committed
61
    from bob.db.base.utils import null
Pedro TOME's avatar
Pedro TOME committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    output = null()

  if bad:
    for f in bad:
      output.write('Cannot find file "%s"\n' % (f.make_path(args.directory, args.extension),))
    output.write('%d files (out of %d) were not found at "%s"\n' % \
      (len(bad), len(r), args.directory))

  return 0


class Interface(BaseInterface):

  def name(self):
    return 'utfvp'

  def version(self):
    import pkg_resources  # part of setuptools
Pedro TOME's avatar
Pedro TOME committed
80
    return pkg_resources.require('bob.db.%s' % self.name())[0].version
Pedro TOME's avatar
Pedro TOME committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

  def files(self):

    from pkg_resources import resource_filename
    raw_files = ('db.sql3',)
    return [resource_filename(__name__, k) for k in raw_files]

  def type(self):
    return 'sqlite'

  def add_commands(self, parser):

    from . import __doc__ as docs

    subparsers = self.setup_parser(parser,
        "UTFVP database", docs)

    # example: get the "create" action from a submodule
    from .create import add_command as create_command
    create_command(subparsers)

    # example: get the "dumplist" action from a submodule
    from .query import Database
    import argparse
    db = Database()

    parser = subparsers.add_parser('dumplist', help=dumplist.__doc__)
    parser.add_argument('-d', '--directory', default='', help="if given, this path will be prepended to every entry returned.")
    parser.add_argument('-e', '--extension', default='', help="if given, this extension will be appended to every entry returned.")
    parser.add_argument('-p', '--protocol', help="if given, limits the dump to a particular subset of the data that corresponds to the given protocol.", choices=db.protocol_names() if db.is_valid() else ())
    parser.add_argument('-u', '--purpose', help="if given, this value will limit the output files to those designed for the given purposes.", choices=db.purposes() if db.is_valid() else ())
    parser.add_argument('-m', '--models', type=str, help="if given, limits the dump to a particular model", choices=db.model_ids() if db.is_valid() else ())
    parser.add_argument('-g', '--group', help="if given, this value will limit the output files to those belonging to a particular protocolar group.", choices=db.groups() if db.is_valid() else ())
    parser.add_argument('-c', '--class', dest='sclass', help="if given, this value will limit the output files to those belonging to the given classes.", choices=('client', 'impostor'))
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=dumplist) #action

    # the "checkfiles" action
    parser = subparsers.add_parser('checkfiles', help=checkfiles.__doc__)
    parser.add_argument('-d', '--directory', default='', help="if given, this path will be prepended to every entry returned.")
    parser.add_argument('-e', '--extension', default='', help="if given, this extension will be appended to every entry returned.")
    parser.add_argument('--self-test', dest="selftest", action='store_true', help=argparse.SUPPRESS)
    parser.set_defaults(func=checkfiles) #action