From 5fd88c03cfa7fd92894c29957a02b068e852b0e1 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 21 Nov 2014 15:12:57 +0100 Subject: [PATCH] bam cli: add ability to checkout into a named location --- client/cli/bam.py | 36 ++++++++++++++++++++++-------------- tests/blendfile_templates.py | 1 - tests/test_cli.py | 30 ++++++++++++++++-------------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/client/cli/bam.py b/client/cli/bam.py index d568400..f36c5f7 100755 --- a/client/cli/bam.py +++ b/client/cli/bam.py @@ -316,22 +316,27 @@ class bam_commands: print("Session %r created" % session_name) - @staticmethod - def checkout(paths): + def checkout(path, output_dir=None): import sys import os import requests cfg = bam_config.load(abort=True) - # TODO(cam) multiple paths - path = paths[0] - del paths - - # TODO(cam) we may want to checkout a single file? how to handle this? - # we may want to checkout a dir too - dst_dir = os.path.splitext(os.path.basename(path))[0] + if output_dir is None: + # fallback to the basename + dst_dir = os.path.splitext(os.path.basename(path))[0] + else: + if os.sep in output_dir.rstrip(os.sep): + # are we a subdirectory? + # (we know this exists, since we have config already) + rootdir = bam_config.find_rootdir(abort=True) + if ".." in os.path.relpath(output_dir, rootdir).split(os.sep): + fatal("Output %r is outside the project path %r" % (output_dir, rootdir)) + del rootdir + dst_dir = output_dir + del output_dir payload = { "filepath": path, @@ -409,8 +414,7 @@ class bam_commands: session_rootdir = paths[0] if not os.path.isdir(session_rootdir): - print("Expected a directory (%r)" % session_rootdir) - sys.exit(1) + fatal("Expected a directory (%r)" % session_rootdir) basedir = bam_config.find_basedir( cwd=session_rootdir, @@ -641,7 +645,7 @@ def subcommand_create_cb(args): def subcommand_checkout_cb(args): - bam_commands.checkout(args.paths) + bam_commands.checkout(args.path, args.output) def subcommand_commit_cb(args): @@ -706,8 +710,12 @@ def create_argparse_checkout(subparsers): help="", ) subparse.add_argument( - dest="paths", nargs="+", - help="Path(s) to operate on", + dest="path", type=str, metavar='REMOTE_PATH', + help="Path to checkout on the server", + ) + subparse.add_argument( + "-o", "--output",dest="output", type=str, metavar='DIRNAME', + help="Local name to checkout the session into (optional, falls back to path name)", ) subparse.set_defaults(func=subcommand_checkout_cb) diff --git a/tests/blendfile_templates.py b/tests/blendfile_templates.py index 07813b8..9e0677e 100644 --- a/tests/blendfile_templates.py +++ b/tests/blendfile_templates.py @@ -39,7 +39,6 @@ def create_image_single(deps): image.save() - if __name__ == "__main__": import sys blendfile, blendfile_deps_json, create_id, returncode = sys.argv[-4:] diff --git a/tests/test_cli.py b/tests/test_cli.py index 9f492c1..6445fb5 100755 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -574,6 +574,7 @@ class BamCommitTest(BamSessionTestCase): def test_commit(self): self.init_repo() + file_name = "testfile.txt" file_data = b"hello world!\n" proj_path = os.path.join(self.path_local_store, self.proj_name) @@ -589,13 +590,14 @@ class BamCommitTest(BamSessionTestCase): self.assertEqual("Nothing to commit!\n", stdout) # now do a real commit - file_quick_write(session_path, "testfile.txt", file_data) + file_quick_write(session_path, file_name, file_data) stdout, stderr = bam_run(["commit", "-m", "test message"], session_path) self.assertEqual("", stderr) def test_checkout(self): self.init_repo() - file_data = b"hello world!\n" + file_data = b"yo world!\n" + file_name = "other_file.txt" proj_path = os.path.join(self.path_local_store, self.proj_name) co_id = "mysession" @@ -605,7 +607,7 @@ class BamCommitTest(BamSessionTestCase): self.assertEqual("", stderr) # now do a real commit - file_quick_write(session_path, "testfile.txt", file_data) + file_quick_write(session_path, file_name, file_data) stdout, stderr = bam_run(["commit", "-m", "test message"], session_path) self.assertEqual("", stderr) @@ -613,12 +615,12 @@ class BamCommitTest(BamSessionTestCase): shutil.rmtree(session_path) # checkout the file again - stdout, stderr = bam_run(["checkout", "testfile.txt"], proj_path) + stdout, stderr = bam_run(["checkout", file_name, "--output", session_path], proj_path) self.assertEqual("", stderr) # wait_for_input() - self.assertTrue(os.path.exists(os.path.join(proj_path, "testfile/testfile.txt"))) + self.assertTrue(os.path.exists(os.path.join(session_path, file_name))) - file_data_test = file_quick_read(os.path.join(proj_path, "testfile/testfile.txt")) + file_data_test = file_quick_read(os.path.join(session_path, file_name)) self.assertEqual(file_data, file_data_test) @@ -666,7 +668,8 @@ class BamBlendTest(BamSimpleTestCase): shutil.rmtree(TEMP_SESSION) def test_empty(self): - blendfile = os.path.join(TEMP, "test.blend") + file_name = "testfile.blend" + blendfile = os.path.join(TEMP, file_name) if not blendfile_template_create(blendfile, "create_blank", []): self.fail("blend file couldn't be created") return @@ -674,7 +677,6 @@ class BamBlendTest(BamSimpleTestCase): self.assertTrue(os.path.exists(blendfile)) - class BamDeleteTest(BamSessionTestCase): """Test for the `bam commit` command when files are being deleted. """ @@ -685,6 +687,7 @@ class BamDeleteTest(BamSessionTestCase): def test_delete(self): self.init_repo() + file_name = "testfile.blend" file_data = b"hello world!\n" proj_path = os.path.join(self.path_local_store, self.proj_name) @@ -695,8 +698,7 @@ class BamDeleteTest(BamSessionTestCase): self.assertEqual("", stderr) # now do a real commit - returncode_test = 42 - blendfile = os.path.join(session_path, "testfile.blend") + blendfile = os.path.join(session_path, file_name) if not blendfile_template_create(blendfile, "create_blank", []): self.fail("blend file couldn't be created") return @@ -712,13 +714,13 @@ class BamDeleteTest(BamSessionTestCase): # New Session # checkout the file again - stdout, stderr = bam_run(["checkout", "testfile.blend"], proj_path) + stdout, stderr = bam_run(["checkout", file_name, "--output", "new_out"], proj_path) self.assertEqual("", stderr) # now delete the file we just checked out - session_path = os.path.join(proj_path, "testfile") - os.remove(os.path.join(session_path, "testfile.blend")) + session_path = os.path.join(proj_path, "new_out") + os.remove(os.path.join(session_path, file_name)) stdout, stderr = bam_run(["commit", "-m", "test deletion"], session_path) self.assertEqual("", stderr) # check if deletion of the file has happened @@ -732,7 +734,7 @@ class BamDeleteTest(BamSessionTestCase): listing = json.loads(stdout) print(listing) for e in listing: - self.assertNotEqual(e[0], "testfile.txt") + self.assertNotEqual(e[0], file_name) if __name__ == '__main__':