From df9625f7f3c97b31d02c4ad05149b272f905760d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20Fr=C3=A9cinaux?= Date: Tue, 1 Nov 2011 16:59:39 +0100 Subject: [PATCH 1/3] Add support for the PATH environment variable. --- hyde/plugin.py | 10 +++++++++- hyde/util.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/hyde/plugin.py b/hyde/plugin.py index 01153c1..ad8a7d0 100644 --- a/hyde/plugin.py +++ b/hyde/plugin.py @@ -8,11 +8,12 @@ from hyde import loader from hyde.exceptions import HydeException from hyde.fs import File -from hyde.util import getLoggerWithNullHandler, first_match +from hyde.util import getLoggerWithNullHandler, first_match, discover_executable from hyde.model import Expando from functools import partial +import os import re import subprocess import traceback @@ -255,6 +256,13 @@ class CLTransformer(Plugin): raise self.template.exception_class( self.executable_not_found_message) + # Honour the PATH environment variable. + if not os.path.isabs(app_path): + app_path = discover_executable(app_path) + if app_path is None: + raise self.template.exception_class( + self.executable_not_found_message) + app = File(app_path) if not app.exists: diff --git a/hyde/util.py b/hyde/util.py index eff646e..0aced1b 100644 --- a/hyde/util.py +++ b/hyde/util.py @@ -2,6 +2,7 @@ Module for python 2.6 compatibility. """ import logging +import os import sys from itertools import ifilter, izip, tee @@ -122,3 +123,14 @@ def first_match(predicate, iterable): if predicate(item): return item return None + +def discover_executable(name): + """ + Finds an executable in the path list provided by the PATH + environment variable. + """ + for path in os.environ['PATH'].split(os.pathsep): + full_name = os.path.join(path, name) + if os.path.exists(full_name): + return full_name + return None From f8176ab65594c0acf8147b0ab0d938a1497241ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20Fr=C3=A9cinaux?= Date: Tue, 1 Nov 2011 17:13:04 +0100 Subject: [PATCH 2/3] Allow plugins to have a default application path. This is very handy in Unix world as most applications have standard installation paths and standard command-line names. Hence, we can now use those plugins without altering the site.yaml file at all. --- hyde/ext/plugins/less.py | 2 ++ hyde/ext/plugins/uglify.py | 4 +++- hyde/plugin.py | 20 ++++++++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/hyde/ext/plugins/less.py b/hyde/ext/plugins/less.py index 4c574fe..adbff34 100644 --- a/hyde/ext/plugins/less.py +++ b/hyde/ext/plugins/less.py @@ -15,6 +15,8 @@ class LessCSSPlugin(CLTransformer): The plugin class for less css """ + default_app_path = "lessc" + def __init__(self, site): super(LessCSSPlugin, self).__init__(site) diff --git a/hyde/ext/plugins/uglify.py b/hyde/ext/plugins/uglify.py index 2e0ca6a..49a0097 100644 --- a/hyde/ext/plugins/uglify.py +++ b/hyde/ext/plugins/uglify.py @@ -11,6 +11,8 @@ class UglifyPlugin(CLTransformer): The plugin class for Uglify JS """ + default_app_path = "uglifyjs" + def __init__(self, site): super(UglifyPlugin, self).__init__(site) @@ -68,4 +70,4 @@ class UglifyPlugin(CLTransformer): self.call_app(args) out = target.read_all() - return out \ No newline at end of file + return out diff --git a/hyde/plugin.py b/hyde/plugin.py index ad8a7d0..7c494a3 100644 --- a/hyde/plugin.py +++ b/hyde/plugin.py @@ -220,6 +220,14 @@ class CLTransformer(Plugin): return {} + @property + def default_app_path(self): + """ + Default command line application path. Can be overridden + by specifying it in config. + """ + return self.plugin_name + @property def executable_not_found_message(self): """ @@ -253,15 +261,15 @@ class CLTransformer(Plugin): try: app_path = getattr(self.settings, 'app') except AttributeError: - raise self.template.exception_class( - self.executable_not_found_message) + app_path = self.default_app_path # Honour the PATH environment variable. - if not os.path.isabs(app_path): + if app_path is not None and not os.path.isabs(app_path): app_path = discover_executable(app_path) - if app_path is None: - raise self.template.exception_class( - self.executable_not_found_message) + + if app_path is None: + raise self.template.exception_class( + self.executable_not_found_message) app = File(app_path) From d31b4393cbd0535913862066ef5d8a31ed6c57b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20Fr=C3=A9cinaux?= Date: Tue, 1 Nov 2011 18:04:09 +0100 Subject: [PATCH 3/3] Update tests to use $PATH. --- hyde/tests/ext/test_less.py | 11 ++++------- hyde/tests/ext/test_optipng.py | 7 +------ hyde/tests/ext/test_stylus.py | 24 ++++++++---------------- hyde/tests/ext/test_uglify.py | 12 ++++-------- 4 files changed, 17 insertions(+), 37 deletions(-) diff --git a/hyde/tests/ext/test_less.py b/hyde/tests/ext/test_less.py index f8cd849..f1cb12e 100644 --- a/hyde/tests/ext/test_less.py +++ b/hyde/tests/ext/test_less.py @@ -29,13 +29,10 @@ class TestLess(object): def test_can_execute_less(self): s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.less.LessCSSPlugin'] - paths = ['/usr/local/share/npm/bin/lessc', '~/local/bin/lessc', - '/usr/bin/lessc', '~/bin/lessc'] - less = [path for path in paths if File(path).exists] - if not less: - assert False, "Cannot find the lessc executable" - less = less[0] - s.config.less = Expando(dict(app=less)) + paths = ['/usr/local/share/npm/bin/lessc'] + for path in paths: + if File(path).exists: + s.config.less = Expando(dict(app=path)) source = TEST_SITE.child('content/media/css/site.less') target = File(Folder(s.config.deploy_root_path).child('media/css/site.css')) gen = Generator(s) diff --git a/hyde/tests/ext/test_optipng.py b/hyde/tests/ext/test_optipng.py index 480e1da..8cb54eb 100644 --- a/hyde/tests/ext/test_optipng.py +++ b/hyde/tests/ext/test_optipng.py @@ -31,12 +31,7 @@ class TestOptipng(object): s = Site(TEST_SITE) s.config.mode = "production" s.config.plugins = ['hyde.ext.plugins.optipng.OptiPNGPlugin'] - paths = ['/usr/local/bin/optipng', '/usr/bin/optipng'] - optipng = [path for path in paths if File(path).exists] - if not optipng: - assert False, "Cannot find the optipng executable" - optipng = optipng[0] - s.config.optipng = Expando(dict(app=optipng, args=dict(quiet=""))) + s.config.optipng = Expando(dict(args=dict(quiet=""))) source =File(TEST_SITE.child('content/media/images/hyde-lt-b.png')) target = File(Folder(s.config.deploy_root_path).child('media/images/hyde-lt-b.png')) gen = Generator(s) diff --git a/hyde/tests/ext/test_stylus.py b/hyde/tests/ext/test_stylus.py index 039bebf..1f37a1f 100644 --- a/hyde/tests/ext/test_stylus.py +++ b/hyde/tests/ext/test_stylus.py @@ -29,14 +29,10 @@ class TestLess(object): def test_can_execute_stylus(self): s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.stylus.StylusPlugin'] - paths = ['/usr/local/share/npm/bin/stylus', '~/local/bin/stylus', - '~/bin/stylus'] - stylus = [path for path in paths if File(path).exists] - if not stylus: - assert False, "Cannot find the stylus executable" - - stylus = stylus[0] - s.config.stylus = Expando(dict(app=stylus)) + paths = ['/usr/local/share/npm/bin/stylus'] + for path in paths: + if File(path).exists: + s.config.stylus = Expando(dict(app=path)) source = TEST_SITE.child('content/media/css/site.styl') target = File(Folder(s.config.deploy_root_path).child('media/css/site.css')) gen = Generator(s) @@ -51,14 +47,10 @@ class TestLess(object): s = Site(TEST_SITE) s.config.mode = "production" s.config.plugins = ['hyde.ext.plugins.stylus.StylusPlugin'] - paths = ['/usr/local/share/npm/bin/stylus', '~/local/bin/stylus', - '~/bin/stylus'] - stylus = [path for path in paths if File(path).exists] - if not stylus: - assert False, "Cannot find the stylus executable" - - stylus = stylus[0] - s.config.stylus = Expando(dict(app=stylus)) + paths = ['/usr/local/share/npm/bin/stylus'] + for path in paths: + if File(path).exists: + s.config.stylus = Expando(dict(app=path)) source = TEST_SITE.child('content/media/css/site.styl') target = File(Folder(s.config.deploy_root_path).child('media/css/site.css')) gen = Generator(s) diff --git a/hyde/tests/ext/test_uglify.py b/hyde/tests/ext/test_uglify.py index 2bd7175..43a593e 100644 --- a/hyde/tests/ext/test_uglify.py +++ b/hyde/tests/ext/test_uglify.py @@ -31,14 +31,10 @@ class TestUglify(object): s = Site(TEST_SITE) s.config.plugins = ['hyde.ext.plugins.uglify.UglifyPlugin'] s.config.mode = "production" - paths = ['/usr/local/share/npm/bin/uglifyjs', '~/local/bin/uglifyjs', - '/usr/bin/uglifyjs', '~/bin/uglifyjs'] - uglify = [path for path in paths if File(path).exists] - if not uglify: - assert False, "Cannot find the uglify executable" - - uglify = uglify[0] - s.config.uglify = Expando(dict(app=uglify)) + paths = ['/usr/local/share/npm/bin/uglifyjs'] + for path in paths: + if File(path).exists: + s.config.uglify = Expando(dict(app=path)) source = TEST_SITE.child('content/media/js/jquery.js') target = File(Folder(s.config.deploy_root_path).child('media/js/jquery.js')) gen = Generator(s)