当前位置:  开发笔记 > 编程语言 > 正文

如何在python中查看perforce中的文件?

如何解决《如何在python中查看perforce中的文件?》经验,为你挑选了2个好方法。

我想在python中编写一些脚本,对源代码进行一些自动更改.如果脚本确定需要更改文件,我想首先将其从perforce中检出.我不关心办理登机手续,因为我总是想先建立和测试.



1> Troy J. Farr..:

Perforce有围绕其C/C++工具的Python包装器,可用于Windows的二进制形式,以及其他平台的源:

http://www.perforce.com/perforce/loadsupp.html#api

您会发现他们的脚本API文档很有帮助:

http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf

使用Python API与命令行客户端非常相似:

PythonWin 2.5.1 (r251:54863, May  1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> import P4
>>> p4 = P4.P4()
>>> p4.connect() # connect to the default server, with the default clientspec
>>> desc = {"Description": "My new changelist description",
...         "Change": "new"
...         }
>>> p4.input = desc
>>> p4.run("changelist", "-i")
['Change 2579505 created.']
>>> 

我将从命令行验证它:

P:\>p4 changelist -o 2579505
# A Perforce Change Specification.
#
#  Change:      The change number. 'new' on a new changelist.
#  Date:        The date this specification was last modified.
#  Client:      The client on which the changelist was created.  Read-only.
#  User:        The user who created the changelist.
#  Status:      Either 'pending' or 'submitted'. Read-only.
#  Description: Comments about the changelist.  Required.
#  Jobs:        What opened jobs are to be closed by this changelist.
#               You may delete jobs from this list.  (New changelists only.)
#  Files:       What opened files from the default changelist are to be added
#               to this changelist.  You may delete files from this list.
#               (New changelists only.)

Change: 2579505

Date:   2008/10/08 13:57:02

Client: MYCOMPUTER-DT

User:   myusername

Status: pending

Description:
        My new changelist description



2> Matt Price..:

这是我想出的:

import os

def CreateNewChangeList(description):
    "Create a new changelist and returns the changelist number as a string"
    p4in, p4out = os.popen2("p4 changelist -i")
    p4in.write("change: new\n")
    p4in.write("description: " + description)
    p4in.close()
    changelist = p4out.readline().split()[1]
    return changelist

def OpenFileForEdit(file, changelist = ""):
    "Open a file for edit, if a changelist is passed in then open it in that list"
    cmd = "p4 edit "
    if changelist:
        cmd += " -c " + changelist + " "
    ret = os.popen(cmd + file).readline().strip()
    if not ret.endswith("opened for edit"):
        print "Couldn't open", file, "for edit:"
        print ret
        raise ValueError


如果我没记错的话,使用p4模块可以将你绑定到特定版本的perforce C++ API.使用命令行是向后兼容的,很简单,如果将命令更改为p4 -G edit,它将把对象作为编组的python字典对象返回.
推荐阅读
可爱的天使keven_464
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有