From e03e4961c7a636fb5465b572a5f352f958e4f45c Mon Sep 17 00:00:00 2001 From: Carl Johnson Date: Mon, 12 Aug 2019 17:38:57 +0100 Subject: [PATCH] Allow client data to be re-used on a different host (#109) --- python/perforce.py | 11 ++++++++++- python/test_perforce.py | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/python/perforce.py b/python/perforce.py index 8d9c236..5dce0d9 100644 --- a/python/perforce.py +++ b/python/perforce.py @@ -87,9 +87,18 @@ def _setup_client(self): self.perforce.save_client(client) self.perforce.client = clientname - if not os.path.isfile(os.path.join(self.root, "p4config")): + p4config = os.path.join(self.root, "p4config") + if not os.path.isfile(p4config): self.perforce.logger.warn("p4config missing, flushing workspace to revision zero") self.perforce.run_flush(['//...@0']) + else: + with open(p4config) as infile: + prev_clientname = next(line.split('=', 1)[-1] + for line in infile.read().splitlines() # removes \n + if line.startswith('P4CLIENT=')) + if prev_clientname != clientname: + self.perforce.logger.warn("p4config last client was %s, flushing workspace to match" % prev_clientname) + self.perforce.run_flush(['//...@%s' % prev_clientname]) self._write_p4config() self.created_client = True diff --git a/python/test_perforce.py b/python/test_perforce.py index 42a03fa..a976069 100644 --- a/python/test_perforce.py +++ b/python/test_perforce.py @@ -189,3 +189,29 @@ def test_backup_shelve(): repo.unshelve(backup_changelist) with open(os.path.join(client_root, "file.txt")) as content: assert content.read() == "Goodbye World\n", "Unexpected content in workspace file" + + +def copytree(src, dst): + """Shim to get around shutil.copytree requiring root dir to not exist""" + for item in os.listdir(src): + s = os.path.join(src, item) + d = os.path.join(dst, item) + if os.path.isdir(s): + shutil.copytree(s, d) + else: + shutil.copy2(s, d) + +def test_client_migration(): + """Test re-use of workspace data when moved to another host""" + with setup_server_and_client() as client_root: + repo = P4Repo(root=client_root) + + assert os.listdir(client_root) == [], "Workspace should be empty" + synced = repo.sync() + assert len(synced) > 0, "Didn't sync any files" + + with tempfile.TemporaryDirectory(prefix="bk-p4-test-") as second_client_root: + copytree(client_root, second_client_root) + repo = P4Repo(root=second_client_root) + synced = repo.sync() # Flushes to match previous client, since p4config is there on disk + assert synced == [], "Should not have synced any files in second client"