Skip to content

Commit

Permalink
refactor: using methods for sourcing the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
fstagni committed Jul 31, 2023
1 parent 6a185cf commit 7ea2343
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
78 changes: 48 additions & 30 deletions Pilot/pilotCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def __init__(self, pilotParams):
from __future__ import division
from __future__ import absolute_import

__RCSID__ = "$Id$"

import sys
import os
import time
Expand Down Expand Up @@ -201,6 +199,41 @@ def __init__(self, pilotParams):
self.installScriptName = "dirac-install.py"
self.installScript = ""

def _sourceEnvironmentFile(self):
"""source the $DIRAC_RC_FILE and save the created environment in self.pp.installEnv
"""

retCode, output = self.executeAndGetOutput("bash -c 'source $DIRAC_RC_PATH && env'", self.pp.installEnv)
if retCode:
self.log.error("Could not parse the %s file [ERROR %d]" % (self.pp.installEnv["DIRAC_RC_PATH"], retCode))
self.exitWithError(retCode)
for line in output.split("\n"):
try:
var, value = [vx.strip() for vx in line.split("=", 1)]
if var == "_" or "SSH" in var or "{" in value or "}" in value: # Avoiding useless/confusing stuff
continue
self.pp.installEnv[var] = value
except (IndexError, ValueError):
continue

def _saveEnvInFile(self, eFile="environmentSourceDirac"):
"""Save pp.installEnv in file (delete if already present)
:param str eFile: file where to save env
"""
if os.path.isfile(eFile):
os.remove(eFile)

with open(eFile, "w") as fd:
for var, val in self.pp.installEnv.items():
if var == "_" or var == "X509_USER_PROXY" or "SSH" in var or "{" in val or "}" in val:
continue
if " " in val and val[0] != '"':
val = '"%s"' % val
bl = "export %s=%s\n" % (var, val.rstrip(":"))
fd.write(bl)


def _setInstallOptions(self):
"""Setup installation parameters"""

Expand Down Expand Up @@ -257,7 +290,10 @@ def _locateInstallationScript(self):
pass

def _installDIRAC(self):
"""Install DIRAC or its extension, then parse the environment file created, and use it for subsequent calls"""
""" Install python2 DIRAC or its extension,
then parse the environment file created, and use it for subsequent calls
"""

# Installing
installCmd = "%s %s" % (self.installScript, " ".join(self.installOpts))
self.log.debug("Installing with: %s" % installCmd)
Expand All @@ -273,19 +309,11 @@ def _installDIRAC(self):

# Parsing the bashrc then adding its content to the installEnv
# at this point self.pp.installEnv may still coincide with os.environ
retCode, output = self.executeAndGetOutput('bash -c "source bashrc && env"', self.pp.installEnv)
if retCode:
self.log.error("Could not parse the bashrc file [ERROR %d]" % retCode)
self.exitWithError(retCode)
for line in output.split("\n"):
try:
var, value = [vx.strip() for vx in line.split("=", 1)]
if var == "_" or "SSH" in var or "{" in value or "}" in value: # Avoiding useless/confusing stuff
continue
self.pp.installEnv[var] = value
except (IndexError, ValueError):
continue
self.pp.installEnv["DIRAC_RC_PATH"] = os.path.join(os.getcwd(), "bashrc")

self._sourceEnvironmentFile()
# At this point self.pp.installEnv should contain all content of bashrc, sourced "on top" of (maybe) os.environ
self._saveEnvInFile()

def _installDIRACpy3(self):
"""Install python3 version of DIRAC client"""
Expand Down Expand Up @@ -353,21 +381,11 @@ def _installDIRACpy3(self):
with open("diracos/diracosrc", "a") as diracosrc:
diracosrc.write("\n".join(lines))

# 6. source diracos/diracosrc then replace the content in installEnv
retCode, output = self.executeAndGetOutput('bash -c "source diracos/diracosrc && env"', installEnv)
self.pp.installEnv = {}
if retCode:
self.log.error("Could not parse the diracos/diracosrc file [ERROR %d]" % retCode)
self.exitWithError(retCode)
for line in output.split("\n"):
try:
var, value = [vx.strip() for vx in line.split("=", 1)]
if var == "_" or "SSH" in var or "{" in value or "}" in value: # Avoiding useless/confusing stuff
continue
self.pp.installEnv[var] = value
except (IndexError, ValueError):
continue

# 6. source diracos/diracosrc
self.pp.installEnv["DIRAC_RC_PATH"] = os.path.join(os.getcwd(), "diracos/diracosrc")
self._sourceEnvironmentFile()
self._saveEnvInFile()

# 7. pip install DIRAC[pilot]
pipInstalling = "pip install %s " % self.pp.pipInstallOptions

Expand Down
2 changes: 1 addition & 1 deletion Pilot/pilotTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def executeAndGetOutput(self, cmd, environDict=None):

self.log.info("Executing command %s" % cmd)
_p = subprocess.Popen(
"%s" % cmd, shell=True, env=environDict, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False
cmd, shell=True, env=environDict, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False
)

# Use non-blocking I/O on the process pipes
Expand Down

0 comments on commit 7ea2343

Please sign in to comment.