-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrm-keys.py
More file actions
executable file
·63 lines (48 loc) · 1.64 KB
/
rm-keys.py
File metadata and controls
executable file
·63 lines (48 loc) · 1.64 KB
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
#! /usr/bin/env python
# This script is in the public domain, and comes with NO WARRANTY OF ANY KIND
# Reads a list of keys on stdin and deletes them from the
# specified cluster.
#
# This program uses the memcached binary protocol, and is intended
# to be used with Couchbase Server via moxi (port 11211).
import fileinput
from optparse import OptionParser
import sys
try:
import mc_bin_client
except ImportError, e:
print """Cannot find couchbase python libraries, please add it to PYTHONPATH
For example, this may work for most Unix users:
export PYTHONPATH=/opt/couchbase/lib/python
"""
raise
parser = OptionParser(usage = "Usage: %prog [options]")
# NB: required=True param breaks %prog --help
parser.add_option("-c", "--cluster", metavar="HOST:PORT")
parser.add_option("-b", "--bucket", metavar="BUCKET_NAME")
parser.add_option("-p", "--password", default="", metavar="BUCKET_PASSWORD")
(options, args) = parser.parse_args()
for opt in ["cluster", "bucket"]:
if getattr(options, opt) is None:
sys.stderr.write("Missing required --%s option\n" % (opt,))
parser.print_help()
sys.exit(1)
host, port = options.cluster.split(':')
port = int(port)
client = mc_bin_client.MemcachedClient(host, port)
client.sasl_auth_plain(options.bucket, options.password)
count = 0
errors = 0
for key in fileinput.input(args):
key = key.rstrip('\n')
try:
client.delete(key)
count += 1
except mc_bin_client.MemcachedError, e:
errors += 1
print "DELETE ERROR '%s': %s" % (e, key)
print "Done (deleted %d keys)" % (count,)
if errors > 0:
print "ERRORS: ", errors
sys.exit(2)
sys.exit(0)