Thursday, September 24, 2009

Convert p4 cygwin commands to p4 win commands

Here’s a simple little script which allows me to use p4.exe from cygwin.  Perforce actually releases a p4.exe for cygwin, but I wanted my p4win and p4.exe to be able to use the same client spec.  This isn’t possible when one requires a root starting with “C:\” and the other requires “/cygdrive/c/”.  

 

This script works by expanding and window-izing all arguments after the p4 command.

 

Here is a series which shows the transformation.

  • p4 edit README
  • p4 edit /home/gwarner/foo/README (where foo is a symbolic link)
  • p4 edit /cygdrive/c/perforceRoot/blah/blah/blah/README
  • p4 edit C:\perforceRoot\blah\blah\blah\README

 

Ta da!

 

1.     #!/usr/bin/python

2.     import sys

3.     from subprocess import *

4.      

5.     def win32Path(path):                                                                                       path = path.replace('/cygdrive/c/','C:\\')

6.         path = path.replace('/','\\')

7.         return '"%s"' % path.strip()                                                                       

8.     if __name__ == '__main__':

9.         p4command = sys.argv[1]

10.      fullFiles = []

11.                 

12.      for file in sys.argv[2:]:

13.          fullPath = Popen(['readlink.exe -f ' + file], stdout=PIPE, shell=True).communicate()[0]

14.          fullFiles.append(win32Path(fullPath))

15.   

16.      newCommand = 'p4 %s %s' % (p4command, ' '.join(fullFiles))

17.   

18.      print newCommand

19.      check_call(newCommand, shell=True)

20.      print 'done.'

 

 

No comments:

Pyjamas