This file is indexed.

/usr/share/pyshared/ZEO/scripts/zeopack.test is in python-zodb 1:3.10.5-0ubuntu3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
zeopack
=======

The zeopack script can be used to pack one or more storages.  It uses
ClientStorage to do this.  To test it's behavior, we'll replace the
normal ClientStorage with a fake one that echos information we'll want
for our test:

    >>> class ClientStorage:
    ...     connect_wait = 0
    ...     def __init__(self, *args, **kw):
    ...         if args[0] == 'bad':
    ...             import logging
    ...             logging.getLogger('test.ClientStorage').error(
    ...                 "I hate this address, %r", args[0])
    ...             raise ValueError("Bad address")
    ...         print "ClientStorage(%s %s)" % (
    ...             repr(args)[1:-1],
    ...             ', '.join("%s=%r" % i for i in sorted(kw.items())),
    ...             )
    ...     def pack(self, t=None, *args, **kw):
    ...         now = time.localtime(time.time())
    ...         local_midnight = time.mktime(now[:3]+(0, 0, 0)+now[6:])
    ...         t -= local_midnight # adjust for tz
    ...         t += 86400*7 # add a week to make sure we're positive
    ...         print "pack(%r,%s %s)" % (
    ...             t, repr(args)[1:-1],
    ...             ', '.join("%s=%r" % i for i in sorted(kw.items())),
    ...             )
    ...     def is_connected(self):
    ...         self.connect_wait -= 1
    ...         print 'is_connected', self.connect_wait < 0
    ...         return self.connect_wait < 0
    ...     def close(self):
    ...         print "close()"

    >>> import ZEO
    >>> ClientStorage_orig = ZEO.ClientStorage.ClientStorage
    >>> ZEO.ClientStorage.ClientStorage = ClientStorage

Now, we're ready to try the script:

    >>> from ZEO.scripts.zeopack import main

If we call it with no arguments, we get help:

>>> import os; os.environ['COLUMNS'] = '80' # for consistent optparse output
>>> main([], 'zeopack')
Usage: zeopack [options] [servers]
<BLANKLINE>
Pack one or more storages hosted by ZEO servers.
<BLANKLINE>
The positional arguments specify 0 or more tcp servers to pack, where
each is of the form:
<BLANKLINE>
    host:port[:name]
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
Options:
  -d DAYS, --days=DAYS  Pack objects that are older than this number of days
  -t TIME, --time=TIME  Time of day to pack to of the form: HH[:MM[:SS]].
                        Defaults to current time.
  -u UNIX_SOCKETS, --unix=UNIX_SOCKETS
                        A unix-domain-socket server to connect to, of the
                        form: path[:name]
  -h HOST               Deprecated: Used with the -p and -S options, specified
                        the host to connect to.
  -p PORT               Deprecated: Used with the -h and -S options, specifies
                        the port to connect to.
  -S NAME               Deprecated: Used with the -h and -p, options, or with
                        the -U option specified the storage name to use.
                        Defaults to 1.
  -U UNIX               Deprecated: Used with the -S option, Unix-domain
                        socket to connect to.

Since packing involves time, we'd better have our way with it.  Replace
time.time() with a function that always returns the same value.  The
value is timezone dependent.

    >>> import time
    >>> time_orig = time.time
    >>> time.time = lambda : time.mktime((2009, 3, 24, 10, 55, 17, 1, 83, -1))
    >>> sleep_orig = time.sleep
    >>> def sleep(t):
    ...     print 'sleep(%r)' % t
    >>> time.sleep = sleep

Normally, we pass one or more TCP server specifications:

    >>> main(["host1:8100", "host1:8100:2"])
    ClientStorage(('host1', 8100), read_only=1, storage='1', wait=False)
    is_connected True
    pack(644117.0, wait=True)
    close()
    ClientStorage(('host1', 8100), read_only=1, storage='2', wait=False)
    is_connected True
    pack(644117.0, wait=True)
    close()

We can also pass unix-domain-sockey servers using the -u option:

    >>> main(["-ufoo", "-ubar:spam", "host1:8100", "host1:8100:2"])
    ClientStorage(('host1', 8100), read_only=1, storage='1', wait=False)
    is_connected True
    pack(644117.0, wait=True)
    close()
    ClientStorage(('host1', 8100), read_only=1, storage='2', wait=False)
    is_connected True
    pack(644117.0, wait=True)
    close()
    ClientStorage('foo', read_only=1, storage='1', wait=False)
    is_connected True
    pack(644117.0, wait=True)
    close()
    ClientStorage('bar', read_only=1, storage='spam', wait=False)
    is_connected True
    pack(644117.0, wait=True)
    close()

The -d option causes a pack time the given number of days earlier to
be used:

    >>> main(["-ufoo", "-ubar:spam", "-d3", "host1:8100", "host1:8100:2"])
    ClientStorage(('host1', 8100), read_only=1, storage='1', wait=False)
    is_connected True
    pack(384917.0, wait=True)
    close()
    ClientStorage(('host1', 8100), read_only=1, storage='2', wait=False)
    is_connected True
    pack(384917.0, wait=True)
    close()
    ClientStorage('foo', read_only=1, storage='1', wait=False)
    is_connected True
    pack(384917.0, wait=True)
    close()
    ClientStorage('bar', read_only=1, storage='spam', wait=False)
    is_connected True
    pack(384917.0, wait=True)
    close()

The -t option allows us to control the time of day:

    >>> main(["-ufoo", "-d3", "-t1:30", "host1:8100:2"])
    ClientStorage(('host1', 8100), read_only=1, storage='2', wait=False)
    is_connected True
    pack(351000.0, wait=True)
    close()
    ClientStorage('foo', read_only=1, storage='1', wait=False)
    is_connected True
    pack(351000.0, wait=True)
    close()

Connection timeout
------------------

The zeopack script tells ClientStorage not to wait for connections
before returning from the constructor, but will time out after 60
seconds of waiting for a connect.

    >>> ClientStorage.connect_wait = 3
    >>> main(["-d3", "-t1:30", "host1:8100:2"])
    ClientStorage(('host1', 8100), read_only=1, storage='2', wait=False)
    is_connected False
    sleep(1)
    is_connected False
    sleep(1)
    is_connected False
    sleep(1)
    is_connected True
    pack(351000.0, wait=True)
    close()

    >>> def call_main(args):
    ...     import sys
    ...     old_stderr = sys.stderr
    ...     sys.stderr = sys.stdout
    ...     try:
    ...         try:
    ...             main(args)
    ...         except SystemExit, v:
    ...             print "Exited", v
    ...     finally:
    ...         sys.stderr = old_stderr

    >>> ClientStorage.connect_wait = 999
    >>> call_main(["-d3", "-t1:30", "host1:8100", "host1:8100:2"])
    ... # doctest: +ELLIPSIS
    ClientStorage(('host1', 8100), read_only=1, storage='1', wait=False)
    is_connected False
    sleep(1)
    ...
    is_connected False
    sleep(1)
    Couldn't connect to: (('host1', 8100), '1')
    close()
    ClientStorage(('host1', 8100), read_only=1, storage='2', wait=False)
    is_connected False
    sleep(1)
    ...
    is_connected False
    sleep(1)
    Couldn't connect to: (('host1', 8100), '2')
    close()

    >>> ClientStorage.connect_wait = 0


Legacy support
--------------

    >>> main(["-d3", "-h", "host1", "-p", "8100", "-S", "2"])
    ClientStorage(('host1', 8100), read_only=1, storage='2', wait=False)
    is_connected True
    pack(384917.0, wait=True)
    close()

    >>> import socket
    >>> old_gethostname = socket.gethostname
    >>> socket.gethostname = lambda : 'test.host.com'
    >>> main(["-d3", "-p", "8100"])
    ClientStorage(('test.host.com', 8100), read_only=1, storage='1', wait=False)
    is_connected True
    pack(384917.0, wait=True)
    close()
    >>> socket.gethostname = old_gethostname

    >>> main(["-d3", "-U", "foo/bar", "-S", "2"])
    ClientStorage('foo/bar', read_only=1, storage='2', wait=False)
    is_connected True
    pack(384917.0, wait=True)
    close()

Error handling
--------------

    >>> call_main(["-d3"])
    Error:
    No servers specified.
    Exited 1

    >>> call_main(["-d3", "a"])
    Error:
    Invalid server specification: 'a'
    Exited 1

    >>> call_main(["-d3", "a:b:c:d"])
    Error:
    Invalid server specification: 'a:b:c:d'
    Exited 1

    >>> call_main(["-d3", "a:b:2"])
    Error:
    Invalid port in server specification: 'a:b:2'
    Exited 1

    >>> call_main(["-d3", "-u", "a:b:2"])
    Error:
    Invalid server specification: 'a:b:2'
    Exited 1

    >>> call_main(["-d3", "-u", "bad"]) # doctest: +ELLIPSIS
    test.ClientStorage ERROR I hate this address, 'bad'
    Traceback (most recent call last):
    ...
    ValueError: Bad address
    Error:
    Error packing storage 1 in 'bad'
    Exited 1



Note that in the previous example, the first line was output through logging.

.. tear down

    >>> ZEO.ClientStorage.ClientStorage = ClientStorage_orig
    >>> time.time = time_orig
    >>> time.sleep = sleep_orig