From c700523f0bfe517fafdacf1596c1fc0ca895ccff Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Mon, 20 Apr 2009 15:41:51 +1000 Subject: [PATCH] Convert to a python package .. from a python module. This means we can use the pkg_resource stuff to grab our templates, stylesheet and image. Signed-off-by: Jeremy Kerr --- hiprofile.py => hiprofile/__init__.py | 17 +++++++------ .../hiprofile => hiprofile/resources}/bar.png | Bin .../resources}/base.html | 0 .../resources}/binary.html | 0 .../resources}/hiprofile.js | 0 .../resources}/jquery-1.3.1.min.js | 0 .../resources}/report.html | 0 .../resources}/style.css | 0 .../resources}/symbol.html | 0 scripts/hiprofile | 5 +--- setup.py | 23 +++++++++--------- 11 files changed, 22 insertions(+), 23 deletions(-) rename hiprofile.py => hiprofile/__init__.py (95%) rename {share/hiprofile => hiprofile/resources}/bar.png (100%) rename {share/hiprofile => hiprofile/resources}/base.html (100%) rename {share/hiprofile => hiprofile/resources}/binary.html (100%) rename {share/hiprofile => hiprofile/resources}/hiprofile.js (100%) rename {share/hiprofile => hiprofile/resources}/jquery-1.3.1.min.js (100%) rename {share/hiprofile => hiprofile/resources}/report.html (100%) rename {share/hiprofile => hiprofile/resources}/style.css (100%) rename {share/hiprofile => hiprofile/resources}/symbol.html (100%) diff --git a/hiprofile.py b/hiprofile/__init__.py similarity index 95% rename from hiprofile.py rename to hiprofile/__init__.py index 6f99d3c..cc071ff 100644 --- a/hiprofile.py +++ b/hiprofile/__init__.py @@ -2,18 +2,17 @@ import subprocess import re -import shutil import os import socket +import pkg_resources from xml.dom.minidom import parse as parseXML -from jinja2 import Environment, FileSystemLoader +from jinja2 import Environment, PackageLoader b_id = 0 s_id = 0 try: - __version__ = __import__('pkg_resources') \ - .get_distribution('hiprofile').version + __version__ = pkg_resources.get_distribution('hiprofile').version except Exception: __version__ = 'unknown' @@ -308,21 +307,23 @@ class Report(object): def __str__(self): return self.machine + '\n' + '\n'.join(map(str, self.binaries)) -def write_report(report, resourcedir, outdir): +def write_report(report, outdir): os.mkdir(outdir) # set up template engine - env = Environment(loader = FileSystemLoader(resourcedir), + env = Environment(loader = PackageLoader(__name__, 'resources'), autoescape = True) templates = {} for name in ['report', 'binary', 'symbol']: templates[name] = env.get_template('%s.html' % name) - # copy required files over + # copy required files over from resources files = ['style.css', 'hiprofile.js', 'bar.png', 'jquery-1.3.1.min.js'] for file in files: - shutil.copy(os.path.join(resourcedir, file), outdir) + f = open(os.path.join(outdir, file), 'w') + f.write(pkg_resources.resource_string(__name__, 'resources/' + file)) + f.close() reportfile = os.path.join(outdir, 'index.html') templates['report'].stream(version = __version__, diff --git a/share/hiprofile/bar.png b/hiprofile/resources/bar.png similarity index 100% rename from share/hiprofile/bar.png rename to hiprofile/resources/bar.png diff --git a/share/hiprofile/base.html b/hiprofile/resources/base.html similarity index 100% rename from share/hiprofile/base.html rename to hiprofile/resources/base.html diff --git a/share/hiprofile/binary.html b/hiprofile/resources/binary.html similarity index 100% rename from share/hiprofile/binary.html rename to hiprofile/resources/binary.html diff --git a/share/hiprofile/hiprofile.js b/hiprofile/resources/hiprofile.js similarity index 100% rename from share/hiprofile/hiprofile.js rename to hiprofile/resources/hiprofile.js diff --git a/share/hiprofile/jquery-1.3.1.min.js b/hiprofile/resources/jquery-1.3.1.min.js similarity index 100% rename from share/hiprofile/jquery-1.3.1.min.js rename to hiprofile/resources/jquery-1.3.1.min.js diff --git a/share/hiprofile/report.html b/hiprofile/resources/report.html similarity index 100% rename from share/hiprofile/report.html rename to hiprofile/resources/report.html diff --git a/share/hiprofile/style.css b/hiprofile/resources/style.css similarity index 100% rename from share/hiprofile/style.css rename to hiprofile/resources/style.css diff --git a/share/hiprofile/symbol.html b/hiprofile/resources/symbol.html similarity index 100% rename from share/hiprofile/symbol.html rename to hiprofile/resources/symbol.html diff --git a/scripts/hiprofile b/scripts/hiprofile index f663cce..4895937 100755 --- a/scripts/hiprofile +++ b/scripts/hiprofile @@ -4,9 +4,6 @@ import sys, os from optparse import OptionParser from hiprofile import Report, Connection, write_report -resourcedir = os.path.join(os.path.dirname(__file__), - '..', 'share', 'hiprofile') - default_thresholds = { 'binary': '5%', 'symbol': '20', @@ -37,7 +34,7 @@ def main(args): report.annotate(conn, options) - write_report(report, resourcedir, options.outdir) + write_report(report, options.outdir) if __name__ == '__main__': sys.exit(main(sys.argv)) diff --git a/setup.py b/setup.py index bc74b13..c03cf7f 100644 --- a/setup.py +++ b/setup.py @@ -10,15 +10,16 @@ setup(name = 'hiprofile', author = 'Jeremy Kerr', author_email = 'jk@ozlabs.org', requires = ['jinja2'], - py_modules = ['hiprofile'], + packages = ['hiprofile'], scripts = ['scripts/hiprofile'], - data_files = [('share/hiprofile/', - ['share/hiprofile/base.html', - 'share/hiprofile/report.html', - 'share/hiprofile/binary.html', - 'share/hiprofile/symbol.html', - 'share/hiprofile/style.css', - 'share/hiprofile/bar.png', - 'share/hiprofile/hiprofile.js', - 'share/hiprofile/jquery-1.3.1.min.js']), - ]) + package_data = {'hiprofile': + ['resources/base.html', + 'resources/report.html', + 'resources/binary.html', + 'resources/symbol.html', + 'resources/style.css', + 'resources/bar.png', + 'resources/hiprofile.js', + 'resources/jquery-1.3.1.min.js'], + } + ) -- 2.39.2