-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
61 lines (55 loc) · 2 KB
/
utils.py
File metadata and controls
61 lines (55 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
import os, errno, sys
import string, random
import requests
import paramiko
from datetime import datetime
def run_remote_cmd(host, user, key, cmd):
res = {'stderr': None, 'stdout': None}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=user, key_filename=key)
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.close()
res['stdout'] = stdout.read()
res['stderr'] = ''.join(filter(lambda x: not x.startswith('zip_safe'), stderr.readlines()))
except:
res['stderr'] = sys.exc_info()[0].__doc__
finally:
ssh.close()
return res
def stringify_dt(data):
if data:
for k, v in data.iteritems():
if type(v) is datetime:
data[k] = str(v)
return data
def random_str(size=8):
chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for _ in range(size))
def get_oauth(url, token):
try:
name = token.split('|')[0].split('=')[1]
except:
return {'status': 401, 'error': 'Invalid user and/or token', 'data': None}
try:
rget = requests.get(url+'/'+name, headers={'Authorization': 'Globus-Goauthtoken %s'%token})
except Exception as e:
return {'status': 504, 'error': 'Unable to connect to OAuth server %s: %s'%(url, e), 'data': None}
if not (rget.ok and rget.text):
return {'status': 504, 'error': 'Unable to connect to OAuth server %s: %s'%(url, rget.raise_for_status()), 'data': None}
data = rget.json
if not (data and isinstance(data, dict)):
return {'status': 401, 'error': 'Invalid user and/or token', 'data': None}
return {'status': 200, 'data': data}
def pid_exists(pid):
"""Check whether pid exists in the current process table."""
if (not pid) or (pid < 0):
return False
try:
os.kill(pid, 0)
except OSError, e:
return e.errno == errno.EPERM
else:
return True