summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/utils.py
blob: 8d2c27b1942f79408eb97965f74e7a6ac91068df (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
import json
import socket
import os
import scanf

def save_json_to_disk(filePath, description, mime, json_data, result):
    with open(filePath, 'w') as outfile:
        json.dump(json_data, outfile, indent=4)
    outfile.close()
    result.append(
        {
            "description": description,
            "filepath": filePath,
            "mimetype": mime
        }
    )

def save_file_to_disk(filePath, description, mime, data, result):
    with open(filePath, 'w') as outfile:
        outfile.write(data)
    outfile.close()
    result.append(
        {
            "description": description,
            "filepath": filePath,
            "mimetype": mime
        }
    )

def save_result (filePath, description, mime, result):
    result.append(
        {
            "description": description,
            "filepath": filePath,
            "mimetype": mime
        }
    )


def str2bool(v):
  return v.lower() in ("yes", "true", "t", "1")

def station2Port(base):
    Name = socket.gethostname()
    res = scanf.scanf("Station%d", Name)
    if res[0]:
        NmBase = int(base)
        res = int(res[0])
        return str(NmBase + res)
    return base


def sys_read(sysnode):
    try:
        f = open(sysnode, "r")
        data = f.readline()
        f.close()
    except IOError as Error:
        return False, '{}'.format(Error.errno)
    return True, data

def removefileIfExist(fname):
    if os.path.exists(fname):
        try:
            os.remove(fname)
        except OSError as error:
            return False, str(error)
    return True, ''