16 Dec 2011 @ 11:19 AM 

It’s a relatively trivial task, really easy to do with the command prompt and GNU wc:

  1.  
  2. for /f "delims=" %P in (‘dir /s /b *.cs’) do wc -l "%P" >> D:\codewordcounts.txt
  3.  

I executed this within the desired directory (my BASeBlock source folder, if you must know) and the result was a file filled with numbers and files; I wrote a quick python script to parse that and add up the numbers that were at the start of each line, but then I figured, why not just write the whole think in python and forget about the rest of it, so I did.

  1.  
  2. #!/usr/bin/python
  3.  
  4. import sys
  5.  
  6. import fnmatch
  7.  
  8. import os
  9.  
  10.  
  11.  
  12. def searchfolder(folder,filemask,excludemask):
  13.  
  14.     filelist= []
  15.  
  16.     for root,subFolders,files in os.walk(folder):
  17.  
  18.         for file in files:
  19.  
  20.             buildpath=os.path.join(root,file)
  21.  
  22.             if os.path.isfile(buildpath):            
  23.  
  24.                 if fnmatch.fnmatch(buildpath,filemask):
  25.  
  26.                     if not fnmatch.fnmatch(buildpath,excludemask):
  27.                         filelist.append(buildpath)
  28.  
  29.     return filelist
  30.  
  31.    
  32.  
  33.  
  34.  
  35.  
  36.  
  37. def Countlines(filename):
  38.  
  39.     #counts the lines in filename.
  40.  
  41.     countit = open(filename)
  42.  
  43.     runner=0
  44.  
  45.     for line in countit:
  46.  
  47.         runner=runner+1
  48.  
  49.        
  50.  
  51.     return runner
  52.  
  53.    
  54.  
  55.    
  56.  
  57.  
  58.  
  59.  
  60.  
  61. searchfor = sys.argv [1]
  62.  
  63. searchin = sys.argv [2]
  64.  
  65. excludethese = sys.argv [3]
  66. print "examining files matching " + searchfor + " in directory " + searchin
  67.  + " excluding " + excludethese
  68. totalcount=0
  69.  
  70. for countthese in searchfolder(searchin,searchfor,excludethese):
  71.  
  72.     #print Countlines(countthese)
  73.  
  74.     totalcount = totalcount + Countlines(countthese)
  75.  
  76.  
  77.  
  78. print "total line count:" + totalcount
  79.  
  80.  
  81.  
  82.  
  83.  

It’s a rather basic script, and I don’t even comment it as much as I ought to. I just wanted a quick tool to be able to count the lines of code in a given directory for a given source file type. Ideally, I’d allow for multiple types, but I didn’t want to complicate the argument parsing code too much. The counting method is pretty barren, it just loops over every line and increments a counter. It seems to work relatively fast. It quickly gave me the result I wanted, which was that BASeBlock’s .cs files comprise about 53K lines of code, excluding the .designer.cs files (thus the third argument). And now I have a nice reusable script to figure this out in a jiffy without too much thinking about shell syntax or what I need to pipe to wc and what arguments I need to pass wc and whatnot. plonked in a location on my windows machine with pathext set to allow execution of .py files directly using the ActivePython interpreter and putting it on my Linux machine and adding a symlink in /usr/bin to it makes it available to me on both machines.

350 total views, 4 views today

Have something to say about this post? Comment!

Posted By: BC_Programming
Last Edit: 16 Dec 2011 @ 11:19 AM

EmailPermalink
Tags


 

Responses to this post » (None)

 
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


 Last 50 Posts
 Back
Change Theme...
  • Users » 860
  • Posts/Pages » 192
  • Comments » 67
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

PP



    No Child Pages.

Windows optimization tips



    No Child Pages.