Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 170 lines (138 sloc) 6.18 KB
#!/usr/bin/python
from __future__ import print_function
#-----------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------
import os,sys,getopt,pwd
#-----------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------
MAIL_SERVER = "mta1.uits.uconn.edu"
RETURN_ADDR = "hpc@uconn.edu"
# Script header
script_header = """#!/bin/bash
cat<<EOM |
"""
# Script footer template (replace values of {{RETURN_ADDR}} and {{MAIL_SERVER}}
# before writing to script.
# The strings {{EMAIL}}, {{NAME}}, and {{LIST-FILE}} are used verbatim
script_footer_template = """EOM
while read netid email name
do
MailViaServer3 -f {{RETURN_ADDR}} -F {{NAME}} -R "$name" -F {{EMAIL}} -R "$email" -F {{LIST-FILE}} -R "$(cat lists/$netid.list)" -s {{MAIL_SERVER}} email.template $email
echo $(date "+%Y-%m-%d %H:%M:%S") $netid $email $name >> mail.log
done
"""
#-----------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------
# Get full_name and email address for netid using pwd module
def get_name_email(netid):
try:
# Extract full name and email address from pwnam.
# Example: 'Sue User {sue.users@uconn.edu} ' -> ['Sue User', 'sue.user@uconn.edu']
return [s.rstrip(' }') for s in pwd.getpwnam(netid).pw_gecos.split('{')]
except KeyError:
return None, None
def Usage(msg=None):
print("""
Usage: dtree-mailing DTREE-OUTPUT [OUTPUT-DIR]
Read output from dtree program in file DTREE-OUTPUT and write mailing
script, email template, and subdirectory with lists for each netid of their
old directories.
INPUT ARGUMENTS
DTREE-OUTPUT - Output from dtree program. This script will read info
on the directories on scratchfs1 and produce a script
that you can run (send-mailing.sh) to email the owners
of the directories. Typically, the input file is in
/gpfs/scratchfs1/admin/gpfs-scan-files/output/YYYY-MM-DD/dtree-180-200g.out
where YYYY-MM-DD is a date.
OUTPUT-DIR - (Optional) Output directory where send-mailing.sh script
is written, email-template, and a subdirectory 'lists/'
that contains one file for each email recipient; the
file contains a list of directories taken from the
DTREE-OUTPUT. The default value is the current
directory.
OUTPUT PRODUCED
send-mailing.sh - A bash script that does the mailing. This script
calls the local script MailViaServer3 to do the actual
mailing.
lists - A directory containing files for each individual email
lists/NETID.lst - For each netid mailed, a list of directories from
DTREE-OUTPUT that they will be asked to remove.
REQUIRED SCRIPTS AND FILES
MailViaServer3 - The dtree-mailing.sh script, when run, will call this script.
This is available via github.uconn.edu:
git clone https://github.uconn.edu/jar02014/MailViaServer3
email.template - An email template file. This is used by the dtree-mailing.sh
shell-script produced this program. The values
{{MAIL}}, {{EMAIL}}, and {{LIST-FILE}} in the template
are replaced with custom input for each email sent.
""")
if msg:
print()
print(msg)
print()
sys.exit()
# For script input, read output from previous run of dtree.py
def read_dtree_output(dtree_file):
# 1) Search input file for section that starts with comment '#SCRATCH DIRECTORIES'.
# 2) From this section, the first two non-blank lines are the two table
# header lines. Save these.
# 3) Read and store the six field from each line
# 4) Stop reading when you encounter either 1) end-of-file 2) blank line 3) comment
headers = []
user_data = {}
with open(dtree_file) as fin:
# Look for '# Scratch Directories
for line in fin:
line = line.strip().upper()
if line.startswith("# SCRATCH DIRECTORIES") or line.startswith("#SCRATCH DIRECTORIES"):
break
# Read two header lines
for lineno, line in enumerate(fin):
if lineno==2: break
headers.append(line)
# Store input lines by netid
for line in fin:
if not line.strip() or line.strip().startswith("#"): break
parts = line.split(None,6)
if not len(parts)==6: continue
netid = parts[4]
if not netid in user_data: user_data[netid] = []
user_data[netid].append(line)
return headers, user_data
def print_strings(strings, print_file=sys.stdout, print_end='\n'):
for s in strings:
print(s, file=print_file, end=print_end)
#-----------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------
def main():
opts, args = getopt.getopt(sys.argv[1:], '')
opts = dict(opts)
if not args: Usage()
input_file = args[0]
output_dir = '.' if len(args)<2 else args[1]
# Read user data
headers, user_data = read_dtree_output(args[0])
# Write directory lists for each user
lists_dir = "%s/lists" % output_dir
if not os.path.isdir(lists_dir): os.makedirs(lists_dir)
for netid, lines in user_data.items():
list_file = "%s/%s.list" % ( lists_dir, netid)
with open(list_file, "w") as fout:
# headers and lines still contain original trailing \n
print_strings(headers, fout, '')
print_strings(lines, fout, '')
# Get list of user netids, full names and email addresses
netid_info = [ [netid] + get_name_email(netid) for netid in user_data.keys() ]
# Write mailing script
with open("send-mailing.sh", "w") as fout:
print(script_header, file=fout, end='')
for netid, full_name, email in netid_info:
if full_name==None: continue
print(netid, email, full_name, file=fout)
print( script_footer_template.replace("{{RETURN_ADDR}}", RETURN_ADDR).replace("{{MAIL_SERVER}}", MAIL_SERVER), file=fout, end='' )
print("Success - All Done")
main()