...
 
Commits (18)
#! /usr/bin/env python3
# Copyright 2019 Stichting Nederlandse Wetenschappelijk Onderzoek Instituten,
# ASTRON Netherlands Institute for Radio Astronomy
# Licensed under the Apache License, Version 2.0 (the "License");
......@@ -20,10 +22,11 @@
# !! This is to ensure that when we change the remote interface, your scripts don't break and you will only have to
# !! upgrade this module.
__version__ = "1.4"
__version__ = "1.5"
import datetime
from os.path import expanduser
import configparser
from os.path import expanduser, exists
# Python2/3 dependent stuff
......@@ -36,32 +39,51 @@ else:
import xmlrpclib
string_types = basestring
def parse_config_file(file):
""" Parse an ini file that shall contain user credentials
for the LOFAR stager API.
"""
if exists(file) is False:
# File not found, nothing to do.
raise Exception
with open(file, 'r') as stream:
print("%s - stager_access: Parsing user credentials found in file \"%s\"." % (datetime.datetime.now(), file))
config = configparser.ConfigParser()
# This may seem odd but the Python configparser cannot parse ini
# files that just contain "A = B" lines without a section name
# like "[Foo Bar]". So I just add a section name just before the
# config file is read.
config.read_string("[LOFAR stager credentials]\n" + stream.read())
return config["LOFAR stager credentials"]
#---
# Determine credentials and create proxy
user = None
passw = None
try:
f = expanduser("~/.awe/Environment.cfg")
with open(f,'r') as file:
print("%s - stager_access: Parsing user credentials from \"%s\"" % (datetime.datetime.now(), f))
for line in file:
if line.startswith("database_user"):
user = line.split(':')[1].strip()
if line.startswith("database_password"):
passw = line.split(':')[1].strip()
except IOError:
f = expanduser("~/.stagingrc")
with open(f,'r') as file:
print("%s - stager_access: Parsing user credentials from \"%s\"" % (datetime.datetime.now(), f))
for line in file:
if line.startswith("user"):
user = line.split('=')[1].strip()
if line.startswith("password"):
passw = line.split('=', 1)[1].strip()
print("%s - stager_access: Creating proxy" % (datetime.datetime.now()))
proxy = xmlrpclib.ServerProxy("https://"+user+':'+passw+"@webportal.astron.nl/service-public/xmlrpc")
LtaStager = proxy.LtaStager
# Iterate over possible files that could contain user credentials.
for f in ["~/.awe/Environment.cfg", "~/.stagingrc"]:
file = expanduser(f)
credentials = []
try:
credentials = parse_config_file(file)
except Exception:
print("%s - stager_access: Could not find user credential file \"%s\"." % (datetime.datetime.now(), file))
continue
user = credentials["user"]
passw = credentials["password"]
break
if user is not None and passw is not None:
print("%s - stager_access: Creating proxy" % (datetime.datetime.now()))
proxy = xmlrpclib.ServerProxy("https://"+user+':'+passw+"@webportal.astron.nl/service-public/xmlrpc")
LtaStager = proxy.LtaStager
else:
print("%s - stager_access: Could not find a file with user credentials." % (datetime.datetime.now()))
# ---
......