4 # --md5sum - display md5 of each file as read from disk
11 parser = argparse.ArgumentParser(
12 description='Copy data from the local filesystem to Keep.')
14 parser.add_argument('paths', metavar='path', type=str, nargs='*',
16 Local file or directory. Default: read from standard input.
19 parser.add_argument('--max-manifest-depth', type=int, metavar='N', default=-1,
21 Maximum depth of directory tree to represent in the manifest
22 structure. A directory structure deeper than this will be represented
23 as a single stream in the manifest. If N=0, the manifest will contain
24 a single stream. Default: -1 (unlimited), i.e., exactly one manifest
25 stream per filesystem directory that contains files.
28 group = parser.add_mutually_exclusive_group()
30 group.add_argument('--as-stream', action='store_true', dest='stream',
35 group.add_argument('--stream', action='store_true',
37 Store the file content and display the resulting manifest on
38 stdout. Do not write the manifest to Keep or save a Collection object
42 group.add_argument('--as-manifest', action='store_true', dest='manifest',
44 Synonym for --manifest.
47 group.add_argument('--in-manifest', action='store_true', dest='manifest',
49 Synonym for --manifest.
52 group.add_argument('--manifest', action='store_true',
54 Store the file data and resulting manifest in Keep, save a Collection
55 object in Arvados, and display the manifest locator (Collection uuid)
56 on stdout. This is the default behavior.
59 group.add_argument('--as-raw', action='store_true', dest='raw',
64 group.add_argument('--raw', action='store_true',
66 Store the file content and display the data block locators on stdout,
67 separated by commas, with a trailing newline. Do not store a
71 parser.add_argument('--use-filename', type=str, default=None, dest='filename',
73 Synonym for --filename.
76 parser.add_argument('--filename', type=str, default=None,
78 Use the given filename in the manifest, instead of the name of the
79 local file. This is useful when "-" or "/dev/stdin" is given as an
80 input file. It can be used only if there is exactly one path given and
81 it is not a directory. Implies --manifest.
84 group = parser.add_mutually_exclusive_group()
85 group.add_argument('--progress', action='store_true',
87 Display human-readable progress on stderr (bytes and, if possible,
88 percentage of total data size). This is the default behavior when
92 group.add_argument('--no-progress', action='store_true',
94 Do not display human-readable progress on stderr, even if stderr is a
98 group.add_argument('--batch-progress', action='store_true',
100 Display machine-readable progress on stderr (bytes and, if known,
104 args = parser.parse_args()
106 if len(args.paths) == 0:
107 args.paths += ['/dev/stdin']
109 if len(args.paths) != 1 or os.path.isdir(args.paths[0]):
112 --filename argument cannot be used when storing a directory or
116 # Turn on --progress by default if stderr is a tty.
117 if (not (args.batch_progress or args.no_progress)
118 and os.isatty(sys.stderr.fileno())):
121 class CollectionWriterWithProgress(arvados.CollectionWriter):
122 def flush_data(self, *args, **kwargs):
123 if not getattr(self, 'display_type', None):
125 if not hasattr(self, 'bytes_flushed'):
126 self.bytes_flushed = 0
127 self.bytes_flushed += self._data_buffer_len
128 super(CollectionWriterWithProgress, self).flush_data(*args, **kwargs)
129 self.bytes_flushed -= self._data_buffer_len
130 if self.display_type == 'machine':
131 sys.stderr.write('%s %d: %d written %d total\n' %
135 getattr(self, 'bytes_expected', -1)))
136 elif getattr(self, 'bytes_expected', 0) > 0:
137 pct = 100.0 * self.bytes_flushed / self.bytes_expected
138 sys.stderr.write('\r%dM / %dM %.1f%% ' %
139 (self.bytes_flushed >> 20,
140 self.bytes_expected >> 20, pct))
142 sys.stderr.write('\r%d ' % self.bytes_flushed)
144 def manifest_text(self, *args, **kwargs):
145 manifest_text = (super(CollectionWriterWithProgress, self)
146 .manifest_text(*args, **kwargs))
147 if getattr(self, 'display_type', None):
148 if self.display_type == 'human':
149 sys.stderr.write('\n')
150 self.display_type = None
154 writer = CollectionWriterWithProgress()
155 writer.display_type = 'human'
156 elif args.batch_progress:
157 writer = CollectionWriterWithProgress()
158 writer.display_type = 'machine'
160 writer = arvados.CollectionWriter()
162 if args.paths == ['-']:
163 args.paths = ['/dev/stdin']
164 if not args.filename:
167 # Walk the given directory trees and stat files, adding up file sizes,
168 # so we can display progress as percent
169 writer.bytes_expected = 0
170 for path in args.paths:
171 if os.path.isdir(path):
172 for filename in arvados.util.listdir_recursive(path):
173 writer.bytes_expected += os.path.getsize(
174 os.path.join(path, filename))
175 elif not os.path.isfile(path):
176 del writer.bytes_expected
179 writer.bytes_expected += os.path.getsize(path)
181 # Copy file data to Keep.
182 for path in args.paths:
183 if os.path.isdir(path):
184 writer.write_directory_tree(path,
185 max_manifest_depth=args.max_manifest_depth)
187 writer.start_new_stream()
188 writer.start_new_file(args.filename or os.path.split(path)[1])
189 with open(path, 'rb') as f:
197 print writer.manifest_text(),
199 writer.finish_current_stream()
200 print ','.join(writer.data_locators())
202 # Register the resulting collection in Arvados.
203 arvados.api().collections().create(
205 'uuid': writer.finish(),
206 'manifest_text': writer.manifest_text(),
210 # Print the locator (uuid) of the new collection.
211 print writer.finish()