-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cloc.py
75 lines (50 loc) · 1.58 KB
/
Cloc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os, re, commands, sys
def openFile():
f = open('input.csv', 'w+')
return f
def closeFile(file):
file.close()
def listProjects(directory):
return [d for d in os.listdir(directory) if os.path.isdir(os.path.join(directory, d))]
def listVersionProjects(directory):
dirs = [directory+d+"/" for d in os.listdir(directory) if os.path.isdir(os.path.join(directory, d))]
return dirs
def execCloc(projectName, pathProject, version):
comando = "cloc --match-f='^*.java' "+ pathProject+" --csv "
conteudo = commands.getoutput(comando)
#explodir a saida do cloc na quebra de linha
csv = conteudo.split('\n')
loc = 0
for row in csv:
if "Java" in row:
loc = row.split(',')[-1]
break
#print projectName, version, loc
#print projectName+";"+ version+";"+pathProject+";"+str(loc)+";"
file.write(";;"+projectName+";"+ version+";"+pathProject+";"+str(loc)+";\n")
return loc
if __name__ == "__main__":
defaultDir = '/home/thiago/git/Projetos/'
#diretorio que contem os projetos a serem computados pelo cloc.
pathDir = ''
print sys.argv[1]
if len(sys.argv) is 2:
dir = str(sys.argv[1])
if os.path.isdir(dir):
pathDir = dir
else:
pathDir = defaultDir
else:
pathDir = defaultDir
file = openFile()
projects = listProjects(pathDir)
#print projects
loc = 0
for project in projects:
allVersionProjects = listProjects(defaultDir+project)
allVersionProjects.sort()
for version in allVersionProjects:
pathProject = pathDir+project+"/"+version+"/"
loc += int(execCloc(project, pathProject, version))
#print "Total Lines of Code:", loc
closeFile(file)