Search code examples
pythonsvnpysvn

How do I limit PySVN checkout to a specific filetype?


Assuming a directory structure on an SVN server that looks similar to this:

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

With a checkout function in a Python script that looks like this:

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)

How do I limit the function to only checkout small-file's? Could be by filetype, by file size (or another smart way)


Solution

  • You can find the file paths you need to get, by using client.ls() (or client.list()) and then filter the results. Note that you cannot checkout individual files, so you need to use client.export() or client.cat().

    The following code should give you a a place to start:

    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