📄 mailer.py
字号:
output.write('Log:\n%s\n'
% (repos.get_rev_prop(svn.core.SVN_PROP_REVISION_LOG) or ''))
# these are sorted by path already
for path, change in changelist:
generate_diff(output, cfg, repos, date, change, group, params, pool)
def _select_adds(change):
return change.added
def _select_deletes(change):
return change.path is None
def _select_modifies(change):
return not change.added and change.path is not None
def generate_list(output, header, changelist, selection):
items = [ ]
for path, change in changelist:
if selection(change):
items.append((path, change))
if items:
output.write('%s:\n' % header)
for fname, change in items:
if change.item_kind == svn.core.svn_node_dir:
is_dir = '/'
else:
is_dir = ''
if change.prop_changes:
if change.text_changed:
props = ' (contents, props changed)'
else:
props = ' (props changed)'
else:
props = ''
output.write(' %s%s%s\n' % (fname, is_dir, props))
if change.added and change.base_path:
if is_dir:
text = ''
elif change.text_changed:
text = ', changed'
else:
text = ' unchanged'
output.write(' - copied%s from r%d, %s%s\n'
% (text, change.base_rev, change.base_path[1:], is_dir))
def generate_diff(output, cfg, repos, date, change, group, params, pool):
if change.item_kind == svn.core.svn_node_dir:
# all changes were printed in the summary. nothing to do.
return
gen_diffs = cfg.get('generate_diffs', group, params)
### Do a little dance for deprecated options. Note that even if you
### don't have an option anywhere in your configuration file, it
### still gets returned as non-None.
if len(gen_diffs):
diff_add = False
diff_copy = False
diff_delete = False
diff_modify = False
list = string.split(gen_diffs, " ")
for item in list:
if item == 'add':
diff_add = True
if item == 'copy':
diff_copy = True
if item == 'delete':
diff_delete = True
if item == 'modify':
diff_modify = True
else:
diff_add = True
diff_copy = True
diff_delete = True
diff_modify = True
### These options are deprecated
suppress = cfg.get('suppress_deletes', group, params)
if suppress == 'yes':
diff_delete = False
suppress = cfg.get('suppress_adds', group, params)
if suppress == 'yes':
diff_add = False
if not change.path:
### params is a bit silly here
if diff_delete == False:
# a record of the deletion is in the summary. no need to write
# anything further here.
return
output.write('\nDeleted: %s\n' % change.base_path)
diff = svn.fs.FileDiff(repos.get_root(change.base_rev),
change.base_path, None, None, pool)
label1 = '%s\t%s' % (change.base_path, date)
label2 = '(empty file)'
singular = True
elif change.added:
if change.base_path and (change.base_rev != -1):
# this file was copied.
if not change.text_changed:
# copies with no changes are reported in the header, so we can just
# skip them here.
return
if diff_copy == False:
# a record of the copy is in the summary, no need to write
# anything further here.
return
# note that we strip the leading slash from the base (copyfrom) path
output.write('\nCopied: %s (from r%d, %s)\n'
% (change.path, change.base_rev, change.base_path[1:]))
diff = svn.fs.FileDiff(repos.get_root(change.base_rev),
change.base_path[1:],
repos.root_this, change.path,
pool)
label1 = change.base_path[1:] + '\t(original)'
label2 = '%s\t%s' % (change.path, date)
singular = False
else:
if diff_add == False:
# a record of the addition is in the summary. no need to write
# anything further here.
return
output.write('\nAdded: %s\n' % change.path)
diff = svn.fs.FileDiff(None, None, repos.root_this, change.path, pool)
label1 = '(empty file)'
label2 = '%s\t%s' % (change.path, date)
singular = True
elif not change.text_changed:
# don't bother to show an empty diff. prolly just a prop change.
return
else:
if diff_modify == False:
# a record of the modification is in the summary, no need to write
# anything further here.
return
output.write('\nModified: %s\n' % change.path)
diff = svn.fs.FileDiff(repos.get_root(change.base_rev),
change.base_path[1:],
repos.root_this, change.path,
pool)
label1 = change.base_path[1:] + '\t(original)'
label2 = '%s\t%s' % (change.path, date)
singular = False
output.write(SEPARATOR + '\n')
if diff.either_binary():
if singular:
output.write('Binary file. No diff available.\n')
else:
output.write('Binary files. No diff available.\n')
return
### do something with change.prop_changes
src_fname, dst_fname = diff.get_files()
output.run(cfg.get_diff_cmd({
'label_from' : label1,
'label_to' : label2,
'from' : src_fname,
'to' : dst_fname,
}))
class Repository:
"Hold roots and other information about the repository."
def __init__(self, repos_dir, rev, pool):
self.repos_dir = repos_dir
self.rev = rev
self.pool = pool
self.repos_ptr = svn.repos.svn_repos_open(repos_dir, pool)
self.fs_ptr = svn.repos.svn_repos_fs(self.repos_ptr)
self.roots = { }
self.root_this = self.get_root(rev)
self.author = self.get_rev_prop(svn.core.SVN_PROP_REVISION_AUTHOR)
def get_rev_prop(self, propname):
return svn.fs.revision_prop(self.fs_ptr, self.rev, propname, self.pool)
def get_root(self, rev):
try:
return self.roots[rev]
except KeyError:
pass
root = self.roots[rev] = svn.fs.revision_root(self.fs_ptr, rev, self.pool)
return root
class Config:
# The predefined configuration sections. These are omitted from the
# set of groups.
_predefined = ('general', 'defaults')
def __init__(self, fname, repos, global_params):
cp = ConfigParser.ConfigParser()
cp.read(fname)
# record the (non-default) groups that we find
self._groups = [ ]
for section in cp.sections():
if not hasattr(self, section):
section_ob = _sub_section()
setattr(self, section, section_ob)
if section not in self._predefined:
self._groups.append((section, section_ob))
else:
section_ob = getattr(self, section)
for option in cp.options(section):
# get the raw value -- we use the same format for *our* interpolation
value = cp.get(section, option, raw=1)
setattr(section_ob, option, value)
### do some better splitting to enable quoting of spaces
self._diff_cmd = string.split(self.general.diff)
# these params are always available, although they may be overridden
self._global_params = global_params.copy()
self._prep_groups(repos)
def get_diff_cmd(self, args):
cmd = [ ]
for part in self._diff_cmd:
cmd.append(part % args)
return cmd
def is_set(self, option):
"""Return None if the option is not set; otherwise, its value is returned.
The option is specified as a dotted symbol, such as 'general.mail_command'
"""
parts = string.split(option, '.')
ob = self
for part in string.split(option, '.'):
if not hasattr(ob, part):
return None
ob = getattr(ob, part)
return ob
def get(self, option, group, params):
if group:
sub = getattr(self, group)
if hasattr(sub, option):
return getattr(sub, option) % params
return getattr(self.defaults, option, '') % params
def _prep_groups(self, repos):
self._group_re = [ ]
repos_dir = os.path.abspath(repos.repos_dir)
# compute the default repository-based parameters. start with some
# basic parameters, then bring in the regex-based params.
default_params = self._global_params.copy()
try:
match = re.match(self.defaults.for_repos, repos_dir)
if match:
default_params.update(match.groupdict())
except AttributeError:
# there is no self.defaults.for_repos
pass
# select the groups that apply to this repository
for group, sub in self._groups:
params = default_params
if hasattr(sub, 'for_repos'):
match = re.match(sub.for_repos, repos_dir)
if not match:
continue
params = self._global_params.copy()
params.update(match.groupdict())
# if a matching rule hasn't been given, then use the empty string
# as it will match all paths
for_paths = getattr(sub, 'for_paths', '')
self._group_re.append((group, re.compile(for_paths), params))
# after all the groups are done, add in the default group
try:
self._group_re.append((None,
re.compile(self.defaults.for_paths),
default_params))
except AttributeError:
# there is no self.defaults.for_paths
pass
def which_groups(self, path):
"Return the path's associated groups."
groups = []
for group, pattern, repos_params in self._group_re:
match = pattern.match(path)
if match:
params = repos_params.copy()
params.update(match.groupdict())
groups.append((group, params))
if not groups:
groups.append((None, self._global_params))
return groups
class _sub_section:
pass
class MissingConfig(Exception):
pass
class UnknownSubcommand(Exception):
pass
# enable True/False in older vsns of Python
try:
_unused = True
except NameError:
True = 1
False = 0
if __name__ == '__main__':
def usage():
sys.stderr.write(
'''USAGE: %s commit REPOS-DIR REVISION [CONFIG-FILE]
%s propchange REPOS-DIR REVISION AUTHOR PROPNAME [CONFIG-FILE]
'''
% (sys.argv[0], sys.argv[0]))
sys.exit(1)
if len(sys.argv) < 4:
usage()
cmd = sys.argv[1]
repos_dir = sys.argv[2]
revision = int(sys.argv[3])
config_fname = None
author = None
propname = None
if cmd == 'commit':
if len(sys.argv) > 5:
usage()
if len(sys.argv) > 4:
config_fname = sys.argv[4]
elif cmd == 'propchange':
if len(sys.argv) < 6 or len(sys.argv) > 7:
usage()
author = sys.argv[4]
propname = sys.argv[5]
if len(sys.argv) > 6:
config_fname = sys.argv[6]
else:
usage()
if config_fname is None:
# default to REPOS-DIR/conf/mailer.conf
config_fname = os.path.join(repos_dir, 'conf', 'mailer.conf')
if not os.path.exists(config_fname):
# okay. look for 'mailer.conf' as a sibling of this script
config_fname = os.path.join(os.path.dirname(sys.argv[0]), 'mailer.conf')
if not os.path.exists(config_fname):
raise MissingConfig(config_fname)
### run some validation on these params
svn.core.run_app(main, cmd, config_fname, repos_dir, revision,
author, propname)
# ------------------------------------------------------------------------
# TODO
#
# * add configuration options
# - default options [DONE]
# - per-group overrides [DONE]
# - group selection based on repos and on path [DONE]
# - each group defines delivery info:
# o how to construct From: [DONE]
# o how to construct To: [DONE]
# o subject line prefixes [DONE]
# o whether to set Reply-To and/or Mail-Followup-To
# (btw: it is legal do set Reply-To since this is the originator of the
# mail; i.e. different from MLMs that munge it)
# - each group defines content construction:
# o max size of diff before trimming
# o max size of entire commit message before truncation
# o flag to disable generation of add/delete diffs
# - per-repository configuration
# o extra config living in repos
# o how to construct a ViewCVS URL for the diff
# o optional, non-mail log file
# o look up authors (username -> email; for the From: header) in a
# file(s) or DBM
# - put the commit author into the params dict [DONE]
# - if the subject line gets too long, then trim it. configurable?
# * get rid of global functions that should properly be class methods
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -