3 # Copyright (C) 2010 Rob Browning
5 # This code is covered under the terms of the GNU Library General
6 # Public License as described in the bup LICENSE file.
8 # TODO: Add tar-like -C option.
9 # TODO: Add tar-like -v support to --list.
12 from bup import metadata
13 from bup import options
14 from bup.helpers import handle_ctrl_c, log, saved_errors
17 bup meta --create [OPTION ...] <PATH ...>
18 bup meta --extract [OPTION ...]
19 bup meta --start-extract [OPTION ...]
20 bup meta --finish-extract [OPTION ...]
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
38 should_recurse = False
39 restore_numeric_ids = False
41 handle_symlinks = True
45 o = options.Options('bup meta', optspec)
46 (opt, flags, remainder) = o.parse(sys.argv[1:])
48 for flag, value in flags:
49 if flag == '--create' or flag == '-c':
51 if flag == '--list' or flag == '-t':
53 elif flag == '--extract' or flag == '-x':
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':
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':
71 elif flag == '--no-paths':
73 elif flag == '--symlinks':
74 handle_symlinks = True
75 elif flag == '--no-symlinks':
76 handle_symlinks = False
77 elif flag == '--verbose' or flag == '-v':
79 elif flag == '--quiet' or flag == '-q':
83 o.fatal("no action specified")
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')
91 output_file = sys.stdout
92 metadata.save_tree(output_file,
94 recurse=should_recurse,
95 write_paths=include_paths,
96 save_symlinks=handle_symlinks)
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')
105 metadata.display_archive(src)
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')
114 metadata.start_extract(src, create_symlinks=handle_symlinks)
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')
123 num_ids = restore_numeric_ids
124 metadata.finish_extract(src, restore_numeric_ids=num_ids)
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')
133 metadata.extract(src,
134 restore_numeric_ids=restore_numeric_ids,
135 create_symlinks=handle_symlinks)
138 log('WARNING: %d errors encountered.\n' % len(saved_errors))