File size: 2,078 Bytes
065fa03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import os
import json
import logging
import argparse
from pathlib import Path


directories = [
        "other",
        "rfcs",
        "discussions",
        "papers"
        ]


def setup_logging():
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s',
    )


def process_file(file_path):
    try:
        with open(file_path, "r") as file:
            return file.read()
    except Exception as e:
        logging.error(f"Failed to process file {file_path}: {e}")
        return None


def create_misc_ds(augmentdir, ds):
    lines = []
    id_ = 0

    for directory in directories:
        directory_path = Path(os.path.join(augmentdir, directory))
        if not directory_path.exists() or not directory_path.is_dir():
            logging.warning(f"Skipping invalid or non-existent directory_path: {directory_path}")
            continue

        for filename in os.listdir(directory_path):
            file_path = directory_path / filename
            if file_path.is_file():
                content = process_file(file_path)
                if content is None:
                    continue

                new_entry = {
                    "repo_name": "__dummy_name",
                    "repo_path": "__dummy_path",
                    "id": id_,
                    "content": content
                }
                id_ += 1
                lines.append(json.dumps(new_entry) + "\n")

    try:
        with open(ds, "w") as fout:
            fout.writelines(lines)
    except Exception as e:
        logging.error(f"Failed to write dataset to {ds}: {e}")


def main():
    parser = argparse.ArgumentParser(description="Process files from directories and create a JSON dataset.")
    parser.add_argument("--dir", required=True, help="Augmentation directory")
    parser.add_argument("-o", "--output", required=True, help="Output JSON file path")

    setup_logging()
    args = parser.parse_args()
    create_misc_ds(args.dir, args.output)


if __name__ == "__main__":
    main()