utils.py 1.4 KB
Newer Older
D
dogsheng 已提交
1 2 3
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Huawei Technologies Co., Ltd.
4 5 6
# A-Tune is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
7
#     http://license.coscl.org.cn/MulanPSL2
D
dogsheng 已提交
8 9 10
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
11
# See the Mulan PSL v2 for more details.
D
dogsheng 已提交
12 13 14 15 16 17 18
# Create: 2019-10-29

"""
Provide an interface to read data from csv.
"""

import os
G
gaoruoshu 已提交
19 20
import logging
import tarfile
D
dogsheng 已提交
21 22 23
import pandas as pd


Z
Zhipeng Xie 已提交
24 25
def read_from_csv(path):
    """read data from csv"""
D
dogsheng 已提交
26 27 28 29 30
    if not os.path.exists(path):
        return None
    if not path.endswith('.csv'):
        return None

Z
Zhipeng Xie 已提交
31
    with open(path, 'r') as file:
32
        data = pd.read_csv(file, header=0)
D
dogsheng 已提交
33 34

    return data
G
gaoruoshu 已提交
35 36 37


def extract_file(file_path, target_path):
H
hanxinke 已提交
38
    """extract file"""
G
gaoruoshu 已提交
39 40 41 42 43
    tar = tarfile.open(file_path)
    logging.debug("%s", tar.getnames())
    tar.extractall(path=target_path)
    tar.close()
    res_path = file_path.rpartition('-')[0]
H
hanxinke 已提交
44
    return res_path
G
gaoruoshu 已提交
45 46 47 48 49 50 51 52 53


def add_data_to_file(data, mode, filename):
    """write tuning result to file"""
    path = "/etc/atuned/webserver/" + filename + ".txt"
    file_handle = open(path, mode)
    file_handle.write(str(data))
    file_handle.write("\n")
    file_handle.close()