]> arthur.barton.de Git - bup.git/blob - cmd/meta-cmd.py
Add --human-readable option to "bup web".
[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
10 import sys
11 from bup import metadata
12 from bup import options
13 from bup.helpers import handle_ctrl_c, log, saved_errors
14
15
16 def open_input(name):
17     if not name or name == '-':
18         return sys.stdin
19     return open(name, 'r')
20
21
22 def open_output(name):
23     if not name or name == '-':
24         return sys.stdout
25     return open(name, 'w')
26
27
28 optspec = """
29 bup meta --create [OPTION ...] <PATH ...>
30 bup meta --list [OPTION ...]
31 bup meta --extract [OPTION ...]
32 bup meta --start-extract [OPTION ...]
33 bup meta --finish-extract [OPTION ...]
34 bup meta --edit [OPTION ...] <PATH ...>
35 --
36 c,create       write metadata for PATHs to stdout (or --file)
37 t,list         display metadata
38 x,extract      perform --start-extract followed by --finish-extract
39 start-extract  build tree matching metadata provided on standard input (or --file)
40 finish-extract finish applying standard input (or --file) metadata to filesystem
41 edit           alter metadata; write to stdout (or --file)
42 f,file=        specify source or destination file
43 R,recurse      recurse into subdirectories
44 xdev,one-file-system  don't cross filesystem boundaries
45 numeric-ids    apply numeric IDs (user, group, etc.) rather than names
46 symlinks       handle symbolic links (default is true)
47 paths          include paths in metadata (default is true)
48 set-uid=       set metadata uid (via --edit)
49 set-gid=       set metadata gid (via --edit)
50 set-user=      set metadata user (via --edit)
51 unset-user     remove metadata user (via --edit)
52 set-group=     set metadata group (via --edit)
53 unset-group    remove metadata group (via --edit)
54 v,verbose      increase log output (can be used more than once)
55 q,quiet        don't show progress meter
56 """
57
58 handle_ctrl_c()
59
60 o = options.Options(optspec)
61 (opt, flags, remainder) = o.parse(['--paths', '--symlinks'] + sys.argv[1:])
62
63 opt.verbose = opt.verbose or 0
64 opt.quiet = opt.quiet or 0
65 metadata.verbose = opt.verbose - opt.quiet
66
67 action_count = sum([bool(x) for x in [opt.create, opt.list, opt.extract,
68                                       opt.start_extract, opt.finish_extract,
69                                       opt.edit]])
70 if action_count > 1:
71     o.fatal("bup: only one action permitted: --create --list --extract --edit")
72 if action_count == 0:
73     o.fatal("bup: no action specified")
74
75 if opt.create:
76     if len(remainder) < 1:
77         o.fatal("no paths specified for create")
78     output_file = open_output(opt.file)
79     metadata.save_tree(output_file,
80                        remainder,
81                        recurse=opt.recurse,
82                        write_paths=opt.paths,
83                        save_symlinks=opt.symlinks,
84                        xdev=opt.xdev)
85 elif opt.list:
86     if len(remainder) > 0:
87         o.fatal("cannot specify paths for --list")
88     src = open_input(opt.file)
89     metadata.display_archive(src)
90 elif opt.start_extract:
91     if len(remainder) > 0:
92         o.fatal("cannot specify paths for --start-extract")
93     src = open_input(opt.file)
94     metadata.start_extract(src, create_symlinks=opt.symlinks)
95 elif opt.finish_extract:
96     if len(remainder) > 0:
97         o.fatal("cannot specify paths for --finish-extract")
98     src = open_input(opt.file)
99     metadata.finish_extract(src, restore_numeric_ids=opt.numeric_ids)
100 elif opt.extract:
101     if len(remainder) > 0:
102         o.fatal("cannot specify paths for --extract")
103     src = open_input(opt.file)
104     metadata.extract(src,
105                      restore_numeric_ids=opt.numeric_ids,
106                      create_symlinks=opt.symlinks)
107 elif opt.edit:
108     if len(remainder) < 1:
109         o.fatal("no paths specified for edit")
110     output_file = open_output(opt.file)
111
112     unset_user = False # True if --unset-user was the last relevant option.
113     unset_group = False # True if --unset-group was the last relevant option.
114     for flag in flags:
115         if flag[0] == '--set-user':
116             unset_user = False
117         elif flag[0] == '--unset-user':
118             unset_user = True
119         elif flag[0] == '--set-group':
120             unset_group = False
121         elif flag[0] == '--unset-group':
122             unset_group = True
123
124     for path in remainder:
125         f = open(path, 'r')
126         try:
127             for m in metadata._ArchiveIterator(f):
128                 if opt.set_uid is not None:
129                     try:
130                         m.uid = int(opt.set_uid)
131                     except ValueError:
132                         o.fatal("uid must be an integer")
133
134                 if opt.set_gid is not None:
135                     try:
136                         m.gid = int(opt.set_gid)
137                     except ValueError:
138                         o.fatal("gid must be an integer")
139
140                 if unset_user:
141                     m.user = ''
142                 elif opt.set_user is not None:
143                     m.user = opt.set_user
144
145                 if unset_group:
146                     m.group = ''
147                 elif opt.set_group is not None:
148                     m.group = opt.set_group
149
150                 m.write(output_file)
151         finally:
152             f.close()
153
154
155 if saved_errors:
156     log('WARNING: %d errors encountered.\n' % len(saved_errors))
157     sys.exit(1)
158 else:
159     sys.exit(0)