4 # --md5sum - display md5 of each file as read from disk
11 def parse_arguments(arguments):
12 parser = argparse.ArgumentParser(
13 description='Copy data from the local filesystem to Keep.')
15 parser.add_argument('paths', metavar='path', type=str, nargs='*',
17 Local file or directory. Default: read from standard input.
20 parser.add_argument('--max-manifest-depth', type=int, metavar='N',
22 Maximum depth of directory tree to represent in the manifest
23 structure. A directory structure deeper than this will be represented
24 as a single stream in the manifest. If N=0, the manifest will contain
25 a single stream. Default: -1 (unlimited), i.e., exactly one manifest
26 stream per filesystem directory that contains files.
29 group = parser.add_mutually_exclusive_group()
31 group.add_argument('--as-stream', action='store_true', dest='stream',
36 group.add_argument('--stream', action='store_true',
38 Store the file content and display the resulting manifest on
39 stdout. Do not write the manifest to Keep or save a Collection object
43 group.add_argument('--as-manifest', action='store_true', dest='manifest',
45 Synonym for --manifest.
48 group.add_argument('--in-manifest', action='store_true', dest='manifest',
50 Synonym for --manifest.
53 group.add_argument('--manifest', action='store_true',
55 Store the file data and resulting manifest in Keep, save a Collection
56 object in Arvados, and display the manifest locator (Collection uuid)
57 on stdout. This is the default behavior.
60 group.add_argument('--as-raw', action='store_true', dest='raw',
65 group.add_argument('--raw', action='store_true',
67 Store the file content and display the data block locators on stdout,
68 separated by commas, with a trailing newline. Do not store a
72 parser.add_argument('--use-filename', type=str, default=None,
73 dest='filename', help="""
74 Synonym for --filename.
77 parser.add_argument('--filename', type=str, default=None,
79 Use the given filename in the manifest, instead of the name of the
80 local file. This is useful when "-" or "/dev/stdin" is given as an
81 input file. It can be used only if there is exactly one path given and
82 it is not a directory. Implies --manifest.
85 group = parser.add_mutually_exclusive_group()
86 group.add_argument('--progress', action='store_true',
88 Display human-readable progress on stderr (bytes and, if possible,
89 percentage of total data size). This is the default behavior when
93 group.add_argument('--no-progress', action='store_true',
95 Do not display human-readable progress on stderr, even if stderr is a
99 group.add_argument('--batch-progress', action='store_true',
101 Display machine-readable progress on stderr (bytes and, if known,
105 args = parser.parse_args(arguments)
107 if len(args.paths) == 0:
108 args.paths += ['/dev/stdin']
110 if len(args.paths) != 1 or os.path.isdir(args.paths[0]):
113 --filename argument cannot be used when storing a directory or
117 # Turn on --progress by default if stderr is a tty.
118 if (not (args.batch_progress or args.no_progress)
119 and os.isatty(sys.stderr.fileno())):
122 if args.paths == ['-']:
123 args.paths = ['/dev/stdin']
124 if not args.filename:
129 class CollectionWriterWithProgress(arvados.CollectionWriter):
130 def flush_data(self, *args, **kwargs):
131 if not getattr(self, 'display_type', None):
133 if not hasattr(self, 'bytes_flushed'):
134 self.bytes_flushed = 0
135 self.bytes_flushed += self._data_buffer_len
136 super(CollectionWriterWithProgress, self).flush_data(*args, **kwargs)
137 self.bytes_flushed -= self._data_buffer_len
138 if self.display_type == 'machine':
139 sys.stderr.write('%s %d: %d written %d total\n' %
143 getattr(self, 'bytes_expected', -1)))
144 elif getattr(self, 'bytes_expected', 0) > 0:
145 pct = 100.0 * self.bytes_flushed / self.bytes_expected
146 sys.stderr.write('\r%dM / %dM %.1f%% ' %
147 (self.bytes_flushed >> 20,
148 self.bytes_expected >> 20, pct))
150 sys.stderr.write('\r%d ' % self.bytes_flushed)
152 def manifest_text(self, *args, **kwargs):
153 manifest_text = (super(CollectionWriterWithProgress, self)
154 .manifest_text(*args, **kwargs))
155 if getattr(self, 'display_type', None):
156 if self.display_type == 'human':
157 sys.stderr.write('\n')
158 self.display_type = None
162 def main(arguments=None):
163 args = parse_arguments(arguments)
166 writer = CollectionWriterWithProgress()
167 writer.display_type = 'human'
168 elif args.batch_progress:
169 writer = CollectionWriterWithProgress()
170 writer.display_type = 'machine'
172 writer = arvados.CollectionWriter()
174 # Walk the given directory trees and stat files, adding up file sizes,
175 # so we can display progress as percent
176 writer.bytes_expected = 0
177 for path in args.paths:
178 if os.path.isdir(path):
179 for filename in arvados.util.listdir_recursive(path):
180 writer.bytes_expected += os.path.getsize(
181 os.path.join(path, filename))
182 elif not os.path.isfile(path):
183 del writer.bytes_expected
186 writer.bytes_expected += os.path.getsize(path)
188 # Copy file data to Keep.
189 for path in args.paths:
190 if os.path.isdir(path):
191 writer.write_directory_tree(
192 path, max_manifest_depth=args.max_manifest_depth)
194 writer.start_new_stream()
195 writer.write_file(path, args.filename or os.path.basename(path))
198 print writer.manifest_text(),
200 writer.finish_current_stream()
201 print ','.join(writer.data_locators())
203 # Register the resulting collection in Arvados.
204 arvados.api().collections().create(
206 'uuid': writer.finish(),
207 'manifest_text': writer.manifest_text(),
211 # Print the locator (uuid) of the new collection.
212 print writer.finish()
214 if __name__ == '__main__':