Mercurial > repos > yating-l > jbrowsehubtoapollo
comparison util/subtools.py @ 3:6f262a92e8dc draft default tip
planemo upload for repository https://github.com/Yating-L/suite_gonramp_apollo.git commit 91b46f7c891c2466bc5b6a063411cdae75964515-dirty
author | yating-l |
---|---|
date | Mon, 27 Nov 2017 12:06:18 -0500 |
parents | 8ff4b84d709f |
children |
comparison
equal
deleted
inserted
replaced
2:8ff4b84d709f | 3:6f262a92e8dc |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 """ | |
4 This file include common used functions for converting file format to gff3 | |
5 """ | |
6 from collections import OrderedDict | |
7 import json | |
8 import subprocess | |
9 import os | |
10 import sys | |
11 import tempfile | |
12 import string | |
13 import logging | |
14 | |
15 class PopenError(Exception): | |
16 def __init__(self, cmd, error, return_code): | |
17 self.cmd = cmd | |
18 self.error = error | |
19 self.return_code = return_code | |
20 | |
21 def __str__(self): | |
22 message = "The subprocess {0} has returned the error: {1}.".format( | |
23 self.cmd, self.return_code) | |
24 message = ','.join( | |
25 (message, "Its error message is: {0}".format(self.error))) | |
26 return repr(message) | |
27 | |
28 | |
29 def _handleExceptionAndCheckCall(array_call, **kwargs): | |
30 """ | |
31 This class handle exceptions and call the tool. | |
32 It maps the signature of subprocess.check_call: | |
33 See https://docs.python.org/2/library/subprocess.html#subprocess.check_call | |
34 """ | |
35 stdout = kwargs.get('stdout', subprocess.PIPE) | |
36 stderr = kwargs.get('stderr', subprocess.PIPE) | |
37 shell = kwargs.get('shell', False) | |
38 stdin = kwargs.get('stdin', None) | |
39 | |
40 cmd = array_call[0] | |
41 | |
42 output = None | |
43 error = None | |
44 | |
45 # TODO: Check the value of array_call and <=[0] | |
46 logging.debug("Calling {0}:".format(cmd)) | |
47 logging.debug("%s", array_call) | |
48 logging.debug("---------") | |
49 | |
50 # TODO: Use universal_newlines option from Popen? | |
51 try: | |
52 p = subprocess.Popen(array_call, stdout=stdout, | |
53 stderr=stderr, shell=shell, stdin=stdin) | |
54 | |
55 # TODO: Change this because of possible memory issues => https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate | |
56 | |
57 output, error = p.communicate() | |
58 | |
59 if stdout == subprocess.PIPE: | |
60 logging.debug("\t{0}".format(output)) | |
61 else: | |
62 logging.debug("\tOutput in file {0}".format(stdout.name)) | |
63 # If we detect an error from the subprocess, then we raise an exception | |
64 # TODO: Manage if we raise an exception for everything, or use CRITICAL etc... but not stop process | |
65 # TODO: The responsability of returning a sys.exit() should not be there, but up in the app. | |
66 if p.returncode: | |
67 if stderr == subprocess.PIPE: | |
68 raise PopenError(cmd, error, p.returncode) | |
69 else: | |
70 # TODO: To Handle properly with a design behind, if we received a option as a file for the error | |
71 raise Exception("Error when calling {0}. Error as been logged in your file {1}. Error code: {2}".format(cmd, stderr.name, p.returncode)) | |
72 | |
73 except OSError as e: | |
74 message = "The subprocess {0} has encountered an OSError: {1}".format( | |
75 cmd, e.strerror) | |
76 if e.filename: | |
77 message = '\n'.join( | |
78 (message, ", against this file: {0}".format(e.filename))) | |
79 logging.error(message) | |
80 sys.exit(-1) | |
81 except PopenError as p: | |
82 message = "The subprocess {0} has returned the error: {1}.".format( | |
83 p.cmd, p.return_code) | |
84 message = '\n'.join( | |
85 (message, "Its error message is: {0}".format(p.error))) | |
86 | |
87 logging.exception(message) | |
88 | |
89 sys.exit(p.return_code) | |
90 except Exception as e: | |
91 message = "The subprocess {0} has encountered an unknown error: {1}".format( | |
92 cmd, e) | |
93 logging.exception(message) | |
94 | |
95 sys.exit(-1) | |
96 return output | |
97 | |
98 def arrow_add_organism(organism_name, organism_dir, public=False): | |
99 array_call = ['arrow', 'organisms', 'add_organism', organism_name, organism_dir] | |
100 if public: | |
101 array_call.append('--public') | |
102 p = _handleExceptionAndCheckCall(array_call) | |
103 #p = subprocess.check_output(array_call) | |
104 return p | |
105 | |
106 def arrow_create_user(user_email, firstname, lastname, password, admin=False): | |
107 """ Create a new user of Apollo, the default user_role is "user" """ | |
108 array_call = ['arrow', 'users', 'create_user', user_email, firstname, lastname, password] | |
109 if admin: | |
110 array_call += ['--role', 'admin'] | |
111 logging.debug("%s", array_call) | |
112 print array_call | |
113 p = subprocess.check_output(array_call) | |
114 print ("p = %s", p) | |
115 return p | |
116 | |
117 def arrow_update_organism_permissions(user_id, organism, **user_permissions): | |
118 array_call = ['arrow', 'users', 'update_organism_permissions', str(user_id), str(organism)] | |
119 admin = user_permissions.get("admin", False) | |
120 write = user_permissions.get("write", False) | |
121 read = user_permissions.get("read", False) | |
122 export = user_permissions.get("export", False) | |
123 if admin: | |
124 array_call.append('--administrate') | |
125 if write: | |
126 array_call.append('--write') | |
127 if read: | |
128 array_call.append('--read') | |
129 if export: | |
130 array_call.append('--export') | |
131 p = subprocess.check_output(array_call) | |
132 return p | |
133 | |
134 def arrow_get_users(user_email): | |
135 array_call = ['arrow', 'users', 'get_users'] | |
136 logging.debug("%s", array_call) | |
137 print array_call | |
138 p = subprocess.check_output(array_call) | |
139 all_users = json.loads(p) | |
140 for d in all_users: | |
141 if d['username'] == user_email: | |
142 return d['userId'] | |
143 logging.error("Cannot find user %s", user_email) | |
144 | |
145 def verify_user_login(username, password, apollo_host): | |
146 user_info = {'username': username, 'password': password} | |
147 array_call = ['curl', | |
148 '-b', 'cookies.txt', | |
149 '-c', 'cookies.txt', | |
150 '-H', 'Content-Type:application/json', | |
151 '-d', json.dumps(user_info), | |
152 apollo_host + '/Login?operation=login' | |
153 ] | |
154 p = _handleExceptionAndCheckCall(array_call) | |
155 msg = json.loads(p) | |
156 if 'error' in msg: | |
157 logging.error("The Authentication for user %s failed. Get error message %s", username, msg['error']) | |
158 exit(-1) | |
159 | |
160 |