Skip to content

Commit 8817d88

Browse files
Add logging, pre-commit hooks, fix setup.py incorrect paths (#141)
* Add in root.pem. change quickstart dir to playground * Add logging; add pre-commit hooks; fix setup.py incorrect paths
1 parent 31cb6d2 commit 8817d88

File tree

18 files changed

+538
-414
lines changed

18 files changed

+538
-414
lines changed

.clang-format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
MaxEmptyLinesToKeep: 2
2+
IndentWidth: 4
3+
NamespaceIndentation: All

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
ignore = E203, E266, E501, W503, W605, F403, F401, C901
3+
max-line-length = 79
4+
max-complexity = 18
5+
select = B,C,E,F,W,T4,B9

.pre-commit-config.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
repos:
2+
- repo: https://github.com/ambv/black
3+
rev: 21.6b0
4+
hooks:
5+
- id: black
6+
language_version: python3
7+
- repo: https://gitlab.com/pycqa/flake8
8+
rev: 3.9.2
9+
hooks:
10+
- id: flake8
11+
- repo: https://github.com/pocc/pre-commit-hooks
12+
rev: v1.1.1
13+
hooks:
14+
- id: clang-format
15+
exclude: ^(src/include/(csv.hpp|json.hpp|base64.h))
16+
args:
17+
- -i
18+
- --style=file
19+
- --fallback-style=Chromium
20+

mc2.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import logging
23
import os
34
import pathlib
45
import shutil
@@ -10,6 +11,13 @@
1011

1112
from envyaml import EnvYAML
1213

14+
# Configure logging
15+
logging.basicConfig(
16+
format="%(asctime)s - %(levelname)s - %(message)s",
17+
level=logging.INFO,
18+
datefmt="%Y-%m-%d %H:%M:%S",
19+
)
20+
logging.Formatter.converter = time.gmtime
1321

1422
parser = argparse.ArgumentParser()
1523
subparsers = parser.add_subparsers(help="Command to run.", dest="command")
@@ -20,19 +28,15 @@
2028
)
2129

2230
# -------------Launch------------------
23-
parser_launch = subparsers.add_parser(
24-
"launch", help="Launch Azure resources"
25-
)
31+
parser_launch = subparsers.add_parser("launch", help="Launch Azure resources")
2632

2733
# -------------Start-------------------
2834
parser_start = subparsers.add_parser(
2935
"start", help="Start services using specificed start up commands"
3036
)
3137

3238
# -------------Upload----------------
33-
parser_upload = subparsers.add_parser(
34-
"upload", help="Encrypt and upload data."
35-
)
39+
parser_upload = subparsers.add_parser("upload", help="Encrypt and upload data.")
3640

3741
# -------------Run--------------------
3842
parser_run = subparsers.add_parser(
@@ -45,19 +49,17 @@
4549
)
4650

4751
# -------------Stop-------------------
48-
parser_stop = subparsers.add_parser(
49-
"stop", help="Stop previously started service"
50-
)
52+
parser_stop = subparsers.add_parser("stop", help="Stop previously started service")
5153

5254
# -------------Teardown---------------
53-
parser_teardown = subparsers.add_parser(
54-
"teardown", help="Teardown Azure resources"
55-
)
55+
parser_teardown = subparsers.add_parser("teardown", help="Teardown Azure resources")
5656

5757
if __name__ == "__main__":
5858
oc_config = os.environ.get("MC2_CONFIG")
5959
if not oc_config:
60-
raise Exception("Please set the environment variable `MC2_CONFIG` to the path of your config file")
60+
raise Exception(
61+
"Please set the environment variable `MC2_CONFIG` to the path of your config file"
62+
)
6163

6264
mc2.set_config(oc_config)
6365
args = parser.parse_args()
@@ -75,8 +77,10 @@
7577

7678
# If the nodes have been manually specified, don't do anything
7779
if config_launch.get("head") or config_launch.get("workers"):
78-
print("Node addresses have been manually specified in the config "\
79-
"... doing nothing")
80+
logging.warning(
81+
"Node addresses have been manually specified in the config "
82+
"... doing nothing"
83+
)
8084
quit()
8185

8286
# Create resource group
@@ -123,16 +127,16 @@
123127

124128
encrypted_data = [d + ".enc" for d in data]
125129

126-
print("Encrypting and uploading data...")
127-
128130
dst_dir = config_upload.get("dst", "")
129131
for i in range(len(data)):
130132
# Encrypt data
131133
if enc_format == "xgb":
132134
mc2.encrypt_data(data[i], encrypted_data[i], None, "xgb")
133135
elif enc_format == "sql":
134136
if schemas is None:
135-
raise Exception("Please specify a schema when uploading data for Opaque SQL")
137+
raise Exception(
138+
"Please specify a schema when uploading data for Opaque SQL"
139+
)
136140
# Remove temporary files from a previous run
137141
if os.path.exists(encrypted_data[i]):
138142
if os.path.isdir(encrypted_data[i]):
@@ -150,7 +154,6 @@
150154
if dst_dir:
151155
remote_path = os.path.join(dst_dir, filename)
152156
mc2.upload_file(encrypted_data[i], remote_path, use_azure)
153-
print("Uploaded data to {}".format(remote_path))
154157

155158
# Remove temporary directory
156159
if os.path.isdir(encrypted_data[i]):
@@ -163,7 +166,7 @@
163166
script = config_run["script"]
164167

165168
if config_run["compute"] == "xgb":
166-
print("run() unimplemented for secure-xgboost")
169+
logging.error("run() unimplemented for secure-xgboost")
167170
quit()
168171
elif config_run["compute"] == "sql":
169172
mc2.configure_job(config)
@@ -183,8 +186,6 @@
183186
remote_results = config_download.get("src", [])
184187
local_results_dir = config_download["dst"]
185188

186-
print("Downloading and decrypting data")
187-
188189
# Create the local results directory if it doesn't exist
189190
if not os.path.exists(local_results_dir):
190191
pathlib.Path(local_results_dir).mkdir(parents=True, exist_ok=True)
@@ -195,15 +196,12 @@
195196

196197
# Fetch file
197198
mc2.download_file(remote_result, local_result, use_azure)
198-
print("Downloaded result to ", local_result)
199199

200200
# Decrypt data
201201
if enc_format == "xgb":
202202
mc2.decrypt_data(local_result, local_result + ".dec", "xgb")
203-
print("Decrypted result saved to ", local_result + ".dec")
204203
elif enc_format == "sql":
205204
mc2.decrypt_data(local_result, local_result + ".dec", "sql")
206-
print("Decrypted result saved to ", local_result + ".dec")
207205
else:
208206
raise Exception("Specified format {} not supported".format(enc_format))
209207

@@ -213,16 +211,18 @@
213211
os.remove(local_result)
214212

215213
elif args.command == "stop":
216-
print("Currently unsupported")
214+
logging.error("`opaque stop` is currently unsupported")
217215
pass
218216

219217
elif args.command == "teardown":
220218
config_teardown = config["teardown"]
221219

222220
# If the nodes have been manually specified, don't do anything
223221
if config["launch"].get("head") or config["launch"].get("workers"):
224-
print("Node addresses have been manually specified in the config "\
225-
"... doing nothing")
222+
logging.warning(
223+
"Node addresses have been manually specified in the config "
224+
"... doing nothing"
225+
)
226226
quit()
227227

228228
delete_container = config_teardown.get("container")
@@ -240,3 +240,8 @@
240240
delete_resource_group = config_teardown.get("resource_group")
241241
if delete_resource_group:
242242
mc2.delete_resource_group()
243+
244+
else:
245+
logging.error(
246+
"Unsupported command specified. Please type `opaque -h` for a list of supported commands."
247+
)

0 commit comments

Comments
 (0)