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