Hi Andreas,

Please find attached a revised version of 'sloc2html.py'. Some issues were
addressed and it's in full Python 3 now.


Kind regards
E.

On Sat, Sep 7, 2024 at 2:20 PM Andreas Tille <andr...@an3as.eu> wrote:

> Control: tags -1 moreinfo
> Thanks
>
> Hi,
>
> since package sloccount came up in the Bug of the Day[1] we tried to
> salvage this packege[2].  I followed your hint to add sloc2html in
> Git[3].  I've used 2to3 to convert the script to Python3. Unfortunately
> it does not work.  If you can fix the script I'd happily add it to the
> package but for the moment I do not see any advantage for our users.
>
> Kind regards
>     Andreas.
>
> [1] https://salsa.debian.org/tille/tiny_qa_tools/-/wikis/Tiny-QA-tasks
> [2] https://bugs.debian.org/1078785
> [3]
> https://salsa.debian.org/salvage-team/sloccount/-/tree/master/debian/sloc2html?ref_type=heads
>
> --
> https://fam-tille.de
>
>

-- 
Elie De Brauwer
#!/usr/bin/env python
"""
Written by Rasmus Toftdahl Olesen <r...@pohldata.dk>
Modified slightly by David A. Wheeler and Elie De Brauwer.
Brought into the year 2024 by Elie De Brauwer
Released under the GNU General Public License v. 2 or higher
"""

import sys

NAME = "sloc2html"
VERSION = "0.0.3"

if len(sys.argv) != 2:
    print("Usage:")
    print("\t" + sys.argv[0] + " <sloc output file>")
    print("\nThe output of sloccount should be with --wide and --multiproject formatting")
    sys.exit()

colors = { "python" : "blue",
           "ansic" : "yellow",
           "perl" : "purple",
           "cpp" : "green",
           "sh" : "red",
           "yacc" : "brown",
           "lex" : "silver",
           # Feel free to make more specific colors.
           "ruby" : "maroon",
           "cs" : "gray",
           "java" : "navy",
           "javascript" : "navy",
           "ada" : "olive",
           "lisp" : "fuchsia",
           "objc" : "purple",
           "fortran" : "purple",
           "cobol" : "purple",
           "pascal" : "purple",
           "asm" : "purple",
           "csh" : "purple",
           "tcl" : "purple",
           "exp" : "purple",
           "awk" : "purple",
           "sed" : "purple",
           "makefile" : "purple",
           "sql" : "purple",
           "php" : "purple",
           "modula3" : "purple",
           "ml" : "purple",
           "haskell" : "purple",
           "vhdl" : "purple",
           "xml" : "purple"
          }


print("<html>")
print("<head>")
print("<title>Counted Source Lines of Code (SLOC)</title>")
print("</head>")
print("<body>")
print("<h1>Counted Source Lines of Code</h1>")

with open(sys.argv[1], "r", encoding="utf-8") as file:

    print("<h2>Projects</h2>")
    line = ""
    while line != "SLOC\tDirectory\tSLOC-by-Language (Sorted)\n":
        line = file.readline()

    print("<table>")
    print("<tr><th>Lines</th><th>Project</th><th>Language distribution</th></tr>")
    line = file.readline()
    while line != "\n":
        num, project, langs = line.split()
        print("<tr><td>" + num + "</td><td>" + project + "</td><td>")
        print("<table width=\"500\"><tr>")
        if langs != "(none)":
            for lang in langs.split(","):
                l, n = lang.split("=")
                color = colors[l] if l in colors else "pink"
                print("<td bgcolor=\"" + color + "\" width=\"" + str( float(n) / float(num) * 500 ) + "\">" + l + "=" + n + "&nbsp;(" + str(int(float(n) / float(num) * 100)) + "%)</td>")
        print("</tr></table>")
        print("</td></tr>")
        line = file.readline()
    print("</table>")

    print("<h2>Languages</h2>")
    while line != "Totals grouped by language (dominant language first):\n":
        line = file.readline()

    print("<table>")
    print("<tr><th>Language</th><th>Lines</th></tr>")
    line = file.readline()
    while line != "\n":
        lang, lines, per = line.split()
        lang = lang[:-1]
        print("<tr><td bgcolor=\"" + colors[lang] + "\">" + lang + "</td><td>" + lines + " " + per + "</td></tr>")
        line = file.readline()
    print("</table>")

    print("<h2>Totals</h2>")
    while line == "\n":
        line = file.readline()

    print("<table>")
    print("<tr><td>Total Physical Lines of Code (SLOC):</td><td>" + line.split("=")[1].strip() + "</td></tr>")
    line = file.readline()
    print("<tr><td>Estimated development effort:</td><td>" + line.split("=")[1].strip() + " person-years (person-months)</td></tr>")
    line = file.readline()
    line = file.readline()
    print("<tr><td>Schedule estimate:</td><td>" + line.split("=")[1].strip() + " years (months)</td></tr>")
    line = file.readline()
    line = file.readline()
    print("<tr><td>Total estimated cost to develop:</td><td>" + line.split("=")[1].strip() + "</td></tr>")
    print("</table>")

print("Please credit this data as \"generated using 'SLOCCount' by David A. Wheeler.\"\n")
print("</body>")
print("</html>")

Reply via email to