The map::at method does not always exist, so instead use map::find.
[cumulus.git] / cumulus-store
1 #!/usr/bin/python
2 #
3 # Generic storage hook for writing LBS backups directly to Amazon's Simple
4 # Storage Service (S3), or any other service supported by the Python storage
5 # implementation.
6 #
7 # Storage protocol: After launching this script (with the remote location
8 # specified on the command-line), send any number of commands as lines to
9 # stdin.  Available commands are:
10 #     PUT <type> <name> <local file>
11 #     LIST <type>
12 # Tokens are whitespace-separated, but may contain any characters by
13 # URI-encoding them.  After completing each operation, a response line is
14 # written to stdout, which is either "OK" (for success) or "ERR" (if an error
15 # occurred).
16
17 import os, sys, traceback
18
19 # Automatically set Python path, based on script directory.  This should be
20 # removed if the tools are properly installed somewhere.
21 script_directory = os.path.dirname(sys.argv[0])
22 sys.path.append(os.path.join(script_directory, 'python'))
23
24 import cumulus
25 from cumulus import store
26
27 remote = store.open(sys.argv[1])
28 while True:
29     cmd = sys.stdin.readline()
30     if cmd == "": break
31     cmd = [cumulus.uri_decode(s) for s in cmd.strip().split()]
32
33     try:
34         if cmd[0] == 'PUT':
35             remote.put(cmd[1], cmd[2], open(cmd[3], 'r'))
36             sys.stdout.write('OK\n')
37         elif cmd[0] == 'LIST':
38             files = remote.list(cmd[1])
39             for f in files:
40                 sys.stdout.write("* " + cumulus.uri_encode(f) + "\n")
41             sys.stdout.write('OK\n')
42     except Exception:
43         traceback.print_exc()
44         sys.stdout.write('ERR\n')
45
46     sys.stdout.flush()