Browse Source

Merge pull request #100 from nud/hyde

---

On Unix-based system, the $PATH variable plays a central role at locating executables. Most of the time, you will be able to find your well-known executable easily by looking up in all the directories listed in $PATH, and you can even add an extra directory by altering the PATH variable in your shell beforehand.

For instance, in my own .bashrc file, the folllowing line is sufficient to be able to call anything I installed in my home directory:
```export PATH=~/.local/bin:$PATH```

This is why I added support for that handy environment variable and for a default application command in hyde. This makes it possible to not do any assumptions in the site.yaml file, making it easier to edit or generate the site on several computers with a different setup (e.g. if lessc is not installed with the same path on both machines, like what happened to me when I wanted to generate your hyde-docs site).
main
Lakshmi Vyasarajan 13 years ago
parent
commit
a736691ca9
8 changed files with 51 additions and 39 deletions
  1. +2
    -0
      hyde/ext/plugins/less.py
  2. +3
    -1
      hyde/ext/plugins/uglify.py
  3. +17
    -1
      hyde/plugin.py
  4. +4
    -7
      hyde/tests/ext/test_less.py
  5. +1
    -6
      hyde/tests/ext/test_optipng.py
  6. +8
    -16
      hyde/tests/ext/test_stylus.py
  7. +4
    -8
      hyde/tests/ext/test_uglify.py
  8. +12
    -0
      hyde/util.py

+ 2
- 0
hyde/ext/plugins/less.py View File

@@ -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)



+ 3
- 1
hyde/ext/plugins/uglify.py View File

@@ -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
return out

+ 17
- 1
hyde/plugin.py View File

@@ -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
@@ -219,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):
"""
@@ -252,6 +261,13 @@ class CLTransformer(Plugin):
try:
app_path = getattr(self.settings, 'app')
except AttributeError:
app_path = self.default_app_path

# Honour the PATH environment variable.
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)



+ 4
- 7
hyde/tests/ext/test_less.py View File

@@ -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)


+ 1
- 6
hyde/tests/ext/test_optipng.py View File

@@ -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)


+ 8
- 16
hyde/tests/ext/test_stylus.py View File

@@ -29,14 +29,10 @@ class TestStylus(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 TestStylus(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)


+ 4
- 8
hyde/tests/ext/test_uglify.py View File

@@ -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)


+ 12
- 0
hyde/util.py View File

@@ -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

Loading…
Cancel
Save