From d4277ccda9284c61153ee95bc69f301820fcc494 Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Thu, 5 Dec 2013 22:04:29 +0100
Subject: [PATCH] Use pkg_resources to get templates location

This removes the need for the top-level package_root function. On the
way we also remove the top-level is_test function that was obsolete.
---
 DEVELOPMENT.md         |  4 ++--
 INSTALL.md             |  2 +-
 fabfile.py             |  2 +-
 mutalyzer/Scheduler.py | 10 ++--------
 mutalyzer/__init__.py  | 27 ---------------------------
 mutalyzer/website.py   | 18 +++++++++++-------
 6 files changed, 17 insertions(+), 46 deletions(-)

diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md
index b62dbd47..09c43ca8 100644
--- a/DEVELOPMENT.md
+++ b/DEVELOPMENT.md
@@ -24,11 +24,11 @@ web service:
 
 Now run the tests with:
 
-    MUTALYZER_ENV=test nosetests -v
+    nosetests -v
 
 Or, if you are in a hurry, skip the long-running tests with:
 
-    MUTALYZER_ENV=test MUTALYZER_QUICK_TEST=1 nosetests -v
+    MUTALYZER_QUICK_TEST=1 nosetests -v
 
 
 Working with feature branches
diff --git a/INSTALL.md b/INSTALL.md
index 7edef864..a4f7c2ce 100644
--- a/INSTALL.md
+++ b/INSTALL.md
@@ -131,7 +131,7 @@ the batch daemon and the webserver (the SOAP part) running.
 
 Now run the tests:
 
-    MUTALYZER_ENV=test nosetests
+    nosetests
 
 
 Upgrade Mutalyzer
diff --git a/fabfile.py b/fabfile.py
index fcbe006d..9f6f8542 100644
--- a/fabfile.py
+++ b/fabfile.py
@@ -70,7 +70,7 @@ def deploy(bootstrap='no'):
                 sudo('bash extras/post-upgrade.sh')
 
             # Run unittests.
-            #run('MUTALYZER_ENV=test nosetests -v')
+            #run('nosetests -v')
 
     # Now that all is set up, delete the folder again.
     # (I don't like to 'sudo rm -Rf'.)
diff --git a/mutalyzer/Scheduler.py b/mutalyzer/Scheduler.py
index d82ba140..39494f5b 100644
--- a/mutalyzer/Scheduler.py
+++ b/mutalyzer/Scheduler.py
@@ -91,14 +91,8 @@ class Scheduler() :
         @arg url: The url containing the results
         @type url: string
         """
-        if mutalyzer.is_test():
-            return
-
-        # Note: The above check with mutalyzer.is_test is bogus, since during
-        # a normal unit test, the batch checker process is not started in the
-        # environment of the unit tests.
-        # As sort of a hack, we therefore check for the patented address
-        # 'test@test.test', used in the unit tests.
+        # Mail is set to 'test@test.test" if the batch job was submitted from
+        # a unit test.
         if mailTo == 'test@test.test':
             return
 
diff --git a/mutalyzer/__init__.py b/mutalyzer/__init__.py
index d087516e..13b87384 100644
--- a/mutalyzer/__init__.py
+++ b/mutalyzer/__init__.py
@@ -3,9 +3,6 @@ HGVS variant nomenclature checker.
 """
 
 
-import os
-
-
 # On the event of a new release, we update the __version_info__ and __date__
 # package globals and set RELEASE to True.
 # Before a release, a development version is denoted by a __version_info__
@@ -38,27 +35,3 @@ NOMENCLATURE_VERSION = '.'.join(NOMENCLATURE_VERSION_INFO)
 COPYRIGHT_YEARS = (2009, int(__date__[-4:]))
 
 SOAP_NAMESPACE = 'http://mutalyzer.nl/2.0/services'
-
-
-def package_root():
-    """
-    Get the absolute path to the mutalyzer package. This is usefull for
-    things like locating HTML templates (which are in a subdirectory of the
-    package).
-
-    @return: Absolute path to the mutalyzer package.
-    @rtype:  string
-    """
-    return os.path.realpath(os.path.split(__file__)[0])
-
-
-def is_test():
-    """
-    Check if we are in a test environment. This is determined by the
-    MUTALYZER_ENV environment variable, which should then be set to 'test'.
-
-    @return: True if we are in a test environment, False otherwise.
-    @rtype:  bool
-    """
-    return 'MUTALYZER_ENV' in os.environ \
-           and os.environ['MUTALYZER_ENV'] == 'test'
diff --git a/mutalyzer/website.py b/mutalyzer/website.py
index ed87d273..bb1787fe 100644
--- a/mutalyzer/website.py
+++ b/mutalyzer/website.py
@@ -5,7 +5,6 @@ General Mutalyzer website interface.
 
 SERVICE_SOAP_LOCATION = '/services'
 SERVICE_JSON_LOCATION = '/json'
-WSDL_VIEWER = 'templates/wsdl-viewer.xsl'
 GENOME_BROWSER_URL = 'http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg19&position={chromosome}:{start}-{stop}&hgt.customText={bed_file}'
 
 
@@ -27,6 +26,7 @@ import urllib
 from collections import defaultdict
 
 from lxml import etree
+import pkg_resources
 from cStringIO import StringIO
 from simpletal import simpleTALES
 from simpletal import simpleTAL
@@ -176,7 +176,7 @@ class render_tal:
 
 
 # TAL template render
-render = render_tal(os.path.join(mutalyzer.package_root(), 'templates'),
+render = render_tal(pkg_resources.resource_filename('mutalyzer', 'templates'),
     globals = {
     'version'             : mutalyzer.__version__,
     'nomenclatureVersion' : mutalyzer.NOMENCLATURE_VERSION,
@@ -231,7 +231,9 @@ class Download:
         The url routing currently makes sure to only call this with filenames
         of the form [a-zA-Z-]+\.(?:py|cs).
         """
-        file_path = os.path.join(mutalyzer.package_root(), 'templates', file)
+        file_path = os.path.join(
+            pkg_resources.resource_filename('mutalyzer', 'templates'),
+            file)
 
         if not os.path.isfile(file_path):
             raise web.notfound()
@@ -263,8 +265,9 @@ class Downloads:
         The url routing currently makes sure to only call this with filenames
         of the form [a-zA-Z\._-]+.
         """
-        file_path = os.path.join(mutalyzer.package_root(), 'templates',
-                                 'downloads', file)
+        file_path = os.path.join(
+            pkg_resources.resource_filename('mutalyzer', 'templates'),
+            'downloads', file)
 
         if not os.path.isfile(file_path):
             raise web.notfound()
@@ -1541,8 +1544,9 @@ class SoapApi:
         wsdl = Wsdl11(soap.application.interface)
         wsdl.build_interface_document(url)
         wsdl_handle = StringIO(wsdl.get_interface_document())
-        xsl_handle = open(os.path.join(mutalyzer.package_root(), WSDL_VIEWER),
-                          'r')
+        xsl_handle = open(os.path.join(
+                pkg_resources.resource_filename('mutalyzer', 'templates'),
+                'wsdl-viewer.xsl'), 'r')
         wsdl_doc = etree.parse(wsdl_handle)
         xsl_doc = etree.parse(xsl_handle)
         transform = etree.XSLT(xsl_doc)
-- 
GitLab