Skip to content

Commit

Permalink
Allow client data to be re-used on a different host (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ca-johnson authored Aug 12, 2019
1 parent 45f3f06 commit e03e496
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion python/perforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions python/test_perforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit e03e496

Please sign in to comment.