Skip to content

Commit

Permalink
refactor(runner): use pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
nritsche committed Jan 20, 2021
1 parent d1d6da6 commit f7e2334
Showing 1 changed file with 22 additions and 35 deletions.
57 changes: 22 additions & 35 deletions caput/scripts/runner.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
from os.path import (
normpath,
expanduser,
expandvars,
realpath,
abspath,
isabs,
join,
exists,
)
import os
import sys

from pathlib import Path

import click
import sys

from caput.config import CaputConfigError

Expand Down Expand Up @@ -69,17 +62,12 @@ def load_venv(configfile):

click.echo("Activating '{}'...".format(venv_path))

# TODO: python2 - use pathlib
import os.path

base = os.path.abspath(venv_path)
base = Path(venv_path).resolve()
if not base.exists():
click.echo("Path defined in 'cluster'/'venv' doesn't exist ({})".format(base))
sys.exit(1)

site_packages = os.path.join(
base, "lib", "python{}".format(sys.version[:3]), "site-packages"
)
site_packages = base / "lib" / "python{}".format(sys.version[:3]) / "site-packages"
prev_sys_path = list(sys.path)

site.addsitedir(site_packages)
Expand Down Expand Up @@ -234,7 +222,6 @@ def queue(configfile, submit=False, lint=True):
same filesystem.
"""

import os.path
import shutil
import yaml

Expand Down Expand Up @@ -279,12 +266,12 @@ def queue(configfile, submit=False, lint=True):
# If the system is specified update the current config with it
if "system" in conf:

sys = conf["system"]
system = conf["system"]

if sys not in system_defaults:
raise ValueError('Specified system "%s": is not known.' % sys)
if system not in system_defaults:
raise ValueError('Specified system "%s": is not known.' % system)

rconf.update(**system_defaults[sys])
rconf.update(**system_defaults[system])

# Update the current config with the rest of the users variables
rconf.update(**conf)
Expand All @@ -301,29 +288,29 @@ def queue(configfile, submit=False, lint=True):

# Construct the working directory
workdir = expandpath(rconf["temp_directory"])
if not isabs(workdir):
if not workdir.is_absolute():
raise ValueError("Working directory path %s must be absolute" % workdir)

# Construct the output directory
finaldir = expandpath(rconf["directory"])
if not isabs(finaldir):
if not finaldir.is_absolute():
raise ValueError("Final output directory path %s must be absolute" % finaldir)

# Create temporary directory if required
jobdir = join(workdir, "job/")
if not exists(jobdir):
os.makedirs(jobdir)
jobdir = workdir / "job/"
if not jobdir.exists():
jobdir.mkdir()

# Copy config file into output directory (check it's not already there first)
sfile = fixpath(configfile)
dfile = fixpath(join(jobdir, "config.yaml"))
dfile = fixpath(jobdir / "config.yaml")
if sfile != dfile:
shutil.copyfile(sfile, dfile)

# Set up virtualenv
if "venv" in rconf:
venvpath = rconf["venv"] + "/bin/activate"
if not exists(venvpath):
if not venvpath.exists():
raise ValueError("Could not find virtualenv at path %s" % rconf["venv"])
rconf["venv"] = venvpath
else:
Expand All @@ -334,9 +321,9 @@ def queue(configfile, submit=False, lint=True):
rconf["workdir"] = workdir
rconf["finaldir"] = finaldir
rconf["scriptpath"] = fixpath(__file__)
rconf["logpath"] = join(jobdir, "jobout.log")
rconf["configpath"] = join(jobdir, "config.yaml")
rconf["statuspath"] = join(jobdir, "STATUS")
rconf["logpath"] = jobdir / "jobout.log"
rconf["configpath"] = jobdir / "config.yaml"
rconf["statuspath"] = jobdir / "STATUS"
rconf["usetemp"] = 1 if rconf["finaldir"] != rconf["workdir"] else 0

pbs_script = """#!/bin/bash
Expand Down Expand Up @@ -429,12 +416,12 @@ def queue(configfile, submit=False, lint=True):

def expandpath(path):
"""Expand any variables, user directories in path"""
return normpath(expandvars(expanduser(path)))
return Path(os.path.expandvars(path)).expanduser().resolve(strict=True)


def fixpath(path):
"""Turn path to an absolute path"""
return realpath(abspath(path))
return Path(path).resolve(strict=True)


# This is needed because the queue script calls this file directly.
Expand Down

0 comments on commit f7e2334

Please sign in to comment.