OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/env python |
2 | |
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 4 # found in the LICENSE file. |
6 | 5 |
7 """Helper script to perform actions as a super-user on ChromeOS. | 6 """Helper script to perform actions as a super-user on ChromeOS. |
8 | 7 |
9 Needs to be run with superuser privileges, typically using the | 8 Needs to be run with superuser privileges, typically using the |
10 suid_python binary. | 9 suid_python binary. |
11 | 10 |
12 Usage: | 11 Usage: |
13 sudo python suid_actions.py --action=clean_flimflam | 12 sudo python suid_actions.py --action=clean_flimflam |
14 """ | 13 """ |
15 | 14 |
16 import optparse | 15 import optparse |
17 import os | 16 import os |
18 import shutil | 17 import shutil |
| 18 import sys |
19 | 19 |
20 | 20 |
21 class SuidAction(object): | 21 class SuidAction(object): |
22 """Helper to perform some super-user actions on ChromeOS.""" | 22 """Helper to perform some super-user actions on ChromeOS.""" |
23 | 23 |
24 def _ParseArgs(self): | 24 def _ParseArgs(self): |
25 parser = optparse.OptionParser() | 25 parser = optparse.OptionParser() |
26 parser.add_option( | 26 parser.add_option( |
27 '-a', '--action', help='Action to perform.') | 27 '-a', '--action', help='Action to perform.') |
28 self._options = parser.parse_args()[0] | 28 self._options = parser.parse_args()[0] |
29 if not self._options.action: | 29 if not self._options.action: |
30 raise RuntimeError('No action specified.') | 30 raise RuntimeError('No action specified.') |
31 | 31 |
32 | |
33 def Run(self): | 32 def Run(self): |
34 self._ParseArgs() | 33 self._ParseArgs() |
35 assert os.geteuid() == 0, 'Needs superuser privileges.' | 34 assert os.geteuid() == 0, 'Needs superuser privileges.' |
36 handler = getattr(self, self._options.action) | 35 handler = getattr(self, self._options.action) |
37 assert handler and callable(handler), \ | 36 assert handler and callable(handler), \ |
38 'No handler for %s' % self._options.action | 37 'No handler for %s' % self._options.action |
39 handler() | 38 handler() |
| 39 return 0 |
40 | 40 |
41 ## Actions ## | 41 ## Actions ## |
42 | 42 |
43 def CleanFlimflamDir(self): | 43 def CleanFlimflamDir(self): |
44 """Clean the contents of /home/chronos/user/flimflam.""" | 44 """Clean the contents of /home/chronos/user/flimflam.""" |
45 flimflam_dir = '/home/chronos/user/flimflam' | 45 flimflam_dir = '/home/chronos/user/flimflam' |
46 if not os.path.exists(flimflam_dir): | 46 if not os.path.exists(flimflam_dir): |
47 return | 47 return |
48 for item in os.listdir(flimflam_dir): | 48 for item in os.listdir(flimflam_dir): |
49 path = os.path.join(flimflam_dir, item) | 49 path = os.path.join(flimflam_dir, item) |
50 if os.path.isdir(path): | 50 if os.path.isdir(path): |
51 shutil.rmtree(path) | 51 shutil.rmtree(path) |
52 else: | 52 else: |
53 os.remove(path) | 53 os.remove(path) |
54 | 54 |
55 | 55 |
56 if __name__ == '__main__': | 56 if __name__ == '__main__': |
57 suid_action = SuidAction() | 57 sys.exit(SuidAction().Run()) |
58 suid_action.Run() | |
OLD | NEW |