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

310 lines
7.8 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
2012-08-03 03:50:57 +00:00
import ConfigParser
2011-03-19 14:06:58 +00:00
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))
2012-08-04 09:11:57 +00:00
MAX_INT = 2147483647
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)
2012-08-03 03:50:57 +00:00
# load ccnet server addr and port from ccnet.conf.
# 'addr:port' is used when downloading a repo
ccnet_config = ConfigParser.ConfigParser()
ccnet_config.read(os.path.join(CCNET_CONF_PATH, 'ccnet.conf'))
if ccnet_config.has_option('General', 'SERVICE_URL') and \
ccnet_config.has_option('Network', 'PORT'):
service_url = ccnet_config.get('General', 'SERVICE_URL').lstrip('http://')
if ':' in service_url:
# strip http port such as ':8000' in 'http://192.168.1.101:8000'
idx = service_url.rindex(':')
service_url = service_url[:idx]
if '/' in service_url:
# strip url suffix like the '/seahub' part of www.gonggeng.org/seahub
2012-08-09 08:49:54 +00:00
idx = service_url.rindex('/')
service_url = service_url[:idx]
2012-08-03 03:50:57 +00:00
CCNET_SERVER_ADDR = service_url
CCNET_SERVER_PORT = ccnet_config.get('Network', 'PORT')
else:
print "Warning: SERVICE_URL not set in ccnet.conf"
CCNET_SERVER_ADDR = None
CCNET_SERVER_PORT = None
2011-08-16 13:05:42 +00:00
#### Basic ccnet API ####
2011-03-19 14:06:58 +00:00
2012-07-27 03:39:55 +00:00
def get_emailusers(start, limit):
try:
users = ccnet_threaded_rpc.get_emailusers(start, limit)
except SearpcError:
users = []
return users
2012-08-01 14:45:58 +00:00
def get_group(group_id):
group_id_int = int(group_id)
try:
group = ccnet_threaded_rpc.get_group(group_id_int)
except SearpcError:
group = None
return group
2012-07-27 03:39:55 +00:00
def check_group_staff(group_id_int, user_or_username):
"""Check where user is group staff"""
from seahub.base.accounts import User
if isinstance(user_or_username, User):
user_or_username = user_or_username.username
return ccnet_threaded_rpc.check_group_staff(group_id_int, user_or_username)
def remove_group_user(user):
"""
Remove group user relationship.
"""
return ccnet_threaded_rpc.remove_group_user(user)
2012-07-27 03:39:55 +00:00
def get_org_groups(org_id, start, limit):
try:
2012-08-04 09:11:57 +00:00
groups = ccnet_threaded_rpc.get_org_groups(org_id, 0, MAX_INT)
2012-07-27 03:39:55 +00:00
except SearpcError:
groups = []
2011-03-19 14:06:58 +00:00
return groups
2012-07-27 03:39:55 +00:00
def get_personal_groups(email):
try:
groups_all = ccnet_threaded_rpc.get_groups(email)
except SearpcError:
return []
2011-03-19 14:06:58 +00:00
2012-07-27 03:39:55 +00:00
personal_groups = []
for group in groups_all:
if not ccnet_threaded_rpc.is_org_group(group.id):
personal_groups.append(group)
return personal_groups
2012-08-04 03:00:04 +00:00
2012-08-04 03:47:00 +00:00
def get_org_id_by_group(group_id):
2012-08-04 03:00:04 +00:00
try:
2012-08-04 03:47:00 +00:00
org_id = ccnet_threaded_rpc.get_org_id_by_group(group_id)
2012-08-04 03:00:04 +00:00
except SearpcError:
2012-08-04 03:47:00 +00:00
org_id = -1
2012-08-04 03:00:04 +00:00
return org_id
2012-07-26 09:08:31 +00:00
def create_org(org_name, url_prefix, username):
ccnet_threaded_rpc.create_org(org_name, url_prefix, username)
2011-04-11 15:06:05 +00:00
2012-07-26 09:08:31 +00:00
def get_orgs_by_user(user):
try:
orgs = ccnet_threaded_rpc.get_orgs_by_user(user)
except SearpcError:
orgs = []
return orgs
def get_org_by_url_prefix(url_prefix):
try:
org = ccnet_threaded_rpc.get_org_by_url_prefix(url_prefix)
except SearpcError:
org = None
return org
2012-07-27 09:54:07 +00:00
def get_org_by_id(org_id):
try:
org = ccnet_threaded_rpc.get_org_by_id(org_id)
except SearpcError:
org = None
return org
2012-07-26 09:08:31 +00:00
def get_user_current_org(user, url_prefix):
orgs = get_orgs_by_user(user)
for org in orgs:
if org.url_prefix == url_prefix:
return org
return None
2012-07-27 03:39:55 +00:00
def add_org_user(org_id, email, is_staff):
try:
ccnet_threaded_rpc.add_org_user(org_id, email, is_staff)
except SearpcError:
pass
def remove_org_user(org_id, email):
try:
ccnet_threaded_rpc.remove_org_user(org_id, email)
except SearpcError:
pass
2012-07-26 09:08:31 +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 create_org_repo(repo_name, repo_desc, user, passwd, org_id):
"""
Create org repo, return valid repo id if success.
"""
try:
repo_id = seafserv_threaded_rpc.create_org_repo(repo_name, repo_desc,
user, passwd, org_id)
except SearpcError:
repo_id = None
return repo_id
2012-07-26 09:08:31 +00:00
def get_org_repos(org_id, start, limit):
"""
2012-07-30 02:25:46 +00:00
List repos created in org.
2012-07-26 09:08:31 +00:00
"""
try:
repos = seafserv_threaded_rpc.get_org_repo_list(org_id, start, limit)
except SearpcError:
repos = []
return repos
2012-07-30 02:25:46 +00:00
def get_org_id_by_repo_id(repo_id):
"""
Get org id according repo id.
"""
try:
org_id = seafserv_threaded_rpc.get_org_id_by_repo_id(repo_id)
except SearpcError:
org_id = ''
return org_id
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
2012-07-30 02:25:46 +00:00
def is_repo_owner(user, repo_id):
"""
Check whether user is repo owner.
"""
try:
ret = seafserv_threaded_rpc.is_repo_owner(user, repo_id)
except SearpcError:
ret = 0
return ret
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 is_valid_filename(file_or_dir):
"""
Check whether file name or directory name is valid.
"""
try:
ret = seafserv_threaded_rpc.is_valid_filename('', file_or_dir)
except SearpcError:
ret = 0
2012-07-26 09:08:31 +00:00
return ret