Mercurial > repos > yating-l > gonramp_apollo
comparison util/subtools.py @ 0:cb8439d64ed3 draft default tip
planemo upload for repository https://github.com/Yating-L/suite_gonramp_apollo.git commit 9009fa8663038d2cb4a1c5130600a51f31f654a1-dirty
| author | yating-l |
|---|---|
| date | Mon, 27 Nov 2017 14:40:53 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:cb8439d64ed3 |
|---|---|
| 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 """ | |
| 108 Create a new user of Apollo, the default user_role is "user" | |
| 109 """ | |
| 110 array_call = ['arrow', 'users', 'create_user', user_email, firstname, lastname, password] | |
| 111 if admin: | |
| 112 array_call += ['--role', 'admin'] | |
| 113 p = _handleExceptionAndCheckCall(array_call) | |
| 114 j = json.loads(p) | |
| 115 if "userId" in j: | |
| 116 return j['userId'] | |
| 117 elif "error" in j: | |
| 118 logging.error("User %s already exist", user_email) | |
| 119 raise Exception(j['error']) | |
| 120 | |
| 121 | |
| 122 def arrow_delete_user(user_email): | |
| 123 array_call = ['arrow', 'users', 'delete_user', user_email] | |
| 124 p = _handleExceptionAndCheckCall(array_call) | |
| 125 j = json.loads(p) | |
| 126 if "error" in j: | |
| 127 raise Exception(j['error']) | |
| 128 | |
| 129 def arrow_add_to_group(groupname, user_email): | |
| 130 if not arrow_get_groups(groupname): | |
| 131 arrow_create_group(groupname) | |
| 132 array_call = ['arrow', 'users', 'add_to_group', groupname, user_email] | |
| 133 p = _handleExceptionAndCheckCall(array_call) | |
| 134 j = json.loads(p) | |
| 135 if j != dict(): | |
| 136 raise Exception("Error add user %s to group %s", user_email, groupname) | |
| 137 | |
| 138 | |
| 139 def arrow_remove_from_group(groupname, user_email): | |
| 140 if arrow_get_groups(groupname): | |
| 141 array_call = ['arrow', 'users', 'remove_from_group', groupname, user_email] | |
| 142 p = _handleExceptionAndCheckCall(array_call) | |
| 143 else: | |
| 144 raise Exception("Group %s doesn't exist. Check if you spell the name correctly", groupname) | |
| 145 | |
| 146 def arrow_create_group(groupname): | |
| 147 if arrow_get_groups(groupname): | |
| 148 raise Exception("Group %s already exist. Create a group with another name.", groupname) | |
| 149 array_call = ['arrow', 'groups', 'create_group', groupname] | |
| 150 p = _handleExceptionAndCheckCall(array_call) | |
| 151 | |
| 152 def arrow_get_groups(groupname): | |
| 153 array_call = ['arrow', 'groups', 'get_groups'] | |
| 154 p = _handleExceptionAndCheckCall(array_call) | |
| 155 all_groups = json.loads(p) | |
| 156 for g in all_groups: | |
| 157 if g['name'] == groupname: | |
| 158 return True | |
| 159 return False | |
| 160 | |
| 161 def arrow_update_organism_permissions(user_id, organism, **user_permissions): | |
| 162 array_call = ['arrow', 'users', 'update_organism_permissions', str(user_id), str(organism)] | |
| 163 admin = user_permissions.get("admin", False) | |
| 164 write = user_permissions.get("write", False) | |
| 165 read = user_permissions.get("read", False) | |
| 166 export = user_permissions.get("export", False) | |
| 167 if admin: | |
| 168 array_call.append('--administrate') | |
| 169 if write: | |
| 170 array_call.append('--write') | |
| 171 if read: | |
| 172 array_call.append('--read') | |
| 173 if export: | |
| 174 array_call.append('--export') | |
| 175 p = _handleExceptionAndCheckCall(array_call) | |
| 176 return p | |
| 177 | |
| 178 def arrow_get_users(user_email): | |
| 179 array_call = ['arrow', 'users', 'get_users'] | |
| 180 p = _handleExceptionAndCheckCall(array_call) | |
| 181 all_users = json.loads(p) | |
| 182 for d in all_users: | |
| 183 if d['username'] == user_email: | |
| 184 return d['userId'] | |
| 185 logging.error("Cannot find user %s", user_email) | |
| 186 | |
| 187 def arrow_get_organism(organism_name): | |
| 188 array_call= ['arrow', 'organisms', 'get_organisms'] | |
| 189 p = _handleExceptionAndCheckCall(array_call) | |
| 190 all_organisms = json.loads(p) | |
| 191 for org in all_organisms: | |
| 192 if org['commonName'] == organism_name: | |
| 193 return org['id'] | |
| 194 | |
| 195 | |
| 196 def arrow_delete_organism(organism_id): | |
| 197 array_call = ['arrow', 'organisms', 'delete_organism', organism_id] | |
| 198 p = _handleExceptionAndCheckCall(array_call) | |
| 199 return p | |
| 200 | |
| 201 def verify_user_login(username, password, apollo_host): | |
| 202 user_info = {'username': username, 'password': password} | |
| 203 array_call = ['curl', | |
| 204 '-b', 'cookies.txt', | |
| 205 '-c', 'cookies.txt', | |
| 206 '-H', 'Content-Type:application/json', | |
| 207 '-d', json.dumps(user_info), | |
| 208 apollo_host + '/Login?operation=login' | |
| 209 ] | |
| 210 p = _handleExceptionAndCheckCall(array_call) | |
| 211 msg = json.loads(p) | |
| 212 if 'error' in msg: | |
| 213 logging.error("The Authentication for user %s failed. Get error message %s", username, msg['error']) | |
| 214 exit(-1) | |
| 215 | |
| 216 |
