forked from picoCTF/picoCTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundles.py
98 lines (74 loc) · 2.35 KB
/
bundles.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Module for interacting with bundles."""
from voluptuous import Required, Schema
import api
from api import check, validate
bundle_schema = Schema(
{
Required("author"): check(("The bundle author must be a string.", [str])),
Required("name"): check(("The bundle name must be a string.", [str])),
Required("description"): check(
("The bundle description must be a string.", [str])
),
"dependencies": check(("The bundle dependencies must be a dict.", [dict])),
"dependencies_enabled": check(
(
"The dependencies enabled state must be a bool.",
[lambda x: type(x) == bool],
)
),
}
)
def get_bundle(bid):
"""
Return the bundle dict corresponding to the given bid.
Args:
bid: bundle ID
Returns:
The associated bundle dict, or None if not found
"""
db = api.db.get_conn()
return db.bundles.find_one({"bid": bid}, {"_id": 0})
def get_all_bundles():
"""Get all bundles."""
db = api.db.get_conn()
return list(db.bundles.find({}, {"_id": 0}))
def upsert_bundle(bundle):
"""
Add or update a bundle.
Args:
bundle: bundle dict
Returns:
The created/updated bundle ID.
"""
# Validate the bundle object
validate(bundle_schema, bundle)
db = api.db.get_conn()
bid = api.common.hash("{}-{}".format(bundle["name"], bundle["author"]))
# If the bundle already exists, update it instead
existing = db.bundles.find_one({"bid": bid}, {"_id": 0})
if existing is not None:
db.bundles.find_one_and_update({"bid": bid}, {"$set": bundle})
return bid
bundle["bid"] = bid
bundle["dependencies_enabled"] = False
db.bundles.insert(bundle)
return bid
def set_bundle_dependencies_enabled(bid, enabled):
"""
Set a bundle's dependencies_enabled field.
This will affect the unlocked problems.
Args:
bid: the bundle id
enabled: whether to enable the bundle's dependencies
Returns:
The bid of the updated bundle, or None if it could not be found.
"""
db = api.db.get_conn()
success = db.bundles.find_one_and_update(
{"bid": bid}, {"$set": {"dependencies_enabled": enabled}}
)
if not success:
return None
else:
api.cache.clear()
return bid