adityanaikhpt commited on
Commit
928ea1f
·
verified ·
1 Parent(s): 0dfa258

Deploy: create_tasks.py

Browse files
Files changed (1) hide show
  1. create_tasks.py +92 -0
create_tasks.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ base_dir = "e:/meta/tasks"
5
+ os.makedirs(os.path.join(base_dir, "type_errors"), exist_ok=True)
6
+ os.makedirs(os.path.join(base_dir, "security_bugs"), exist_ok=True)
7
+
8
+ def write_task(folder, name, task_id, difficulty, desc, buggy, test):
9
+ py_path = os.path.join(base_dir, folder, f"{name}.py")
10
+ json_path = os.path.join(base_dir, folder, f"{name}.json")
11
+
12
+ py_content = f'''from server.models import TaskInfo
13
+
14
+ TASK = TaskInfo(
15
+ task_id="{task_id}",
16
+ difficulty="{difficulty}",
17
+ description="{desc}",
18
+ buggy_code="""{buggy}""",
19
+ test_code="""{test}""",
20
+ optimal_time_seconds=0.05
21
+ )
22
+ '''
23
+ with open(py_path, "w", encoding="utf-8") as f:
24
+ f.write(py_content)
25
+
26
+ json_content = {
27
+ "task_id": task_id,
28
+ "difficulty": difficulty,
29
+ "description": desc,
30
+ "buggy_code": buggy,
31
+ "test_code": test,
32
+ "optimal_time_seconds": 0.05
33
+ }
34
+ with open(json_path, "w", encoding="utf-8") as f:
35
+ json.dump(json_content, f, indent=2)
36
+
37
+
38
+ # Type Error 1
39
+ write_task("type_errors", "type_error_1", "type_errors-1", "type_errors",
40
+ "Fix the function to sum a list of numbers that might be passed as strings. It currently tries to add int and str.",
41
+ "def sum_all(items):\n total = 0\n for item in items:\n total = total + item\n return total",
42
+ "\nimport unittest\nclass TestTypeError1(unittest.TestCase):\n def test_normal(self):\n self.assertEqual(sum_all([1, 2, 3]), 6)\n def test_strings(self):\n self.assertEqual(sum_all(['1', '2', '3']), 6)\n def test_mixed(self):\n self.assertEqual(sum_all([1, '2', 3]), 6)\n")
43
+
44
+ # Type Error 2
45
+ write_task("type_errors", "type_error_2", "type_errors-2", "type_errors",
46
+ "Fix the function to count frequencies. It incorrectly calls .append() on a dict.",
47
+ "def count_frequencies(words):\n counts = {}\n for word in words:\n if word not in counts:\n counts.append({word: 1})\n else:\n counts[word] += 1\n return counts",
48
+ "\nimport unittest\nclass TestTypeError2(unittest.TestCase):\n def test_normal(self):\n self.assertEqual(count_frequencies(['apple', 'banana', 'apple']), {'apple': 2, 'banana': 1})\n def test_empty(self):\n self.assertEqual(count_frequencies([]), {})\n")
49
+
50
+ # Type Error 3
51
+ write_task("type_errors", "type_error_3", "type_errors-3", "type_errors",
52
+ "Fix the function to format names. It incorrectly calls .upper() on an int ID.",
53
+ "def format_records(records):\n formatted = []\n for user_id, name in records:\n formatted.append(f\"{user_id.upper()} - {name.upper()}\")\n return formatted",
54
+ "\nimport unittest\nclass TestTypeError3(unittest.TestCase):\n def test_normal(self):\n self.assertEqual(format_records([(1, 'alice'), (2, 'bob')]), ['1 - ALICE', '2 - BOB'])\n")
55
+
56
+
57
+ # Security Bug 1
58
+ write_task("security_bugs", "security_bug_1", "security_bugs-1", "security_bugs",
59
+ "Fix the function to parse JSON safely without using eval().",
60
+ "import json\ndef parse_user_data(data_string):\n return eval(data_string)",
61
+ "\nimport unittest\nimport inspect\nclass TestSecurity1(unittest.TestCase):\n def test_normal(self):\n self.assertEqual(parse_user_data('{\"name\": \"alice\"}'), {\"name\": \"alice\"})\n def test_security(self):\n source = inspect.getsource(parse_user_data)\n self.assertNotIn(\"eval(\", source)\n")
62
+
63
+ # Security Bug 2
64
+ write_task("security_bugs", "security_bug_2", "security_bugs-2", "security_bugs",
65
+ "Remove the hardcoded secret token and load it from the os.environ dictionary as 'API_TOKEN'.",
66
+ "import os\ndef get_api_token():\n token = \"secret_12345\"\n return token",
67
+ "\nimport unittest\nimport inspect\nimport os\nclass TestSecurity2(unittest.TestCase):\n def test_normal(self):\n os.environ['API_TOKEN'] = 'my_secure_token'\n self.assertEqual(get_api_token(), 'my_secure_token')\n def test_security(self):\n source = inspect.getsource(get_api_token)\n self.assertNotIn(\"secret_12345\", source)\n")
68
+
69
+ # Security Bug 3
70
+ write_task("security_bugs", "security_bug_3", "security_bugs-3", "security_bugs",
71
+ "Fix the ping command to avoid shell injection. Use a list of arguments and shell=False.",
72
+ "import subprocess\ndef ping_host(host):\n return subprocess.check_output(f\"ping -c 1 {host}\", shell=True)",
73
+ "\nimport unittest\nimport inspect\nclass TestSecurity3(unittest.TestCase):\n def test_security(self):\n source = inspect.getsource(ping_host)\n self.assertNotIn(\"shell=True\", source.replace(\" \", \"\"))\n self.assertIn(\"[\", source)\n")
74
+
75
+ # Rewrite __init__.py
76
+ init_content = """from .easy import EASY_TASK
77
+ from .medium import MEDIUM_TASK
78
+ from .hard import HARD_TASK
79
+ from .type_errors.type_error_1 import TASK as TE1
80
+ from .type_errors.type_error_2 import TASK as TE2
81
+ from .type_errors.type_error_3 import TASK as TE3
82
+ from .security_bugs.security_bug_1 import TASK as SB1
83
+ from .security_bugs.security_bug_2 import TASK as SB2
84
+ from .security_bugs.security_bug_3 import TASK as SB3
85
+
86
+ ALL_TASKS = [EASY_TASK, MEDIUM_TASK, HARD_TASK, TE1, TE2, TE3, SB1, SB2, SB3]
87
+ """
88
+
89
+ with open(os.path.join(base_dir, "__init__.py"), "w", encoding="utf-8") as f:
90
+ f.write(init_content)
91
+
92
+ print("Tasks generated successfully!")