2012-10-12 12:52:35 +02:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
2015-08-24 17:23:54 +02:00
|
|
|
# Copyright © 2012-2015 Ejwa Software. All rights reserved.
|
2012-10-12 12:52:35 +02:00
|
|
|
#
|
|
|
|
# This file is part of gitinspector.
|
|
|
|
#
|
|
|
|
# gitinspector is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# gitinspector is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with gitinspector. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
2013-06-25 16:37:18 +02:00
|
|
|
from __future__ import unicode_literals
|
2012-11-13 00:49:26 +01:00
|
|
|
import base64
|
2012-10-18 15:32:55 +02:00
|
|
|
import os
|
2014-02-20 04:06:47 +01:00
|
|
|
import textwrap
|
|
|
|
import time
|
2012-11-13 00:49:26 +01:00
|
|
|
import zipfile
|
2015-11-02 02:30:28 +01:00
|
|
|
from .localization import N_
|
2015-10-21 05:26:20 +02:00
|
|
|
from . import basedir, localization, terminal, version
|
2015-10-20 18:40:08 +02:00
|
|
|
|
2015-10-24 02:44:45 +02:00
|
|
|
__available_formats__ = ["html", "htmlembedded", "json", "text", "xml"]
|
2013-07-09 12:40:59 +02:00
|
|
|
|
2015-10-24 02:44:45 +02:00
|
|
|
DEFAULT_FORMAT = __available_formats__[3]
|
2012-10-12 12:52:35 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
__selected_format__ = DEFAULT_FORMAT
|
|
|
|
|
2012-10-12 12:52:35 +02:00
|
|
|
class InvalidFormatError(Exception):
|
2013-07-14 04:57:32 +02:00
|
|
|
def __init__(self, msg):
|
|
|
|
super(InvalidFormatError, self).__init__(msg)
|
|
|
|
self.msg = msg
|
2012-10-12 12:52:35 +02:00
|
|
|
|
|
|
|
def select(format):
|
|
|
|
global __selected_format__
|
|
|
|
__selected_format__ = format
|
|
|
|
|
|
|
|
return format in __available_formats__
|
2012-10-18 16:59:52 +02:00
|
|
|
|
2013-03-11 00:23:50 +01:00
|
|
|
def get_selected():
|
|
|
|
return __selected_format__
|
|
|
|
|
2012-10-19 02:00:34 +02:00
|
|
|
def is_interactive_format():
|
|
|
|
return __selected_format__ == "text"
|
|
|
|
|
2012-11-13 00:49:26 +01:00
|
|
|
def __output_html_template__(name):
|
|
|
|
template_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), name)
|
|
|
|
file_r = open(template_path, "rb")
|
2015-11-07 05:49:24 +01:00
|
|
|
template = file_r.read().decode("utf-8", "replace")
|
|
|
|
|
|
|
|
file_r.close()
|
|
|
|
return template
|
|
|
|
|
2013-06-06 03:05:30 +02:00
|
|
|
def __get_zip_file_content__(name, file_name="/html/flot.zip"):
|
|
|
|
zip_file = zipfile.ZipFile(basedir.get_basedir() + file_name, "r")
|
2012-11-13 00:49:26 +01:00
|
|
|
content = zip_file.read(name)
|
2015-11-07 05:50:15 +01:00
|
|
|
|
2012-11-13 00:49:26 +01:00
|
|
|
zip_file.close()
|
2013-05-08 18:52:08 +02:00
|
|
|
return content.decode("utf-8", "replace")
|
2012-11-03 02:13:53 +01:00
|
|
|
|
2015-11-02 02:30:28 +01:00
|
|
|
INFO_ONE_REPOSITORY = N_("Statistical information for the repository '{0}' was gathered on {1}.")
|
|
|
|
INFO_MANY_REPOSITORIES = N_("Statistical information for the repositories '{0}' was gathered on {1}.")
|
|
|
|
|
|
|
|
def output_header(repos):
|
|
|
|
repos_string = ", ".join([repo.name for repo in repos])
|
|
|
|
|
2013-06-26 02:36:13 +02:00
|
|
|
if __selected_format__ == "html" or __selected_format__ == "htmlembedded":
|
2012-11-13 12:18:30 +01:00
|
|
|
base = basedir.get_basedir()
|
2012-11-13 11:27:56 +01:00
|
|
|
html_header = __output_html_template__(base + "/html/html.header")
|
2015-10-05 06:17:10 +02:00
|
|
|
tablesorter_js = __get_zip_file_content__("jquery.tablesorter.min.js",
|
|
|
|
"/html/jquery.tablesorter.min.js.zip").encode("latin-1", "replace")
|
2015-02-22 00:53:38 +01:00
|
|
|
tablesorter_js = tablesorter_js.decode("utf-8", "ignore")
|
2012-11-13 00:49:26 +01:00
|
|
|
flot_js = __get_zip_file_content__("jquery.flot.js")
|
2012-11-17 00:33:47 +01:00
|
|
|
pie_js = __get_zip_file_content__("jquery.flot.pie.js")
|
2013-07-27 23:48:34 +02:00
|
|
|
resize_js = __get_zip_file_content__("jquery.flot.resize.js")
|
2012-11-13 00:49:26 +01:00
|
|
|
|
2012-11-13 11:27:56 +01:00
|
|
|
logo_file = open(base + "/html/gitinspector_piclet.png", "rb")
|
2012-11-13 00:49:26 +01:00
|
|
|
logo = logo_file.read()
|
|
|
|
logo_file.close()
|
|
|
|
logo = base64.b64encode(logo)
|
|
|
|
|
2013-06-26 02:36:13 +02:00
|
|
|
if __selected_format__ == "htmlembedded":
|
|
|
|
jquery_js = ">" + __get_zip_file_content__("jquery.js")
|
|
|
|
else:
|
2015-11-03 04:25:40 +01:00
|
|
|
jquery_js = " src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\">"
|
2013-06-26 02:36:13 +02:00
|
|
|
|
2015-11-03 00:36:05 +01:00
|
|
|
print(html_header.format(title=_("Repository statistics for '{0}'").format(repos_string),
|
2015-10-12 03:15:30 +02:00
|
|
|
jquery=jquery_js,
|
|
|
|
jquery_tablesorter=tablesorter_js,
|
|
|
|
jquery_flot=flot_js,
|
|
|
|
jquery_flot_pie=pie_js,
|
|
|
|
jquery_flot_resize=resize_js,
|
|
|
|
logo=logo.decode("utf-8", "replace"),
|
|
|
|
logo_text=_("The output has been generated by {0} {1}. The statistical analysis tool"
|
2013-06-26 01:19:47 +02:00
|
|
|
" for git repositories.").format(
|
2015-08-24 17:34:14 +02:00
|
|
|
"<a href=\"https://github.com/ejwa/gitinspector\">gitinspector</a>",
|
2014-03-17 08:47:49 +01:00
|
|
|
version.__version__),
|
2015-11-03 00:36:05 +01:00
|
|
|
repo_text=_(INFO_ONE_REPOSITORY if len(repos) <= 1 else INFO_MANY_REPOSITORIES).format(
|
|
|
|
repos_string, localization.get_date()),
|
2015-10-12 03:15:30 +02:00
|
|
|
show_minor_authors=_("Show minor authors"),
|
|
|
|
hide_minor_authors=_("Hide minor authors"),
|
|
|
|
show_minor_rows=_("Show rows with minor work"),
|
|
|
|
hide_minor_rows=_("Hide rows with minor work")))
|
2015-10-24 02:44:45 +02:00
|
|
|
elif __selected_format__ == "json":
|
|
|
|
print("{\n\t\"gitinspector\": {")
|
2015-11-03 22:42:23 +01:00
|
|
|
print("\t\t\"version\": \"" + version.__version__ + "\",")
|
|
|
|
|
|
|
|
if len(repos) <= 1:
|
2015-11-07 05:55:14 +01:00
|
|
|
print("\t\t\"repository\": \"" + repos_string + "\",")
|
2015-11-03 22:42:23 +01:00
|
|
|
else:
|
|
|
|
repos_json = "\t\t\"repositories\": [ "
|
|
|
|
|
|
|
|
for repo in repos:
|
|
|
|
repos_json += "\"" + repo.name + "\", "
|
|
|
|
|
|
|
|
print(repos_json[:-2] + " ],")
|
|
|
|
|
|
|
|
print("\t\t\"report_date\": \"" + time.strftime("%Y/%m/%d") + "\",")
|
|
|
|
|
2012-11-13 00:49:26 +01:00
|
|
|
elif __selected_format__ == "xml":
|
2012-10-18 16:59:52 +02:00
|
|
|
print("<gitinspector>")
|
2013-03-25 03:30:27 +01:00
|
|
|
print("\t<version>" + version.__version__ + "</version>")
|
2015-11-03 23:00:17 +01:00
|
|
|
|
|
|
|
if len(repos) <= 1:
|
|
|
|
print("\t<repository>" + repos_string + "</repository>")
|
|
|
|
else:
|
|
|
|
print("\t<repositories>")
|
|
|
|
|
|
|
|
for repo in repos:
|
|
|
|
print("\t\t<repository>" + repo.name + "</repository>")
|
|
|
|
|
|
|
|
print("\t</repositories>")
|
|
|
|
|
2014-02-20 04:06:47 +01:00
|
|
|
print("\t<report-date>" + time.strftime("%Y/%m/%d") + "</report-date>")
|
|
|
|
else:
|
2015-11-02 02:30:28 +01:00
|
|
|
print(textwrap.fill(_(INFO_ONE_REPOSITORY if len(repos) <= 1 else INFO_MANY_REPOSITORIES).format(
|
|
|
|
repos_string, localization.get_date()), width=terminal.get_size()[0]))
|
2012-10-18 16:59:52 +02:00
|
|
|
|
|
|
|
def output_footer():
|
2014-11-22 20:38:40 +01:00
|
|
|
if __selected_format__ == "html" or __selected_format__ == "htmlembedded":
|
|
|
|
base = basedir.get_basedir()
|
|
|
|
html_footer = __output_html_template__(base + "/html/html.footer")
|
2012-11-13 00:49:26 +01:00
|
|
|
print(html_footer)
|
2015-10-24 02:44:45 +02:00
|
|
|
elif __selected_format__ == "json":
|
|
|
|
print("\n\t}\n}")
|
2012-11-13 00:49:26 +01:00
|
|
|
elif __selected_format__ == "xml":
|
2012-10-18 16:59:52 +02:00
|
|
|
print("</gitinspector>")
|