aboutsummaryrefslogtreecommitdiffstats
path: root/python/citco_submission.py
blob: 9e636599dbfeb4b2a3a14a507a5e6f466469ce2c (plain)
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
from stat import S_ISREG
import time
import pickle
from serenitas.utils.remote import SftpClient
from citco_ops.utils import CitcoSubmission


def run():
    sftp = SftpClient.from_creds("citco", folder="/outgoing/notifications")
    while True:
        try:
            with open("citco.pickle", "rb") as fh:
                already_uploaded = pickle.load(fh)
        except FileNotFoundError:
            already_uploaded = {}
        try:
            for f in sftp.client.listdir_iter():
                if S_ISREG(f.st_mode):
                    if f.filename not in already_uploaded:
                        _insert_queue = []
                        with sftp.client.open(f.filename) as fh:
                            CitcoSubmission.process(fh, f.filename)
                        CitcoSubmission.commit()
                        already_uploaded[f.filename] = None
            with open("citco.pickle", "wb") as fh:
                pickle.dump(already_uploaded, fh)
        except (SSHException, OSError):
            sftp.client.close()
            sftp = SftpClient.from_creds("citco", folder="/outgoing/notifications")
        time.sleep(60)


if __name__ == "__main__":
    run()