aboutsummaryrefslogtreecommitdiff
path: root/htc_utils/bindings/__init__.py
blob: 9cfa584b0ad7ed63cd9f6c768e6983bd50969208 (plain) (blame)
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
62
63
64
65
66
67
68
69
import os
import subprocess
from collections import OrderedDict
from distutils.spawn import find_executable

__all__ = ['htcondor_path', 'recast', 'Job', 'Submit', 'Wait']

def htcondor_path(path=None):
    programs = ['condor_master', 'condor_submit', 'condor_wait']
    paths = []

    if '/usr/sbin' not in os.environ['PATH']:
        os.environ['PATH'] += ":/usr/sbin"

    if path is not None:
        os.environ['PATH'] = ':'.join([os.path.join(path, 'bin'), os.environ['PATH']])
        os.environ['PATH'] = ':'.join([os.path.join(path, 'sbin'), os.environ['PATH']])

    for program in programs:
        executable = find_executable(program, os.environ['PATH'])
        if not executable:
            continue
        path = os.path.dirname(executable)
        paths.append(path)

    if not paths:
        raise OSError('HTCondor installation not found. '
                'Modify your PATH variable and try again.'.format(", ".join(programs)))

    return ':'.join(paths)


def recast(value):
    ''' Convert value to string
    '''
    if type(value) is bool and value == True:
        value = 'true'
        return value

    if type(value) is bool and value == False:
        value = 'false'
        return value

    if isinstance(value, list) or \
        isinstance(value, tuple):
            values = ' '.join([ str(x) for x in value ])
            return values

    if isinstance(value, dict):
        temp = []
        for key, val in value.items():
            val = str(val)
            temp.append('='.join([str(key), str(val)]))
        # Ad-hoc string quoting
        return ';'.join(str(x) for x in temp)


    try:
        value = str(value)
    except:
        raise TypeError('Unable to cast "{}" to str'.format(type(value)))

    return value



from .job import *
from .submit import *
from .wait import *