如何将PySVN签出限制为特定的文件类型?

2024-09-28 23:28:50 发布

您现在位置:Python中文网/ 问答频道 /正文

假设SVN服务器上的目录结构与此类似:

/ mainfolder 
../ subfolder1
   -big-file1.xlm
   -small-file1.txt
../ subfolder2
   -big-file2.xlm
   -small-file2.txt

Python脚本中的签出函数如下所示:

client = pysvn.Client()
client.callback_get_login = svnlogin

try:
    client.checkout(svnurl()+"/mainfolder",
    './examples/pysvntest')
    print("done")   

except pysvn.ClientError as e:
    print("SVN Error occured: ", e)

如何将函数限制为仅签出small-file? 可以是文件类型、文件大小(或其他智能方式)


Tags: 函数服务器目录txtclientsvn结构file1
1条回答
网友
1楼 · 发布于 2024-09-28 23:28:50

您可以使用client.ls()(或client.list())找到需要获取的文件路径,然后过滤结果。 注意您无法签出单个文件,因此需要使用client.export()client.cat()

以下代码应为您提供一个起点:

import pysvn

url = '...'
checkout_path = '...'
file_ext = '.txt'

client = pysvn.Client()
client.checkout(path=checkout_path, url=url, depth=pysvn.depth.empty)

files_and_dirs = client.ls(url_or_path=url)

for file_or_dir in files_and_dirs:
    if file_or_dir.kind == pysvn.node_kind.file and file_or_dir.name.endswith(file_ext):
        client.export(dest_path=checkout_path, src_url_or_path=file_or_dir.name)  # TODO: Export to the correct location. Can also use client.cat() here, to get the file content into a string

相关问题 更多 >