Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply black to python directory #1577

Merged
merged 19 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ran python directory through black python formatter
4cd83cb3ee6d85eb909403487abf5eeaf4d98911
0aa2957c1f8603c63fa30b11295c06cfddff44a5
2cdb380febb274478e84cd90945aee93f29fa2e6
19 changes: 19 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: black check on push and PR
#
# Run the python formatting in check mode
#
on: [push, pull_request]

jobs:
black-check:
runs-on: ubuntu-latest
steps:
# Checkout the code
- uses: actions/checkout@v2
# Use the latest stable version of the github action
- uses: psf/black@stable
with:
# Use options and version identical to the conda environment
options: "--check --config python/pyproject.toml"
src: "./"
version: "22.3.0"
2 changes: 1 addition & 1 deletion python/ctsm/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ indent-after-paren=4
indent-string=' '

# Maximum number of characters on a single line.
max-line-length=100
max-line-length=88 # Make sure this agrees with black

# Maximum number of lines in a module.
max-module-lines=1000
Expand Down
1 change: 1 addition & 0 deletions python/ctsm/add_cime_to_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
"""

from ctsm.path_utils import add_cime_lib_to_path

_ = add_cime_lib_to_path()
6 changes: 3 additions & 3 deletions python/ctsm/args_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

logger = logging.getLogger(__name__)


def plat_type(plat):
"""
Function to define lat type for the parser
Expand All @@ -26,11 +27,10 @@ def plat_type(plat):
"""
plat_out = float(plat)
if plat_out < -90 or plat_out > 90:
raise argparse.ArgumentTypeError(
"ERROR: Latitude should be between -90 and 90."
)
raise argparse.ArgumentTypeError("ERROR: Latitude should be between -90 and 90.")
return plat_out


def plon_type(plon):
"""
Function to define lon type for the parser and
Expand Down
8 changes: 2 additions & 6 deletions python/ctsm/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def get_config_value(
try:
val = config.get(section, item)
except configparser.NoSectionError:
abort(
"ERROR: Config file {} must contain section '{}'".format(file_path, section)
)
abort("ERROR: Config file {} must contain section '{}'".format(file_path, section))
except configparser.NoOptionError:
abort(
"ERROR: Config file {} must contain item '{}' in section '{}'".format(
Expand All @@ -74,9 +72,7 @@ def get_config_value(
)

if val == _CONFIG_PLACEHOLDER:
abort(
"Error: {} needs to be specified in config file {}".format(item, file_path)
)
abort("Error: {} needs to be specified in config file {}".format(item, file_path))

val = _handle_config_value(
var=val,
Expand Down
2 changes: 1 addition & 1 deletion python/ctsm/gen_mksurf_namelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def main():
glc_flag,
start_year,
end_year,
hres_flag
hres_flag,
)

logger.info("--------------------------")
Expand Down
10 changes: 2 additions & 8 deletions python/ctsm/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def get_ctsm_git_short_hash():
sha (str) : git short hash for ctsm repository
"""
sha = (
subprocess.check_output(
["git", "-C", path_to_ctsm_root(), "rev-parse", "--short", "HEAD"]
)
subprocess.check_output(["git", "-C", path_to_ctsm_root(), "rev-parse", "--short", "HEAD"])
.strip()
.decode()
)
Expand Down Expand Up @@ -59,9 +57,5 @@ def get_ctsm_git_describe():
Returns:
label (str) : ouput of running 'git describe' for the CTSM repository
"""
label = (
subprocess.check_output(["git", "-C", path_to_ctsm_root(), "describe"])
.strip()
.decode()
)
label = subprocess.check_output(["git", "-C", path_to_ctsm_root(), "describe"]).strip().decode()
return label
34 changes: 22 additions & 12 deletions python/ctsm/joblauncher/job_launcher_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@

logger = logging.getLogger(__name__)


class JobLauncherBase(object):
"""Base class for job launchers. Not meant to be instantiated directly"""

def __init__(self, queue=None, walltime=None, account=None,
required_args=None, extra_args=None):
def __init__(
self,
queue=None,
walltime=None,
account=None,
required_args=None,
extra_args=None,
):
"""Initialize a job launcher object.

Note that some of these arguments (e.g., queue and walltime) aren't needed by some
Expand Down Expand Up @@ -109,13 +116,16 @@ def run_command_logger_message(self, command, stdout_path, stderr_path):
raise NotImplementedError

def __repr__(self):
return (type(self).__name__ +
"(queue='{queue}', "
"walltime='{walltime}', "
"account='{account}', "
"required_args='{required_args}', "
"extra_args='{extra_args}')".format(queue=self._queue,
walltime=self._walltime,
account=self._account,
required_args=self._required_args,
extra_args=self._extra_args))
return (
type(self).__name__ + "(queue='{queue}', "
"walltime='{walltime}', "
"account='{account}', "
"required_args='{required_args}', "
"extra_args='{extra_args}')".format(
queue=self._queue,
walltime=self._walltime,
account=self._account,
required_args=self._required_args,
extra_args=self._extra_args,
)
)
38 changes: 24 additions & 14 deletions python/ctsm/joblauncher/job_launcher_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
JOB_LAUNCHER_QSUB = "qsub"
JOB_LAUNCHER_FAKE = "fake"

def create_job_launcher(job_launcher_type,
account=None, queue=None, walltime=None,
nice_level=None,
required_args=None, extra_args=None,
allow_missing_entries=False):

def create_job_launcher(
job_launcher_type,
account=None,
queue=None,
walltime=None,
nice_level=None,
required_args=None,
extra_args=None,
allow_missing_entries=False,
):
"""
Creates and returns a job launcher object of the specified type

Expand Down Expand Up @@ -52,18 +58,22 @@ def create_job_launcher(job_launcher_type,

if job_launcher_type == JOB_LAUNCHER_QSUB:
if not allow_missing_entries:
_assert_not_none(queue, 'queue', job_launcher_type)
_assert_not_none(walltime, 'walltime', job_launcher_type)
return JobLauncherQsub(queue=queue,
walltime=walltime,
account=account,
required_args=required_args,
extra_args=extra_args)
_assert_not_none(queue, "queue", job_launcher_type)
_assert_not_none(walltime, "walltime", job_launcher_type)
return JobLauncherQsub(
queue=queue,
walltime=walltime,
account=account,
required_args=required_args,
extra_args=extra_args,
)

raise RuntimeError("Unexpected job launcher type: {}".format(job_launcher_type))


def _assert_not_none(arg, arg_name, job_launcher_type):
"""Raises an exception if the given argument has value None"""
if arg is None:
raise TypeError("{} cannot be None for job launcher of type {}".format(
arg_name, job_launcher_type))
raise TypeError(
"{} cannot be None for job launcher of type {}".format(arg_name, job_launcher_type)
)
17 changes: 8 additions & 9 deletions python/ctsm/joblauncher/job_launcher_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# cmd (str): space-delimited string giving this command
# out (str): path to stdout
# err (str): path to stderr
Command = namedtuple('Command', ['cmd', 'out', 'err'])
Command = namedtuple("Command", ["cmd", "out", "err"])


class JobLauncherFake(JobLauncherBase):
"""A fake JobLauncher that just records the commands it is told to run"""
Expand All @@ -16,16 +17,14 @@ def __init__(self):
self._commands = []

def run_command_impl(self, command, stdout_path, stderr_path):
self._commands.append(Command(cmd=' '.join(command),
out=stdout_path,
err=stderr_path))
self._commands.append(Command(cmd=" ".join(command), out=stdout_path, err=stderr_path))

def run_command_logger_message(self, command, stdout_path, stderr_path):
message = 'Appending: <{}> ' \
'with stdout = {} ' \
'and stderr = {}'.format(' '.join(command),
stdout_path,
stderr_path)
message = (
"Appending: <{}> "
"with stdout = {} "
"and stderr = {}".format(" ".join(command), stdout_path, stderr_path)
)
return message

def get_commands(self):
Expand Down
23 changes: 12 additions & 11 deletions python/ctsm/joblauncher/job_launcher_no_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
from ctsm.joblauncher.job_launcher_base import JobLauncherBase


class JobLauncherNoBatch(JobLauncherBase):
"""Job launcher for systems where we can run a big job directly on the login node

Expand Down Expand Up @@ -42,22 +43,22 @@ def _preexec(self):
os.nice(self._nice_level)

def run_command_impl(self, command, stdout_path, stderr_path):
with open(stdout_path, 'w') as outfile, \
open(stderr_path, 'w') as errfile:
with open(stdout_path, "w") as outfile, open(stderr_path, "w") as errfile:
# Note that preexec_fn is POSIX-only; also, it may be unsafe in the presence
# of threads (hence the need for disabling the pylint warning)
# pylint: disable=subprocess-popen-preexec-fn
self._process = subprocess.Popen(command,
stdout=outfile,
stderr=errfile,
preexec_fn=self._preexec)
self._process = subprocess.Popen(
command, stdout=outfile, stderr=errfile, preexec_fn=self._preexec
)

def run_command_logger_message(self, command, stdout_path, stderr_path):
message = 'Running: <{command_str}> ' \
'with stdout = {outfile} ' \
'and stderr = {errfile}'.format(command_str=' '.join(command),
outfile=stdout_path,
errfile=stderr_path)
message = (
"Running: <{command_str}> "
"with stdout = {outfile} "
"and stderr = {errfile}".format(
command_str=" ".join(command), outfile=stdout_path, errfile=stderr_path
)
)
return message

def wait_for_last_process_to_complete(self):
Expand Down
60 changes: 35 additions & 25 deletions python/ctsm/joblauncher/job_launcher_qsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,59 @@

logger = logging.getLogger(__name__)


class JobLauncherQsub(JobLauncherBase):
"""Job launcher for systems where we run big jobs via qsub"""

def __init__(self, queue, walltime, account, required_args, extra_args):
"""
account can be None or the empty string on a machine that doesn't require an account
"""
JobLauncherBase.__init__(self,
queue=queue,
walltime=walltime,
account=account,
required_args=required_args,
extra_args=extra_args)
JobLauncherBase.__init__(
self,
queue=queue,
walltime=walltime,
account=account,
required_args=required_args,
extra_args=extra_args,
)

def run_command_impl(self, command, stdout_path, stderr_path):
qsub_process = subprocess.Popen(self._qsub_command(stdout_path, stderr_path),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
(out, err) = qsub_process.communicate(' '.join(command))
qsub_process = subprocess.Popen(
self._qsub_command(stdout_path, stderr_path),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
(out, err) = qsub_process.communicate(" ".join(command))
if err:
logger.info('qsub ERROR:\n%s', err)
logger.info("qsub ERROR:\n%s", err)
if out:
logger.info('qsub OUTPUT:\n%s', out)
logger.info("qsub OUTPUT:\n%s", out)

def run_command_logger_message(self, command, stdout_path, stderr_path):
message = 'Running: <{command_str}> ' \
'via <{qsub_str}>'.format(
command_str=' '.join(command),
qsub_str=' '.join(self._qsub_command(stdout_path,
stderr_path)))
message = "Running: <{command_str}> " "via <{qsub_str}>".format(
command_str=" ".join(command),
qsub_str=" ".join(self._qsub_command(stdout_path, stderr_path)),
)
return message

def _qsub_command(self, stdout_path, stderr_path):
"""Returns the qsub command"""
qsub_cmd = ['qsub',
'-q', self._queue,
'-l', 'walltime={}'.format(self._walltime),
'-o', stdout_path,
'-e', stderr_path]
qsub_cmd = [
"qsub",
"-q",
self._queue,
"-l",
"walltime={}".format(self._walltime),
"-o",
stdout_path,
"-e",
stderr_path,
]
if self._account:
qsub_cmd.extend(['-A', self._account])
qsub_cmd.extend(["-A", self._account])
if self._required_args:
qsub_cmd.extend(self._required_args.split())
if self._extra_args:
Expand Down
Loading