From: Fred L. D. <fd...@us...> - 2003-07-10 12:56:14
|
Update of /cvsroot/cvs-syncmail/syncmail In directory sc8-pr-cvs1:/tmp/cvs-serv7143 Modified Files: Tag: new-config-branch syncmail tests.py Log Message: Move the setting of options from main() to a helper object. All options are attributes of the object; no more global variables! Index: tests.py =================================================================== RCS file: /cvsroot/cvs-syncmail/syncmail/Attic/tests.py,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- tests.py 10 Jul 2003 05:40:10 -0000 1.1.2.1 +++ tests.py 10 Jul 2003 12:56:11 -0000 1.1.2.2 @@ -22,21 +22,24 @@ eq(replace("abc$FOO-def${SPLAT}"), "abc<whack!>-def") eq(replace("$FOO"), "<whack!>") -config, args = load_configuration([]) -eq(config.getint("context-lines"), 2) -eq(config.getbool("verbose"), 1) -eq(config.getaddress("smtp-server"), (MAILHOST, MAILPORT)) +config = load_configuration([]) +eq(config.contextlines, 2) +eq(config.verbose, 1) +eq(config.smtp_server, MAILHOST) +eq(config.smtp_port, MAILPORT) -config, args = load_configuration(['-q', '--mailhost=smtp.example.com']) -eq(config.getint("context-lines"), 2) -eq(config.getbool("verbose"), 0) -eq(config.getaddress("smtp-server"), ("smtp.example.com", MAILPORT)) +config = load_configuration(['-q', '--mailhost=smtp.example.com']) +eq(config.verbose, 0) +eq(config.smtp_server, "smtp.example.com") +eq(config.smtp_port, MAILPORT) -config, args = load_configuration(['--mailhost=smtp.example.com:8025']) -eq(config.getaddress("smtp-server"), ("smtp.example.com", 8025)) +config = load_configuration(['--mailhost=smtp.example.com:8025']) +eq(config.smtp_server, "smtp.example.com") +eq(config.smtp_port, 8025) -config, args = load_configuration(['--mailhost=:8025']) -eq(config.getaddress("smtp-server"), (MAILHOST, 8025)) +config = load_configuration(['--mailhost=:8025']) +eq(config.smtp_server, MAILHOST) +eq(config.smtp_port, 8025) dicts = [ {'common': '1', 'first': 'one'}, Index: syncmail =================================================================== RCS file: /cvsroot/cvs-syncmail/syncmail/syncmail,v retrieving revision 1.36.2.2 retrieving revision 1.36.2.3 diff -u -d -r1.36.2.2 -r1.36.2.3 --- syncmail 10 Jul 2003 06:08:47 -0000 1.36.2.2 +++ syncmail 10 Jul 2003 12:56:11 -0000 1.36.2.3 @@ -150,7 +150,7 @@ -def calculate_diff(entry, contextlines): +def calculate_diff(config, entry): file = entry.name oldrev = entry.revision newrev = entry.new_revision @@ -200,8 +200,8 @@ # File has been changed. # This /has/ to happen in the background, otherwise we'll run into CVS # lock contention. What a crock. - if contextlines > 0: - difftype = "-C " + str(contextlines) + if config.diff_type == "context": + difftype = "-C " + str(config.contextlines) else: difftype = "-u" diffcmd = "/usr/bin/cvs -f diff -kk %s --minimal -r %s -r %s %s" \ @@ -229,7 +229,7 @@ -def blast_mail(subject, people, entries, contextlines, fromhost, replyto): +def blast_mail(config, entries): # cannot wait for child process or that will cause parent to retain cvs # lock for too long. Urg! if not os.fork(): @@ -238,11 +238,10 @@ time.sleep(2) # Create the smtp connection to the localhost conn = smtplib.SMTP() - conn.connect(MAILHOST, MAILPORT) + conn.connect(config.smtp_server, config.smtp_port) user = pwd.getpwuid(os.getuid())[0] name = string.split(pwd.getpwuid(os.getuid())[4], ',')[0] - domain = fromhost or getfqdn() - address = '%s@%s' % (user, domain) + address = '%s@%s' % (user, config.fromhost) s = StringIO() sys.stdout = s datestamp = time.strftime('%a, %d %b %Y %H:%M:%S +0000', @@ -250,16 +249,16 @@ try: vars = {'address' : address, 'name' : quotename(name), - 'people' : people, - 'subject' : subject, + 'people' : config.people, + 'subject' : config.subject, 'version' : __version__, 'date' : datestamp, } print '''\ From: %(name)s <%(address)s> To: %(people)s''' % vars - if replyto: - print 'Reply-To: %s' % replyto + if config.replyto: + print 'Reply-To: %s' % config.replyto print '''\ Subject: %(subject)s Date: %(date)s @@ -269,10 +268,10 @@ # append the diffs if available print for entry in entries: - print calculate_diff(entry, contextlines) + print calculate_diff(config, entry) finally: sys.stdout = sys.__stdout__ - resp = conn.sendmail(address, people, s.getvalue()) + resp = conn.sendmail(address, config.people, s.getvalue()) conn.close() os._exit(0) @@ -533,7 +532,7 @@ cmdline['config-file'] = arg elif opt == '--no-config': if cmdline.has_key('config-file'): - def cmdline['config-file'] + del cmdline['config-file'] elif opt in ('-C', '--context'): cmdline['context-lines'] = arg elif opt == '-c': @@ -541,7 +540,7 @@ elif opt == '-u': cmdline['diff-type'] = 'unified' elif opt in ('-S', '--subject-prefix'): - cmdline['subject-prefix'] = arg + cmdline['subject-prefix'] = string.strip(arg) elif opt in ('-R', '--reply-to'): cmdline['reply-to'] = arg elif opt in ('-q', '--quiet'): @@ -552,6 +551,29 @@ cmdline['smtp-server'] = arg return cmdline, args +class Options: + def __init__(self, config, args): + self.contextlines = config.getint('context-lines') + self.verbose = config.getbool('verbose') + self.subject_prefix = config.get('subject-prefix') + self.replyto = config.get('reply-to') + self.fromhost = config.get('from-host') + self.email = config.getbool('email') + address = config.getaddress('smtp-server') + self.smtp_server, self.smtp_port = address + self.args = args + if args: + self.cvsinfo = args[0] + self.subject = self.subject_prefix + " " + self.cvsinfo + else: + self.cvsinfo = "" + self.subject = self.subject_prefix + " changes" + # The remaining args should be the email addresses + if len(args) >= 2: + people = string.join(args[1:], COMMASPACE) + else: + people = config.get("to") + def load_configuration(args, branch=None): cmdline, args = load_cmdline(args) cfgfile = cmdline.get('config-file') @@ -559,8 +581,8 @@ # to transform the path appropriately, since that won't be the # current directory when we try to read it. In fact, a wrong # config file may exist at the alternate location. - return load_configfile(cfgfile, cmdline, branch), args - + config = load_configfile(cfgfile, cmdline, branch) + return Options(config, args) def main(): @@ -568,31 +590,13 @@ # avoid threading so many positional args through everything. # load the options - config, args = load_configuration(sys.argv[1:], load_branch_name()) - contextlines = config.getint('context-lines') - verbose = config.getbool('verbose') - subject_prefix = config.get('subject-prefix') - replyto = config.get('reply-to') - fromhost = config.get('from-host') - email = config.getbool('email') - global MAILHOST, MAILPORT - MAILHOST, MAILPORT = config.getaddress('smtp-server') + config = load_configuration(sys.argv[1:], load_branch_name()) # args[0] is the specification containing the files that were # modified. The argument actually must be split, with the first component # containing the directory the checkin is being made in, relative to # $CVSROOT, followed by the list of files that are changing. - if not args: - usage(1, 'No CVS module specified') - subject = subject_prefix + args[0] - specs = string.split(args[0]) - del args[0] - - # The remaining args should be the email addresses - if args: - people = string.join(args, COMMASPACE) - else: - people = config.get("to") + specs = string.split(config.cvsinfo) if specs[-3:] == ['-', 'Imported', 'sources']: # What to do here should be configurable. @@ -601,12 +605,11 @@ changes = load_change_info() - if verbose: + if config.verbose: print 'Mailing %s...' % people print 'Generating notification message...' - blast_mail(subject, people, changes.values(), - contextlines, fromhost, replyto) - if verbose: + blast_mail(config, changes.values()) + if config.verbose: print 'Generating notification message... done.' |