Upload cloc.sh with huggingface_hub
Browse files
cloc.sh
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# This scripts counts the lines of code and comments in all source files
|
4 |
+
# and prints the results to the command line. It uses the commandline tool
|
5 |
+
# "cloc". You can either pass --loc, --comments or --percentage to show the
|
6 |
+
# respective values only.
|
7 |
+
# Some parts below need to be adapted to your project!
|
8 |
+
|
9 |
+
# Get the location of this script.
|
10 |
+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
11 |
+
|
12 |
+
# Run cloc - this counts code lines, blank lines and comment lines
|
13 |
+
# for the specified languages. You will need to change this accordingly.
|
14 |
+
# For C++, you could use "C++,C/C++ Header" for example.
|
15 |
+
# We are only interested in the summary, therefore the tail -1
|
16 |
+
SUMMARY="$(cloc "${SCRIPT_DIR}" --include-lang="Python" --md | tail -1)"
|
17 |
+
|
18 |
+
# The $SUMMARY is one line of a markdown table and looks like this:
|
19 |
+
# SUM:|101|3123|2238|10783
|
20 |
+
# We use the following command to split it into an array.
|
21 |
+
IFS='|' read -r -a TOKENS <<<"$SUMMARY"
|
22 |
+
|
23 |
+
# Store the individual tokens for better readability.
|
24 |
+
NUMBER_OF_FILES=${TOKENS[1]}
|
25 |
+
COMMENT_LINES=${TOKENS[3]}
|
26 |
+
LINES_OF_CODE=${TOKENS[4]}
|
27 |
+
|
28 |
+
# To make the estimate of commented lines more accurate, we have to
|
29 |
+
# subtract any copyright header which is included in each file.
|
30 |
+
# For Fly-Pie, this header has the length of five lines.
|
31 |
+
# All dumb comments like those /////////// or those // ------------
|
32 |
+
# are also subtracted. As cloc does not count inline comments,
|
33 |
+
# the overall estimate should be rather conservative.
|
34 |
+
# Change the lines below according to your project.
|
35 |
+
# DUMB_COMMENTS="$(grep -r -E '//////|// -----' "${SCRIPT_DIR}" | wc -l)"
|
36 |
+
# COMMENT_LINES=$(($COMMENT_LINES - 5 * $NUMBER_OF_FILES - $DUMB_COMMENTS))
|
37 |
+
|
38 |
+
# Print all results if no arguments are given.
|
39 |
+
if [[ $# -eq 0 ]]; then
|
40 |
+
awk -v a=$LINES_OF_CODE \
|
41 |
+
'BEGIN {printf "Lines of source code: %6.1fk\n", a/1000}'
|
42 |
+
awk -v a=$COMMENT_LINES \
|
43 |
+
'BEGIN {printf "Lines of comments: %6.1fk\n", a/1000}'
|
44 |
+
awk -v a=$COMMENT_LINES -v b=$LINES_OF_CODE \
|
45 |
+
'BEGIN {printf "Comment Percentage: %6.1f%\n", 100*a/b}'
|
46 |
+
exit 0
|
47 |
+
fi
|
48 |
+
|
49 |
+
# Show lines of code if --loc is given.
|
50 |
+
if [[ $* == *--loc* ]]; then
|
51 |
+
awk -v a=$LINES_OF_CODE \
|
52 |
+
'BEGIN {printf "%.1fk\n", a/1000}'
|
53 |
+
fi
|
54 |
+
|
55 |
+
# Show lines of comments if --comments is given.
|
56 |
+
if [[ $* == *--comments* ]]; then
|
57 |
+
awk -v a=$COMMENT_LINES \
|
58 |
+
'BEGIN {printf "%.1fk\n", a/1000}'
|
59 |
+
fi
|
60 |
+
|
61 |
+
# Show precentage of comments if --percentage is given.
|
62 |
+
if [[ $* == *--percentage* ]]; then
|
63 |
+
awk -v a=$COMMENT_LINES -v b=$LINES_OF_CODE \
|
64 |
+
'BEGIN {printf "%.1f\n", 100*a/b}'
|
65 |
+
fi
|