SLOC count with Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elton
    Visitor
    • Jul 2013
    • 3

    SLOC count with Python

    Hi everyone,

    I am new to Beyond Compare so bare with me. I am currently brainstorming a Python code that will read in all the source files from a directory (including source files from subfolders) in a network, take the physical SLOC (Source Line of Code) count for each file, then sum them up and output it on a xml file.

    From what I have read on the "Automating with Scripts" page, it does not seem BC3 has a library set that I can use on other languages like Python. So what I will probably have to do is write a BC script to take in all the source files from a directory, then get and output the SLOC count. Then write a Python code to run the script, then parse the SLOC counts from the output file, add all the SLOC counts up (unless I can get the total SLOC count directly), and generate an xml file with that info. Do you think that is the most logical way of doing things?

    I am not entirely sure what BC scripts are capable of, like what functions to use to get all the files from a directory. Also, I don't need to compare any files. I just need the SLOC counts. I guess I could name the same directory twice, but it seems redundant.

    Any help is appreciated.

    Thanks.

    Elton
  • Chris
    Team Scooter
    • Oct 2007
    • 5538

    #2
    Beyond Compare doesn't have an API, you can only call scripts from the command line.

    Here's an example script that will output statistics on important and unimportant lines of text in a single folder:

    load c:\folder
    expand all
    select all.files
    text-report layout:summary output-tout.txt

    To run the above script, save it as a text file and use the command line:
    bcompare.exe @c:\script.txt

    The @ character tells Beyond Compare to run a file as a script rather than load it for comparison.

    In the context of the report, unimportant lines are those that contain only whitespace and/or comments.

    The summary layout is designed to be human readable. You can also use "text-report layout:statistics output-tout.txt" to output results as comma separated values. The statistics layout also includes a total at the end, summary doesn't report a total.
    Chris K Scooter Software

    Comment

    • Elton
      Visitor
      • Jul 2013
      • 3

      #3
      Hi Chris,

      Thank you for your suggestion! Much is appreciated! It really helped me a lot. Since there is no API, what I ended up doing on Python is to generate a bcscript.txt file and a batch file to execute the script, and it worked great. This is my output:



      This is pretty much what I wanted. However, bcscript.txt, cmdBCScript.bat, rawSLOC.csv, and SLOCCounter.py should not be counted because those are not the source files. Those are files I am using to get my SLOC count. I was wondering if there are any BC script commands to filter specific files names. It should also not read in file with the *.txt extension, so I was wondering how to filter those out also. So really, the only file that should have been counted for is the findDup.cpp.

      Thanks.

      Elton

      Comment

      • Chris
        Team Scooter
        • Oct 2007
        • 5538

        #4
        Use the filter command to limit the files affected by the script.

        Example:

        filter "-*.py;-*.txt;-*.bat;-*.csv"
        load c:\folder
        expand all
        select all.files
        text-report layout:summary output-tout.txt

        Filters can be file masks or exact filenames, with multiple filters separated by semicolons.
        Chris K Scooter Software

        Comment

        • Chris
          Team Scooter
          • Oct 2007
          • 5538

          #5
          You can also use "filter *.cpp" to only see files with .cpp extensions.
          Chris K Scooter Software

          Comment

          • Elton
            Visitor
            • Jul 2013
            • 3

            #6
            Thanks, Chris! My program is running perfectly. I just found the "Scripting Reference" page too at the support reference, so I will look more into it other scripting functions.

            Thanks again.

            Elton

            Comment

            Working...