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

Added pre-commit config and ran entire code base through it #78

Merged
merged 5 commits into from
Aug 11, 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
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.931
hooks:
- id: mypy
name: mypy (no tests or bin)
exclude: ^(tests/|bin/)
args: [--strict]
- id: mypy
name: mypy (bin)
files: bin/
args: [ --scripts-are-modules, --ignore-missing-imports ]
- repo: https://github.com/PyCQA/pylint
rev: v2.13.9
hooks:
- id: pylint
args: [ --disable=all, --rcfile=.pylintrc ] # Go through this. Maybe we want to modify this list
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ disable=raw-checker-failed,
useless-suppression,
deprecated-pragma,
use-symbolic-message-instead
line-too-long

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
jobsub_lite is a wrapper for Condor job submission, intended
to be backwards compatible with the actively used options of
the past Fermilab jobsub tools, while being smaller and easier
to maintain, and handling new requirements (i.e SciTokens
to maintain, and handling new requirements (i.e SciTokens
authentication, etc.)

The basic design of jobsub_lite is straightforward. It will:
Expand Down Expand Up @@ -35,4 +35,3 @@ This is currently implemented using Python's `requests` module for https access
## Tempates of .cmd .sh, and .dag files

The [Jinja](http://jinja.pocoo.org/docs/dev/templates/) template code is used to generate the job submission files. For example, the template for the .cmd file for a simple submission is [simple.cmd](https://github.com/marcmengel/jobsub_lite/blob/master/templates/simple/simple.cmd) where a name (or expression) in doubled curly braces `{{name}}` is replaced when generating the output, and conditional geneartion is done with `{%if expr%}...{%endif%}`. (There are other Jinja features, but that is mainly what is used now in jobsub_lite). The majority of template replacement values are directly command line options or their defaults, which makes adding new features easy; you add an option (say `--fred` ) to the command line parser, and then add a suitable entry to the appropriate template(s) using that value ( `{{fred}}` or `{%if fred %} xyzzy={{fred}} {%endif%}` .

9 changes: 1 addition & 8 deletions bin/condor_submit
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import sys
import os
import os.path
import argparse
from typing import Dict, Union
import htcondor

#
Expand All @@ -48,13 +47,7 @@ class StoreGroupinEnvironment(argparse.Action):

# pylint: disable=too-few-public-methods

def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: Dict[str, str],
option_string: Union[None, str] = None,
):
def __call__(self, parser, namespace, values, option_string): # type: ignore
os.environ["GROUP"] = values
setattr(namespace, self.dest, values)

Expand Down
10 changes: 5 additions & 5 deletions bin/decode_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
decode_token() {
#
# token is 3 base64 segments separated by dots
# we want the middle one.
# the ending of the base64 segment is sometimes
# we want the middle one.
# the ending of the base64 segment is sometimes
# chopped, so ignore errors about that
#
sed -e 's/.*\.\(.*\)\..*/\1==/' "$1" 2>/dev/null | base64 -d 2>/dev/null
Expand All @@ -20,9 +20,9 @@ get_field() {
#
# filter out a field from a json block
# rip off up to "fieldname":, then rip off everything
# after the comma.
# after the comma.
#
# BUGS
# BUGS
# Borked for fields that contain commas.
#
sed -e "s/.*\"$1\"://" -e s'/,.*//'
Expand All @@ -35,7 +35,7 @@ usage() {
echo "option -e lets you extract a particular field"
}

case x$1 in
case x$1 in
x-e) extractfilt="get_field $2"; shift; shift;;
x-h) usage; exit 0;;
x) usage; exit 1;;
Expand Down
9 changes: 1 addition & 8 deletions bin/jobsub_cmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import argparse
import os
import sys
import re
from typing import Union, Dict

PREFIX = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(PREFIX, "lib"))
Expand All @@ -39,13 +38,7 @@ class StoreGroupinEnvironment(argparse.Action):

# pylint: disable=too-few-public-methods

def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: Dict[str, str],
option_string: Union[None, str] = None,
):
def __call__(self, parser, namespace, values, option_string): # type: ignore
os.environ["GROUP"] = values
setattr(namespace, self.dest, values)

Expand Down
8 changes: 4 additions & 4 deletions bin/jobsub_submit
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import glob
import os
import os.path
import sys
from typing import Union, List, Dict
from typing import Union, List, Dict, Any

#
# we are in prefix/bin/jobsub_submit, so find our prefix
Expand Down Expand Up @@ -62,7 +62,7 @@ def get_basefiles(dlist: List[str]) -> List[str]:


def render_files(
srcdir: str, values: Dict[str, str], dest: str, dlist: Union[None, List[str]] = None
srcdir: str, values: Dict[str, Any], dest: str, dlist: Union[None, List[str]] = None
):
"""use jinja to render the templates from srcdir into the dest directory
using values dict for substitutions
Expand Down Expand Up @@ -92,7 +92,7 @@ def render_files(
except jinja.exceptions.UndefinedError as e:
err = f"""Cannot render template file {f} due to undefined template variables.
{e}
Please open a ticket to the Service Desk and include this error message
Please open a ticket to the Service Desk and include this error message
in its entirety.
"""
print(err)
Expand All @@ -107,7 +107,7 @@ def cleanup(tmp: str) -> None:
# we actually leave everything in the sandbox right now..


def do_dataset_defaults(varg: Dict[str, str]) -> None:
def do_dataset_defaults(varg: Dict[str, Any]) -> None:
"""
make sure to pass appropriate SAM_* environment variables if we
are doing datasets. Pick a SAM_PROJECT name if we don't have one.
Expand Down
7 changes: 3 additions & 4 deletions config.d/50-jobsub_lite.configs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#
SEC_CREDENTIAL_GETTOKEN_OPTS=-a fermicloud543.fnal.gov
CREDMON_OAUTH=/usr/sbin/condor_credmon_vault
SEC_CREDENTIAL_STORER=/usr/bin/condor_vault_storer
SEC_CREDENTIAL_STORER=/usr/bin/condor_vault_storer

#
# We want to use scitokens, and our service certs are signed
# We want to use scitokens, and our service certs are signed
# by grid authorities
#
AUTH_SSL_CLIENT_CADIR=/etc/grid-security/certificates
SEC_CLIENT_AUTHENTICATION_METHODS=SCITOKENS
SEC_CLIENT_AUTHENTICATION_METHODS=SCITOKENS

#
# Authentication requirement:
Expand All @@ -29,4 +29,3 @@ SEC_CLIENT_INTEGRITY=OPTIONAL
# Not sure why we need these
#
LOG=/storage/local/data1/condor/log

14 changes: 8 additions & 6 deletions lib/condor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import re
import random
import subprocess
from typing import Dict, List
from typing import Dict, List, Any, Tuple, Optional, Union

import htcondor
import classad
import htcondor # type: ignore
import classad # type: ignore

import packages

Expand Down Expand Up @@ -84,7 +84,7 @@ def get_schedd(vargs: Dict[str, str]) -> classad.ClassAd:
return res


def load_submit_file(filename: str) -> Dict[str, str]:
def load_submit_file(filename: str) -> Tuple[Any, Optional[int]]:
"""pull in a condor submit file, make a dictionary"""

#
Expand Down Expand Up @@ -113,7 +113,9 @@ def load_submit_file(filename: str) -> Dict[str, str]:


# pylint: disable-next=dangerous-default-value
def submit(f: str, vargs: Dict[str, str], schedd_name: str, cmd_args: List[str] = []):
def submit(
f: str, vargs: Dict[str, str], schedd_name: str, cmd_args: List[str] = []
) -> Union[Any, bool]:
"""Actually submit the job, using condor python bindings"""

schedd_args = f"-remote {schedd_name}"
Expand Down Expand Up @@ -177,7 +179,7 @@ def submit(f: str, vargs: Dict[str, str], schedd_name: str, cmd_args: List[str]
# pylint: disable-next=dangerous-default-value
def submit_dag(
f: str, vargs: Dict[str, str], schedd_name: str, cmd_args: List[str] = []
):
) -> Union[Any, bool]:
"""
Actually submit the dag
for the moment, we call the commandline condor_submit_dag,
Expand Down
4 changes: 2 additions & 2 deletions lib/creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# limitations under the License.
""" credential related routines """
import os
from typing import Dict
from typing import Dict, Tuple

import fake_ifdh

# pylint: disable-next=dangerous-default-value
def get_creds(args: Dict[str, str] = {}):
def get_creds(args: Dict[str, str] = {}) -> Tuple[str, str]:
"""get credentials -- Note this does not currently push to
myproxy, nor does it yet deal with tokens, but those should
be done here as needed.
Expand Down
10 changes: 5 additions & 5 deletions lib/dagnabbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import sys
import os
import os.path
from typing import Dict
from typing import Dict, List, Any

import jinja2 as jinja
import jinja2 as jinja # type: ignore

import creds
from get_parser import get_parser
Expand All @@ -28,11 +28,11 @@

def parse_dagnabbit(
srcdir: str,
values: Dict[str, str],
values: Dict[str, Any],
dest: str,
schedd_name: str,
debug_comments: bool = True,
):
) -> None:
"""
parse a dagnabbit dag file generating a .dag file and .cmd files
in the dest directory, using global cmdline options from values
Expand All @@ -51,7 +51,7 @@ def parse_dagnabbit(
in_parallel = False
in_serial = False
last_serial = None
parallel_l = []
parallel_l: List[str] = []
for line in df:
line = line.strip()
line = os.path.expandvars(line)
Expand Down
27 changes: 19 additions & 8 deletions lib/fake_ifdh.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import os
import time
import argparse
from typing import Union
from typing import Union, Optional

VAULT_HOST = "fermicloud543.fnal.gov"
DEFAULT_ROLE = "Analysis"
Expand All @@ -35,7 +35,7 @@ def getTmp() -> str:
return os.environ.get("TMPDIR", "/tmp")


def getExp() -> str:
def getExp() -> Union[str, None]:
"""return current experiment name"""
for ev in ["GROUP", "EXPERIMENT", "SAM_EXPERIMENT"]:
if os.environ.get(ev, None):
Expand All @@ -47,7 +47,7 @@ def getExp() -> str:
return exp


def getRole(role_override: Union[str, None] = None) -> str:
def getRole(role_override: Optional[str] = None) -> str:
"""get current role"""
if role_override:
return role_override
Expand All @@ -58,11 +58,22 @@ def getRole(role_override: Union[str, None] = None) -> str:

def checkToken(tokenfile: str) -> bool:
"""check if token is (almost) expired"""
if not os.path.exists(tokenfile):
return False
exp_time = None
cmd = f"decode_token.sh -e exp {tokenfile} 2>/dev/null"
with os.popen(cmd, "r") as f:
exp_time = f.read()
return exp_time and ((int(exp_time) - time.time()) > 60)
try:
return int(exp_time) - time.time() > 60
except ValueError as e:
print(
"decode_token.sh could not successfully extract the "
f"expiration time from token file {tokenfile}. Please open "
"a ticket to Distributed Computing Support if you need further "
"assistance."
)
raise


def getToken(role: str = DEFAULT_ROLE) -> str:
Expand All @@ -71,7 +82,7 @@ def getToken(role: str = DEFAULT_ROLE) -> str:
tmp = getTmp()
exp = getExp()
if exp == "samdev":
issuer = "fermilab"
issuer: Optional[str] = "fermilab"
else:
issuer = exp

Expand Down Expand Up @@ -112,7 +123,7 @@ def getProxy(role: str = DEFAULT_ROLE) -> str:
igroup = exp
else:
issuer = "fermilab"
igroup = "fermilab/" + exp
igroup = f"fermilab/{exp}"
vomsfile = f"{tmp}/x509up_{exp}_{role}_{pid}"
chk_cmd = f"voms-proxy-info -exists -valid 0:10 -file {vomsfile}"
if 0 != os.system(chk_cmd):
Expand Down Expand Up @@ -155,9 +166,9 @@ def cp(src: str, dest: str) -> None:

try:
if opts.command[0] == "cp":
commands[opts.command[0]](*opts.cpargs[0])
commands[opts.command[0]](*opts.cpargs[0]) # type: ignore
else:
result = commands[opts.command[0]](myrole)
result = commands[opts.command[0]](myrole) # type: ignore
if result is not None:
print(result)
except PermissionError as pe:
Expand Down
6 changes: 3 additions & 3 deletions lib/get_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# pylint: disable=too-few-public-methods
import argparse
import os
from typing import Union
from typing import Union, Any


def verify_executable_starts_with_file_colon(s: str) -> str:
Expand All @@ -37,9 +37,9 @@ def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values,
values: Any,
option_string: Union[None, str] = None,
):
) -> None:
os.environ["GROUP"] = values
setattr(namespace, self.dest, values)

Expand Down
4 changes: 2 additions & 2 deletions lib/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def pkg_find(p: str, qual: str = "") -> None:
if not SAVED_ENV:
SAVED_ENV = os.environ.copy()
path = None
if not path and os.environ.get("SPACK_ROOT", None):
if not path and os.environ.get("SPACK_ROOT"):
cmd = f"spack find --paths --variants '{p} os=fe' 'py-{p} os=fe'"
with os.popen(cmd, "r") as f:
for line in f:
Expand All @@ -50,7 +50,7 @@ def pkg_find(p: str, qual: str = "") -> None:
path = line.split()[1]
break

if not path and os.environ.get("PRODUCTS", None):
if not path and os.environ.get("PRODUCTS"):
cmd = f"ups list -a4 -Kproduct:@prod_dir {p} {qual}, -a0 -Kproduct:@prod_dir {p} {qual}"
with os.popen(cmd, "r") as f:
for line in f:
Expand Down
Loading