aboutsummaryrefslogtreecommitdiff
path: root/htc_utils/bindings/__init__.py
blob: 7f403e17849a3693f83cbe83c497e0957e8d40a1 (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
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):
    needles = ['condor_master', 'condor_submit', 'condor_wait']
    paths = []

    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 needle in needles:
        try:
            executable = find_executable(needle, os.environ['PATH'])
            path = os.path.dirname(executable)
            paths.append(path)
        except AttributeError:
            pass

    if not paths:
        raise OSError('Unable to find a valid HTCondor installation.')

    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 *