WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.6% of users enabled 2FA.

storage.py 2.14 KB
Newer Older
Zixian Cai's avatar
Zixian Cai committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/env python3
# Copyright 2017 The Australian National University
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Zixian Cai's avatar
Zixian Cai committed
15 16

from mubench.conf import settings
17 18 19
from pathlib import Path
import json
import os
Zixian Cai's avatar
Zixian Cai committed
20 21


22
class Storage:
Zixian Cai's avatar
Zixian Cai committed
23 24 25
    pass


26 27
class LocalStorage(Storage):
    STORAGE_PATH = Path(settings.WEB_RESULT_DIR)
Zixian Cai's avatar
Zixian Cai committed
28

29 30 31
    @classmethod
    def load(cls, repo, host, rev):
        dirname = cls.STORAGE_PATH / repo / host
Zixian Cai's avatar
Zixian Cai committed
32
        filename = dirname.glob("{}*".format(rev)).__next__()
33 34
        with open(filename) as logfile:
            return json.load(logfile)
Zixian Cai's avatar
Zixian Cai committed
35

36 37 38 39
    @classmethod
    def store(cls, repo, host, rev, timestamp, text_form):
        dirname = cls.STORAGE_PATH / repo / host
        filename = dirname / "{}_{}".format(rev, timestamp)
Zixian Cai's avatar
Zixian Cai committed
40
        os.makedirs(os.path.dirname(str(filename.absolute())), exist_ok=True)
41 42 43 44 45 46 47 48 49 50 51 52
        with open(filename, "w") as logfile:
            logfile.write(text_form)

    @classmethod
    def load_relative(cls, repo, host, num):
        dirname = cls.STORAGE_PATH / repo / host
        fs = sorted(
            [f.stem for f in dirname.iterdir() if not f.stem.startswith(".")],
            key=lambda x: int(x.split("_")[1]))
        filename = dirname / fs[-(1 + num)]
        with open(filename) as logfile:
            return json.load(logfile)
Zixian Cai's avatar
Zixian Cai committed
53 54 55 56 57 58 59 60 61 62 63 64

    @classmethod
    def load_all(cls, repo, host):
        dirname = cls.STORAGE_PATH / repo / host
        fs = sorted(
            [f.stem for f in dirname.iterdir() if not f.stem.startswith(".")],
            key=lambda x: int(x.split("_")[1]))
        results = []
        for f in fs:
            with open(dirname / f) as logfile:
                results.append(json.load(logfile))
        return results