11

Is there an easy way to count the lines of code you have written for your django project?

Edit: The shell stuff is cool, but how about on Windows?

Right leg
  • 16,080
  • 7
  • 48
  • 81
Joe
  • 4,553
  • 9
  • 51
  • 57
  • Do you mean all lines in any *.py files in the project or just lines of code that *you* wrote, excluding any scaffolding code? – Andrew Hare Jul 15 '09 at 19:24
  • @Andrew, arn't the DJango distribution files usually housed away from the site-root anyway? – Aiden Bell Jul 15 '09 at 19:26
  • @Andrew Hare, yeah I just want to find out the lines of code I have written. Really just the view.py, model.py and urls.py files would work ... although that would still miss a lot of code in my context_processors and so on. @Aiden Bell yeah they are. – Joe Jul 15 '09 at 19:46
  • 1
    You should fix the "pythong" tag. – Xiong Chiamiov Jul 15 '09 at 21:25
  • 2
    I often accidentally type `pythong` at the shell... Freudian? – harto Jul 17 '09 at 02:20

5 Answers5

19

Yep:

shell]$ find /my/source -name "*.py" -type f -exec cat {} + | wc -l

Job's a good 'un.

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
8

You might want to look at CLOC -- it's not Django specific but it supports Python. It can show you lines counts for actual code, comments, blank lines, etc.

Steve Losh
  • 19,642
  • 2
  • 51
  • 44
4

Starting with Aiden's answer, and with a bit of help in a question of my own, I ended up with this god-awful mess:

# find the combined LOC of files
# usage: loc Documents/fourU py html
function loc {
    #find $1 -name $2 -type f -exec cat {} + | wc -l
    namelist=''
    let i=2
    while [ $i -le $# ]; do
        namelist="$namelist -name \"*.$@[$i]\""
        if [ $i != $# ]; then
            namelist="$namelist -or "
        fi
        let i=i+1
    done
    #echo $namelist
    #echo "find $1 $namelist" | sh
    #echo "find $1 $namelist" | sh | xargs cat
    echo "find $1 $namelist" | sh | xargs cat | wc -l
}

which allows you to specify any number of extensions you want to match. As far as I can tell, it outputs the right answer, but... I thought this would be a one-liner, else I wouldn't have started in bash, and it just kinda grew from there.

I'm sure that those more knowledgable than I can improve upon this, so I'm going to put it in community wiki.

Community
  • 1
  • 1
Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
  • loc() { D=$1; shift echo "$@" | xargs -n 1 echo | sed 's,^, -or -name *.,' | xargs find $D -type f | xargs cat | wc -l } – rzab Jul 16 '09 at 14:09
  • Alright, oneliner: echo py html | xargs -n 1 echo | sed 's,^, -or -name *.,' | xargs find Documents -type f | xargs cat | wc -l – rzab Jul 16 '09 at 14:11
  • Ah, I didn't even think about shift! As I said, this answer is in community wiki, so you can edit it directly... if not, I might get around to fixing it later. – Xiong Chiamiov Jul 16 '09 at 16:31
1

Check out the wc command on unix.

Dan Lorenc
  • 5,376
  • 1
  • 23
  • 34
0

Get wc command on Windows using GnuWin32 (http://gnuwin32.sourceforge.net/packages/coreutils.htm)

wc *.py

sharjeel
  • 5,825
  • 7
  • 34
  • 49