[FIX] Compute starting year value
[cds-indico.git] / bin / legacy / zeopack.py
blob40be579ee3102659313163a630ea7246c61cc66e
1 # -*- coding: utf-8 -*-
2 ##
3 ##
4 ## This file is part of Indico.
5 ## Copyright (C) 2002 - 2012 European Organization for Nuclear Research (CERN).
6 ##
7 ## Indico is free software; you can redistribute it and/or
8 ## modify it under the terms of the GNU General Public License as
9 ## published by the Free Software Foundation; either version 3 of the
10 ## License, or (at your option) any later version.
12 ## Indico is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ## General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with Indico;if not, see <http://www.gnu.org/licenses/>.
20 #!python
21 """Connect to a ZEO server and ask it to pack.
23 Usage: zeopack.py [options]
25 Options:
27 -p port -- port to connect to
29 -h host -- host to connect to (default is current host)
31 -U path -- Unix-domain socket to connect to
33 -S name -- storage name (default is '1')
35 -d days -- pack objects more than days old
37 -1 -- Connect to a ZEO 1 server
39 -W -- wait for server to come up. Normally the script tries to
40 connect for 10 seconds, then exits with an error. The -W
41 option is only supported with ZEO 1.
43 You must specify either -p and -h or -U.
44 """
46 import getopt
47 import socket
48 import sys
49 import time
51 from ZEO.ClientStorage import ClientStorage
53 WAIT = 10 # wait no more than 10 seconds for client to connect
55 def connect(storage):
56 # The connect-on-startup logic that ZEO provides isn't too useful
57 # for this script. We'd like to client to attempt to startup, but
58 # fail if it can't get through to the server after a reasonable
59 # amount of time. There's no external support for this, so we'll
60 # expose the ZEO 1.0 internals. (consenting adults only)
61 t0 = time.time()
62 while t0 + WAIT > time.time():
63 storage._call.connect()
64 if storage._connected:
65 return
66 raise RuntimeError, "Unable to connect to ZEO server"
68 def pack1(addr, storage, days, wait):
69 cs = ClientStorage(addr, storage=storage,
70 wait_for_server_on_startup=wait)
71 if wait:
72 # _startup() is an artifact of the way ZEO 1.0 works. The
73 # ClientStorage doesn't get fully initialized until registerDB()
74 # is called. The only thing we care about, though, is that
75 # registerDB() calls _startup().
76 cs._startup()
77 else:
78 connect(cs)
79 cs.invalidator = None
80 cs.pack(wait=1, days=days)
81 cs.close()
83 def pack2(addr, storage, days):
84 cs = ClientStorage(addr, storage=storage, wait=1, read_only=1)
85 cs.pack(wait=1, days=days)
86 cs.close()
88 def usage(exit=1):
89 print __doc__
90 print " ".join(sys.argv)
91 sys.exit(exit)
93 def main():
94 host = None
95 port = None
96 unix = None
97 storage = '1'
98 days = 0
99 wait = 0
100 zeoversion = 2
101 try:
102 opts, args = getopt.getopt(sys.argv[1:], 'p:h:U:S:d:W1')
103 for o, a in opts:
104 if o == '-p':
105 port = int(a)
106 elif o == '-h':
107 host = a
108 elif o == '-U':
109 unix = a
110 elif o == '-S':
111 storage = a
112 elif o == '-d':
113 days = int(a)
114 elif o == '-W':
115 wait = 1
116 elif o == '-1':
117 zeoversion = 1
118 except Exception, err:
119 print err
120 usage()
122 if unix is not None:
123 addr = unix
124 else:
125 if host is None:
126 host = socket.gethostname()
127 if port is None:
128 usage()
129 addr = host, port
131 if zeoversion == 1:
132 pack1(addr, storage, days, wait)
133 else:
134 pack2(addr, storage, days)
136 if __name__ == "__main__":
137 try:
138 main()
139 except Exception, err:
140 print err
141 sys.exit(1)