Edgewall Software

MacroBazaar: SvnFiles.2.py

File SvnFiles.2.py, 1.3 KB (added by krachtwerk, 6 years ago)

Small update to SvnFiles?, to make it work under 0.9

Line 
1"""
2
3Lists all files for a given svn directory matching a given filetype
4
5This macro takes 2 parameters, the path and the filetype
6
7"""
8
9import posixpath
10import os.path
11import re
12from StringIO import StringIO
13from svn import core, repos, fs
14
15def execute(hdf, args, env):
16
17  path = filetype = ''
18  if args:
19    argv = [arg.strip() for arg in args.split(',')]
20    if len(argv) > 0:
21      path = argv[0]
22      if len(argv) > 1:
23        filetype = argv[1]
24
25  repos_dir = env.config.get('trac', 'repository_dir')
26
27  core.apr_initialize()
28  pool = core.svn_pool_create(None)
29  # Remove any trailing slash or else subversion might abort
30  if not os.path.split(repos_dir)[1]:
31    repos_dir = os.path.split(repos_dir)[0]
32
33  rep = repos.svn_repos_open(repos_dir, pool)
34  fs_ptr = repos.svn_repos_fs(rep)
35
36  rev =fs.youngest_rev(fs_ptr, pool)
37  root =fs.revision_root(fs_ptr, rev, pool)
38  node_type =fs.check_path(root, path, pool)
39
40  buf = StringIO()
41  if node_type == core.svn_node_dir:
42    entries = fs.dir_entries(root, path, pool)
43
44    for item in entries.keys():
45      m = re.search(filetype,item)
46      if m:
47        fullpath = posixpath.join(path,item)
48        buf.write('<a class="ext-link" href="%s"><span class="icon"></span>%s</a>' % (env.href.file(fullpath), item))
49
50  return buf.getvalue()