Package: code-size-counter
Code size counterThis repository contains a simple command line utility used for calculating total size (both
KB and lines of code) of program's code.IntroductionThe main goal of this program is to calculate total size of a source code.
You need to specify the directory that contains the source code and file extension(s) of the desired files.
(for examplehtml,cssandjs).
After that, the program searches the directory for files with the given file extension(s). It's also possible to exclude selected subdirectories from the search
(this is especially useful for directories like.venv,.gitand so on).
The program typically prints:number of files foundtotal number of lines in these files (newline at the end of the file isn't counted)total size of these files in KB (rounded to 2 decimal places). 1 KB = 1024 bytesSee the usage section for further details.RequirementsPython version >= 3.9pipInstallationInstall via pippipinstallcode-size-counterThis installs the program as a local python package and allows you to usecode-size-counterCLI command.See thePyPIpage for release history.UsageBelow, you can see list of all possible arguments (you can also get the help by runningcode-size-counter -h).directoryis the only
one required.usage: main.py [-h] [-d DIRECTORY] [-e EXTENSION [EXTENSION ...]] [-l] [-x EXCLUDE [EXCLUDE ...]] [-p {kb_size,lines,files}]

Calculate the total size (both KB and lines of code) of program's code.

options:
  -h, --help            show this help message and exit
  -d DIRECTORY, --directory DIRECTORY
                        Path to the directory where to search files. The path can be either absolute or relative; leave empty if you want to search the current directory.
  -e EXTENSION [EXTENSION ...], --extension EXTENSION [EXTENSION ...]
                        extensions of the files that we're searching (separated by spaces) for. Do not prefix them with a dot (e.g. use "py" instead of ".py"). Leave empty if you want to search for all files regardless of their extension.
  -l, --log             If present, the program prints its progress (e.g. 'file XXX processed')
  -x EXCLUDE [EXCLUDE ...], --exclude EXCLUDE [EXCLUDE ...]
                        path to directories & files to exclude (separated by spaces). These paths are relative to the given directory (-d parameter)
  -p {kb_size,lines,files}, --print {kb_size,lines,files}
                        Print just the selected value (KB size, total files or lines of code)
PS C:\Users\vojta\Programming\misc\code-size-counter>The typical usage of this program looks like this (see below). In this case, we're looking for.pyand.txtfiles inC:/Users/hacker123/Documents/hacking_appdirectory,
except those in.venvand.gitsubdirectories.
Note that we don't prefix the file extensions with a dot (e.g. usepyinstead of.py).code-size-counter-dC:/Users/hacker123/Documents/hacking_app-epytxt-x.venv.gitPossible output+---------------------------------------------------------+
| Extension   Total files   Total lines   Total size (KB) |
+---------------------------------------------------------+
|       .py             1            50              1.51 |
|      .txt             1             2              0.03 |
+---------------------------------------------------------+
|     TOTAL             2            52              1.54 |
+---------------------------------------------------------+See also other examples below.ExamplesExample 1Calculate the size of.pyfiles inside./tests/complex-test-dirdirectory.code-size-counter-d./tests/complex-test-dir-epyOutput+---------------------------------------------------------+
| Extension   Total files   Total lines   Total size (KB) |
+---------------------------------------------------------+
|       .py            31         13030            462.61 |
+---------------------------------------------------------+Example 2Calculate the size of.pyfiles inside./tests/complex-test-dirdirectory. Excludevirtualenvandsrc/module1subdirectories.code-size-counter-d./tests/complex-test-dir-epy-xvirtualenvsrc/module1Output+---------------------------------------------------------+
| Extension   Total files   Total lines   Total size (KB) |
+---------------------------------------------------------+
|       .py             8           268              8.26 |
+---------------------------------------------------------+Example 3Calculate the size of.pyfiles inside./tests/complex-test-dirdirectory. Excludevirtualenvandsrc/module1subdirectories and print logs.code-size-counter-d./tests/complex-test-dir-epy-xvirtualenvsrc/module1-lOutput./tests/complex-test-dir/src/module2/main.py processed
./tests/complex-test-dir/src/module2/__init__.py processed
./tests/complex-test-dir/src/code_size_counter.py processed
./tests/complex-test-dir/src/file_tools.py processed
./tests/complex-test-dir/src/__init__.py processed
./tests/complex-test-dir/tests/test_code_size_counter.py processed
./tests/complex-test-dir/tests/__init__.py processed
./tests/complex-test-dir/code_size_counter.py processed
+---------------------------------------------------------+
| Extension   Total files   Total lines   Total size (KB) |
+---------------------------------------------------------+
|       .py             8           268              8.26 |
+---------------------------------------------------------+Example 4Calculate the size of.py,.mdand.ymlfiles inside./tests/complex-test-dirdirectory. Excludevirtualenvandsrc/module1subdirectories.code-size-counter-d./tests/complex-test-dir-epymdyml-xvirtualenvsrc/module1Output+---------------------------------------------------------+
| Extension   Total files   Total lines   Total size (KB) |
+---------------------------------------------------------+
|      .yml             1            36              1.16 |
|       .py             8           268              8.26 |
|       .md             1             3              0.14 |
+---------------------------------------------------------+
|     TOTAL            10           307              9.56 |
+---------------------------------------------------------+Example 5Calculate the size of all files (regardless of their extension) inside./directories/my-dirdirectory.code-size-counter-d./directories/my-dirPossible output+----------------------------------------------------------+
|  Extension   Total files   Total lines   Total size (KB) |
+----------------------------------------------------------+
|     (NONE)             1            10              0.11 |
|    .flake8             1             2              0.03 |
| .gitignore             2           140              1.95 |
|        .md             2           215              8.83 |
|        .py            44         13533            477.44 |
|       .yml             2            76              2.34 |
+----------------------------------------------------------+
|      TOTAL            52         13976            490.70 |
+----------------------------------------------------------+Also note the(NONE)extension, which means that the file has no extensionExample 6Calculate the size of.pyfiles inside./tests/complex-test-dirdirectory. Excludevirtualenvandsrc/module1subdirectories
and print just the number of lines.code-size-counter-d./tests/complex-test-dir-epy-xvirtualenvsrc/module1-plinesOutput268You can then pipe the result (as you can see on the example below).code-size-counter-d./tests/complex-test-dir-epy-xvirtualenvsrc/module1-plines|python-c"loc = input(); print(f'I copied {loc} lines from StackOverflow.')"OutputI copied 268 lines from StackOverflow.Developer documentationWe're usingPoetryfor dependency management and packaging.Program structurecode_size_counterfolder - source code files of the programmain.pyis the entry-point of the apptestsfolder - unit tests of the program (usingPython unittestmodule). The subfolders serve as a test data.To run the tests, use the following commandpoetryrunpython-munittestdiscovertests-vRun this command in therootfolder to executemain.pyas a script.poetryrunpython-mcode_size_counter.main