]> arthur.barton.de Git - bup.git/blob - cmd/meta-cmd.py
Default to stdin/stdout in bup meta when -f is not specified.
[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
17 def open_input(name):
18     if not name or name == '-':
19         return sys.stdin
20     return open(name, 'r')
21
22
23 def open_output(name):
24     if not name or name == '-':
25         return sys.stdout
26     return open(name, 'w')
27
28
29 optspec = """
30 bup meta --create [OPTION ...] <PATH ...>
31 bup meta --extract [OPTION ...]
32 bup meta --start-extract [OPTION ...]
33 bup meta --finish-extract [OPTION ...]
34 --
35 c,create       write metadata for PATHs to stdout (or --file)
36 t,list         display metadata
37 x,extract      perform --start-extract followed by --finish-extract
38 start-extract  build tree matching metadata provided on standard input (or --file)
39 finish-extract finish applying standard input (or --file) metadata to filesystem
40 f,file=        specify source or destination file
41 R,recurse      recurse into subdirectories
42 xdev,one-file-system  don't cross filesystem boundaries
43 numeric-ids    apply numeric IDs (user, group, etc.), not names, during restore
44 symlinks       handle symbolic links (default is true)
45 paths          include paths in metadata (default is true)
46 v,verbose      increase log output (can be used more than once)
47 q,quiet        don't show progress meter
48 """
49
50 handle_ctrl_c()
51
52 o = options.Options(optspec)
53 (opt, flags, remainder) = o.parse(['--paths', '--symlinks'] + sys.argv[1:])
54
55 opt.verbose = opt.verbose or 0
56 opt.quiet = opt.quiet or 0
57 metadata.verbose = opt.verbose - opt.quiet
58
59 action_count = sum([bool(x) for x in [opt.create, opt.list, opt.extract,
60                                       opt.start_extract, opt.finish_extract]])
61 if action_count > 1:
62     o.fatal("bup: only one action permitted: --create --list --extract")
63 if action_count == 0:
64     o.fatal("bup: no action specified")
65
66 if opt.create:
67     if len(remainder) < 1:
68         o.fatal("no paths specified for create")
69     output_file = open_output(opt.file)
70     metadata.save_tree(output_file,
71                        remainder,
72                        recurse=opt.recurse,
73                        write_paths=opt.paths,
74                        save_symlinks=opt.symlinks,
75                        xdev=opt.xdev)
76 elif opt.list:
77     if len(remainder) > 0:
78         o.fatal("cannot specify paths for --list")
79     src = open_input(opt.file)
80     metadata.display_archive(src)
81 elif opt.start_extract:
82     if len(remainder) > 0:
83         o.fatal("cannot specify paths for --start-extract")
84     src = open_input(opt.file)
85     metadata.start_extract(src, create_symlinks=opt.symlinks)
86 elif opt.finish_extract:
87     if len(remainder) > 0:
88         o.fatal("cannot specify paths for --finish-extract")
89     src = open_input(opt.file)
90     metadata.finish_extract(src, restore_numeric_ids=opt.numeric_ids)
91 elif opt.extract:
92     if len(remainder) > 0:
93         o.fatal("cannot specify paths for --extract")
94     src = open_input(opt.file)
95     metadata.extract(src,
96                      restore_numeric_ids=opt.numeric_ids,
97                      create_symlinks=opt.symlinks)
98
99 if saved_errors:
100     log('WARNING: %d errors encountered.\n' % len(saved_errors))
101     sys.exit(1)
102 else:
103     sys.exit(0)