Mercurial > hrm
annotate kharon.py @ 4:702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 08 Dec 2008 14:32:06 -0800 |
parents | 4734efec5732 |
children | ce037f485614 |
rev | line source |
---|---|
0 | 1 #! /usr/bin/env python3.0 |
2 | |
1 | 3 ''' |
4 This is a drop-in replacement for rm, with support for undo. | |
5 ''' | |
6 | |
0 | 7 import os |
8 import sys | |
9 import json | |
10 import subprocess | |
11 import distutils.dir_util | |
12 from optparse import OptionParser | |
13 | |
14 HADES_DIR = os.path.expanduser('~/.Hades') | |
15 STATE_FILENAME = os.path.join(HADES_DIR, 'state.json') | |
16 | |
17 class Config(object): | |
18 def __init__(self, filename): | |
19 self.__filename = filename | |
20 self.nextid = 0 | |
21 if os.path.exists(self.__filename): | |
22 self.__dict__.update(json.load(open(self.__filename, 'r'))) | |
23 else: | |
24 self.save() | |
25 | |
26 def save(self): | |
27 state = {} | |
28 keys = [key for key in self.__dict__ | |
29 if not key.startswith('__')] | |
30 for key in keys: | |
31 state[key] = self.__dict__[key] | |
32 json.dump(state, open(self.__filename, 'w')) | |
33 | |
34 def shell(*params): | |
35 popen = subprocess.Popen(params) | |
36 popen.wait() | |
37 if popen.returncode: | |
38 raise Exception('Process failed: %s' % repr(params)) | |
39 | |
4
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
40 def dir_for_trans(transid): |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
41 return os.path.join(HADES_DIR, '%.9d' % transid) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
42 |
0 | 43 if __name__ == '__main__': |
44 if not (os.path.exists(HADES_DIR) and os.path.isdir(HADES_DIR)): | |
45 shell('mkdir', HADES_DIR) | |
46 | |
47 parser = OptionParser() | |
2
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
48 parser.add_option('-u', '--undo', |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
49 dest='undo', action='store_true', default=False, |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
50 help='undo a transaction (default is latest).') |
0 | 51 (options, args) = parser.parse_args() |
52 | |
53 if not args: | |
54 parser.print_help() | |
55 sys.exit(-1) | |
56 | |
57 config = Config(STATE_FILENAME) | |
2
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
58 |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
59 if options.undo: |
4
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
60 transactions = [] |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
61 for arg in args: |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
62 try: |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
63 transid = int(arg) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
64 except ValueError: |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
65 print('Unknown transaction: %s' % arg) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
66 sys.exit(-1) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
67 transactions.append(transid) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
68 if not transactions and config.nextid: |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
69 transactions.append(config.nextid - 1) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
70 for transid in transactions: |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
71 dirname = dir_for_trans(transid) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
72 if not os.path.exists(dirname): |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
73 print('Transaction %d does not exist.' % transid) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
74 sys.exit(-1) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
75 |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
76 print('Sorry, undo is not implemented yet.' |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
77 'But you can find what you need in the following ' |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
78 'directory/directories:\n') |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
79 print('\n'.join([dir_for_trans(transid) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
80 for transid in transactions])) |
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
81 sys.exit(-1) |
2
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
82 else: |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
83 files = [] |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
84 for arg in args: |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
85 filename = os.path.abspath(os.path.expanduser(arg)) |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
86 if not os.path.exists(filename): |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
87 print('File does not exist: %s' % arg) |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
88 sys.exit(-1) |
3
4734efec5732
Can't move fles in or above HADES_DIR now.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
89 realpath = os.path.realpath(filename) |
4734efec5732
Can't move fles in or above HADES_DIR now.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
90 if (realpath.startswith(HADES_DIR) or |
4734efec5732
Can't move fles in or above HADES_DIR now.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
91 HADES_DIR.startswith(realpath)): |
4734efec5732
Can't move fles in or above HADES_DIR now.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
92 print('Cannot move files in or above %s.' % HADES_DIR) |
4734efec5732
Can't move fles in or above HADES_DIR now.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
93 sys.exit(-1) |
2
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
94 files.append(filename) |
0 | 95 |
2
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
96 thisid = config.nextid |
4
702b55be9030
Added more of an implementation for undo, although it's still unimplemented.
Atul Varma <varmaa@toolness.com>
parents:
3
diff
changeset
|
97 basedir = dir_for_trans(thisid) |
2
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
98 shell('mkdir', basedir) |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
99 config.nextid = thisid + 1 |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
100 config.save() |
0 | 101 |
2
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
102 print("Creating transaction %d." % thisid) |
0 | 103 |
2
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
104 for source in files: |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
105 dest = os.path.join(basedir, filename[1:]) |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
106 distutils.dir_util.mkpath(os.path.dirname(dest)) |
05cece4371a4
Added an undo option, though it's not implemented yet.
Atul Varma <varmaa@toolness.com>
parents:
1
diff
changeset
|
107 shell('mv', source, dest) |