summaryrefslogtreecommitdiffstats
path: root/extras/volgen/Common.py
blob: 298dee989437ceb8d20daf6a36c43515d9b1fe78 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os, sys, re, string

def check_duplicate_entry(args):
    """Check duplicate entries in incoming arguments"""
    _tmp = []
    for server in args:
        if server not in _tmp:
            _tmp.append (server)
        else:
            print "Duplicate arguments detected (%s)" % server
            raise ValueError

    return

def args2dict(args):

    keyvalue = {}
    for arg in args:
        if int(arg.find(':')) == -1:
            continue
        first = arg.split(':')[0]
        keyvalue[first] = []

    for arg in args:
        if int(arg.find(':')) == -1:
            continue
        first = arg.split(':')[0]
        if arg.split(':')[1] not in keyvalue[first]:
            if arg.split(':')[1][0] != '/':
                print "Absolute export path required for %s" % arg
                raise ValueError
            keyvalue[first].append (arg.split(':')[1])

    return keyvalue

def args2array(args):

    array = []

    for arg in args:
        if int(arg.find(':')) == -1:
            continue
        array.append(arg)

    return array

def list_export_vols(configdir, volumename):

    list_export = []
    if os.path.isdir(configdir):
        for line in os.listdir(configdir):
            if re.match(r'[a-zA-Z0-9_]\S+%s-export.vol' % volumename, line):
                list_export.append(line)

    return list_export

def get_old_server_args(exports, configdir):

    list_args = []
    for export in exports:
        array = gfParser("%s/%s" % (configdir, export))
        for dt in array:
            if dt.has_key('option'):
                if re.match("\w+tory", dt['option']):
                    list_args.append(export.split('-')[0] + ":" + dt['option'].split()[1])

    return list_args

def gfParser (volfile):

    volfile_rl = open (volfile).readlines()
    volume_array = []
    for line in volfile_rl:
        line = line.strip()
        volfile_dict = {}
        if re.match(r"[a-zA-Z0-9_]+", line):
            if line.split() > 1:
                volfile_dict[line.split()[0]] = string.join (line.split()[1:], ' ')  
            else: 
                volfile_dict[line.split()[0]] = " "
            volume_array.append(volfile_dict)

    return volume_array