Jump to content


P4Python Move Changelist

P4Python. Scripting Changelists

  • Please log in to reply
10 replies to this topic

#1 jaime.mahaffey

jaime.mahaffey

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 23 July 2019 - 11:11 PM

I switch frequently between two workspaces, one on my laptop and one on my desktop.  I'd like to automate the task at the end of my workday of reconciling offline work to specified changelist, shelving all files and moving the changelist to my other workspace.  I've set up an external tool in P4V that will call my python script and pass it the changelist number to reconcile to, the name of the "other" workspace and the root of the workspace.  

What I have so far is below.  example would be python swapworkspaces.py <CLNUM> <OTHERWS_NAME> <WSROOTDIR>

import os
import sys

from P4 import P4
clNum = sys.argv[1]
newWS = sys.argv[2]
working_dir = sys.argv[3]
os.chdir(working_dir)
p4 = P4()

with p4.connect():
result = p4.run_reconcile('-c', clNum, '-e', '-a', '-d')
result2 = p4.run_shelve('-f', '-a leaveunchanged', '-c', clNum)

What I can't figure out is how to do the last step of moving the changelist from WS1 to WS2.  Basically I want the functionality of the workspace field in the Change Ownership dialog.
p4owner.PNG

#2 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 976 posts

Posted 25 July 2019 - 10:18 PM

If you look at the log pane in P4V you can figure out what command it's running to do that, but I think it's "p4 change -U":

https://www.perforce.../p4_change.html

#3 jaime.mahaffey

jaime.mahaffey

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 25 July 2019 - 10:41 PM

It does a p4 change -i  .    That -i suggests that p4v is perhaps piping in some kind of data that doesn't show up in the log.  The -U would allow me to change the owner of the changelist but as far as I can tell won't allow me to change the workspace.

#4 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 976 posts

Posted 25 July 2019 - 10:53 PM

Is it possible that changing the workspace is permitted only if there are no files open?  Does P4V do a "revert" before it does that "change -i"?

#5 jaime.mahaffey

jaime.mahaffey

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 25 July 2019 - 10:56 PM

That's correct, you cannot have files open before changing the workspace and/or owner assigned to the changelist. All files must be shelved and then reverted.

#6 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 976 posts

Posted 25 July 2019 - 11:27 PM

Your script isn't doing the revert part -- maybe if you do the revert then you'll be able to modify the change via p4.fetch_change and p4.save_change?

#7 jaime.mahaffey

jaime.mahaffey

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 26 July 2019 - 06:06 PM

Got it working! thanks for the help.  While the revert is definitely needed in the script, setting a breakpoint after fetch_change showed me that the field 'Client' was what I was after.  This is the modified script.  

Two minor issues, but aren't big deals... The reconcile takes a long time and I get no output to the console window in P4V from my print statements until the entire script is run.  I don't know if there's a way to pipe that output back to P4V.


import os
import sys
from P4 import P4
clNum = sys.argv[1]
newWS = sys.argv[2]
working_dir = sys.argv[3]
os.chdir(working_dir)
p4 = P4()
try:
with p4.connect():
	 try:
		 print("Reconciling offline work from ", working_dir)
		 p4.run_reconcile('-c', clNum, '-e', '-a', '-d')
	 except P4.P4Exception:
		 for e in p4.errors:
			 print(e)
	 print("Shelving Files")
	 p4.run_shelve('-f', '-a leaveunchanged', '-c', clNum)
	 print("Reverting...")
	 p4.run_revert("-w", "-c", clNum, "//...")
	 change = p4.fetch_change(clNum)
	 print("Assigning changelist ", clNum, " to client: ", newWS)
	 change["Client"] = newWS
	 print("Saving changelist...")
	 p4.save_change(change)
except P4.P4Exception:
for e in p4.errors:
	 print(e)
print("Complete.")


#8 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 976 posts

Posted 26 July 2019 - 06:20 PM

Two ways to speed up reconcile:

1) Set the "modtime" option in your client and use the "-m" flag to reconcile to do comparisons by modtime rather than checksum.  Note that this will miss files if you save files and force the old modtime to be retained (IIRC vi's "w!" does this, bizarrely).

2) Use the standard Perforce workflow of opening files as you work on them; then you don't need to use reconcile at all.  ;)

#9 jaime.mahaffey

jaime.mahaffey

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 30 July 2019 - 05:37 PM

View PostSambwise, on 26 July 2019 - 06:20 PM, said:

1) Set the "modtime" option in your client and use the "-m" flag to reconcile to do comparisons by modtime rather than checksum.  Note that this will miss files if you save files and force the old modtime to be retained (IIRC vi's "w!" does this, bizarrely).

I will certainly try this.  I'm on windows and Visual Studio updates modtime as far as I know.  

View PostSambwise, on 26 July 2019 - 06:20 PM, said:

2) Use the standard Perforce workflow of opening files as you work on them; then you don't need to use reconcile at all.  ;)

I wasn't a big fan of this workflow but tradeoffs yada yada yada.  Also the Perforce Visual Studio plugin really bogs down Visual Studio in my experience.   All that being said, I think the overhead of the plugin or just manually checking files out may be the safest option in the long run.

#10 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 976 posts

Posted 30 July 2019 - 05:45 PM

View Postjaime.mahaffey, on 30 July 2019 - 05:37 PM, said:

I wasn't a big fan of this workflow but tradeoffs yada yada yada.  Also the Perforce Visual Studio plugin really bogs down Visual Studio in my experience.   All that being said, I think the overhead of the plugin or just manually checking files out may be the safest option in the long run.

A hybrid approach is to open the entire group of files you might work on (e.g. a folder) in one shot, and then "revert unchanged" before you submit (or use the "revertunchanged" option in the client).  "Revert unchanged" does the same work as reconcile (i.e. checksum diffs) but it's only doing it for the files you already opened.  If your workspace contains a million files but you're doing most of your work within a folder that contains 100 files, that workflow is going to be 10,000 times faster than doing a reconcile over the entire thing.  If you happen to work on a file that's outside that folder, the fact that it's read-only will alert you to the fact that it needs to be opened for edit, even if you're not using the VS plugin.

FWIW, I almost never used the VS plugin when I was using Visual Studio because I had the same experience of it generally slowing down the IDE (which isn't really the fault of the plugin so much as the fact that once you install a source control plugin VS starts spamming calls to it with wild abandon); I'd just have a P4Win window open next to it.

#11 jaime.mahaffey

jaime.mahaffey

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 02 August 2019 - 03:03 PM

View Postjaime.mahaffey, on 26 July 2019 - 06:06 PM, said:

... I get no output to the console window in P4V from my print statements until the entire script is run.  I don't know if there's a way to pipe that output back to P4V.

adding flush=True to the print call will flush the output to the terminal window instead of waiting for the entire script to run.  

print("Some important message", flush=True)






Also tagged with one or more of these keywords: P4Python., Scripting, Changelists

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users