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
|
2012-01-29 05:05:35 +00:00
|
|
|
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
|
2012-01-29 05:05:35 +00:00
|
|
|
enctrypted: True if the repo is encrypted
|
|
|
|
passwd: The password
|
|
|
|
|
2011-08-16 13:05:42 +00:00
|
|
|
|
|
|
|
Branch:
|
|
|
|
name:
|
|
|
|
commit_id:
|
|
|
|
repo_id:
|
|
|
|
|
2012-01-29 05:05:35 +00:00
|
|
|
Commit:
|
2011-11-01 14:21:01 +00:00
|
|
|
id:
|
2012-01-29 05:05:35 +00:00
|
|
|
creator_name:
|
|
|
|
creator: The id of the creator
|
|
|
|
desc:
|
|
|
|
ctime:
|
2011-11-01 14:21:01 +00:00
|
|
|
repo_id:
|
2012-01-29 05:05:35 +00:00
|
|
|
root_id:
|
|
|
|
parent_id:
|
|
|
|
second_parent_id:
|
2011-11-01 14:21:01 +00:00
|
|
|
|
|
|
|
|
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
|
2012-01-29 05:05:35 +00:00
|
|
|
from pysearpc import SearpcError
|
2011-03-19 14:06:58 +00:00
|
|
|
|
2012-01-29 05:05:35 +00:00
|
|
|
if 'win' in sys.platform:
|
|
|
|
DEFAULT_CCNET_CONF_PATH = "~/ccnet"
|
|
|
|
else:
|
2011-11-01 14:21:01 +00:00
|
|
|
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
|
|
|
|
|
2012-01-29 05:05:35 +00:00
|
|
|
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
|
2012-01-29 05:05:35 +00:00
|
|
|
|
2011-03-19 14:06:58 +00:00
|
|
|
pool = ccnet.ClientPool(CCNET_CONF_PATH)
|
2012-04-15 03:10:08 +00:00
|
|
|
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)
|
2011-11-01 14:21:01 +00:00
|
|
|
monitor_rpc = seafile.MonitorRpcClient(pool)
|
2012-04-15 03:10:08 +00:00
|
|
|
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]
|
2012-08-09 08:07:37 +00:00
|
|
|
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('/')
|
2012-08-09 08:07:37 +00:00
|
|
|
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
|
|
|
|
2012-07-28 08:41:24 +00:00
|
|
|
def check_group_staff(group_id_int, user_or_username):
|
|
|
|
"""Check where user is group staff"""
|
2012-08-07 08:48:26 +00:00
|
|
|
from seahub.base.accounts import User
|
|
|
|
if isinstance(user_or_username, User):
|
2012-07-28 08:41:24 +00:00
|
|
|
user_or_username = user_or_username.username
|
|
|
|
|
|
|
|
return ccnet_threaded_rpc.check_group_staff(group_id_int, user_or_username)
|
|
|
|
|
2012-07-30 05:54:11 +00:00
|
|
|
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-08 12:16:00 +00:00
|
|
|
def get_group_members(group_id):
|
|
|
|
group_id_int = int(group_id)
|
|
|
|
try:
|
|
|
|
members = ccnet_threaded_rpc.get_group_members(group_id_int)
|
|
|
|
except SearpcError:
|
|
|
|
members = []
|
|
|
|
return members
|
|
|
|
|
2012-08-13 13:26:58 +00:00
|
|
|
def check_group_repo(group_id, repo_id, user):
|
|
|
|
group_id_int = int(group_id)
|
|
|
|
|
|
|
|
try:
|
|
|
|
ret = seafserv_threaded_rpc.check_group_repo(group_id_int, repo_id,
|
|
|
|
user)
|
|
|
|
except SearpcError:
|
|
|
|
ret = 0
|
|
|
|
return ret
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2012-01-29 05:05:35 +00:00
|
|
|
######## 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
|
|
|
|
2012-07-28 08:41:24 +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
|
|
|
|
|
2012-01-29 05:05:35 +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)
|
|
|
|
|
2012-05-02 12:58:06 +00:00
|
|
|
def get_binding_peerids(email):
|
|
|
|
"""Get peer ids of a given email"""
|
2012-04-02 15:56:41 +00:00
|
|
|
try:
|
2012-06-25 12:42:19 +00:00
|
|
|
peer_ids = ccnet_threaded_rpc.get_binding_peerids(email)
|
2012-04-02 15:56:41 +00:00
|
|
|
except SearpcError:
|
|
|
|
return []
|
|
|
|
|
2012-05-15 02:59:16 +00:00
|
|
|
if not peer_ids:
|
|
|
|
return []
|
|
|
|
|
2012-05-02 12:58:06 +00:00
|
|
|
peerid_list = []
|
|
|
|
for peer_id in peer_ids.split("\n"):
|
|
|
|
if peer_id == '':
|
2012-04-02 15:56:41 +00:00
|
|
|
continue
|
2012-05-02 12:58:06 +00:00
|
|
|
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
|
|
|
|
|
2012-07-28 08:41:24 +00:00
|
|
|
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
|
|
|
|
2012-07-28 08:41:24 +00:00
|
|
|
return ret
|