|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +File: ComputeCodeLines.py |
| 5 | +Author: Zeyue Liang |
| 6 | +Date: September 2, 2015 |
| 7 | +Email: zeyue.liang@gmail.com |
| 8 | +Github: https://github.com/zeyue |
| 9 | +Description: |
| 10 | + A program that can count code line numbers in a directory. |
| 11 | + Usage: Launce the program in console with directory as argument, it can add |
| 12 | + several directories in one time: |
| 13 | + $python -3 ComputeCodeLines.py <first directory> [second] [...] |
| 14 | + ATTENTION: use "/" in the place of "\" |
| 15 | +
|
| 16 | + This program has been tested using some files in C and in Python, |
| 17 | + maybe support java as well. |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import sys |
| 22 | + |
| 23 | +""" |
| 24 | +Global variables |
| 25 | +""" |
| 26 | +file_suffix = "not defined" |
| 27 | +inline_comment_syntax = "not defined" |
| 28 | +start_comment_syntax = "not defined" |
| 29 | +end_comment_syntax = "not defined" |
| 30 | +multilineCommentStartFlag = 0 |
| 31 | +result = {"Code files": 0, |
| 32 | + "No blank lines": 0, |
| 33 | + "Code lines": 0, |
| 34 | + "Comment lines": 0, |
| 35 | + "Blank lines": 0} |
| 36 | + |
| 37 | + |
| 38 | +""" |
| 39 | +Funcitions |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +def getFilesList(directory): |
| 44 | + """Get files's list in a directory |
| 45 | +
|
| 46 | + :directory: files' directory |
| 47 | + :returns: file path list |
| 48 | +
|
| 49 | + """ |
| 50 | + file_paths = [] |
| 51 | + |
| 52 | + for root, directories, files in os.walk(directory): |
| 53 | + for filename in files: |
| 54 | + file_path = os.path.join(root, filename) |
| 55 | + file_paths.append(file_path) |
| 56 | + |
| 57 | + return file_paths |
| 58 | + |
| 59 | + |
| 60 | +def getSuffix(full_file_path): |
| 61 | + """Get the suffix of current file. |
| 62 | +
|
| 63 | + :full_file_path: path of one file |
| 64 | + :returns: suffix |
| 65 | +
|
| 66 | + """ |
| 67 | + return os.path.splitext(full_file_path)[1][1:] |
| 68 | + |
| 69 | + |
| 70 | +def identifyFileType(suffix): |
| 71 | + """Identify the file type and return correct syntax. |
| 72 | +
|
| 73 | + :suffix: file suffix |
| 74 | + :returns: [inline comment syntax, multiple line comment syntax] |
| 75 | +
|
| 76 | + """ |
| 77 | + if suffix == "py": |
| 78 | + return "#", "\"\"\"", "\"\"\"" |
| 79 | + elif suffix == "c" or suffix == "h" or suffix == "cpp" or suffix == "hpp": |
| 80 | + return "//", "/*", "*/" |
| 81 | + elif suffix == "java": |
| 82 | + return "//", "/*", "*/" |
| 83 | + else: |
| 84 | + return "not defined" |
| 85 | + |
| 86 | + |
| 87 | +def isInlineComment(string_line): |
| 88 | + """Check if string line is an inline comment or not. |
| 89 | +
|
| 90 | + :string_line: input line |
| 91 | + :returns: true or false |
| 92 | +
|
| 93 | + """ |
| 94 | + commentLen = len(inline_comment_syntax) |
| 95 | + if string_line[0:commentLen] == inline_comment_syntax: |
| 96 | + # print("zero") |
| 97 | + return True |
| 98 | + else: |
| 99 | + return False |
| 100 | + |
| 101 | + |
| 102 | +def isMultilineComment(string_line): |
| 103 | + """Check if the string line is a multiple comment or not. |
| 104 | +
|
| 105 | + :string_line: one string line in the file |
| 106 | + :returns: True or False |
| 107 | +
|
| 108 | + """ |
| 109 | + global multilineCommentStartFlag |
| 110 | + commentLen = len(str(start_comment_syntax)) |
| 111 | + # print(multilineCommentStartFlag) |
| 112 | + if(string_line[0:commentLen] == start_comment_syntax and |
| 113 | + multilineCommentStartFlag == 0): |
| 114 | + multilineCommentStartFlag = 1 |
| 115 | + # print("one") |
| 116 | + if(len(string_line) > commentLen and |
| 117 | + string_line[-commentLen:] == end_comment_syntax): |
| 118 | + multilineCommentStartFlag = 0 |
| 119 | + return True |
| 120 | + elif(string_line[0:commentLen] != start_comment_syntax and |
| 121 | + multilineCommentStartFlag == 1): |
| 122 | + # print("two") |
| 123 | + if(len(string_line) > commentLen and |
| 124 | + string_line[-commentLen:] == end_comment_syntax): |
| 125 | + multilineCommentStartFlag = 0 |
| 126 | + return True |
| 127 | + elif(string_line[-commentLen:] == end_comment_syntax and |
| 128 | + multilineCommentStartFlag == 1): |
| 129 | + multilineCommentStartFlag = 0 |
| 130 | + # print("three") |
| 131 | + # print(string_line[-commentLen:]) |
| 132 | + return True |
| 133 | + else: |
| 134 | + return False |
| 135 | + |
| 136 | + |
| 137 | +def countOneFile(full_file_path): |
| 138 | + """Count code lines in one file. |
| 139 | +
|
| 140 | + :full_file_path: full path of the file |
| 141 | + :returns: null |
| 142 | +
|
| 143 | + """ |
| 144 | + global inline_comment_syntax, start_comment_syntax, end_comment_syntax |
| 145 | + global multilineCommentStartFlag |
| 146 | + local_result = {"No blank lines": 0, |
| 147 | + "Comment lines": 0, |
| 148 | + "Code lines": 0, |
| 149 | + "Blank lines": 0} |
| 150 | + file_suffix = getSuffix(full_file_path) |
| 151 | + if(identifyFileType(file_suffix) != "not defined"): |
| 152 | + result["Code files"] += 1 |
| 153 | + (inline_comment_syntax, |
| 154 | + start_comment_syntax, |
| 155 | + end_comment_syntax) = identifyFileType(file_suffix) |
| 156 | + with open(full_file_path, "r") as lines: |
| 157 | + for line in lines: |
| 158 | + line = line.strip() |
| 159 | + if line.strip(): |
| 160 | + local_result["No blank lines"] += 1 |
| 161 | + if isInlineComment(line): |
| 162 | + local_result["Comment lines"] += 1 |
| 163 | + elif isMultilineComment(line): |
| 164 | + local_result["Comment lines"] += 1 |
| 165 | + else: |
| 166 | + # print("Oops...") |
| 167 | + local_result["Code lines"] += 1 |
| 168 | + else: |
| 169 | + local_result["Blank lines"] += 1 |
| 170 | + print(full_file_path) |
| 171 | + for key in local_result: |
| 172 | + print(str(key) + ": " + str(local_result[key])) |
| 173 | + result["No blank lines"] += local_result["No blank lines"] |
| 174 | + result["Comment lines"] += local_result["Comment lines"] |
| 175 | + result["Code lines"] += local_result["Code lines"] |
| 176 | + result["Blank lines"] += local_result["Blank lines"] |
| 177 | + |
| 178 | + |
| 179 | +def countInDirectory(directory): |
| 180 | + """Count code lines in one directory. |
| 181 | +
|
| 182 | + :directory: the directory that will be counted |
| 183 | + :return: null |
| 184 | +
|
| 185 | + """ |
| 186 | + full_file_paths = getFilesList(directory) |
| 187 | + for f in full_file_paths: |
| 188 | + countOneFile(f) |
| 189 | + |
| 190 | + |
| 191 | +def main(): |
| 192 | + """main function. |
| 193 | +
|
| 194 | + """ |
| 195 | + global file_suffix, inline_comment_syntax, start_comment_syntax |
| 196 | + global end_comment_syntax, multilineCommentStartFlag, result |
| 197 | + for directory in sys.argv[1:]: |
| 198 | + file_suffix = "not defined" |
| 199 | + inline_comment_syntax = "not defined" |
| 200 | + start_comment_syntax = "not defined" |
| 201 | + end_comment_syntax = "not defined" |
| 202 | + multilineCommentStartFlag = 0 |
| 203 | + result = {"Code files": 0, |
| 204 | + "No blank lines": 0, |
| 205 | + "Code lines": 0, |
| 206 | + "Comment lines": 0, |
| 207 | + "Blank lines": 0} |
| 208 | + |
| 209 | + print("this is the directory: " + directory) |
| 210 | + countInDirectory(directory) |
| 211 | + |
| 212 | + print() |
| 213 | + print("Final result in the directory") |
| 214 | + print(directory) |
| 215 | + for key in result: |
| 216 | + print(str(key) + ": " + str(result[key])) |
| 217 | + |
| 218 | +""" |
| 219 | +Launch |
| 220 | +""" |
| 221 | +main() |
0 commit comments