Mercurial > repos > yating-l > jbrowsehubtoapollo
annotate apollo/ApolloInstance.py @ 1:2ae1e96a8380 draft
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
author | yating-l |
---|---|
date | Tue, 24 Oct 2017 18:24:40 -0400 |
parents | bc00f5c4c59e |
children | 8ff4b84d709f |
rev | line source |
---|---|
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
1 #!/usr/bin/env python |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
2 import os |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
3 import json |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
4 import shutil |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
5 import tempfile |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
6 import logging |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
7 import random |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
8 import string |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
9 from util import subtools |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
10 from mako.lookup import TemplateLookup |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
11 |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
12 |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
13 class ApolloInstance(object): |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
14 def __init__(self, apollo_host, apollo_admin, tool_directory): |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
15 self.apollo_host = apollo_host |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
16 self.tool_directory = tool_directory |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
17 self.logger = logging.getLogger(__name__) |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
18 self.apollo_admin = apollo_admin |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
19 self.apolloTemplate = self._getApolloTemplate() |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
20 self._arrow_init() |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
21 |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
22 |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
23 def _arrow_init(self): |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
24 subtools.verify_user_login(self.apollo_admin.user_email, self.apollo_admin.password) |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
25 arrow_config = tempfile.NamedTemporaryFile(bufsize=0) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
26 with open(arrow_config.name, 'w') as conf: |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
27 htmlMakoRendered = self.apolloTemplate.render( |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
28 apollo_host = self.apollo_host, |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
29 admin_user = self.apollo_admin.user_email, |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
30 admin_pw = self.apollo_admin.password |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
31 ) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
32 conf.write(htmlMakoRendered) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
33 |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
34 home_dir = os.path.expanduser('~') |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
35 arrow_config_dir = os.path.join(home_dir, '.apollo-arrow.yml') |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
36 shutil.copyfile(arrow_config.name, arrow_config_dir) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
37 self.logger.debug("Initated arrow: apollo-arrow.yml= %s", arrow_config_dir) |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
38 |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
39 #TODO: Encode admin password |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
40 ''' |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
41 def _generatePassword(self, length=8): |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
42 chars = string.digits + string.letters |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
43 pw = ''.join([random.choice(chars) for _ in range(length)]) |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
44 return pw |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
45 ''' |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
46 |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
47 def _getApolloTemplate(self): |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
48 mylookup = TemplateLookup(directories=[os.path.join(self.tool_directory, 'templates')], |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
49 output_encoding='utf-8', encoding_errors='replace') |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
50 apolloTemplate = mylookup.get_template("apollo-arrow.yml") |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
51 return apolloTemplate |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
52 |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
53 def createApolloUser(self, apollo_user, admin=None): |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
54 p = subtools.arrow_create_user(apollo_user.user_email, apollo_user.firstname, apollo_user.lastname, apollo_user.password, admin) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
55 user_info = json.loads(p) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
56 user_id = user_info.get('userId') |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
57 if not user_id: |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
58 self.logger.debug("Cannot create new user: %s; The user may already exist", apollo_user.user_email) |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
59 subtools.verify_user_login(apollo_user.user_email, apollo_user.password) |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
60 user_id = subtools.arrow_get_users(apollo_user.user_email) |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
61 self.logger.debug("Got user_id for new or existing user: user_id = %s", str(user_id)) |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
62 return user_id |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
63 |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
64 def grantPermission(self, user_id, organism_id, **user_permissions): |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
65 subtools.arrow_update_organism_permissions(user_id, organism_id, **user_permissions) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
66 self.logger.debug("Grant user %s permissions to organism %s, permissions = %s", str(user_id), str(organism_id), ','.join(user_permissions)) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
67 |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
68 def addOrganism(self, organism_name, organism_dir): |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
69 p = subtools.arrow_add_organism(organism_name, organism_dir) |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
70 if not p: |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
71 self.logger.error("The user is not authorized to add organism") |
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
72 exit(-1) |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
73 organism = json.loads(p) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
74 organism_id = organism['id'] |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
75 self.logger.debug("Added new organism to Apollo instance, %s", p) |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
76 return organism_id |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
77 |
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
78 def loadHubToApollo(self, apollo_user, organism_name, organism_dir, admin_user=False, **user_permissions): |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
79 #user_id = self.createApolloUser(apollo_user, admin_user) |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
80 organism_id = self.addOrganism(organism_name, organism_dir) |
1
2ae1e96a8380
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
yating-l
parents:
0
diff
changeset
|
81 #self.grantPermission(user_id, organism_id, **user_permissions) |
0
bc00f5c4c59e
planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff
changeset
|
82 self.logger.debug("Successfully load the hub to Apollo") |