• Python API
  • Seafile Python API" level="1">Seafile Python API
    • Install Seafile Server" level="2">Install Seafile Server
    • Example: Copy Library" level="2">Example: Copy Library
      • Set Environment Variable" level="3">Set Environment Variable
      • Copy Library" level="3">Copy Library
    • List Of Seafile-API" level="2">List Of Seafile-API

    Python API



    Seafile Python API" class="reference-link">Seafile Python API

    This tutorial show you how to use seafile-api, and will accomplish a “library copy” work under Ubuntu as example.

    Install Seafile Server" class="reference-link">Install Seafile Server

    First of all, make sure you have Download and Setup Seafile Server successfully. And your directory layout will be like this:

    1. # tree . -L 3
    2. .
    3. ├── ccnet
    4. ├── ccnet.conf
    5. ├── ......
    6. │......
    7. ├── seafile-server-3.0.3
    8. ├── seafile
    9. ├── seafile.sh
    10. ├── seahub
    11. ├── seahub.sh
    12. ├── setup-seafile.sh
    13. ├── upgrade
    14. ├── README
    15. ├── seaf_migrate_3.py
    16. ├── seaf_migrate_3.sh
    17. ├── ......
    18. ├── ......
    19. │......

    Example: Copy Library" class="reference-link">Example: Copy Library

    In this example, two script files will be used: seaf_migrate_3.sh and seaf_migrate_3.py. We put them in the upgrade directory as you see above.

    Set Environment Variable" class="reference-link">Set Environment Variable

    If you want use Seafile-API, set environment variable first. That’s what seaf_migrate_3.sh does:

    1. get ccnet/seafile config file path and export them;
    2. export Python path;
    3. call seaf_migrate_3.py.

    Example code

    1. #!/bin/bash
    2. #get path of ccnet.conf
    3. SCRIPT=$(readlink -f "$0") # haiwen/seafile-server-3.0.3/upgrade/seaf_migrate_3.sh
    4. UPGRADE_DIR=$(dirname "$SCRIPT") # haiwen/seafile-server-3.0.3/upgrade/
    5. INSTALLPATH=$(dirname "$UPGRADE_DIR") # haiwen/seafile-server-3.0.3/
    6. TOPDIR=$(dirname "${INSTALLPATH}") # haiwen/
    7. default_ccnet_conf_dir=${TOPDIR}/ccnet
    8. #get path of seafile.conf
    9. function read_seafile_data_dir () {
    10. seafile_ini=${default_ccnet_conf_dir}/seafile.ini
    11. if [[ ! -f ${seafile_ini} ]]; then
    12. echo "${seafile_ini} not found. Now quit"
    13. exit 1
    14. fi
    15. seafile_data_dir=$(cat "${seafile_ini}")
    16. if [[ ! -d ${seafile_data_dir} ]]; then
    17. echo "Your seafile server data directory \"${seafile_data_dir}\" is invalid or doesn't exits."
    18. echo "Please check it first, or create this directory yourself."
    19. echo ""
    20. exit 1;
    21. fi
    22. export SEAFILE_CONF_DIR=$seafile_data_dir
    23. }
    24. export CCNET_CONF_DIR=${default_ccnet_conf_dir}
    25. read_seafile_data_dir;
    26. export PYTHONPATH=${INSTALLPATH}/seafile/lib/python2.6/site-packages:${INSTALLPATH}/seafile/lib64/python2.6/site-packages:${INSTALLPATH}/seafile/lib/python2.7/site-packages:${INSTALLPATH}/seahub/thirdpart:$PYTHONPATH
    27. export PYTHONPATH=${INSTALLPATH}/seafile/lib/python2.7/site-packages:${INSTALLPATH}/seafile/lib64/python2.7/site-packages:$PYTHONPATH
    28. function usage () {
    29. echo "Usage: `basename $0` <repo-id>"
    30. echo "exit."
    31. exit 1
    32. }
    33. if [ $# != 1 ]; then
    34. usage
    35. fi
    36. python seaf_migrate_3.py $1

    NOTE:
    You can get repo_id at address bar of Seahub or through Seafile web API

    Copy Library" class="reference-link">Copy Library

    Then seaf_migrate_3.py will call Seafile-API to copy library:

    1. Get library ID from input.
    2. Get origin_repo object.
    3. Create a new library, set name, desc and owner.
    4. Copy stuffs from old library to new library.

    Example code

    1. #!/usr/bin/env python
    2. import os
    3. import stat
    4. import sys
    5. from seaserv import seafile_api
    6. def count_files_recursive(repo_id, path='/'):
    7. num_files = 0
    8. for e in seafile_api.list_dir_by_path(repo_id, path):
    9. if stat.S_ISDIR(e.mode):
    10. num_files += count_files_recursive(repo_id,
    11. os.path.join(path, e.obj_name))
    12. else:
    13. num_files += 1
    14. return num_files
    15. #Get library ID from input
    16. origin_repo_id = sys.argv[1]
    17. #Get origin_repo object
    18. origin_repo = seafile_api.get_repo(origin_repo_id)
    19. username = seafile_api.get_repo_owner(origin_repo_id)
    20. #Create a new library, set name, desc and owner
    21. new_repo_id = seafile_api.create_repo(name=origin_repo.name,
    22. desc=origin_repo.desc,
    23. username=username, passwd=None)
    24. #Copy stuffs from old library to new library
    25. dirents = seafile_api.list_dir_by_path(origin_repo_id, '/')
    26. for e in dirents:
    27. print "copying: " + e.obj_name
    28. obj_name = e.obj_name
    29. seafile_api.copy_file(origin_repo_id, '/', obj_name, new_repo_id, '/',
    30. obj_name, username, 0, 1)
    31. print "*" * 60
    32. print "OK, verifying..."
    33. print "Origin library(%s): %d files. New Library(%s): %d files." % (
    34. origin_repo_id[:8], count_files_recursive(origin_repo_id),
    35. new_repo_id[:8], count_files_recursive(new_repo_id))
    36. print "*" * 60

    If you execute script file successfully, you will see these output, and of course a new library at myhome page of Seahub.

    1. foo@foo:~/haiwen/seafile-server-3.0.3/upgrade$ ./seaf_migrate_test.sh c8bbb088-cbaf-411d-8bd8-9870763f0e5f
    2. Loading ccnet config from /home/foo/haiwen/ccnet
    3. Loading seafile config from /home/foo/haiwen/seafile-data
    4. copying: test.html
    5. copying: test-dir-2
    6. copying: test-dir
    7. copying: solar.html
    8. copying: examples.desktop
    9. ************************************************************
    10. OK, verifying...
    11. Origin library(c8bbb088): 10 files. New Library(4d6f4837): 10 files.
    12. ************************************************************

    List Of Seafile-API" class="reference-link">List Of Seafile-API

    This list is based on seafile-server-3.0.3, and parameter was omitted.

    For more infomation about Seafile-API, please see api.py.

    • seafile_api.add_inner_pub_repo()
    • seafile_api.cancel_copy_task()
    • seafile_api.change_repo_passwd()
    • seafile_api.check_passwd()
    • seafile_api.check_permission()
    • seafile_api.check_quota()
    • seafile_api.check_repo_access_permission()
    • seafile_api.copy_file()
    • seafile_api.count_inner_pub_repos()
    • seafile_api.create_enc_repo()
    • seafile_api.create_repo()
    • seafile_api.create_virtual_repo()
    • seafile_api.del_file()
    • seafile_api.delete_repo_token()
    • seafile_api.delete_repo_tokens_by_peer_id()
    • seafile_api.diff_commits()
    • seafile_api.edit_repo()
    • seafile_api.generate_repo_token()
    • seafile_api.get_commit_list()
    • seafile_api.get_copy_task()
    • seafile_api.get_decrypt_key()
    • seafile_api.get_deleted()
    • seafile_api.get_dir_id_by_commit_and_path()
    • seafile_api.get_dir_id_by_path()
    • seafile_api.get_file_id_by_commit_and_path()
    • seafile_api.get_file_id_by_path()
    • seafile_api.get_file_revisions()
    • seafile_api.get_file_size()
    • seafile_api.get_files_last_modified()
    • seafile_api.get_group_repo_list()
    • seafile_api.get_group_repoids()
    • seafile_api.get_group_repos_by_owner()
    • seafile_api.get_fileserver_access_token()
    • seafile_api.get_inner_pub_repo_list()
    • seafile_api.get_orphan_repo_list()
    • seafile_api.get_owned_repo_list()
    • seafile_api.get_repo()
    • seafile_api.get_repo_list()
    • seafile_api.get_repo_owner()
    • seafile_api.get_repo_size()
    • seafile_api.get_share_in_repo_list()
    • seafile_api.get_share_out_repo_list()
    • seafile_api.get_shared_groups_by_repo()
    • seafile_api.get_user_quota()
    • seafile_api.get_user_self_usage()
    • seafile_api.get_user_share_usage()
    • seafile_api.get_virtual_repo()
    • seafile_api.get_virtual_repos_by_owner()
    • seafile_api.group_share_repo()
    • seafile_api.group_unshare_repo()
    • seafile_api.is_inner_pub_repo()
    • seafile_api.is_password_set()
    • seafile_api.is_repo_owner()
    • seafile_api.is_valid_filename()
    • seafile_api.list_dir_by_commit_and_path()
    • seafile_api.list_dir_by_dir_id()
    • seafile_api.list_dir_by_path()
    • seafile_api.list_file_by_file_id()
    • seafile_api.list_repo_tokens()
    • seafile_api.list_repo_tokens_by_email()
    • seafile_api.move_file()
    • seafile_api.post_dir()
    • seafile_api.post_empty_file()
    • seafile_api.post_file()
    • seafile_api.put_file()
    • seafile_api.query_fileserver_access_token()
    • seafile_api.remove_inner_pub_repo()
    • seafile_api.remove_repo()
    • seafile_api.remove_share()
    • seafile_api.rename_file()
    • seafile_api.revert_dir()
    • seafile_api.revert_file()
    • seafile_api.revert_repo()
    • seafile_api.set_group_repo_permission()
    • seafile_api.set_passwd()
    • seafile_api.set_repo_owner()
    • seafile_api.set_share_permission()
    • seafile_api.set_user_quota()
    • seafile_api.share_repo()
    • seafile_api.unset_passwd()