1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-05 11:17:36 +00:00
seahub/thirdpart/seaserv/service.py

181 lines
4.6 KiB
Python
Raw Normal View History

2011-08-16 13:05:42 +00:00
"""
Repo:
id: Repo ID
name: Repo Name
desc: Repo description
worktree: The full path of the worktree of the repo
worktree_changed: True if the worktree is changed
worktree_checktime: The last check time of whether worktree is changed
2011-08-16 13:05:42 +00:00
head_branch: The name of the head branch
enctrypted: True if the repo is encrypted
passwd: The password
2011-08-16 13:05:42 +00:00
Branch:
name:
commit_id:
repo_id:
Commit:
id:
creator_name:
creator: The id of the creator
desc:
ctime:
repo_id:
root_id:
parent_id:
second_parent_id:
2011-08-16 13:05:42 +00:00
"""
2011-03-19 14:06:58 +00:00
from datetime import datetime
2011-08-16 13:05:42 +00:00
import json
2011-03-19 14:06:58 +00:00
import os
import sys
import ccnet
2011-04-11 15:06:05 +00:00
import seafile
from pysearpc import SearpcError
2011-03-19 14:06:58 +00:00
if 'win' in sys.platform:
DEFAULT_CCNET_CONF_PATH = "~/ccnet"
else:
DEFAULT_CCNET_CONF_PATH = "~/.ccnet"
2011-03-19 14:06:58 +00:00
if 'CCNET_CONF_DIR' in os.environ:
CCNET_CONF_PATH = os.environ['CCNET_CONF_DIR']
else:
CCNET_CONF_PATH = DEFAULT_CCNET_CONF_PATH
print "Load config from " + CCNET_CONF_PATH
CCNET_CONF_PATH = os.path.normpath(os.path.expanduser(CCNET_CONF_PATH))
2011-03-19 14:06:58 +00:00
pool = ccnet.ClientPool(CCNET_CONF_PATH)
ccnet_rpc = ccnet.CcnetRpcClient(pool, req_pool=True)
2012-06-25 12:42:19 +00:00
ccnet_threaded_rpc = ccnet.CcnetThreadedRpcClient(pool, req_pool=True)
monitor_rpc = seafile.MonitorRpcClient(pool)
seafserv_rpc = seafile.ServerRpcClient(pool, req_pool=True)
seafserv_threaded_rpc = seafile.ServerThreadedRpcClient(pool, req_pool=True)
2011-08-16 13:05:42 +00:00
#### Basic ccnet API ####
2011-03-19 14:06:58 +00:00
2011-08-16 13:05:42 +00:00
def get_ccnetuser(username=None, userid=None):
# Get emailuser from db
2012-05-24 12:19:14 +00:00
if username:
2012-06-25 12:42:19 +00:00
emailuser = ccnet_threaded_rpc.get_emailuser(username)
2012-05-24 12:19:14 +00:00
if userid:
2012-06-25 12:42:19 +00:00
emailuser = ccnet_threaded_rpc.get_emailuser_by_id(userid)
2012-05-24 12:19:14 +00:00
if not emailuser:
return None
2012-06-20 11:39:21 +00:00
# Check whether is business account
2012-06-25 12:42:19 +00:00
org = ccnet_threaded_rpc.get_org_by_user(emailuser.email)
2012-06-20 11:39:21 +00:00
emailuser.org = org
# And convert to ccnetuser
from seahub.base.accounts import convert_to_ccnetuser
ccnetuser = convert_to_ccnetuser(emailuser)
return ccnetuser
2011-04-11 15:06:05 +00:00
2011-03-19 14:06:58 +00:00
def get_groups():
2011-08-16 13:05:42 +00:00
"""Get group object list. """
2012-06-25 12:42:19 +00:00
group_ids = ccnet_threaded_rpc.list_groups()
2011-03-19 14:06:58 +00:00
if not group_ids:
return []
groups = []
for group_id in group_ids.split("\n"):
# too handle the ending '\n'
if group_id == '':
continue
2012-06-25 12:42:19 +00:00
group = ccnet_threaded_rpc.get_group(group_id)
2011-03-19 14:06:58 +00:00
groups.append(group)
return groups
def get_group(group_id):
2012-06-25 12:42:19 +00:00
group = ccnet_threaded_rpc.get_group(group_id)
2011-08-16 13:05:42 +00:00
if not group:
return None
2011-03-19 14:06:58 +00:00
group.members = group.props.members.split(" ")
group.followers = group.props.followers.split(" ")
2011-08-16 13:05:42 +00:00
group.maintainers = group.props.maintainers.split(" ")
2011-03-19 14:06:58 +00:00
return group
2011-04-11 15:06:05 +00:00
2011-08-16 13:05:42 +00:00
def send_command(command):
client = pool.get_client()
client.send_cmd(command)
ret = client.response[2]
pool.return_client(client)
return ret
######## seafserv API ####
2011-08-16 13:05:42 +00:00
2011-04-11 15:06:05 +00:00
def get_repos():
2011-08-16 13:05:42 +00:00
"""
Return repository list.
"""
2012-02-27 07:24:27 +00:00
return seafserv_threaded_rpc.get_repo_list("", 100)
2011-04-11 15:06:05 +00:00
def get_repo(repo_id):
2012-02-27 07:24:27 +00:00
return seafserv_threaded_rpc.get_repo(repo_id)
2011-04-11 15:06:05 +00:00
def get_commits(repo_id, offset, limit):
"""Get commit lists."""
2012-02-27 07:24:27 +00:00
return seafserv_threaded_rpc.get_commit_list(repo_id, offset, limit)
2011-08-16 13:05:42 +00:00
def get_branches(repo_id):
"""Get branches of a given repo"""
2012-02-27 07:24:27 +00:00
return seafserv_threaded_rpc.branch_gets(repo_id)
def get_binding_peerids(email):
"""Get peer ids of a given email"""
try:
2012-06-25 12:42:19 +00:00
peer_ids = ccnet_threaded_rpc.get_binding_peerids(email)
except SearpcError:
return []
2012-05-15 02:59:16 +00:00
if not peer_ids:
return []
peerid_list = []
for peer_id in peer_ids.split("\n"):
if peer_id == '':
continue
peerid_list.append(peer_id)
return peerid_list
2012-05-15 02:59:16 +00:00
def get_group_repoids(group_id=None):
"""Get repo ids of a given group id or username"""
try:
repo_ids = seafserv_threaded_rpc.get_group_repoids(group_id)
except SearpcError:
return []
if not repo_ids:
return []
repoid_list = []
for repo_id in repo_ids.split("\n"):
if repo_id == '':
continue
repoid_list.append(repo_id)
return repoid_list
def check_group_staff(group_id_int, user_or_username):
"""Check where user is group staff"""
from seahub.base.accounts import CcnetUser
if isinstance(user_or_username, CcnetUser):
user_or_username = user_or_username.username
2012-06-25 12:42:19 +00:00
return ccnet_threaded_rpc.check_group_staff(group_id_int, user_or_username)