0

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)

nordmanden
  • 340
  • 1
  • 10
  • 1
    You can use [sparse checkout](https://stackoverflow.com/questions/50945/can-you-do-a-partial-checkout-with-subversion) to checkout individual directories. I haven't tried checking out individual files, but it's worth a try. – Asaf Amnony Sep 21 '20 at 09:37
  • Yeah, but the issue is the files are not specific to a specific folder/directory, the file I need and the file I don't need is in the same folder – nordmanden Sep 21 '20 at 09:39
  • 1
    What about getting the file paths you need to checkout, by using `ls` and then filter the results? – Asaf Amnony Sep 21 '20 at 09:52
  • If you have the time, could you provide an example? I would appreciate it – nordmanden Sep 21 '20 at 09:59
  • Something along [these lines](https://gist.github.com/aamnony/eaf952242998508fc17930fc8f5de8e8#file-svn_spare_checkout-py). However I hit a wall I can't solve - can't actually checkout the files themselves, only their containing directories (not included in the example). When trying to checkout a single file I get a `pysvn.ClientError` "refers to a file, not a directory". If a WC is not explicitly needed, an `export` is an option – Asaf Amnony Sep 21 '20 at 10:31
  • Thank you for the example, I appreciate it. A WC is not needed and the files will ideally be deleted after my program has read the contents. With your example in mind, could I replace checkout with export to archive my goal? – nordmanden Sep 21 '20 at 10:39
  • Yes, I think so, I tried it myself and it seems to work fine (I did see some misbehavior when using the `recurse` flag, so keep that in mind). Do note the parameter name differences between the methods and the building of the destination paths. – Asaf Amnony Sep 21 '20 at 11:07
  • BTW, if you only need to read the content of the files, you can go on a different route by using `client.cat` which reads the file content into a string. – Asaf Amnony Sep 21 '20 at 11:10
  • Excellent, I really appreciate it! If you post your answer as an answer to this question, I'll give you a "solved" checkmark and mark this as closed :-) – nordmanden Sep 21 '20 at 12:25
  • Glad to help out :) – Asaf Amnony Sep 21 '20 at 12:34

1 Answers1

1

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
Asaf Amnony
  • 205
  • 1
  • 9
  • Thank you very much, I'm on my phone right now but I'll get this marked as soon as I'm home and tested it! – nordmanden Sep 21 '20 at 13:58
  • 1
    Update: Overall this works with some modifications, as an example be aware `client.export` is not able to create folders so you must add some `os.mkdir` inbetween each iteration, assuming the folders are nested like mine. Also I found out `client.ls` is quite slow (it takes over 10 minutes in my case). Will look further into this as it works, but sadly much slower than expected. Thank you again – nordmanden Sep 22 '20 at 11:44