File: CodeCountTest.pm

package info (click to toggle)
pgbackrest 2.57.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,344 kB
  • sloc: ansic: 127,546; xml: 19,452; perl: 12,761; pascal: 3,279; sh: 91; sql: 32; makefile: 23
file content (225 lines) | stat: -rw-r--r-- 7,637 bytes parent folder | download | duplicates (2)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
####################################################################################################################################
# Classify files and generate code totals
####################################################################################################################################
package pgBackRestTest::Common::CodeCountTest;

####################################################################################################################################
# Perl includes
####################################################################################################################################
use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use English '-no_match_vars';

use Exporter qw(import);
    our @EXPORT = qw();

use pgBackRestDoc::Common::Log;

use pgBackRestTest::Common::ExecuteTest;

####################################################################################################################################
# Scan all files and assign types
####################################################################################################################################
sub codeCountScan
{
    my $oStorage = shift;
    my $strBasePath = shift;

    # Load YAML
    require YAML::XS;
    YAML::XS->import(qw(Load Dump));

    my $hCodeCount = {};

    # Build manifest of all files
    my $hManifest = $oStorage->manifest($strBasePath);
    my $strYamlDetail = undef;

    foreach my $strFile (sort(keys(%{$hManifest})))
    {
        # Only interested in files
        next if $hManifest->{$strFile}{type} ne 'f';

        # Exclude these directories/files entirely
        next if ($strFile =~ '^\.' ||
                 $strFile =~ '\.DS_Store$' ||
                 $strFile =~ '\.gitignore$' ||
                 $strFile =~ '\.md$' ||
                 $strFile =~ '\.log$' ||
                 $strFile eq 'LICENSE' ||
                 $strFile =~ '^doc/example/' ||
                 $strFile =~ '^doc/output/' ||
                 $strFile =~ '^doc/resource/fake\-cert' ||
                 $strFile =~ '\.png$' ||
                 $strFile =~ '\.eps$' ||
                 $strFile =~ '\.cache$' ||
                 $strFile =~ '^doc/site/' ||
                 $strFile =~ '^src/build/autom4te.cache/' ||
                 $strFile eq 'test/Dockerfile' ||
                 $strFile eq 'test/Vagrantfile' ||
                 $strFile =~ '^test/\.vagrant/' ||
                 $strFile =~ '^test/certificate/' ||
                 $strFile =~ '^test/code-count/' ||
                 $strFile =~ '^test/data/' ||
                 $strFile =~ '^test/patch/' ||
                 $strFile =~ '^test/result/' ||
                 $strFile =~ '^test/scratch' ||
                 $strFile =~ '^test/src/valgrind\.suppress\.' ||
                 $strFile eq 'test/uncrustify.cfg');

        # Classify the source file
        my $strClass = 'test/harness';

        if ($strFile =~ '^doc/xml/' || $strFile eq 'doc/manifest.xml')
        {
            $strClass = 'doc/source';
        }
        elsif ($strFile =~ '^doc/' || $strFile =~ '^doc/resource/')
        {
            $strClass = 'doc/core';
        }
        elsif ($strFile =~ '^build/' || $strFile =~ '^src/build/' || $strFile =~ 'meson\.build$' ||
               $strFile =~ 'meson_options\.txt$')
        {
            $strClass = 'build';
        }
        elsif ($strFile =~ '^test/lib/pgBackRestTest/Module/' || $strFile =~ '^test/src/module/')
        {
            $strClass = 'test/module';
        }
        elsif ($strFile =~ '^src/')
        {
            $strClass = 'core';
        }

        # Append auto if an auto-generated file
        if ($strFile =~ '\.auto\..$' || $strFile =~ '\.auto\..\.inc$' || $strFile =~ 'Auto\.pm$')
        {
            $strClass .= '/auto';
        }

        # Append vendor if a vendorized file
        if ($strFile =~ '\.vendor\..$' || $strFile =~ '\.vendor\..\.inc$')
        {
            $strClass .= '/vendor';
        }

        # Force unrecognized file types
        my $strForceLang = undef;
        my $strType = undef;

        if ($strFile =~ '\.xs$')
        {
            $strType = 'xs';
            $strForceLang = 'XS';
        }
        elsif ($strFile =~ '\.h$' || $strFile =~ '\.h\.in$' || $strFile =~ '\.xsh$')
        {
            $strType = 'c/h';
            $strForceLang = 'C/C++ Header';
        }
        elsif ($strFile =~ '\.c$' || $strFile =~ '\.c.inc$')
        {
            $strType = 'c';
            $strForceLang = 'C';
        }
        elsif ($strFile =~ '\.t$' || $strFile =~ '\.pl$' || $strFile =~ '\.pm$' || $strFile =~ '\.PL$')
        {
            $strType = 'perl';
            $strForceLang = 'Perl';
        }
        elsif ($strFile =~ '\.yaml$')
        {
            $strType = 'yaml';
            $strForceLang = 'YAML';
        }
        elsif ($strFile =~ 'meson\.build$' || $strFile =~ 'meson_options\.txt$')
        {
            $strType = 'meson';
            $strForceLang = 'make';
        }
        elsif ($strFile =~ '\.xml$')
        {
            $strType = 'xml';
            $strForceLang = 'XML';
        }
        elsif ($strFile =~ '\.css$')
        {
            $strType = 'css';
            $strForceLang = 'CSS';
        }
        elsif ($strFile =~ '\.dtd$')
        {
            $strType = 'dtd';
            $strForceLang = 'DTD';
        }
        elsif ($strFile =~ '\.tex$')
        {
            $strType = 'latex';
            $strForceLang = 'Latex';
        }
        else
        {
            confess &log(ERROR, "unable to map type for ${strFile}");
        }

        # Load the file counts
        my $strYaml = executeTest(
            "cloc --yaml ${strBasePath}/${strFile}" .
            " --force-lang='${strForceLang}'", {bSuppressStdErr => true});

        # Error if the file was ignored
        if ($strYaml =~ '1 file ignored')
        {
            confess &log(ERROR, "file type for ${strBasePath}/${strFile} not recognized:\n${strYaml}");
        }

        # Clean out extraneous keys
        my $hFileCount = Load($strYaml);
        delete($hFileCount->{header});
        delete($hFileCount->{SUM});

        # There should only be one key - the file type
        if (keys(%{$hFileCount}) != 1)
        {
            confess &log(ERROR, "more than one file type in ${strBasePath}/${strFile}:\n" . Dump($hFileCount));
        }

        # Translate type
        my ($strTypeRaw) = keys(%{$hFileCount});
        $hFileCount = $hFileCount->{$strTypeRaw};

        # Append to yaml file
        $strYamlDetail .=
            (defined($strYamlDetail) ? "\n" : '') .
            "${strFile}:\n" .
            "  class: ${strClass}\n" .
            "  type: ${strType}\n";

        # Summarize
        $hCodeCount->{$strClass}{$strType}{code} += $hFileCount->{code};
        $hCodeCount->{$strClass}{$strType}{comment} += $hFileCount->{comment};

        $hCodeCount->{$strClass}{total}{code} += $hFileCount->{code};
        $hCodeCount->{$strClass}{total}{comment} += $hFileCount->{comment};

        $hCodeCount->{total}{code} += $hFileCount->{code};
        $hCodeCount->{total}{comment} += $hFileCount->{comment};
    }

    # Save the file
    $oStorage->put(
        'test/code-count/file-type.yaml',
        "# File types for source files in the project\n" . $strYamlDetail);

    # Reload the file to make sure there aren't any formatting issues
    Load(${$oStorage->get('test/code-count/file-type.yaml')});

    # Display code count summary
    &log(INFO, "code count summary:\n" . Dump($hCodeCount));
}

push @EXPORT, qw(codeCountScan);

1;