source: trunk/lib/Directory.cc @ 274

Last change on this file since 274 was 274, checked in by Peter Johansson, 14 years ago

support for Trac. Fixes #125. Also added a config file.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1// $Id: Directory.cc 274 2007-05-02 09:05:55Z peter $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6
7  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Directory.h"
26#include "File.h"
27#include "html_utility.h"
28#include "Node.h"
29#include "SVN.h"
30#include "SVNlog.h"
31#include "utility.h"
32
33#include <algorithm>
34#include <cassert>
35#include <fstream>
36#include <functional>
37#include <iostream>
38#include <iterator>
39#include <list>
40#include <map>
41
42#include <cerrno> // Needed to check error state below.
43#include <dirent.h>
44#include <sys/stat.h>
45
46namespace theplu{
47namespace svndigest{
48
49
50  Directory::Directory(const u_int level, const std::string& path, 
51                       const std::string& output)
52    : Node(level,path,output)
53  {
54    using namespace std;
55    DIR* directory=opendir(path.c_str());    // C API from dirent.h
56    if (!directory)
57      throw NodeException("ERROR: opendir() failed; " + path +
58                          " is not a directory");
59    list<string> entries;
60    struct dirent* entry;
61    errno=0;  // Global variable used by C to track errors, from errno.h
62    while ((entry=readdir(directory)))       // C API from dirent.h
63      entries.push_back(string(entry->d_name));
64    if (errno)
65      throw NodeException("ERROR: readdir() failed on " + path);
66    closedir(directory);
67
68    SVN* svn=SVN::instance();
69    for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
70      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
71        string fullpath(path_+'/'+(*i));
72        switch (svn->version_controlled(fullpath)) {
73        case SVN::uptodate:
74          struct stat nodestat;                // C api from sys/stat.h
75          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
76          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
77            daughters_.push_back(new Directory(level_+1,fullpath,local_path()));
78          else
79            daughters_.push_back(new File(level_,fullpath,local_path()));
80          break;
81        case SVN::unresolved:
82          throw NodeException(fullpath+" is not up to date");
83        case SVN::unversioned: ; // do nothing
84        }
85      }
86    daughters_.sort(NodePtrLess());
87  }
88
89
90  Directory::~Directory(void)
91  {
92    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
93      delete *i;
94  }
95
96  bool Directory::dir(void) const
97  {
98    return true;
99  }
100
101  std::string Directory::href(void) const
102  { 
103    return name() + "/index.html";
104  }
105
106  const Stats& Directory::parse(const bool verbose)
107  {
108    stats_.reset();
109    // Directories themselved give no contribution to statistics.
110    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
111      if (!(*i)->ignore())
112        stats_ += (*i)->parse(verbose);
113    return stats_;
114  }
115
116
117  const std::string Directory::node_type(void) const
118  {
119    return std::string("directory");
120  }
121
122
123  void Directory::print_core(const bool verbose) const
124  {
125    // print daughter nodes, i.e., this function is recursive
126    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
127      (*i)->print(verbose);
128  }
129
130
131  void Directory::print_core(const std::string& user, 
132                             const std::string& line_type,
133                             const SVNlog& log) const
134  {
135
136    std::string outdir = user+"/"+line_type+"/"+local_path_;
137    if (local_path_=="")
138      outdir = user+"/"+line_type;
139
140    mkdir(outdir);
141    std::string imagedir = "images/"+line_type+"/"+local_path_;
142    if (user=="all")
143      mkdir(imagedir);
144
145    std::string html_name = outdir+"/index.html";
146    std::ofstream os(html_name.c_str());
147    assert(os.good());
148    if (local_path().empty())
149      print_header(os, name(), level_+2, user, line_type, "index.html");
150    else
151      print_header(os, name(), level_+2, user, line_type, 
152                   local_path()+"/index.html");
153    path_anchor(os);
154    os << "<p align=center>\n<img src='"; 
155    for (size_t i=0; i<level_; ++i)
156      os << "../";
157    os << "../../";
158    if (user=="all")
159      os << stats_.plot(imagedir+"/index.png", line_type);
160    else
161      os << imagedir << "/index.png";
162    os << "' alt='[plot]' border=0><br>\n";
163    os << "<h3>File Summary";
164    if (user!="all")
165      os << " for " << user;
166    os << "</h3>";     
167    os << "<table class=\"listings\">\n";
168    os << "<thead>";
169    os << "<tr>\n";
170    os << "<th>Node</th>\n";
171    os << "<th>Lines</th>\n";
172    os << "<th>Code</th>\n";
173    os << "<th>Comments</th>\n";
174    os << "<th>Other</th>\n";
175    os << "<th>Revision</th>\n";
176    os << "<th>Author</th>\n";
177    os << "</tr>\n</thead>\n";
178    os << "<tbody>";
179
180    std::string color("light");
181    if (level_){
182      os << "<tr class=\"light\">\n";
183      os << "<td class=\"directory\" colspan=\"6\">";
184      os << anchor("../index.html", "../");
185      os << "</td>\n</tr>\n";
186      color = "dark";
187    }
188
189    // print html links to daughter nodes
190    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
191      (*d)->html_tablerow(os,color, user);
192      if (color=="dark")
193        color = "light";
194      else
195        color = "dark";
196    }
197    os << "<tr class=\"" << color << "\">\n";
198    os << "<td>Total</td>\n";
199    if (user=="all"){
200      os << "<td>" << stats_.lines() << "</td>\n";
201      os << "<td>" << stats_.code() << "</td>\n";
202      os << "<td>" << stats_.comments() << "</td>\n";
203      os << "<td>" << stats_.empty() << "</td>\n";
204    }
205    else {
206      os << "<td>" << stats_.lines(user) << "</td>\n";
207      os << "<td>" << stats_.code(user) << "</td>\n";
208      os << "<td>" << stats_.comments(user) << "</td>\n";
209      os << "<td>" << stats_.empty(user) << "</td>\n";
210    }
211    os << "<td>" << trac_revision(stats_.last_changed_rev()) << "</td>\n";
212    os << "<td>" << author() << "</td>\n";
213    os << "</tr>\n";
214    os << "</table>\n";
215    print_author_summary(os, line_type, log);
216    os << "</p>\n";
217    print_footer(os);
218    os.close(); 
219  }
220
221
222  void Directory::print_copyright
223  (std::map<std::string, std::string>& alias) const {
224    if (!ignore()){
225      // print daughter nodes, i.e, this function is recursive
226      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
227        (*i)->print_copyright(alias);
228    }
229  }
230
231}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.