]> arthur.barton.de Git - bup.git/blob - cmd/meta-cmd.py
Defer metadata aquisition and application errors during create/extract.
[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 numeric-ids    apply numeric IDs (user, group, etc.), not names, during restore
30 symlinks       handle symbolic links (default is true)
31 paths          include paths in metadata (default is true)
32 v,verbose      increase log output (can be used more than once)
33 q,quiet        don't show progress meter
34 """
35
36 action = None
37 target_filename = ''
38 should_recurse = False
39 restore_numeric_ids = False
40 include_paths = True
41 handle_symlinks = True
42
43 handle_ctrl_c()
44
45 o = options.Options('bup meta', optspec)
46 (opt, flags, remainder) = o.parse(sys.argv[1:])
47
48 for flag, value in flags:
49     if flag == '--create' or flag == '-c':
50         action = 'create'
51     if flag == '--list' or flag == '-t':
52         action = 'list'
53     elif flag == '--extract' or flag == '-x':
54         action = 'extract'
55     elif flag == '--start-extract':
56         action = 'start-extract'
57     elif flag == '--finish-extract':
58         action = 'finish-extract'
59     elif flag == '--file' or flag == '-f':
60         target_filename = value
61     elif flag == '--recurse' or flag == '-R':
62         should_recurse = True
63     elif flag == '--no-recurse':
64         should_recurse = False
65     elif flag == '--numeric-ids':
66         restore_numeric_ids = True
67     elif flag == '--no-numeric-ids':
68         restore_numeric_ids = False
69     elif flag == '--paths':
70         include_paths = True
71     elif flag == '--no-paths':
72         include_paths = False
73     elif flag == '--symlinks':
74         handle_symlinks = True
75     elif flag == '--no-symlinks':
76         handle_symlinks = False
77     elif flag == '--verbose' or flag == '-v':
78         metadata.verbose += 1
79     elif flag == '--quiet' or flag == '-q':
80         metadata.verbose = 0
81
82 if not action:
83     o.fatal("no action specified")
84
85 if action == 'create':
86     if len(remainder) < 1:
87         o.fatal("no paths specified for create")
88     if target_filename != '-':
89         output_file = open(target_filename, 'w')
90     else:
91         output_file = sys.stdout
92     metadata.save_tree(output_file,
93                        remainder,
94                        recurse=should_recurse,
95                        write_paths=include_paths,
96                        save_symlinks=handle_symlinks)
97
98 elif action == 'list':
99     if len(remainder) > 0:
100         o.fatal("cannot specify paths for --list")
101     if target_filename != '-':
102         src = open(target_filename, 'r')
103     else:
104         src = sys.stdin
105     metadata.display_archive(src)
106
107 elif action == 'start-extract':
108     if len(remainder) > 0:
109         o.fatal("cannot specify paths for --start-extract")
110     if target_filename != '-':
111         src = open(target_filename, 'r')
112     else:
113         src = sys.stdin
114     metadata.start_extract(src, create_symlinks=handle_symlinks)
115
116 elif action == 'finish-extract':
117     if len(remainder) > 0:
118         o.fatal("cannot specify paths for --finish-extract")
119     if target_filename != '-':
120         src = open(target_filename, 'r')
121     else:
122         src = sys.stdin
123     num_ids = restore_numeric_ids
124     metadata.finish_extract(src, restore_numeric_ids=num_ids)
125
126 elif action == 'extract':
127     if len(remainder) > 0:
128         o.fatal("cannot specify paths for --extract")
129     if target_filename != '-':
130         src = open(target_filename, 'r')
131     else:
132         src = sys.stdin
133     metadata.extract(src,
134                      restore_numeric_ids=restore_numeric_ids,
135                      create_symlinks=handle_symlinks)
136
137 if saved_errors:
138     log('WARNING: %d errors encountered.\n' % len(saved_errors))
139     sys.exit(1)
140 else:
141     sys.exit(0)