]> arthur.barton.de Git - bup.git/blob - cmd/meta-cmd.py
Merge branch 'master' into meta
[bup.git] / cmd / meta-cmd.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2010 Rob Browning
4 #
5 # This code is covered under the terms of the GNU Library General
6 # Public License as described in the bup LICENSE file.
7
8 # TODO: Add tar-like -C option.
9 # TODO: Add tar-like -v support to --list.
10
11 import sys
12 from bup import metadata
13 from bup import options
14 from bup.helpers import handle_ctrl_c, log, saved_errors
15
16 optspec = """
17 bup meta --create [OPTION ...] <PATH ...>
18 bup meta --extract [OPTION ...]
19 bup meta --start-extract [OPTION ...]
20 bup meta --finish-extract [OPTION ...]
21 --
22 c,create       write metadata for PATHs to stdout (or --file)
23 t,list         display metadata
24 x,extract      perform --start-extract followed by --finish-extract
25 start-extract  build tree matching metadata provided on standard input (or --file)
26 finish-extract finish applying standard input (or --file) metadata to filesystem
27 f,file=        specify source or destination file
28 R,recurse      recurse into subdirectories
29 xdev,one-file-system  don't cross filesystem boundaries
30 numeric-ids    apply numeric IDs (user, group, etc.), not names, during restore
31 symlinks       handle symbolic links (default is true)
32 paths          include paths in metadata (default is true)
33 v,verbose      increase log output (can be used more than once)
34 q,quiet        don't show progress meter
35 """
36
37 action = None
38 target_filename = ''
39 should_recurse = False
40 restore_numeric_ids = False
41 include_paths = True
42 handle_symlinks = True
43 xdev = False
44
45 handle_ctrl_c()
46
47 o = options.Options(optspec)
48 (opt, flags, remainder) = o.parse(sys.argv[1:])
49
50 for flag, value in flags:
51     if flag == '--create' or flag == '-c':
52         action = 'create'
53     elif flag == '--list' or flag == '-t':
54         action = 'list'
55     elif flag == '--extract' or flag == '-x':
56         action = 'extract'
57     elif flag == '--start-extract':
58         action = 'start-extract'
59     elif flag == '--finish-extract':
60         action = 'finish-extract'
61     elif flag == '--file' or flag == '-f':
62         target_filename = value
63     elif flag == '--recurse' or flag == '-R':
64         should_recurse = True
65     elif flag == '--no-recurse':
66         should_recurse = False
67     elif flag in frozenset(['--xdev', '--one-file-system']):
68         xdev = True
69     elif flag in frozenset(['--no-xdev', '--no-one-file-system']):
70         xdev = False
71     elif flag == '--numeric-ids':
72         restore_numeric_ids = True
73     elif flag == '--no-numeric-ids':
74         restore_numeric_ids = False
75     elif flag == '--paths':
76         include_paths = True
77     elif flag == '--no-paths':
78         include_paths = False
79     elif flag == '--symlinks':
80         handle_symlinks = True
81     elif flag == '--no-symlinks':
82         handle_symlinks = False
83     elif flag == '--verbose' or flag == '-v':
84         metadata.verbose += 1
85     elif flag == '--quiet' or flag == '-q':
86         metadata.verbose = 0
87
88 if not action:
89     o.fatal("no action specified")
90
91 if action == 'create':
92     if len(remainder) < 1:
93         o.fatal("no paths specified for create")
94     if target_filename != '-':
95         output_file = open(target_filename, 'w')
96     else:
97         output_file = sys.stdout
98     metadata.save_tree(output_file,
99                        remainder,
100                        recurse=should_recurse,
101                        write_paths=include_paths,
102                        save_symlinks=handle_symlinks,
103                        xdev=xdev)
104
105 elif action == 'list':
106     if len(remainder) > 0:
107         o.fatal("cannot specify paths for --list")
108     if target_filename != '-':
109         src = open(target_filename, 'r')
110     else:
111         src = sys.stdin
112     metadata.display_archive(src)
113
114 elif action == 'start-extract':
115     if len(remainder) > 0:
116         o.fatal("cannot specify paths for --start-extract")
117     if target_filename != '-':
118         src = open(target_filename, 'r')
119     else:
120         src = sys.stdin
121     metadata.start_extract(src, create_symlinks=handle_symlinks)
122
123 elif action == 'finish-extract':
124     if len(remainder) > 0:
125         o.fatal("cannot specify paths for --finish-extract")
126     if target_filename != '-':
127         src = open(target_filename, 'r')
128     else:
129         src = sys.stdin
130     num_ids = restore_numeric_ids
131     metadata.finish_extract(src, restore_numeric_ids=num_ids)
132
133 elif action == 'extract':
134     if len(remainder) > 0:
135         o.fatal("cannot specify paths for --extract")
136     if target_filename != '-':
137         src = open(target_filename, 'r')
138     else:
139         src = sys.stdin
140     metadata.extract(src,
141                      restore_numeric_ids=restore_numeric_ids,
142                      create_symlinks=handle_symlinks)
143
144 if saved_errors:
145     log('WARNING: %d errors encountered.\n' % len(saved_errors))
146     sys.exit(1)
147 else:
148     sys.exit(0)