source: trunk/lib/Directory.cc @ 864

Last change on this file since 864 was 864, checked in by Jari Häkkinen, 12 years ago

Addresses #97. All plots generated. Now its time to go for publication quality plots.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.9 KB
Line 
1// $Id: Directory.cc 864 2009-11-20 20:12:30Z jari $
2
3/*
4  Copyright (C) 2005, 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://dev.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Directory.h"
23
24#include "Alias.h"
25#include "File.h"
26#include "html_utility.h"
27#include "Node.h"
28#include "SVN.h"
29#include "SVNlog.h"
30#include "utility.h"
31
32#include <algorithm>
33#include <cassert>
34#include <fstream>
35#include <functional>
36#include <iostream>
37#include <iterator>
38#include <list>
39#include <map>
40#include <sstream>
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 unsigned int level, const std::string& path, 
51                       const std::string& output)
52    : Node(level,path,output)
53  {
54    output_dir_=local_path();
55    if (!output_dir_.empty())
56      output_dir_+='/';
57
58    using namespace std;
59    DIR* directory=opendir(path.c_str());    // C API from dirent.h
60    if (!directory)
61      throw NodeException("ERROR: opendir() failed; " + path +
62                          " is not a directory");
63    list<string> entries;
64    struct dirent* entry;
65    errno=0;  // Global variable used by C to track errors, from errno.h
66    while ((entry=readdir(directory)))       // C API from dirent.h
67      entries.push_back(string(entry->d_name));
68    if (errno)
69      throw NodeException("ERROR: readdir() failed on " + path);
70    closedir(directory);
71
72    SVN* svn=SVN::instance();
73    for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
74      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
75        string fullpath(path_+'/'+(*i));
76        switch (svn->version_controlled(fullpath)) {
77        case SVN::uptodate:
78          struct stat nodestat;                // C api from sys/stat.h
79          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
80          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
81            daughters_.push_back(new Directory(level_+1,fullpath,local_path()));
82          else
83            daughters_.push_back(new File(level_,fullpath,local_path()));
84          break;
85        case SVN::unresolved:
86          throw NodeException(fullpath+" is not up to date");
87        case SVN::unversioned: ; // do nothing
88        }
89      }
90    std::sort(daughters_.begin(), daughters_.end(), NodePtrLess());
91  }
92
93
94  Directory::~Directory(void)
95  {
96    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
97      delete *i;
98  }
99
100  bool Directory::dir(void) const
101  {
102    return true;
103  }
104
105  std::string Directory::href(void) const
106  { 
107    return name() + "/index.html";
108  }
109
110
111  svn_revnum_t Directory::last_changed_rev(void) const
112  {
113    svn_revnum_t res = svn_info().last_changed_rev();
114    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
115      res = std::max(res, (*i)->last_changed_rev());
116    return res;
117  }
118
119
120  void Directory::log_core(SVNlog& log) const
121  {
122    for (NodeConstIterator i(daughters_.begin()); i != daughters_.end(); ++i)
123      log += (*i)->log();
124  }
125
126  std::string Directory::node_type(void) const
127  {
128    return std::string("directory");
129  }
130
131
132  std::string Directory::output_path(void) const
133  {
134    return output_dir()+"index.html";
135  }
136
137  const StatsCollection& Directory::parse(bool verbose, bool ignore)
138  {
139    stats_.reset();
140    // Directories themselved give no contribution to statistics.
141    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
142      if (!(*i)->ignore())
143        stats_ += (*i)->parse(verbose, ignore);
144    return stats_;
145  }
146
147
148  void Directory::print_core(const bool verbose) const
149  {
150    mkdir("blame_output/" + local_path_);
151    // print daughter nodes
152    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
153      (*i)->print(verbose);
154  }
155
156
157  void Directory::print_core(const std::string& stats_type,
158                             const std::string& user, 
159                             const std::string& line_type,
160                             const SVNlog& log) const
161  {
162
163    const Stats& stats = stats_[stats_type];
164    std::string outdir = stats_type+"/"+user+"/"+line_type+"/"+local_path_;
165    if (local_path_=="")
166      outdir = stats_type+"/"+user+"/"+line_type;
167
168    mkdir(outdir);
169    std::string imagedir = stats_type+"/"+"images/"+line_type+"/"+local_path_;
170    if (user=="all")
171      mkdir(imagedir);
172    std::string html_name = outdir+"/index.html";
173    std::ofstream os(html_name.c_str());
174    assert(os.good());
175    if (local_path().empty())
176      print_header(os, name(), level_+3, user, line_type, "index.html", 
177                   stats_type);
178    else
179      print_header(os, name(), level_+3, user, line_type, 
180                   local_path()+"/index.html", stats_type);
181    path_anchor(os);
182
183    std::stringstream ss;
184    for (size_t i=0; i<level_; ++i)
185      ss << "../";
186    ss << "../../../";
187    if (user=="all")
188      ss << stats.plot(imagedir+"/index.svg", line_type);
189    else
190      os << imagedir << "/index.svg";
191    os << "<p class=\"plot\">\n"; 
192    os << "<object data='" << ss.str() << "' type='image/svg+xml' width='600'\n"
193       << "<embed src='" << ss.str() << "' type='image/svg+xml' width='600' />\n"
194       << "</object>\n";
195    os << "</p>\n";
196
197    os << "<h3>File Summary";
198    if (user!="all")
199      os << " for " << user;
200    os << "</h3>";     
201    os << "<table class=\"listings\">\n";
202    os << "<thead>\n";
203    os << "<tr>\n";
204    os << "<th>Node</th>\n";
205    os << "<th>Lines</th>\n";
206    os << "<th>Code</th>\n";
207    os << "<th>Comments</th>\n";
208    os << "<th>Other</th>\n";
209    os << "<th>Revision</th>\n";
210    os << "<th>Author</th>\n";
211    os << "</tr>\n</thead>\n";
212    os << "<tbody>\n";
213
214    std::string color("light");
215    if (level_){
216      os << "<tr class=\"light\">\n";
217      os << "<td class=\"directory\" colspan=\"7\">";
218      os << anchor("../index.html", "../");
219      os << "</td>\n</tr>\n";
220      color = "dark";
221    }
222
223    // print html links to daughter nodes
224    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
225      (*d)->html_tablerow(os,stats_type, color, user);
226      if (color=="dark")
227        color = "light";
228      else
229        color = "dark";
230    }
231    os << "<tr class=\"" << color << "\">\n";
232    os << "<td>Total</td>\n";
233    if (user=="all"){
234      os << "<td>" << stats.lines() << "</td>\n";
235      os << "<td>" << stats.code() << "</td>\n";
236      os << "<td>" << stats.comments() << "</td>\n";
237      os << "<td>" << stats.empty() << "</td>\n";
238    }
239    else {
240      os << "<td>" << stats.lines(user); 
241      if (stats.lines(user)) 
242        os << " (" << percent(stats.lines(user),stats.lines()) << "%)"; 
243      os << "</td>\n";
244      os << "<td>" << stats.code(user); 
245      if (stats.code(user)) 
246        os << " (" << percent(stats.code(user),stats.code()) << "%)"; 
247      os << "</td>\n";
248      os << "<td>" << stats.comments(user); 
249      if (stats.comments(user)) 
250        os << " (" << percent(stats.comments(user),stats.comments()) << "%)"; 
251      os << "</td>\n";
252      os << "<td>" << stats.empty(user); 
253      if (stats.empty(user)) 
254        os << " (" << percent(stats.empty(user),stats.empty()) << "%)"; 
255      os << "</td>\n";
256    }
257    os << "<td>" << trac_revision(last_changed_rev()) << "</td>\n";
258    os << "<td>" << author() << "</td>\n";
259    os << "</tr>\n";
260    os << "</tbody>\n";
261    os << "</table>\n";
262    print_author_summary(os, stats, line_type, log);
263    os << "\n";
264    print_footer(os);
265    os.close(); 
266  }
267
268
269  void Directory::print_copyright(std::map<std::string, Alias>& alias, 
270                                  bool verbose,
271                                  const std::map<int,svn_revnum_t>& y2r) const 
272  {
273    if (!ignore()){
274      // print daughter nodes, i.e, this function is recursive
275      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
276        (*i)->print_copyright(alias, verbose, y2r);
277    }
278  }
279
280}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.