Menu

#70 SLOCCount XML output

v1.61
closed
nobody
1
2015-06-27
2014-05-10
Anonymous
No

Could you add a SLOCCount output?
An SLOCCount output would be extremely handy since some plugin (like Jenkins' SLOCCount Plugin) only support this output format.
Unfortunately, SLOCCount is written in C and thus it's not multi-platform and causes problems on my Jenkins server. However, this plugin is very cool and gives me detailed information how my code has evolved over time.

I know, there is a way to add CLOCK support to this plugin but I need to transform CLOC's XML output to the sc file using xsltproc and it may not work as expected (and I think it is annoying to add this xsl template to every project, especially since I'm only allowed to change certain parts of some project).

Discussion

  • Al Danial

    Al Danial - 2014-05-12

    Attach an example of the kind of input accepted by the Jenkins plugin and I'll give it a shot.

     
    • Matteo Kloiber

      Matteo Kloiber - 2014-05-12
      This report has been generated by cloc 1.60.
      For more details see: http://cloc.sourceforge.net
      
      295 Java    Users   [...]/test/de/matt3o12/robotic/DisplayUtilTest.java
      124 Java    Users   [...]/src/de/matt3o12/robotic/DisplayUnit.java
      52  Java    Users   [...]/src/de/matt3o12/robotic/dialog/DialogManager.java
      48  Java    Users   [...]/src/de/matt3o12/robotic/Run.java
      45  Java    Users   [...]/test/de/matt3o12/robotic/RunTest.java
      43  Java    Users   [...]/src/de/matt3o12/robotic/dialog/SimpleDialog.java
      31  Java    Users   [...]/src/de/matt3o12/robotic/programs/DialogProgram.java
      22  Java    Users   [...]/src/de/matt3o12/robotic/dialog/MessageDialog.java
      17  Java    Users   [...]/src/de/matt3o12/robotic/programs/ShowManual.java
      14  Java    Users   [...]/src/de/matt3o12/robotic/MessageTooLongException.java
      7   Java    Users   [...]/src/de/matt3o12/robotic/dialog/SimpleDialogResult.java
      7   Java    Users   [...]/src/de/matt3o12/robotic/programs/Program.java
      7   Java    Users   [...]/src/de/matt3o12/robotic/dialog/DialogListener.java
      6   Java    Users   [...]/src/de/matt3o12/robotic/dialog/Dialog.java
      

      The numbers are the lines of code. Java is the used language. The module ("Users") is a little bit buggy when using the cloc to SCLOCcount converter script.
      In my case it should be "test" and "src" because I enter cloc test/ src/ to generate the results and that's what SCLOCcount would've done, too. However, if only one dictionary was provided, SCLOCcount uses the following algorithm to determine what modules have been used:

         If you give sloccount only a single directory, sloccount tries to automatically find a reasonable breakdown for purposes of reporting (so it'll pro-
         duce a useful report).  In this case, if the directory has at least two subdirectories, then those subdirectories will be used as the breakdown.  If
         the single directory contains files as well as directories (or if you give sloccount some files as parameters), those files will be assigned to  the
         directory "top_dir" so you can tell them apart from other contents.  Finally, if there's a subdirectory named "src", then that subdirectory is again
         broken down, with all the further subdirectories prefixed with "src_".  Thus, if directory "X" has a subdirectory "src", which contains subdirectory
         "modules", sloccount will report a separate count for "src_modules".
      

      (copied from the man page).

      I don't think this works very well (it creates very weird results for some of my projects). So, I'd suggest you just return one module if only one directory was given. You can implement this algorithm if you want the results to be close to the original SCLOCCount script but I don't think it's worth the time.

      The last value is the absolute path to every single file. I've shorten with [...].

      I also attached some results created by the original script. They aren't much different from the results above. However, one file shows what the results look like when no arguments were entered.

       
  • Al Danial

    Al Danial - 2014-05-13

    Let me know if the output of this is sufficient for your needs:

    cloc --quiet --csv --by-file /path/to/your/code | perl -MFile::Basename -a -F, -n -e 'printf "%5d %-12s %-12s %s\n", $F[4], $F[0], basename(dirname("$F[1]")), $F[1]' | grep -Pv "language.*?filename" | grep -Pv "\s+files\s*"
    
     
  • Matteo Kloiber

    Matteo Kloiber - 2014-05-13

    The grep commands don't work (grep version: grep (GNU grep) 2.5.1 on OS X 9). It seems that my grep doesn't understand -P).
    The output without the grep commands is:

        0 
                .            
        0 language     .            filename
      124 Java         robotic      src/de/matt3o12/robotic/DisplayUnit.java
       59 Java         dialog       src/de/matt3o12/robotic/dialog/DialogManager.java
       48 Java         robotic      src/de/matt3o12/robotic/Run.java
       43 Java         dialog       src/de/matt3o12/robotic/dialog/SimpleDialog.java
       31 Java         programs     src/de/matt3o12/robotic/programs/DialogProgram.java
       22 Java         dialog       src/de/matt3o12/robotic/dialog/MessageDialog.java
       17 Java         programs     src/de/matt3o12/robotic/programs/ShowManual.java
       14 Java         robotic      src/de/matt3o12/robotic/MessageTooLongException.java
        7 Java         programs     src/de/matt3o12/robotic/programs/Program.java
        7 Java         dialog       src/de/matt3o12/robotic/dialog/SimpleDialogResult.java
        7 Java         dialog       src/de/matt3o12/robotic/dialog/DialogListener.java
        6 Java         dialog       src/de/matt3o12/robotic/dialog/Dialog.java
        0 Java         dialog       src/de/matt3o12/robotic/dialog/YesNoDialog.java
    

    The first lines won't properly work and sloccount generates absolut paths. These paths are relative.

     

    Last edit: Matteo Kloiber 2014-05-13
  • Al Danial

    Al Danial - 2014-05-13

    Interesting that the OS X version of GNU grep doesn't support the -P ("use Perl-style regular expressions") option; GNU grep 2.5.1 on Linux does.

    Regarding relative v. absolute paths, if you give cloc absolute paths as inputs, it will print absolute paths in the output. However, absolute paths can be forced with the File::Spec-rel2abs() function.

    This version uses grep without -P, removes the bad first line, and forces absolute paths:

    cloc --quiet --csv --by-file /path/to/your/code | perl -MFile::Spec -MFile::Basename -a -F, -n -e '$full=File::Spec->rel2abs($F[1]); printf "%5d %-12s %-12s %s\n", $F[4], $F[0], basename(dirname("$full")), $full' | grep -v "Counting:" | grep "^ *[0-9]" | grep -v "0 language"
    

    True, it is not pretty, but should get the job done.

     
  • Matteo Kloiber

    Matteo Kloiber - 2014-05-15

    It works, thank you! :)

     
  • Al Danial

    Al Danial - 2014-05-18
    • status: open --> pending
     
  • Al Danial

    Al Danial - 2015-06-27
    • status: pending --> closed
     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.