From: Fred L. D. <fd...@us...> - 2003-07-14 17:01:36
|
Update of /cvsroot/cvs-syncmail/syncmail In directory sc8-pr-cvs1:/tmp/cvs-serv14964 Modified Files: Tag: new-config-branch syncmail Log Message: Merge in many changes and refactorings based on the development of branchctl. Index: syncmail =================================================================== RCS file: /cvsroot/cvs-syncmail/syncmail/syncmail,v retrieving revision 1.36.2.16 retrieving revision 1.36.2.17 diff -u -d -r1.36.2.16 -r1.36.2.17 --- syncmail 11 Jul 2003 18:47:35 -0000 1.36.2.16 +++ syncmail 14 Jul 2003 17:01:32 -0000 1.36.2.17 @@ -39,7 +39,7 @@ --config=file Use file as the configuration file. By default, a file named - syncmail.conf is used if it exists. If the name of the + %(DEFAULT_CONFIGURATION_FILE)s is used if it exists. If the name of the configuration file is relative, it is interpreted as relative to the CVSROOT administrative directory of the repository. --config is incompatible with --no-config. @@ -131,9 +131,21 @@ fqdn = 'localhost.localdomain' return fqdn - from cStringIO import StringIO +DEFAULT_CONFIGURATION_FILE = "syncmail.conf" + +DEFAULT_CONFIGURATION = { + "context-lines": "2", + "diff-type": "unified", + "email": "true", + "from-host": "$HOSTNAME", + "smtp-server": "localhost", + "smtp-server": "localhost", + "subject-prefix": "", + "verbose": "true", + } + # Which SMTP server to do we connect to? MAILHOST = 'localhost' MAILPORT = 25 @@ -315,6 +327,9 @@ parts = string.split(line, "/") _, name, revision, timestamp, options, tagdate = parts tagdate = string.rstrip(tagdate) or None + if tagdate: + assert tagdate[0] == "T" + tagdate = tagdate[1:] key = namekey(prefix, name) try: entry = mapping[key] @@ -336,12 +351,11 @@ else: return name -def load_change_info(prefix=None): +def load_entries(prefix=None): if prefix is not None: entries_fn = os.path.join(prefix, "CVS", "Entries") else: entries_fn = os.path.join("CVS", "Entries") - entries_log_fn = entries_fn + ".Log" mapping = {} f = open(entries_fn) while 1: @@ -364,6 +378,13 @@ get_entry(prefix, mapping, line, entries_fn) # else: bogus Entries line f.close() + return mapping + +def load_change_info(mapping, prefix=None): + if prefix is not None: + entries_log_fn = os.path.join(prefix, "CVS", "Entries.Log") + else: + entries_log_fn = os.path.join("CVS", "Entries.Log") if os.path.isfile(entries_log_fn): f = open(entries_log_fn) while 1: @@ -502,35 +523,23 @@ class ConfigurationDatabase: - # The defaults set covers what we need but might not get from the - # command line or configuration file. - defaults = { - "context-lines": "2", - "diff-type": "unified", - "email": "true", - "from-host": "$HOSTNAME", - "smtp-server": "localhost", - "smtp-server": "localhost", - "subject-prefix": "", - "verbose": "true", - } - def __init__(self, filename, cmdline, args): self.args = args self.cmdline = cmdline self.config = None if filename and os.path.isfile(filename): self.config = ConfigParser() - if hasattr(cp, "readfp"): + if hasattr(self.config, "readfp"): self.config.readfp(open(filename), filename) else: # We have to use this old method for compatibility with # ancient versions of Python. self.config.read([filename]) - def get_config(self, branch): + def get_config(self, branch=None): dicts = [] if self.config: + cp = self.config if branch: dicts.append(get_section_as_dict(cp, "branch " + branch)) dicts.append(get_section_as_dict(cp, "branch")) @@ -539,9 +548,71 @@ else: dicts.append(self.cmdline) dicts = filter(None, dicts) - dicts.append(self.defaults) + dicts.append(DEFAULT_CONFIGURATION) return Options(OptionLookup(dicts, branch), self.args) +class OptionsBase: + + def __init__(self, config, args): + self.args = args + self.verbose = config.getbool('verbose') + fn = os.path.join("CVS", "Repository") + if os.path.isfile(fn): + f = open(fn) + self.repodir = string.strip(f.readline()) + f.close() + else: + self.repodir = os.curdir + + +def get_admin_file(name): + if os.environ.has_key("CVSROOT"): + p = os.path.join(os.environ["CVSROOT"], "CVSROOT", name) + return os.path.abspath(p) + else: + return os.path.join("CVSROOT", name) + +def load_configuration(args, branch=None): + cmdline, args = load_cmdline(args) + defconfig = get_admin_file(DEFAULT_CONFIGURATION_FILE) + if cmdline.has_key('config-file'): + cfgfile = get_admin_file(cmdline['config-file']) + del cmdline['config-file'] + elif os.path.isfile(defconfig): + cfgfile = defconfig + else: + cfgfile = None + return ConfigurationDatabase(cfgfile, cmdline, args) + + +class Options(OptionsBase): + + def __init__(self, config, args): + OptionsBase.__init__(self, config, args) + + self.contextlines = config.getint('context-lines') + self.diff_type = config.get('diff-type') + if self.diff_type not in ('context', 'unified'): + usage(1, "invalid diff-type specified in configuration file") + 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 + 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: + self.people = string.join(args[1:], COMMASPACE) + else: + self.people = config.get("to") + + def load_cmdline(args): try: opts, args = getopt.getopt(args, @@ -584,64 +655,11 @@ set('smtp-server', arg) return cmdline, args -class Options: - def __init__(self, config, args): - self.contextlines = config.getint('context-lines') - self.diff_type = config.get('diff-type') - if self.diff_type not in ('context', 'unified'): - usage(1, "invalid diff-type specified in configuration file") - 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: - self.people = string.join(args[1:], COMMASPACE) - else: - self.people = config.get("to") - - fn = os.path.join("CVS", "Repository") - if os.path.isfile(fn): - f = open(fn) - self.repodir = string.strip(f.readline()) - f.close() - else: - self.repodir = os.curdir - -def get_admin_file(name): - if os.environ.has_key("CVSROOT"): - p = os.path.join(os.environ["CVSROOT"], "CVSROOT", name) - return os.path.abspath(p) - else: - return os.path.join("CVSROOT", name) -def load_configuration(args, branch=None): - cmdline, args = load_cmdline(args) - defconfig = get_admin_file("syncmail.conf") - if cmdline.has_key('config-file'): - cfgfile = get_admin_file(cmdline['config-file']) - del cmdline['config-file'] - elif os.path.isfile(defconfig): - cfgfile = defconfig - else: - cfgfile = None - db = ConfigurationDatabase(cfgfile, cmdline, args) - return db.get_config(branch) - - def main(): # load the options - config = load_configuration(sys.argv[1:], load_branch_name()) + db = load_configuration(sys.argv[1:]) + config = db.get_config(load_branch_name()) if not config.people: # Nobody to send mail to! @@ -660,7 +678,7 @@ print 'Not sending email for imported sources.' return - items = load_change_info().items() + items = load_change_info(load_entries()).items() items.sort() changes = [] for name, entry in items: |