WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: My bugfixed tcz sending script  (Read 3621 times)

Offline Akane

  • Jr. Member
  • **
  • Posts: 71
    • My home server by RaspberryPi
My bugfixed tcz sending script
« on: October 07, 2012, 07:18:04 PM »
Hi.

I made a script that sends the bugfixed(made by user) tcz by Python.
Please examine this. Request for comments.

Code: [Select]
#!/bin/sh -e
# restcz.sh
# Send a bugfixed tcz to the e-mail addr.
# 2012 Akane mail:i.can.speak.c.and.basic@gmail.com
# How to use:
# $ ./restcz.sh [tcz archive file]
# e.g.,$ ./restcz.sh gfortran.tar.gz.bfe
#
################################
# NOTICE. READ BEFORE RUNNING. #
#################################################
# The e-mail addr which is to send bugfixed tcz #
# is "tcesubmit _at_ gmail _dot_ com".          #
# So please subst it to the "to_addr" below.    #
#################################################

normalsmtp=\"smtp.anywhere.com\"
googlesmtp=\"smtp.gmail.com:587\"

yourgmailuser="getpass.getuser()"
yourgmailpasswd="getpass.getpass()"

# Uncomment these if needed.
#from_addr="you@anywhere.com"

# change _at and _dot_ before uncommenting
#to_addr="tcesubmit _at_ gmail _dot_ com"

if ! which python >/dev/null;then
echo install python.tcz first. >&2
exit 1
fi

usage(){
cat <<END >&2
$@.
Usage: $0 [file to mail]
END
exit 1
}

[ $# -ne 1 ] && usage invalid number of the argument
sendfile=$1
[ -e "$sendfile" ] || usage $sendfile does not exists
echo $sendfile | grep \.tar\.gz\.bfe >/dev/null || \
[ $0 = $sendfile ] || \
usage $sendfile is not a tcz archive

basesendfile=$(basename $sendfile)

subject="$(basename $sendfile .tar.gz.bfe).tcz update"

if [ -z "$to_addr" ];then
echo -n input the e-mail addr to send $sendfile:
read to_addr
fi

if [ -z "$from_addr" ];then
echo -n input your e-mail addr:
read from_addr
fi

echo input the body of the mail. press ^D to exit editing.
body=$(cat)

lessthem(){
less <<END
Things will be sent.
Subject: $subject
To: $to_addr
From: $from_addr
Attach: $sendfile
Message:
$body
END
}

while ! [ "$ans" = y ];do
lessthem
echo -n Is this OK? \(y,n,r\)
read ans
if [ "$ans" = n ];then
cat <<END
You chose n. Now exit.
Writing such data on this script is recommended.
It will be easy for you!
END
exit 2
fi
done

echo sending a mail to $to_addr. this may take some time.

/usr/bin/env python <<END
import os.path
import datetime
import smtplib
import getpass

from email import Encoders
from email.Utils import formatdate
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

def create_message(from_addr, to_addr, subject, body, attach_file):
'''
Build the mail data.
'''
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = from_addr
msg["To"] = to_addr
msg["Date"] = formatdate()

body = MIMEText(body)
msg.attach(body)

# set the MIME type of the attachment
#(actually, this is not needed)
attachment = MIMEBase("Unknown","Unknown")
# set the data of attachment
file = open(attach_file)
attachment.set_payload(file.read())
file.close()
Encoders.encode_base64(attachment)
msg.attach(attachment)
attachment.add_header("Content-Disposition", \
"attachment", filename=os.path.basename(attach_file))
return msg

def send_by_google_smtp(from_addr, to_addr, msg):
"""
Send a mail by using gmail smtp server.
"""
username=$yourgmailuser
password=$yourgmailpasswd
server = smtplib.SMTP($googlesmtp)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()

def send_by_normal_smtp(from_addr, to_addr, msg):
"""
Send a mail by using normal(don't use TLS) smtp server.
"""
smtp = smtplib.SMTP($normalsmtp)
smtp.sendmail(from_addr, to_addr, msg.as_string())
smtp.close()

if __name__ == '__main__':
from_addr = "$from_addr"
to_addr = "$to_addr"
subject = "$subject"
body = "$body"
sendfile = "$sendfile"
msg = create_message(from_addr, to_addr, \
subject, body, sendfile)
send_by_google_smtp(from_addr, to_addr, msg)
END

echo done. thank you for using this script. by Akane.
Sorry for my terrible English.
Country: Japan
Interested in: HPC, low-level programming, embedded system and so on

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: My bugfixed tcz sending script
« Reply #1 on: October 07, 2012, 10:56:01 PM »
Why do you wrap the Python program into a shell script instead of having it as a Python script?
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline Akane

  • Jr. Member
  • **
  • Posts: 71
    • My home server by RaspberryPi
Re: My bugfixed tcz sending script
« Reply #2 on: October 08, 2012, 12:53:39 AM »
hello bmarkus.

The processes that is in the script can express as shell commands.
I think separating the shell script and python script to another file makes the treatment bother.

Thanks.
Sorry for my terrible English.
Country: Japan
Interested in: HPC, low-level programming, embedded system and so on

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: My bugfixed tcz sending script
« Reply #3 on: October 08, 2012, 01:48:00 AM »
Why do you need the shells cript? It is just a bootstrap for the Python. I would drop shell and use only Python.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline Akane

  • Jr. Member
  • **
  • Posts: 71
    • My home server by RaspberryPi
Re: My bugfixed tcz sending script
« Reply #4 on: October 11, 2012, 12:58:34 AM »
Thanks for your reply.

I will write two scripts using Python and Shell.
One is to send a mail,
and the other is to ask user and send the bugfixed tcz.

I have few times to fix it because I have to write a report.
Next script will be functional.

Please wait a few days.
Sorry for my terrible English.
Country: Japan
Interested in: HPC, low-level programming, embedded system and so on