|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 1996-2023 Cyberbotics Ltd. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Display the number of lines of codes in this repository for different programming languages.""" |
| 18 | + |
| 19 | +import glob |
| 20 | +import os |
| 21 | + |
| 22 | + |
| 23 | +def count_lines(filename): |
| 24 | + f = open(filename, 'rb') |
| 25 | + lines = 0 |
| 26 | + buf_size = 1024 * 1024 |
| 27 | + read_f = f.raw.read |
| 28 | + buf = read_f(buf_size) |
| 29 | + while buf: |
| 30 | + lines += buf.count(b'\n') |
| 31 | + buf = read_f(buf_size) |
| 32 | + return lines |
| 33 | + |
| 34 | + |
| 35 | +def count_files(extensions, name): |
| 36 | + line_counter = 0 |
| 37 | + file_counter = 0 |
| 38 | + if isinstance(extensions, str): |
| 39 | + extensions = [extensions] |
| 40 | + for extension in extensions: |
| 41 | + files = glob.glob(os.path.join('..', '..', '**', '*.' + extension), recursive=True) |
| 42 | + file_counter += len(files) |
| 43 | + for f in files: |
| 44 | + line_counter += count_lines(f) |
| 45 | + print((name + ':').ljust(12) + str(line_counter).ljust(6) + ' lines of codes in ' + str(file_counter).ljust(4) + ' files') |
| 46 | + |
| 47 | + |
| 48 | +count_files(['cpp', 'hpp'], 'C++') |
| 49 | +count_files(['c', 'h'], 'C') |
| 50 | +count_files(['py'], 'Python') |
| 51 | +count_files('js', 'JavaScript') |
| 52 | +count_files('java', 'Java') |
| 53 | +count_files('m', 'MATLAB') |
| 54 | +count_files('vert', 'GLSL') |
0 commit comments