Skip to content

Commit

Permalink
Added function cleanLast in run-*_container.sh; and sort releases lis…
Browse files Browse the repository at this point in the history
…ting in run-atlas_container.sh
  • Loading branch information
Shuwei Ye authored and Shuwei Ye committed Oct 26, 2023
1 parent c4f3b3e commit 3fb83e5
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 26 deletions.
97 changes: 72 additions & 25 deletions run-atlas_container.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
# coding: utf-8
# version=2023-10-25-beta02
# version=2023-10-26-beta01
# author: Shuwei Ye <yesw@bnl.gov>
"true" '''\'
myScript="${BASH_SOURCE:-$0}"
Expand Down Expand Up @@ -60,7 +60,7 @@ import subprocess
from time import sleep
import fnmatch
from shutil import which
from shutil import which, rmtree
from subprocess import getstatusoutput
from urllib.request import urlopen, Request
Expand All @@ -71,6 +71,27 @@ ATLAS_PROJECTS = ['athena', 'analysisbase', 'athanalysis', 'analysistop', 'athan
DOCKERHUB_REPO = "https://hub.docker.com/v2/repositories/atlas"
class Version(str):
def __lt__(self, other):
va = re.split('[.-]', self)
vb = re.split('[.-]', other)
# print("va=",va, "; vb=",vb)
for i in range(min(len(va), len(vb))):
ai = va[i]
bi = vb[i]
if ai.isdigit() and bi.isdigit():
if ai != bi:
return int(ai) < int(bi)
elif ai != bi:
return ai < bi
return len(va) < len(vb)
def set_default_subparser(parser, default_subparser, index_action=1):
"""default subparser selection. Call after setup, just before parse_args()
Expand Down Expand Up @@ -225,6 +246,7 @@ def listReleases(args):
for tagKey in imageTags.keys():
if fnmatch.fnmatch(tagKey, release):
tags += [ tagKey ]
tags.sort(key=Version)
if len(tags) > 0:
pp = pprint.PrettyPrinter(indent=4, compact=True)
print("Found the following release container list for the project=", project, releasePrint)
Expand Down Expand Up @@ -431,6 +453,52 @@ eval $stopCmd
shellFile.close()
def getMyImageInfo(filename):
shellFile = open(filename, 'r')
myImageInfo = {}
for line in shellFile:
if re.search(r'^(cont|image|docker|sand|runOpt|lines).*=', line):
key, value = line.strip().split('=')
myImageInfo[key] = value
return myImageInfo
def printMe(args):
if not os.path.exists(args.shellFilename):
print("No previous container/sandbox setup is found")
return None
myImageInfo = getMyImageInfo(args.shellFilename)
contCmd = myImageInfo["contCmd"]
if "runOpt" in myImageInfo:
myImageInfo.pop("runOpt")
pp = pprint.PrettyPrinter(indent=4)
print("The image/container used in the current work directory:")
pp.pprint(myImageInfo)
def cleanLast(filename):
if not os.path.exists(filename):
return
myImageInfo = getMyImageInfo(filename)
if len(myImageInfo) > 0:
contCmd = myImageInfo['contCmd']
if contCmd == 'singularity' or contCmd == 'apptainer':
sandboxPath = myImageInfo['sandboxPath']
print("Removing the last sandbox=", sandboxPath)
try:
rmtree(sandboxPath)
except:
pass
os.rename(filename, filename + '.last')
else:
contName = myImageInfo['contName']
print("Removing the last container=", contName)
rmCmd = "%s rm -f %s" % (contCmd, contName)
run_shellCmd(rmCmd, exitOnFailure=False)
os.rename(filename, filename + '.last')
def setup(args):
releaseTags = parseArgTags(args.tags, requireRelease=True)
project = releaseTags['project']
Expand All @@ -451,6 +519,8 @@ def setup(args):
print("Please install one of the above tool first")
sys.exit(1)
cleanLast(args.shellFilename)
contCmd = contCmds[0]
if args.contCmd is not None:
if args.contCmd in contCmds:
Expand Down Expand Up @@ -480,29 +550,6 @@ def setup(args):
sleep(1)
def getMyImageInfo(filename):
shellFile = open(filename, 'r')
myImageInfo = {}
for line in shellFile:
if re.search(r'^(cont|image|docker|sand|runOpt|lines).*=', line):
key, value = line.strip().split('=')
myImageInfo[key] = value
return myImageInfo
def printMe(args):
if not os.path.exists(args.shellFilename):
print("No previous container/sandbox setup is found")
return None
myImageInfo = getMyImageInfo(args.shellFilename)
contCmd = myImageInfo["contCmd"]
if "runOpt" in myImageInfo:
myImageInfo.pop("runOpt")
pp = pprint.PrettyPrinter(indent=4)
print("The image/container used in the current work directory:")
pp.pprint(myImageInfo)
def jupyter(args):
if not os.path.exists(args.shellFilename):
print("No previous container/sandbox setup is found")
Expand Down
62 changes: 61 additions & 1 deletion run-ml_container.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
# coding: utf-8
# version=2023-10-25-r01
# version=2023-10-26-r01
"true" '''\'
myScript="${BASH_SOURCE:-$0}"
ret=0
Expand Down Expand Up @@ -503,6 +503,64 @@ eval $runCmd
shellFile.close()
def getMyImageInfo(filename):
shellFile = open(filename, 'r')
myImageInfo = {}
for line in shellFile:
if re.search(r'^(cont|image|docker|sand|runOpt|lines).*=', line):
key, value = line.strip().split('=')
myImageInfo[key] = value
return myImageInfo
def printMe(args):
if not os.path.exists(args.shellFilename):
print("No previous container/sandbox setup is found")
return None
myImageInfo = getMyImageInfo(args.shellFilename)
contCmd = myImageInfo["contCmd"]
linesCondaHistory = myImageInfo.pop("linesCondaHistory")
if "runOpt" in myImageInfo:
myImageInfo.pop("runOpt")
pp = pprint.PrettyPrinter(indent=4)
print("The image/container used in the current work directory:")
pp.pprint(myImageInfo)
if contCmd == 'singularity':
contNamePath = myImageInfo["sandboxPath"]
else:
contNamePath = myImageInfo["contName"]
pkgs, channels = listNewPkgs(contCmd, contNamePath, linesCondaHistory)
if len(pkgs) > 0:
print("\nThe following additional pkgs and their dependencies are installed")
pp.pprint(pkgs)
if len(channels) > 0:
print("\nThe following channels besides the default channel 'conda-forge' are needed")
pp.pprint(channels)
def cleanLast(filename):
if not os.path.exists(filename):
return
myImageInfo = getMyImageInfo(filename)
if len(myImageInfo) > 0:
contCmd = myImageInfo['contCmd']
if contCmd == 'singularity' or contCmd == 'apptainer':
sandboxPath = myImageInfo['sandboxPath']
print("Removing the last sandbox=", sandboxPath)
try:
rmtree(sandboxPath)
except:
pass
os.rename(filename, filename + '.last')
else:
contName = myImageInfo['contName']
print("Removing the last container=", contName)
rmCmd = "%s rm -f %s" % (contCmd, contName)
run_shellCmd(rmCmd, exitOnFailure=False)
os.rename(filename, filename + '.last')
def setup(args):
images = list_FoundImages(args.name)
if len(images) == 0:
Expand Down Expand Up @@ -531,6 +589,8 @@ def setup(args):
print("Please install one of the above tool first")
sys.exit(1)
cleanLast(args.shellFilename)
contCmd = contCmds[0]
if args.contCmd is not None:
if args.contCmd in contCmds:
Expand Down

0 comments on commit 3fb83e5

Please sign in to comment.