InstanceRunningException   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
c 0
b 0
f 0
dl 0
loc 2
rs 10
1
#################################################################
2
# MET v2 Metadate Explorer Tool
3
#
4
# This Software is Open Source. See License: https://github.com/TERENA/met/blob/master/LICENSE.md
5
# Copyright (c) 2012, TERENA All rights reserved.
6
#
7
# This Software is based on MET v1 developed for TERENA by Yaco Sistemas, http://www.yaco.es/
8
# MET v2 was developed for TERENA by Tamim Ziai, DAASI International GmbH, http://www.daasi.de
9
# Current version of MET has been revised for performance improvements by Andrea Biancini,
10
# Consortium GARR, http://www.garr.it
11
#########################################################################################
12
13
import sys, os
14
import logging
15
import logging.config
16
from optparse import OptionParser
17
18
os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs/'
19
20
current_directory = os.path.join(os.path.dirname(__file__), '..')
21
activate_this = os.path.join(current_directory, '..', 'met-venv/bin/activate_this.py')
22
execfile(activate_this, dict(__file__=activate_this))
23
24
sys.path.append(current_directory)
25
sys.path.append(os.path.join(current_directory, 'met'))
26
os.environ['DJANGO_SETTINGS_MODULE'] = 'met.settings'
27
28
import django
29
from met.metadataparser.refresh_metadata import refresh
30
31
django.setup()
32
33
class SingleRun(object):
34
    class InstanceRunningException(Exception):
35
        pass
36
37
    def __init__(self, lock_file):
38
        #define the lock file name
39
        self.lock_file =  "/tmp/%s.pid" % lock_file
40
41
    def __call__(self, func):
42
        def fnc(*args, **kwargs):
43
            if os.path.exists(self.lock_file):
44
                #get process id, if lock file exists
45
                pid = open(self.lock_file, "rt").read()
46
                if not os.path.exists("/proc/%s" % pid):
47
                    #if process is not alive remove the lock file
48
                    os.unlink(self.lock_file)
49
                else:
50
                    #process is running
51
                    raise self.InstanceRunningException(pid)
52
53
            try:
54
                #store process id
55
                open(self.lock_file, "wt").write(str(os.getpid()))
56
                #execute wrapped function
57
                func(*args,**kwargs)
58
            finally:
59
                if os.path.exists(self.lock_file):
60
                    os.unlink(self.lock_file)
61
        return fnc
62
63
class RefreshMetaData(object):
64
    @classmethod
65
    def process(cls, options):
66
        fed_name = options.fed_name
67
        force_refresh = options.force_refresh
68
        
69
        logger = None
70
        if options.log:
71
            logging.config.fileConfig(options.log)
72
            logger = logging.getLogger("Refresh")
73
    
74
        try:
75
            refresh(fed_name, force_refresh, logger)
76
        except Exception as e:
77
            if logger:
78
	        logger.error("%s" % e)
79
80
@SingleRun(lock_file="met-metadatarefresh")
81
def commandline_call(convert_class=RefreshMetaData):
82
    opt_parser = OptionParser()
83
    opt_parser.set_usage("refresh [--federation <fed_name>] [--log  <file>] [--force-refresh]")
84
    
85
    opt_parser.add_option(
86
        "-l",
87
        "--log",
88
        type="string",
89
        dest="log",
90
        help="The logger configuration file",
91
        default=None,
92
        metavar="LOG")
93
94
    opt_parser.add_option(
95
        "-f",
96
        "--federation",
97
        type="string",
98
        dest="fed_name",
99
        help="The federation to be updated (None for anyone)",
100
        default=None,
101
        metavar="FED")
102
103
    opt_parser.add_option(
104
        "-r",
105
        "--force-refresh",
106
        action="store_true",
107
        dest="force_refresh",
108
        help="Force refresh of metadata information (even if file has not changed)",
109
        metavar="REF")
110
111
    (options, _) = opt_parser.parse_args()
112
    
113
    error_message = ""
114
    if options.log and not os.path.exists(options.log):
115
        error_message = "File '%s' does not exist." % options.log
116
    
117
    if error_message:
118
        print(error_message)
119
        print(opt_parser.get_usage())
120
        exit (1)
121
    
122
    obj_convert = convert_class()
123
    obj_convert.process(options)
124
125
if __name__ == '__main__':
126
    log_args = {'level': logging.ERROR}
127
    logging.basicConfig(**log_args)
128
129
    commandline_call()
130