forked from dmwm/cms-htcondor-es
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffiliation_cache.py
60 lines (54 loc) · 2.01 KB
/
affiliation_cache.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Christian Ariza <christian.ariza AT gmail [DOT] com>
# This script uses the AffiliationManager class to create or update
# the affiliation cache file (an indexed structure of username/login
# and affiliation institution and country from cric data)
# How to run:
# python affiliation_cache.py
# This will create a file in the users' home called .affiliation_dir.json,
# if the file already exists it will overwrite it if and only if it's older
# than one day. You can specify the location and the recreation period using
# the optional parameters:
# python affiliation_cache.py --output /htcondor_es/aff_dir.json --days 3
# Thiis script can be setup as a daily cronjob and use the parameters to modify
# how often the script is updated.
import os
import argparse
import traceback
from htcondor_es.AffiliationManager import (
AffiliationManager,
AffiliationManagerException,
)
def generate_affiliation_cache(output_file, days=1):
"""
Update the cache file if older than a given number of days.
"""
try:
AffiliationManager(recreate_older_days=days, dir_file=output_file)
except AffiliationManagerException as e:
traceback.print_exc()
print("There was an error creating the affiliation manager")
if __name__ == "__main__":
output_file = os.getenv(
"AFFILIATION_DIR_LOCATION",
AffiliationManager._AffiliationManager__DEFAULT_DIR_PATH,
)
parser = argparse.ArgumentParser()
parser.add_argument(
"--output",
help="""location of the affiliation cache file
By default, it will use the AFFILIATION_DIR_LOCATION env variable if set
or the users home as default.""",
default=None,
)
parser.add_argument(
"--days",
help="How often should the file be updated? (days)",
type=int,
default=1,
)
args = parser.parse_args()
if args.output:
output_file = args.output
generate_affiliation_cache(output_file, args.days)