3036: Fix arv-put to write name/owner of collections object directly when
[arvados.git] / sdk / python / arvados / commands / put.py
index fa648dac4592d6adee7821a0eac523355584cdc4..71e31b24184079a00ccfc69ecccdd75b83e97350 100644 (file)
@@ -40,11 +40,13 @@ stream per filesystem directory that contains files.
 """)
 
 upload_opts.add_argument('--project-uuid', metavar='UUID', help="""
-When a Collection is made, make a Link to save it under the specified project.
+Store the collection in the specified project, instead of your Home
+project.
 """)
 
 upload_opts.add_argument('--name', help="""
-When a Collection is linked to a project, use the specified name.
+Save the collection with the specified name, rather than the default
+generic name "Saved at {time} by {username}@{host}".
 """)
 
 _group = upload_opts.add_mutually_exclusive_group()
@@ -352,31 +354,29 @@ def prep_project_link(args, stderr, project_exists=check_project_exists):
     # to create the desired project link for this Collection, or None.
     # Raises ValueError if the arguments request something impossible.
     making_collection = not (args.raw or args.stream)
-    any_link_spec = args.project_uuid or args.name
     if not making_collection:
-        if any_link_spec:
+        if args.name or args.project_uuid:
             raise ValueError("Requested a Link without creating a Collection")
         return None
-    elif not any_link_spec:
-        stderr.write(
-            "arv-put: No --project-uuid or --name specified.  This data will be cached\n"
-            "in Keep.  You will need to find this upload by its locator(s) later.\n")
-        return None
-    elif not args.project_uuid:
-        raise ValueError("--name requires --project-uuid")
-    elif not project_exists(args.project_uuid):
+    link = {'tail_uuid': args.project_uuid,
+            'link_class': 'name',
+            'name': args.name}
+    if not link['tail_uuid']:
+        link['tail_uuid'] = arvados.api('v1').users().current().execute()['uuid']
+    elif not project_exists(link['tail_uuid']):
         raise ValueError("Project {} not found".format(args.project_uuid))
-    link = {'tail_uuid': args.project_uuid, 'link_class': 'name'}
-    if args.name:
-        link['name'] = args.name
+    if not link['name']:
+        link['name'] = "Saved at {} by {}@{}".format(
+            datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC"),
+            pwd.getpwuid(os.getuid()).pw_name,
+            socket.gethostname())
+        stderr.write(
+            "arv-put: No --name specified. Saving as \"%s\"\n" % link['name'])
+    link['owner_uuid'] = link['tail_uuid']
     return link
 
 def create_project_link(locator, link):
     link['head_uuid'] = locator
-    link.setdefault('name', "Collection saved by {}@{} at {}".format(
-            pwd.getpwuid(os.getuid()).pw_name,
-            socket.gethostname(),
-            datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")))
     return arvados.api('v1').links().create(body=link).execute()
 
 def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
@@ -447,7 +447,6 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
         # Register the resulting collection in Arvados.
         collection = arvados.api().collections().create(
             body={
-                'uuid': writer.finish(),
                 'manifest_text': writer.manifest_text(),
                 },
             ).execute()
@@ -456,7 +455,12 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
         output = collection['uuid']
         if project_link is not None:
             try:
-                create_project_link(output, project_link)
+                if 'name' in collection:
+                    arvados.api().collections().update(uuid=output,
+                                                       body={"owner_uuid": project_link["tail_uuid"],
+                                                             "name": project_link["name"]}).execute()
+                else:
+                    create_project_link(output, project_link)
             except apiclient.errors.Error as error:
                 print >>stderr, (
                     "arv-put: Error adding Collection to project: {}.".format(