Merge branch '8784-dir-listings'
[arvados.git] / sdk / python / arvados / crunch.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 from builtins import object
6 import json
7 import os
8
9 class TaskOutputDir(object):
10     """Keep-backed directory for staging outputs of Crunch tasks.
11
12     Example, in a crunch task whose output is a file called "out.txt"
13     containing "42":
14
15         import arvados
16         import arvados.crunch
17         import os
18
19         out = arvados.crunch.TaskOutputDir()
20         with open(os.path.join(out.path, 'out.txt'), 'w') as f:
21             f.write('42')
22         arvados.current_task().set_output(out.manifest_text())
23     """
24     def __init__(self):
25         self.path = os.environ['TASK_KEEPMOUNT_TMP']
26
27     def __str__(self):
28         return self.path
29
30     def manifest_text(self):
31         snapshot = os.path.join(self.path, '.arvados#collection')
32         return json.load(open(snapshot))['manifest_text']