Skip to content

Commit

Permalink
Adds support for binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvi-ranawat committed Oct 30, 2023
1 parent d48fafc commit 3e5ee4e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion runner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import time
import os
from base64 import b64decode
from datetime import datetime
from abc import ABC, abstractmethod
from ase import db
Expand Down Expand Up @@ -481,7 +482,11 @@ def _write_run_data(self, atoms, tasks, files, status, log_msg):
"""
# write files
for i, string in files.items():
write_mode = "wb" if isinstance(string, bytes) else "w"
if string.startswith("data:application/octet-stream;base64,"):
write_mode = "wb"
string = b64decode(string[37:].encode())
else:
write_mode = "w"
with open(i, write_mode) as file_o:
file_o.write(string)

Expand Down
6 changes: 5 additions & 1 deletion runner/utils/runnerdata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Utility to handle runner data"""
import json
import os
from base64 import b64encode
from copy import copy

from runner.utils.utils import json_keys2int, get_db_connect
Expand Down Expand Up @@ -161,7 +162,10 @@ def add_file(self, filename, add_as=None):
# file is binary
with open(filename, "rb") as fio:
basename = os.path.basename(add_as)
self.data["files"][basename] = fio.read()
self.data["files"][basename] = (
"data:application/octet-stream;base64,"
+ b64encode(fio.read()).decode()
)

def add_files(self, filenames, add_as=None):
"""Adds files to runner data
Expand Down
7 changes: 6 additions & 1 deletion tests/test_baserunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
def main(atoms, conf=0):
assert conf == 0, 'bad params'
assert isinstance(atoms[0], Atoms), 'bad atoms'
with open("test.bin", "rb") as fio:
assert fio.read() == b"\\xff;:"
return atoms
"""
runner = {
Expand All @@ -24,7 +26,10 @@ def main(atoms, conf=0):
["python", "energy.py", {"conf": 0}],
["python", "energy.py", {"conf": 0}, "python3"],
],
"files": {"energy.py": energy},
"files": {
"energy.py": energy,
"test.bin": "data:application/octet-stream;base64,/zs6",
},
}


Expand Down
5 changes: 5 additions & 0 deletions tests/test_runnerdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,11 @@ def test_to_from_data():

with db.connect("database.db") as fdb:
fdb.write(Atoms())
with open("test.bin", "wb") as fio:
fio.write(b"\xff;:")

runner = RunnerData.from_data_dict(success)
runner.add_file("test.bin")
runner.to_db("database.db", 1)
runner.to_json("database.json")
runner = RunnerData.from_db("database.db", 1)
Expand All @@ -271,6 +274,7 @@ def test_to_from_data():
assert parents[0] == 1 and parents[1] == 2
assert isinstance(scheduler_options, dict)
assert isinstance(files, dict)
assert files["test.bin"] == "data:application/octet-stream;base64,/zs6"
assert "energy.py" in files
assert len(tasks) == 4
runner = RunnerData.from_json("database.json")
Expand All @@ -280,5 +284,6 @@ def test_to_from_data():
assert parents[0] == 1 and parents[1] == 2
assert isinstance(scheduler_options, dict)
assert isinstance(files, dict)
assert files["test.bin"] == "data:application/octet-stream;base64,/zs6"
assert "energy.py" in files
assert len(tasks) == 4

0 comments on commit 3e5ee4e

Please sign in to comment.