Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
lofar_stager_api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ASTRON SDC
lofar_stager_api
Commits
b60d83bf
Commit
b60d83bf
authored
5 years ago
by
Thomas Jürges
Browse files
Options
Downloads
Patches
Plain Diff
ROHD-2105
: Use Python's configparser to extract credentials
parent
6076a841
No related branches found
Branches containing commit
Tags
1.5
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
stager_access.py
+43
-23
43 additions, 23 deletions
stager_access.py
with
43 additions
and
23 deletions
stager_access.py
+
43
−
23
View file @
b60d83bf
...
...
@@ -25,7 +25,8 @@
__version__
=
"
1.5
"
import
datetime
from
os.path
import
expanduser
import
configparser
from
os.path
import
expanduser
,
exists
# Python2/3 dependent stuff
...
...
@@ -38,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
:
p
rint
(
"
%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
"
]
p
assw
=
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
())
)
# ---
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment