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-09-03 08:14:15 +00:00
|
|
|
# group
|
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-09-22 09:46:56 +00:00
|
|
|
def get_personal_groups(start, limit):
|
|
|
|
try:
|
|
|
|
groups_all = ccnet_threaded_rpc.get_all_groups(start, limit)
|
|
|
|
except SearpcError:
|
|
|
|
return []
|
|
|
|
return [ x for x in groups_all if not is_org_group(x.id) ]
|
|
|
|
|
|
|
|
def get_personal_groups_by_user(email):
|
|
|
|
try:
|
|
|
|
groups_all = ccnet_threaded_rpc.get_groups(email)
|
|
|
|
except SearpcError:
|
|
|
|
return []
|
|
|
|
|
2012-09-22 11:28:49 +00:00
|
|
|
return [ x for x in groups_all if not is_org_group(x.id) ]
|
2012-09-22 09:46:56 +00:00
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
# group user
|
2012-08-14 09:53:58 +00:00
|
|
|
def is_group_user(group_id, user):
|
|
|
|
try:
|
|
|
|
ret = ccnet_threaded_rpc.is_group_user(group_id, user)
|
|
|
|
except SearpcError:
|
|
|
|
ret = 0
|
|
|
|
return ret
|
|
|
|
|
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-09-03 08:14:15 +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
|
|
|
|
|
|
|
|
# org group
|
|
|
|
def is_org_group(group_id):
|
|
|
|
try:
|
|
|
|
ret = ccnet_threaded_rpc.is_org_group(group_id)
|
|
|
|
except SearpcError:
|
|
|
|
ret = -1
|
|
|
|
return True if ret == 1 else False
|
|
|
|
|
|
|
|
def get_org_id_by_group(group_id):
|
|
|
|
try:
|
|
|
|
org_id = ccnet_threaded_rpc.get_org_id_by_group(group_id)
|
|
|
|
except SearpcError:
|
|
|
|
org_id = -1
|
|
|
|
return org_id
|
|
|
|
|
2012-07-27 03:39:55 +00:00
|
|
|
def get_org_groups(org_id, start, limit):
|
|
|
|
try:
|
2012-09-05 13:51:17 +00:00
|
|
|
groups = ccnet_threaded_rpc.get_org_groups(org_id, start, limit)
|
2012-07-27 03:39:55 +00:00
|
|
|
except SearpcError:
|
|
|
|
groups = []
|
2011-03-19 14:06:58 +00:00
|
|
|
return groups
|
|
|
|
|
2012-08-31 11:50:24 +00:00
|
|
|
def get_org_groups_by_user(org_id, user):
|
|
|
|
"""
|
2012-09-01 09:46:46 +00:00
|
|
|
Get user's groups in org.
|
2012-08-31 11:50:24 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
groups_all = ccnet_threaded_rpc.get_groups(user)
|
|
|
|
except SearpcError:
|
|
|
|
return []
|
|
|
|
|
2012-09-22 11:28:49 +00:00
|
|
|
return [ x for x in groups_all if org_id == get_org_id_by_group(x.id) ]
|
2012-08-31 11:50:24 +00:00
|
|
|
|
2012-09-01 09:46:46 +00:00
|
|
|
# org
|
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_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-09-01 09:46:46 +00:00
|
|
|
# org user
|
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-08-31 09:28:50 +00:00
|
|
|
|
2012-09-01 09:46:46 +00:00
|
|
|
def org_user_exists(org_id, user):
|
|
|
|
try:
|
|
|
|
ret = ccnet_threaded_rpc.org_user_exists(org_id, user)
|
|
|
|
except SearpcError:
|
|
|
|
ret = -1
|
|
|
|
return True if ret == 1 else False
|
|
|
|
|
|
|
|
def get_org_users_by_url_prefix(url_prefix, start, limit):
|
|
|
|
"""
|
|
|
|
List org users.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
users = ccnet_threaded_rpc.get_org_emailusers(url_prefix, start, limit)
|
|
|
|
except:
|
|
|
|
users = []
|
|
|
|
return users
|
|
|
|
|
|
|
|
def get_orgs_by_user(user):
|
|
|
|
try:
|
|
|
|
orgs = ccnet_threaded_rpc.get_orgs_by_user(user)
|
|
|
|
except SearpcError:
|
|
|
|
orgs = []
|
|
|
|
|
|
|
|
return orgs
|
|
|
|
|
2012-08-31 09:28:50 +00:00
|
|
|
def is_org_staff(org_id, user):
|
|
|
|
"""
|
|
|
|
Check whether user is staff of a org.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
ret = ccnet_threaded_rpc.is_org_staff(org_id, user)
|
|
|
|
except SearpcError:
|
|
|
|
ret = -1
|
|
|
|
return True if ret == 1 else False
|
2012-09-01 09:46:46 +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
|
|
|
|
|
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-09-03 08:14:15 +00:00
|
|
|
def get_binding_peerids(email):
|
|
|
|
"""Get peer ids of a given email"""
|
|
|
|
try:
|
|
|
|
peer_ids = ccnet_threaded_rpc.get_binding_peerids(email)
|
|
|
|
except SearpcError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
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
|
2011-08-16 13:05:42 +00:00
|
|
|
|
2012-01-29 05:05:35 +00:00
|
|
|
######## seafserv API ####
|
2011-08-16 13:05:42 +00:00
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
# repo
|
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-09-03 08:14:15 +00:00
|
|
|
def get_repo(repo_id):
|
|
|
|
return seafserv_threaded_rpc.get_repo(repo_id)
|
|
|
|
|
2012-09-29 08:51:36 +00:00
|
|
|
def list_personal_repos_by_owner(owner):
|
|
|
|
"""
|
|
|
|
List users owned repos in personal context.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
repos = seafserv_threaded_rpc.list_owned_repos(owner)
|
|
|
|
except SearpcError:
|
|
|
|
repos = []
|
|
|
|
return repos
|
|
|
|
|
2012-09-03 08:14:15 +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-09-01 09:46:46 +00:00
|
|
|
# org repo
|
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-09-03 08:14:15 +00:00
|
|
|
|
2012-09-04 13:05:20 +00:00
|
|
|
def is_org_repo(repo_id):
|
|
|
|
org_id = get_org_id_by_repo_id(repo_id)
|
|
|
|
return True if org_id > 0 else False
|
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
def list_org_repos_by_owner(org_id, user):
|
2012-07-26 09:08:31 +00:00
|
|
|
try:
|
2012-09-03 08:14:15 +00:00
|
|
|
repos = seafserv_threaded_rpc.list_org_repos_by_owner(org_id, user)
|
2012-07-26 09:08:31 +00:00
|
|
|
except SearpcError:
|
|
|
|
repos = []
|
|
|
|
return repos
|
2012-07-30 02:25:46 +00:00
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
def get_org_repos(org_id, start, limit):
|
2012-08-29 05:40:47 +00:00
|
|
|
"""
|
2012-09-03 08:14:15 +00:00
|
|
|
List repos created in org.
|
2012-08-29 05:40:47 +00:00
|
|
|
"""
|
|
|
|
try:
|
2012-09-03 08:14:15 +00:00
|
|
|
repos = seafserv_threaded_rpc.get_org_repo_list(org_id, start, limit)
|
2012-08-29 05:40:47 +00:00
|
|
|
except SearpcError:
|
|
|
|
repos = []
|
2012-09-03 08:14:15 +00:00
|
|
|
|
2012-09-04 13:05:20 +00:00
|
|
|
if repos:
|
|
|
|
for r in repos:
|
2012-09-05 08:25:32 +00:00
|
|
|
r.owner = get_org_repo_owner(r.id)
|
2012-09-04 13:05:20 +00:00
|
|
|
|
2012-08-29 05:40:47 +00:00
|
|
|
return repos
|
2012-09-03 08:14:15 +00:00
|
|
|
|
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:
|
2012-09-03 04:03:06 +00:00
|
|
|
org_id = -1
|
2012-07-30 02:25:46 +00:00
|
|
|
return org_id
|
|
|
|
|
2012-09-04 13:05:20 +00:00
|
|
|
def is_org_repo_owner(org_id, repo_id, user):
|
|
|
|
"""
|
|
|
|
Check whether user is org repo owner.
|
|
|
|
NOTE:
|
|
|
|
`org_id` may used in future.
|
|
|
|
"""
|
|
|
|
owner = get_org_repo_owner(repo_id)
|
|
|
|
if not owner:
|
|
|
|
return False
|
|
|
|
return True if owner == user else False
|
|
|
|
|
|
|
|
def get_org_repo_owner(repo_id):
|
|
|
|
"""
|
|
|
|
Get owner of repo repo.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
owner = seafserv_threaded_rpc.get_org_repo_owner(repo_id)
|
|
|
|
except SearpcError:
|
|
|
|
owner = None
|
|
|
|
return owner
|
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
# commit
|
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
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
# branch
|
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-09-03 08:14:15 +00:00
|
|
|
# group repo
|
|
|
|
def get_shared_groups_by_repo(repo_id):
|
2012-04-02 15:56:41 +00:00
|
|
|
try:
|
2012-09-03 08:14:15 +00:00
|
|
|
group_ids = seafserv_threaded_rpc.get_shared_groups_by_repo(repo_id)
|
2012-04-02 15:56:41 +00:00
|
|
|
except SearpcError:
|
2012-09-03 12:54:07 +00:00
|
|
|
group_ids = ''
|
|
|
|
if not group_ids:
|
2012-04-02 15:56:41 +00:00
|
|
|
return []
|
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
groups = []
|
|
|
|
for group_id in group_ids.split('\n'):
|
|
|
|
if not group_id:
|
2012-04-02 15:56:41 +00:00
|
|
|
continue
|
2012-09-03 08:14:15 +00:00
|
|
|
group = get_group(group_id)
|
|
|
|
if group:
|
|
|
|
groups.append(group)
|
|
|
|
return groups
|
2012-05-15 02:59:16 +00:00
|
|
|
|
2012-08-30 12:15:17 +00:00
|
|
|
def get_group_repoids(group_id):
|
|
|
|
"""Get repo ids of a given group id."""
|
2012-05-15 02:59:16 +00:00
|
|
|
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-08-30 12:15:17 +00:00
|
|
|
def get_group_repos(group_id, user):
|
|
|
|
"""Get repos of a given group id."""
|
|
|
|
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)
|
|
|
|
|
|
|
|
repos = []
|
|
|
|
for repo_id in repoid_list:
|
|
|
|
if not repo_id:
|
|
|
|
continue
|
|
|
|
repo = get_repo(repo_id)
|
|
|
|
if not repo:
|
|
|
|
continue
|
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
repo.owner = seafserv_threaded_rpc.get_group_repo_owner(repo_id)
|
2012-08-30 12:15:17 +00:00
|
|
|
repo.share_from_me = True if user == repo.owner else False
|
|
|
|
|
|
|
|
try:
|
|
|
|
repo.latest_modify = get_commits(repo.id, 0, 1)[0].ctime
|
|
|
|
except:
|
|
|
|
repo.latest_modify = None
|
|
|
|
|
|
|
|
repos.append(repo)
|
|
|
|
repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify))
|
|
|
|
|
|
|
|
return repos
|
2012-09-03 08:14:15 +00:00
|
|
|
|
|
|
|
# org group repo
|
|
|
|
def del_org_group_repo(repo_id, org_id, group_id):
|
|
|
|
seafserv_threaded_rpc.del_org_group_repo(repo_id, org_id, group_id)
|
|
|
|
|
2012-08-30 12:15:17 +00:00
|
|
|
def get_org_group_repos(org_id, group_id, user):
|
|
|
|
"""Get org repos of a given group id."""
|
|
|
|
try:
|
|
|
|
repo_ids = seafserv_threaded_rpc.get_org_group_repoids(org_id, 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)
|
|
|
|
|
|
|
|
repos = []
|
|
|
|
for repo_id in repoid_list:
|
|
|
|
if not repo_id:
|
|
|
|
continue
|
|
|
|
repo = get_repo(repo_id)
|
|
|
|
if not repo:
|
|
|
|
continue
|
|
|
|
|
|
|
|
repo.owner = seafserv_threaded_rpc.get_org_group_repo_owner(org_id,
|
|
|
|
group_id,
|
|
|
|
repo_id)
|
|
|
|
repo.sharecd_from_me = True if user == repo.owner else False
|
|
|
|
|
|
|
|
try:
|
|
|
|
repo.latest_modify = get_commits(repo.id, 0, 1)[0].ctime
|
|
|
|
except:
|
|
|
|
repo.latest_modify = None
|
|
|
|
|
|
|
|
repos.append(repo)
|
|
|
|
repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify))
|
|
|
|
|
|
|
|
return repos
|
|
|
|
|
2012-09-03 12:54:07 +00:00
|
|
|
def get_org_groups_by_repo(org_id, repo_id):
|
|
|
|
try:
|
|
|
|
group_ids = seafserv_threaded_rpc.get_org_groups_by_repo(org_id,
|
|
|
|
repo_id)
|
|
|
|
except SearpcError:
|
|
|
|
group_ids = ''
|
|
|
|
if not group_ids:
|
|
|
|
return []
|
|
|
|
|
|
|
|
groups = []
|
|
|
|
for group_id in group_ids.split('\n'):
|
|
|
|
if not group_id:
|
|
|
|
continue
|
|
|
|
group = get_group(group_id)
|
|
|
|
if group:
|
|
|
|
groups.append(group)
|
|
|
|
return groups
|
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
# inner pub repo
|
2012-09-26 02:49:00 +00:00
|
|
|
def list_inner_pub_repos(username):
|
2012-09-03 11:36:47 +00:00
|
|
|
"""
|
|
|
|
List inner pub repos, which can be access by everyone.
|
|
|
|
"""
|
2012-09-26 02:49:00 +00:00
|
|
|
try:
|
|
|
|
shared_repos = seafserv_threaded_rpc.list_inner_pub_repos()
|
|
|
|
except:
|
|
|
|
shared_repos = []
|
2012-09-03 11:36:47 +00:00
|
|
|
|
2012-09-26 02:49:00 +00:00
|
|
|
for repo in shared_repos:
|
|
|
|
perm = seafserv_threaded_rpc.check_permission(repo.props.repo_id,
|
|
|
|
username)
|
|
|
|
repo.user_perm = perm
|
2012-09-03 11:36:47 +00:00
|
|
|
|
2012-09-26 02:49:00 +00:00
|
|
|
shared_repos.sort(lambda x, y: cmp(y.props.last_modified, x.props.last_modified))
|
|
|
|
return shared_repos
|
2012-09-03 11:36:47 +00:00
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
def is_inner_pub_repo(repo_id):
|
2012-07-28 08:41:24 +00:00
|
|
|
"""
|
2012-09-03 08:14:15 +00:00
|
|
|
Check whether a repo is public.
|
|
|
|
Return 0 if repo is not inner public, otherwise non-zero.
|
2012-07-28 08:41:24 +00:00
|
|
|
"""
|
|
|
|
try:
|
2012-09-03 08:14:15 +00:00
|
|
|
ret = seafserv_threaded_rpc.is_inner_pub_repo(repo_id)
|
2012-07-28 08:41:24 +00:00
|
|
|
except SearpcError:
|
|
|
|
ret = 0
|
2012-07-26 09:08:31 +00:00
|
|
|
|
2012-07-28 08:41:24 +00:00
|
|
|
return ret
|
2012-08-17 08:35:50 +00:00
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
# org inner pub repo
|
2012-09-26 02:49:00 +00:00
|
|
|
def list_org_inner_pub_repos(org_id, username, start=None, limit=None):
|
2012-08-17 08:35:50 +00:00
|
|
|
"""
|
2012-09-03 08:14:15 +00:00
|
|
|
List org inner pub repos, which can be access by all org members.
|
2012-08-17 08:35:50 +00:00
|
|
|
"""
|
|
|
|
try:
|
2012-09-26 02:49:00 +00:00
|
|
|
shared_repos = seafserv_threaded_rpc.list_org_inner_pub_repos(org_id)
|
2012-08-17 08:35:50 +00:00
|
|
|
except SearpcError:
|
2012-09-26 02:49:00 +00:00
|
|
|
shared_repos = []
|
|
|
|
|
|
|
|
for repo in shared_repos:
|
|
|
|
perm = seafserv_threaded_rpc.check_permission(repo.props.repo_id,
|
|
|
|
username)
|
|
|
|
repo.user_perm = perm
|
|
|
|
|
|
|
|
# sort repos by last modify time
|
|
|
|
shared_repos.sort(lambda x, y: cmp(y.props.last_modified, x.props.last_modified))
|
|
|
|
return shared_repos
|
2012-08-27 06:38:36 +00:00
|
|
|
|
2012-09-03 08:14:15 +00:00
|
|
|
# repo permissoin
|
2012-08-27 06:38:36 +00:00
|
|
|
def check_permission(repo_id, user):
|
|
|
|
"""
|
|
|
|
Check whether user has permission to access repo.
|
|
|
|
Return true if user has permission otherwise false.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
ret = seafserv_threaded_rpc.check_permission(repo_id, user)
|
|
|
|
except SearpcError:
|
2012-09-21 03:22:46 +00:00
|
|
|
ret = ""
|
|
|
|
return ret
|
2012-08-30 12:15:17 +00:00
|
|
|
|
2012-09-03 04:03:06 +00:00
|
|
|
def is_personal_repo(repo_id):
|
|
|
|
"""
|
|
|
|
Check whether repo is personal repo.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
owner = seafserv_threaded_rpc.get_repo_owner(repo_id)
|
|
|
|
except SearpcError:
|
|
|
|
owner = ''
|
|
|
|
return True if owner else False
|
|
|
|
|
|
|
|
def list_personal_shared_repos(user, user_type, start, limit):
|
|
|
|
"""
|
|
|
|
List personal repos that user share with others.
|
|
|
|
If `user_type` is 'from_email', list repos user shares to others;
|
|
|
|
If `user_type` is 'to_email', list repos others sahre to user.
|
|
|
|
"""
|
|
|
|
try:
|
2012-09-26 02:49:00 +00:00
|
|
|
share_repos = seafserv_threaded_rpc.list_share_repos(user, user_type,
|
|
|
|
start, limit)
|
2012-09-03 04:03:06 +00:00
|
|
|
except SearpcError:
|
2012-09-26 02:49:00 +00:00
|
|
|
share_repos = []
|
2012-09-03 04:03:06 +00:00
|
|
|
|
2012-09-26 02:49:00 +00:00
|
|
|
for repo in share_repos:
|
|
|
|
repo.user_perm = check_permission(repo.props.repo_id, user)
|
2012-09-03 04:03:06 +00:00
|
|
|
|
2012-09-26 02:49:00 +00:00
|
|
|
share_repos.sort(lambda x, y: cmp(y.last_modified, x.last_modified))
|
|
|
|
return share_repos
|
2012-09-03 04:03:06 +00:00
|
|
|
|
2012-09-26 02:49:00 +00:00
|
|
|
def list_org_shared_repos(org_id, user, user_type, start, limit):
|
2012-09-03 04:03:06 +00:00
|
|
|
"""
|
|
|
|
List org repos that user share with others.
|
|
|
|
If `user_type` is 'from_email', list repos user shares to others;
|
|
|
|
If `user_type` is 'to_email', list repos others sahre to user.
|
|
|
|
"""
|
|
|
|
try:
|
2012-09-26 02:49:00 +00:00
|
|
|
share_repos = seafserv_threaded_rpc.list_org_share_repos(org_id,
|
|
|
|
user, user_type,
|
|
|
|
start, limit)
|
2012-09-03 04:03:06 +00:00
|
|
|
except SearpcError:
|
2012-09-26 02:49:00 +00:00
|
|
|
share_repos = []
|
2012-09-03 04:03:06 +00:00
|
|
|
|
2012-09-26 02:49:00 +00:00
|
|
|
for repo in share_repos:
|
|
|
|
repo.user_perm = check_permission(repo.props.repo_id, user)
|
|
|
|
|
|
|
|
share_repos.sort(lambda x, y: cmp(y.last_modified, x.last_modified))
|
|
|
|
return share_repos
|
2012-09-03 08:14:15 +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
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2012-09-13 09:30:31 +00:00
|
|
|
def get_file_size(file_id):
|
|
|
|
try:
|
|
|
|
fs = seafserv_threaded_rpc.get_file_size(file_id)
|
|
|
|
except SearpcError, e:
|
|
|
|
fs = 0
|
|
|
|
return fs
|
2012-09-24 12:30:17 +00:00
|
|
|
|
|
|
|
def get_related_users_by_repo(repo_id):
|
|
|
|
"""Give a repo id, returns a list of users of:
|
|
|
|
- the repo owner
|
|
|
|
- members of groups to which the repo is shared
|
|
|
|
- users to which the repo is shared
|
|
|
|
"""
|
|
|
|
owner = seafserv_threaded_rpc.get_repo_owner(repo_id)
|
2012-10-15 13:03:42 +00:00
|
|
|
if not owner:
|
|
|
|
# Can't happen
|
|
|
|
return []
|
|
|
|
|
2012-09-24 12:30:17 +00:00
|
|
|
users = [owner]
|
|
|
|
|
|
|
|
groups = get_shared_groups_by_repo(repo_id)
|
2012-10-15 13:03:42 +00:00
|
|
|
|
2012-09-24 12:30:17 +00:00
|
|
|
for group in groups:
|
|
|
|
members = get_group_members(group.id)
|
|
|
|
for member in members:
|
|
|
|
if member.user_name not in users:
|
|
|
|
users.append(member.user_name)
|
|
|
|
|
2012-10-15 13:03:42 +00:00
|
|
|
share_repos = seafserv_threaded_rpc.list_share_repos(owner, \
|
|
|
|
'from_email', -1, -1)
|
|
|
|
|
|
|
|
for repo in share_repos:
|
|
|
|
if repo.repo_id == repo_id:
|
|
|
|
if repo.user not in users:
|
|
|
|
users.append(repo.user)
|
|
|
|
|
|
|
|
return users
|
|
|
|
|
|
|
|
def get_related_users_by_org_repo(org_id, repo_id):
|
|
|
|
"""Org version of get_related_users_by_repo
|
|
|
|
"""
|
|
|
|
owner = get_org_repo_owner(repo_id)
|
|
|
|
|
|
|
|
if not owner:
|
|
|
|
# Can't happen
|
|
|
|
return []
|
|
|
|
|
|
|
|
users = [owner]
|
|
|
|
|
|
|
|
groups = get_org_groups_by_repo(org_id, repo_id)
|
|
|
|
|
|
|
|
for group in groups:
|
|
|
|
members = get_group_members(group.id)
|
|
|
|
for member in members:
|
|
|
|
if member.user_name not in users:
|
|
|
|
users.append(member.user_name)
|
|
|
|
|
|
|
|
share_repos = seafserv_threaded_rpc.list_org_share_repos(org_id, \
|
|
|
|
owner, 'from_email', -1, -1)
|
|
|
|
|
2012-09-24 12:30:17 +00:00
|
|
|
for repo in share_repos:
|
2012-10-15 13:03:42 +00:00
|
|
|
if repo.repo_id == repo_id:
|
|
|
|
if repo.user not in users:
|
|
|
|
users.append(repo.user)
|
2012-09-24 12:30:17 +00:00
|
|
|
|
|
|
|
return users
|
2012-11-17 09:31:37 +00:00
|
|
|
|
|
|
|
# quota
|
|
|
|
def check_quota(repo_id):
|
|
|
|
try:
|
|
|
|
ret = seafserv_threaded_rpc.check_quota(repo_id)
|
|
|
|
except SearpcError, e:
|
|
|
|
ret = -1
|
|
|
|
return ret
|
|
|
|
|
|
|
|
# access token
|
|
|
|
def web_get_access_token(repo_id, obj_id, op, username):
|
|
|
|
try:
|
|
|
|
ret = seafserv_rpc.web_get_access_token(repo_id, obj_id, op, username)
|
|
|
|
except SearpcError, e:
|
|
|
|
ret = ''
|
|
|
|
return ret
|
|
|
|
|
2012-11-22 06:26:39 +00:00
|
|
|
# password management
|
|
|
|
def unset_repo_passwd(repo_id, user):
|
|
|
|
"""
|
|
|
|
Remove user password of a encrypt repo.
|
|
|
|
Arguments:
|
|
|
|
- `repo_id`: encrypt repo id
|
|
|
|
- `user`: username
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
ret = seafserv_threaded_rpc.unset_passwd(repo_id, user)
|
|
|
|
except SearpcError, e:
|
|
|
|
ret = -1
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def is_passwd_set(repo_id, user):
|
|
|
|
try:
|
|
|
|
ret = seafserv_rpc.is_passwd_set(repo_id, user)
|
|
|
|
except SearpcError, e:
|
|
|
|
ret = -1
|
|
|
|
return True if ret == 1 else False
|
|
|
|
|