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