Replace boost::scoped_ptr with std::unique_ptr.
[cumulus.git] / cumulus-store
1 #!/usr/bin/python
2 #
3 # Cumulus: Efficient Filesystem Backup to the Cloud
4 # Copyright (C) 2008, 2010 The Cumulus Developers
5 # See the AUTHORS file for a list of contributors.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 # Generic storage hook for writing LBS backups directly to Amazon's Simple
22 # Storage Service (S3), or any other service supported by the Python storage
23 # implementation.
24 #
25 # Storage protocol: After launching this script (with the remote location
26 # specified on the command-line), send any number of commands as lines to
27 # stdin.  Available commands are:
28 #     PUT <type> <name> <local file>
29 #     LIST <type>
30 # Tokens are whitespace-separated, but may contain any characters by
31 # URI-encoding them.  After completing each operation, a response line is
32 # written to stdout, which is either "OK" (for success) or "ERR" (if an error
33 # occurred).
34
35 from __future__ import division, print_function, unicode_literals
36
37 import os, sys, traceback
38
39 # Automatically set Python path, based on script directory.  This should be
40 # removed if the tools are properly installed somewhere.
41 script_directory = os.path.dirname(sys.argv[0])
42 sys.path.append(os.path.join(script_directory, 'python'))
43
44 import cumulus
45 from cumulus import store
46
47 remote = store.open(sys.argv[1])
48 while True:
49     cmd = sys.stdin.readline()
50     if cmd == "": break
51     cmd = [cumulus.uri_decode(s) for s in cmd.strip().split()]
52
53     try:
54         if cmd[0] == 'PUT':
55             remote.put(cmd[1], cmd[2], open(cmd[3], 'r'))
56             sys.stdout.write('OK\n')
57         elif cmd[0] == 'LIST':
58             files = remote.list(cmd[1])
59             for f in files:
60                 sys.stdout.write("* " + cumulus.uri_encode(f) + "\n")
61             sys.stdout.write('OK\n')
62     except Exception:
63         traceback.print_exc()
64         sys.stdout.write('ERR\n')
65
66     sys.stdout.flush()
67
68 remote.close()