-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrub_kpf_guide.py
More file actions
144 lines (109 loc) · 4.68 KB
/
Copy pathscrub_kpf_guide.py
File metadata and controls
144 lines (109 loc) · 4.68 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import logging
import argparse
import configparser
from datetime import datetime, timedelta
import scrubber_utils as utils
APP_PATH = os.path.abspath(os.path.dirname(__file__))
CONFIG_FILE = f'{APP_PATH}/scrub_sdata_config.live.ini'
def remove_guider_imgs(direct, utd2):
utd_obj = datetime.strptime(utd2, '%Y-%m-%d')
server = utils.get_config_param(config, 'servers', 'KPF')
cnt = 0
expected = 0
for root, dirs, files in os.walk(direct):
for file in files:
try:
file_path = os.path.join(root, file)
creation_time = os.path.getctime(file_path)
creation_date = datetime.fromtimestamp(creation_time)
except Exception as err:
log.warning(f'Error getting time: {err}')
continue
# uncomment to see what it is doing
if creation_date < utd_obj:
# remove_path = '/' + file_path.lstrip('/').lstrip('s')
# cmd = f'/bin/rm {remove_path}'
cmd = f'/bin/rm {file_path}'
if '.fits' not in cmd:
continue
cnt += remove_file(log, server, cmd, file_path)
expected += 1
return cnt, expected
def remove_file(log, server, cmd, local_path):
# remove_path = '/' + local_path.lstrip('/').lstrip('s')
try:
std_out = utils.execute_remote_cmd(server, cmd, account, pw)
except Exception as err:
log.warning(f'exception in executing remote command: {err}')
log.warning(f'error with command: {server} {cmd} {account} {pw}')
return 0
# check that it was removed locally (with /s)
if utils.chk_file_exists(local_path):
# log.error(f"File not removed, check path: {remove_path}")
log.error(f"File not removed, check path: {local_path}, "
f"ssh/rm output: {std_out}")
return 0
# log.info(f"File removed from: {remove_path}")
log.info(f"File removed from: {local_path}")
return 1
def setup_log(config):
if not args.logdir:
log_dir = utils.get_config_param(config, config_type, 'log_dir')
else:
log_dir = args.logdir
log_name, log_stream = utils.create_logger('kpf_guide_scrubber', log_dir)
log = logging.getLogger(log_name)
print(f'writing log to: {log_dir}/{log_name}')
return log
def parse_args(config, inst):
"""
Parse the command line arguments.
:return: <obj> commandline arguments
"""
now = datetime.now()
parser = argparse.ArgumentParser(description="Run the Data Scrubber")
parser.add_argument("--dev", action="store_true",
help="Only log the commands, do not execute")
parser.add_argument("--logdir", type=str,
help="Define the directory for the log.")
# add inst specific start/end ndays from the config if exist
args, unknown_args = parser.parse_known_args()
try:
start = int(utils.get_config_param(config, 'TIMEFRAME', f'{inst.lower()}_start'))
end = int(utils.get_config_param(config, 'TIMEFRAME', f'{args.inst.lower()}_end'))
except:
start = int(utils.get_config_param(config, 'TIMEFRAME', 'start'))
end = int(utils.get_config_param(config, 'TIMEFRAME', 'end'))
parser.add_argument("--utd", type=str,
default=(now - timedelta(days=start)).strftime('%Y-%m-%d'),
help="Start date to process YYYY-MM-DD.")
parser.add_argument("--utd2", type=str,
default=(now - timedelta(days=end)).strftime('%Y-%m-%d'),
help="End date to process YYYY-MM-DD.")
return parser.parse_args()
if __name__ == '__main__':
"""
to run:
python scrub_kpf_guide.py --utd 2021-02-11 --utd2 2021-02-12
"""
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
args = parse_args(config, 'KPF')
print(args)
if args.dev:
config_type = 'DEV'
else:
config_type = 'DEFAULT'
direct = utils.get_config_param(config, config_type, 'kpf_guide_dir')
account = utils.get_config_param(config, config_type, 'kpf_account')
pw = utils.get_config_param(config, 'passwords', 'eng_account')
log = setup_log(config)
log.info(f"Scrubbing kpfguider images created before: {args.utd2}\n")
log.info(f"directory: {direct}.\n")
cnt, expected_cnt = remove_guider_imgs(direct, args.utd2)
if cnt != expected_cnt:
report = f"The count from the KPF Guide Scrubber does not match, " \
f"expected to be deleted: {expected_cnt}, actually " \
f"deleted; {cnt}"
utils.write_emails(config, report, log, prefix=f'KPF GUIDE SDATA')