0

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

Payam30
  • 689
  • 1
  • 5
  • 20

2 Answers2

2

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.

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • I edited my code to what you suggested. The ouput error say that there is already a file with that name and can not be added. The file was not checked out. exactly as you said. I thought that run_adds adds files to CL for editing. What I'm trying to achieve is to check out some files and ha e them ready to be edited. – Payam30 Feb 20 '20 at 18:17
  • That's the `p4 edit` command, as you've already figured out. Just use the right command and it'll work -- what's the problem? :) – Samwise Feb 20 '20 at 20:46
  • Do you mean " p4.edit("c", changelist, file) . I get an error. and when I try p4.run("edit","changelist, file) I still dont get the file in the changelist – Payam30 Feb 21 '20 at 12:31
  • I changed it to p4.run_edit("-c", changelist, items) and it worked. – Payam30 Feb 21 '20 at 12:47
  • Yes, it's the exact same flags as the `run_add` command you were originally using; just change `add` (add files) to `edit` (edit files). – Samwise Feb 21 '20 at 15:32
0

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)

Payam30
  • 689
  • 1
  • 5
  • 20