-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_uploads.py
90 lines (71 loc) · 2.98 KB
/
check_uploads.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
import datetime
import re
from string import Template
from pathlib import Path
from ftplib import FTP_TLS
from ftplib import all_errors as ftp_errors
from SendMail import missing_schedule, network_changed
import skd_parser.skd as skd_parser
from Helper import read_emails, read_master
def check_uploads(settings, fallback_email=""):
today = datetime.date.today()
root = Path("tmp_download")
root.mkdir(exist_ok=True, parents=True)
for program in settings.sections():
if program == "general":
continue
if program.startswith("GOW"):
continue
if settings[program].get("upload").lower() != "ivs":
continue
s_program = settings[program]
pattern = re.compile(s_program["pattern"])
emails = read_emails(s_program, fallback_email)
delta_days = s_program.getint("upload_date", 10)
for dt in range(delta_days):
target_day = today + datetime.timedelta(days=dt)
year = target_day.year % 100
year_long = target_day.year
# read master files
template_master = Template(s_program.get("master", "master$YY.txt"))
master = template_master.substitute(YY=str(year), YYYY=str(year_long))
master = Path("MASTER") / master
sessions = read_master(master)
if not sessions:
continue
sessions = [s for s in sessions if s["date"].date() == target_day and pattern.match(s["name"])]
for s in sessions:
name = f"{s['code'].lower()}.skd"
folder = f"pub/vlbi/ivsdata/aux/{2000 + year}/{s['code'].lower()}/"
# download skd file
try:
# connect to FTP server
ftp = FTP_TLS("ivs.bkg.bund.de", user="anonymous", passwd="anonymous")
ftp.prot_p()
ftp.set_pasv(True)
ftp.cwd(folder)
out = root / name
msg = ftp.retrbinary("RETR " + name, open(out, 'wb').write)
except ftp_errors as err:
missing_schedule(s, program, emails)
continue
# read .skd file to get network
skd = skd_parser.skdParser(out)
skd.parse()
skd_stations = [s.name for s in skd.stations]
master_stations = s["stations"]
# check if networks are the same
network_same = True
for sta in master_stations:
if sta not in skd_stations:
network_same = False
for sta in skd_stations:
if sta not in master_stations:
network_same = False
# if not write email
if not network_same:
network_changed(s, program, skd_stations, emails)
# delete all downloads
for f in root.iterdir():
f.unlink()
pass