#!/usr/bin/env python3
from __future__ import print_function
import P4
import sys
import os
USER = "sven_erik_knop" # replace this
PASSWORD = "password" # replace this
def create_client(p4):
"""Create a client in the server and return its name"""
# define a client name
clientName = USER + ".python-sdk.test.ws"
# fetch the default client template
client = p4.fetch_client(clientName)
# set the client root explicitly
client._root = os.getcwd()
# set the client view
client._view = [ "//guest/{user}/python-sdk/test/... //{client_name}/...".format(user=USER, client_name=clientName) ]
# set the client options to allwrite to make all files writable
client._options = client._options.replace("noallwrite", "allwrite")
# write the client spec back to the server. Client now exists
p4.save_client( client )
return clientName
p4 = P4.P4()
p4.port = "public.perforce.com:1666"
try:
with p4.connect():
p4.user = USER
p4.password = PASSWORD
p4.run_login()
# create the client
p4.client = create_client(p4)
# sync the files, ignore warning that all files are synced already
p4.run_sync(exception_level = 1)
# open a file for append (or create, if non existent), write some strings
with open("file.txt", "a") as f:
print("Changed by user {}".format(USER), file=f)
# discover any changed files
p4.run_reconcile()
# submit the change
change = p4.fetch_change()
change._description = "My change description"
p4.run_submit( change )
except P4.P4Exception as e:
print("Encountered error: {}".format(e), sys.stderr)
sys.exit(1)