Skip to content

Commit

Permalink
Move crontabline add to get_crontabs.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielabdi-noaa committed May 7, 2022
1 parent cab9197 commit ea76cc1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 76 deletions.
74 changes: 2 additions & 72 deletions ush/generate_FV3LAM_wflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from setup import setup
from set_FV3nml_sfc_climo_filenames import set_FV3nml_sfc_climo_filenames
from get_crontab_contents import get_crontab_contents
from get_crontab_contents import add_crontab_line
from fill_jinja_template import fill_jinja_template
from set_namelist import set_namelist

Expand Down Expand Up @@ -449,77 +449,7 @@ def generate_FV3LAM_wflow():
#-----------------------------------------------------------------------
#
if USE_CRON_TO_RELAUNCH:
#
# Make a backup copy of the user's crontab file and save it in a file.
#
time_stamp = datetime.now().strftime("%F_%T")
crontab_backup_fp=os.path.join(EXPTDIR,f"crontab.bak.{time_stamp}")
print_info_msg(f'''
Copying contents of user cron table to backup file:
crontab_backup_fp = \"{crontab_backup_fp}\"''',verbose=VERBOSE)

global called_from_cron
try: called_from_cron
except: called_from_cron = False

crontab_cmd,crontab_contents = get_crontab_contents(called_from_cron=called_from_cron)
# To create the backup crontab file and add a new job to the user's
# existing cron table, use the "printf" command, not "echo", to print
# out variables. This is because "echo" will add a newline at the end
# of its output even if its input argument is a null string, resulting
# in extranous blank lines in the backup crontab file and/or the cron
# table itself. Using "printf" prevents the appearance of these blank
# lines.
run_command(f'''printf "%s" '{crontab_contents}' > "{crontab_backup_fp}"''')
#
# Below, we use "grep" to determine whether the crontab line that the
# variable CRONTAB_LINE contains is already present in the cron table.
# For that purpose, we need to escape the asterisks in the string in
# CRONTAB_LINE with backslashes. Do this next.
#
(_,crontab_line_esc_astr,_) = run_command(f'''printf "%s" '{CRONTAB_LINE}' | \
{SED} -r -e "s%[*]%\\\\*%g"''')
# In the grep command below, the "^" at the beginning of the string
# passed to grep is a start-of-line anchor, and the "$" at the end is
# an end-of-line anchor. Thus, in order for grep to find a match on
# any given line of the cron table's contents, that line must contain
# exactly the string in the variable crontab_line_esc_astr without any
# leading or trailing characters. This is to eliminate situations in
# which a line in the cron table contains the string in crontab_line_esc_astr
# but is precedeeded, for example, by the comment character "#" (in which
# case cron ignores that line) and/or is followed by further commands
# that are not part of the string in crontab_line_esc_astr (in which
# case it does something more than the command portion of the string in
# crontab_line_esc_astr does).
#
if MACHINE == "WCOSS_DELL_P3":
(exit_status,grep_output,_)=run_command(f'''grep '^{crontab_line_esc_astr}$' "/u/{USER}/cron/mycrontab"''')
else:
(exit_status,grep_output,_)=run_command(f'''printf "%s" '{crontab_contents}' | grep "^{crontab_line_esc_astr}$"''')

if exit_status == 0:

print_info_msg(f'''
The following line already exists in the cron table and thus will not be
added:
CRONTAB_LINE = \"{CRONTAB_LINE}\"''')

else:

print_info_msg(f'''
Adding the following line to the user's cron table in order to automatically
resubmit SRW workflow:
CRONTAB_LINE = \"{CRONTAB_LINE}\"''',verbose=VERBOSE)

if MACHINE == "WCOSS_DELL_P3":
run_command(f'''printf "%s\n" '{CRONTAB_LINE}' >> "/u/{USER}/cron/mycrontab"''')
else:
# Add a newline to the end of crontab_contents only if it is not empty.
# This is needed so that when CRONTAB_LINE is printed out, it appears on
# a separate line.
if crontab_contents:
crontab_contents += "\n"
run_command(f'''( printf "%s" '{crontab_contents}'; printf "%s\n" '{CRONTAB_LINE}' ) | {crontab_cmd}''')
add_crontab_line()
#
#-----------------------------------------------------------------------
#
Expand Down
62 changes: 58 additions & 4 deletions ush/get_crontab_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import datetime

from python_utils import import_vars, set_env_var, print_input_args, \
run_command, define_macos_utilities
run_command, define_macos_utilities, print_info_msg
from constants import valid_vals_BOOLEAN

def get_crontab_contents(called_from_cron):
Expand Down Expand Up @@ -38,11 +38,11 @@ def get_crontab_contents(called_from_cron):
"""

print_input_args(locals())

#import selected env vars
IMPORTS = ["MACHINE", "USER"]
import_vars(env_vars=IMPORTS)

if MACHINE == "WCOSS_DELL_P3":
__crontab_cmd__=""
(_,__crontab_contents__,_)=run_command(f'''cat "/u/{USER}/cron/mycrontab"''')
Expand Down Expand Up @@ -70,6 +70,58 @@ def get_crontab_contents(called_from_cron):

return __crontab_cmd__, __crontab_contents__

def add_crontab_line():
""" Add crontab line to cron table """

#import selected env vars
IMPORTS = ["MACHINE", "USER", "CRONTAB_LINE", "VERBOSE", "EXPTDIR"]
import_vars(env_vars=IMPORTS)

#
# Make a backup copy of the user's crontab file and save it in a file.
#
time_stamp = datetime.now().strftime("%F_%T")
crontab_backup_fp=os.path.join(EXPTDIR,f"crontab.bak.{time_stamp}")
print_info_msg(f'''
Copying contents of user cron table to backup file:
crontab_backup_fp = \"{crontab_backup_fp}\"''',verbose=VERBOSE)

global called_from_cron
try: called_from_cron
except: called_from_cron = False

# Get crontab contents
crontab_cmd,crontab_contents = get_crontab_contents(called_from_cron=called_from_cron)

# Create backup
run_command(f'''printf "%s" '{crontab_contents}' > "{crontab_backup_fp}"''')

# Add crontab line
if CRONTAB_LINE in crontab_contents:

print_info_msg(f'''
The following line already exists in the cron table and thus will not be
added:
CRONTAB_LINE = \"{CRONTAB_LINE}\"''')

else:

print_info_msg(f'''
Adding the following line to the user's cron table in order to automatically
resubmit SRW workflow:
CRONTAB_LINE = \"{CRONTAB_LINE}\"''',verbose=VERBOSE)

#add new line to crontab contents if it doesn't have one
NEWLINE_CHAR=""
if crontab_contents and crontab_contents[-1] != "\n":
NEWLINE_CHAR="\n"

#add the crontab line
if MACHINE == "WCOSS_DELL_P3":
run_command(f'''printf "%b%s\n" '{NEWLINE_CHAR}' '{CRONTAB_LINE}' >> "/u/{USER}/cron/mycrontab"''')
else:
run_command(f'''printf "%s%b%s\n" '{crontab_contents}' '{NEWLINE_CHAR}' '{CRONTAB_LINE}' | {crontab_cmd}''')

def delete_crontab_line(called_from_cron):
""" Delete crontab line after job is complete i.e. either SUCCESS/FAILURE
but not IN PROGRESS status"""
Expand All @@ -96,7 +148,9 @@ def delete_crontab_line(called_from_cron):
run_command(f'''echo '{crontab_contents}' | {crontab_cmd}''')

def parse_args(argv):
""" Parse command line arguments """
""" Parse command line arguments for deleting crontab line.
This is needed because it is called from shell script
"""
parser = argparse.ArgumentParser(
description='Crontab job manupilation program.'
)
Expand Down

0 comments on commit ea76cc1

Please sign in to comment.