如何在Perforce触发器中运行的Python脚本中检查文件内容?
目标:
我想对用户想要提交的文件进行检查,以验证这些文件的有效性,并阻止不合格的文件提交。
文档:
我参考了这个链接 https://www.perforce.com/manuals/p4sag/Content/P4SAG/scripting.triggers.submits.content.html,里面提到:
下面的变更内容触发器是一个Bourne shell脚本,它确保每个变更列表中的每个文件都包含当前年份的版权声明。
接下来是:
# p4 print -q //depot/src/file.c@=12345
我的代码:
我的perforce触发器表条目大概是这样的:
check_hydra_json_trigger change-submit //foo/... "C:\Python38\python.exe %//foo/script.py% %changelist%"
我的 script.py
文件内容如下:
changelist_number = sys.argv[1]
change = p4.run_describe(changelist_number)
file_list = change[0]['depotFile']
for file_path in file_list:
if file_path.endswith('.json'):
output = p4.run("print", "-o", temp_file, f"{file_path}@{changelist_number}")
data = None
try:
with open(temp_file, "r") as temp_file_contents:
data = json.load(temp_file_contents)
except Exception as e:
print(f"Error: Failed to load json {file_path}. Is file properly formatted?")
print(f"Exception: {e}")
我的问题是 temp_file
总是包含仓库文件的内容,而不是我们想要提交的文件。
尝试用 change-content
作为我的触发器也没有成功……
1 个回答
2
把这一行改成:
output = p4.run("print", "-o", temp_file, f"{file_path}@{changelist_number}")
改成:
output = p4.run("print", "-o", temp_file, f"{file_path}@={changelist_number}")
并确保你的触发器是以 change-content
的方式运行,而不是 change-submit
。
这里有个重要的细节,就是 @=
这个修订版本说明符,它可以用来指代服务器上那些还没有提交的文件(比如在 change-content
触发器中正在提交的文件,或者是暂存的更改——注意,当 change-submit
被触发时,这些文件还没有上传到服务器上)。
通常情况下,修订版本说明符只能指代已经 提交 的版本,但 @=
是个特例,它可以指代那些正在等待提交的更改,这些更改可能包含正在提交的文件或暂存的文件。