<commit_before>#!/usr/bin/env python3

import os
import subprocess
import shutil
import sys

MAIN_PATCHED_SCRIPT="/usr/local/lib/python3.6/site-packages/checkipaconsistency/main.py"
CIPA_METRICS_PATH="/var/lib/node_exporter/files/cipa.prom"
CIPA_CONFIG_PATH="/root/.config/checkipaconsistency"
CIPA_BIN_PATH="/usr/local/bin/cipa"

def run_cipa():
    global CIPA_BIN_PATH
    proc = subprocess.Popen("%s --output metrics" % CIPA_BIN_PATH,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            stdin=subprocess.PIPE,
                            shell=True)
    output, err = proc.communicate()
    return proc.returncode, output, err

def check_content(fname):
    with open(fname) as dataf:
        return any("--output" in line for line in dataf)

def main():
    global MAIN_PATCHED_SCRIPT, CIPA_METRICS_PATH, CIPA_CONFIG_PATH, CIPA_BIN_PATH
    if not os.path.exists(MAIN_PATCHED_SCRIPT) or not check_content(MAIN_PATCHED_SCRIPT):
        print("No cipa tool installed or OpenMetrics format output is not supported.")
        sys.exit(0)
    if not os.path.exists(CIPA_BIN_PATH) or not os.path.exists(CIPA_CONFIG_PATH):
        print("[SKIP] cipa tool is required (with root config).")
        sys.exit(0)
    ret_code, out, err= run_cipa()
    if ret_code != 0:
        print("Executing cipa command failed: %s" % str(err))
        sys.exit(1)
    current_pid=os.getpid()
    cipa_metrics_temp_path="%s.%s" % (CIPA_METRICS_PATH, current_pid)
    if out:
        out_decoded=out.decode()
        with open(cipa_metrics_temp_path, 'a') as f:
           f.write(out_decoded)
        if os.path.exists(cipa_metrics_temp_path):
            shutil.move(cipa_metrics_temp_path, CIPA_METRICS_PATH)

if __name__ == "__main__":
    main()<commit_msg>CB-18691 : [RHEL8] Upgrade Py3.6 to Py3.8 | MAIN_PATCHED_SCRIPT_PATH<commit_after>#!/usr/bin/env python3

import os
import subprocess
import shutil
import sys

MAIN_PATCHED_SCRIPT = "/usr/local/lib/python" + str(sys.version_info.major) + "." + str(sys.version_info.minor) + "/site-packages/checkipaconsistency/main.py"
CIPA_METRICS_PATH="/var/lib/node_exporter/files/cipa.prom"
CIPA_CONFIG_PATH="/root/.config/checkipaconsistency"
CIPA_BIN_PATH="/usr/local/bin/cipa"

def run_cipa():
    global CIPA_BIN_PATH
    proc = subprocess.Popen("%s --output metrics" % CIPA_BIN_PATH,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            stdin=subprocess.PIPE,
                            shell=True)
    output, err = proc.communicate()
    return proc.returncode, output, err

def check_content(fname):
    with open(fname) as dataf:
        return any("--output" in line for line in dataf)

def main():
    global MAIN_PATCHED_SCRIPT, CIPA_METRICS_PATH, CIPA_CONFIG_PATH, CIPA_BIN_PATH
    if not os.path.exists(MAIN_PATCHED_SCRIPT) or not check_content(MAIN_PATCHED_SCRIPT):
        print("No cipa tool installed or OpenMetrics format output is not supported. Package location path: " + MAIN_PATCHED_SCRIPT)
        sys.exit(0)
    if not os.path.exists(CIPA_BIN_PATH) or not os.path.exists(CIPA_CONFIG_PATH):
        print("[SKIP] cipa tool is required (with root config).")
        sys.exit(0)
    ret_code, out, err= run_cipa()
    if ret_code != 0:
        print("Executing cipa command failed: %s" % str(err))
        sys.exit(1)
    current_pid=os.getpid()
    cipa_metrics_temp_path="%s.%s" % (CIPA_METRICS_PATH, current_pid)
    if out:
        out_decoded=out.decode()
        with open(cipa_metrics_temp_path, 'a') as f:
           f.write(out_decoded)
        if os.path.exists(cipa_metrics_temp_path):
            shutil.move(cipa_metrics_temp_path, CIPA_METRICS_PATH)

if __name__ == "__main__":
    main()
{'fraction_non_alphanumeric': 0.10825688073394496, 'fraction_numerical': 0.005766710353866317, 'mean_word_length': 3.0, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1090153', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1362, 'n_words': 259}