Payam30
Payam30

Reputation: 729

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

Upvotes: 0

Views: 1912

Answers (2)

Payam30
Payam30

Reputation: 729

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)

Upvotes: 0

Samwise
Samwise

Reputation: 71454

Your script discards the output of the run_add call. Try changing this:

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

to:

    for items in files:
        abs_path = script_dir + "\\" + items
        output = p4.run_add("-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)

That will show you the results of the p4 add commands that you're running. Based on the fact that a p4 edit opens the files, I expect you'll find a message like this:

C:\Perforce\test>p4 add foo
//stream/main/foo - can't add existing file

The p4 add and p4 edit commands are not synonymous; one is for adding a new file, one is for editing an existing file. If your script is editing existing files, it should be calling run_edit, not run_add.

Upvotes: 2

Related Questions