Search code examples
python-3.7perforcep4python

P4Python does not check out the file in Perforce


I have following piece of code. I'm trying to check out two files from Perforce and put them in a changelist. But run_add does not check the files out. The only thing I see in Perforce is a empty changelist with no files in it.

""" Checks out files from workspace using P4"""
files = ['analyse-location.cfg', 'CMakeLists.txt']
p4 = P4()

# Connect and disconnect
if (p4.connected()):
    p4.disconnect()

p4.port = portp4
p4.user = usernameP4
p4.password = passwordP4
p4.client = clientP4
try:
    p4.connect()
    if p4.connected():
        change = p4.fetch_change()
        change['Description'] = "Auto"
        change['Files'] = []
        changeList = p4.save_change(change)[0].split()[1]

        for items in files:
            abs_path = script_dir + "\\" + items
            p4.run_add("-c", changeList, items)
            print("Adding file "+ abs_path + " to "+ changeList)

    # Done! Disconnect!
    p4.disconnect()

except P4Exception:
    print("Something went wrong in P4 connection. The errors are: ")
    for e in p4.errors:
        print(e)
    p4.disconnect()

However, when I have instead p4.run("edit", items) it puts the files in the default changelist.It really gets on my nervs. I don't know I am doing that is wrong. The changes list created as well. I use python 3.7 32 bits on Windows


Solution

  • I changed my question to following and it worked.

    p4.port = portp4
    p4.user = usernameP4
    p4.password = passwordP4
    p4.client = clientP4
    
    try:
        p4.connect()
        if p4.connected():
            change = p4.fetch_change()
            change['Description'] = "Auto"
            change['Files'] = []
            changeList = p4.save_change(change)[0].split()[1]
    
            for items in files:
                abs_path = script_dir + "\\" + items
                output = p4.run_edit("-c", changeList, items)
                print("Adding file "+ abs_path + " to "+ changeList)
                if output:
                    print(output)
    
        if p4.errors:
            print(p4.errors)
        if p4.warnings:
            print(p4.warnings)
    
        p4.disconnect()
    except P4Exception:
        print("Something went wrong in P4 connection. The errors are: ")
        for e in p4.errors:
            print(e)
        p4.disconnect()
    

    Thanks to @Sam Stafford for his hint. Now it works just like a charm. The key was to change p4.run_add("-c", changelist, items) to p4.run_edit("-c", changelist, items)