CoolFace
Apppublic

OSS-forge/CodeQualityEval

sourceHugging Facecc-by-sa-4.0updated 10mo agoView on Hugging Face
12likes
pylint_ODC.py82 linesDownload Raw Back to 3_Code_Defects_Analysis
1import json2import os3import tempfile4import subprocess5from tqdm import tqdm6import pandas as pd7 8# === CONFIG ===9INPUT_FILE = "1_dataset_sample_100/python_dataset.jsonl"     # Your dataset10 11CODE_FIELD = os.environ.get("CODE_FIELD", "human_code")12 13# Nice short labels for filenames14FIELD_LABELS = {15    "human_code": "Human",16    "chatgpt_code": "ChatGPT",17    "dsc_code": "DSC",18    "qwen_code": "Qwen",19}20 21CODE_LABEL = FIELD_LABELS.get(CODE_FIELD, CODE_FIELD)22 23OUTPUT_PREFIX = f"Pylint_{CODE_LABEL}"      # e.g. Pylint_Human, Pylint_ChatGPT, ...24OUTPUT_FILE = f"{OUTPUT_PREFIX}.jsonl"25 26ODC_MAPPING_XLSX = "2_ODC_Mapping/Mapping_Pylint_ODC.xlsx"  # mapping file27 28# === Load ODC Mapping from Excel ===29def load_odc_mapping_from_excel(xlsx_path: str) -> dict:30    df = pd.read_excel(xlsx_path, engine="openpyxl")31    return dict(zip(df["Pylint Symbol"], df["ODC Defect Type"]))32 33odc_mapping = load_odc_mapping_from_excel(ODC_MAPPING_XLSX)34 35# === Run pylint and capture JSON output ===36def run_pylint_json(code: str) -> list:37    with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as tmp:38        tmp.write(code)39        tmp_filename = tmp.name40 41    try:42        result = subprocess.run(43            ["pylint", tmp_filename, "--output-format=json", "--score=no", "-j=21"],44            stdout=subprocess.PIPE,45            stderr=subprocess.PIPE,46            text=True,47            timeout=1048        )49        output = result.stdout.strip()50        json_output = json.loads(output) if output else []51    except subprocess.TimeoutExpired:52        json_output = [{"type": "fatal", "message": "Pylint timeout"}]53    except Exception as e:54        json_output = [{"type": "fatal", "message": str(e)}]55    finally:56        os.unlink(tmp_filename)57 58    # Add ODC category to each message59    filtered_output = []60    for msg in json_output:61        symbol = msg.get("symbol")62        msg["odc_category"] = odc_mapping.get(symbol, "--")63        filtered_output.append(msg)64 65    return filtered_output66 67# === Main loop ===68with open(INPUT_FILE, "r") as infile, open(OUTPUT_FILE, "w") as outfile:69    for line in tqdm(infile, desc=f"Analyzing {CODE_LABEL}"):70        item = json.loads(line)71        hm_index = item.get("hm_index")72        code = item.get(CODE_FIELD, "")73        if not code.strip():74            continue75 76        pylint_json = run_pylint_json(code)77        outfile.write(json.dumps({78            "hm_index": hm_index,79            "pylint_output": pylint_json80        }) + "\n")81 82print(f"Output saved to {OUTPUT_FILE}")