Package pyrobase :: Package paver :: Module support
[hide private]
[frames] | no frames]

Source Code for Module pyrobase.paver.support

  1  # -*- coding: utf-8 -*- 
  2  # pylint: disable= 
  3  """ Paver Task Implementation Helpers. 
  4   
  5      Copyright (c) 2011 The PyroScope Project <pyroscope.project@gmail.com> 
  6  """ 
  7  # This program is free software; you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation; either version 2 of the License, or 
 10  # (at your option) any later version. 
 11  # 
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  # GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License along 
 18  # with this program; if not, write to the Free Software Foundation, Inc., 
 19  # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 20  import os 
 21  import sys 
 22  import pkg_resources 
 23   
 24  from paver import easy, tasks 
 25  from pyrobase import iterutil 
 26   
 27   
28 -def venv_bin(name=None):
29 """ Get the directory for virtualenv stubs, or a full executable path 30 if C{name} is provided. 31 """ 32 if not hasattr(sys, "real_prefix"): 33 easy.error("ERROR: '%s' is not a virtualenv" % (sys.executable,)) 34 sys.exit(1) 35 36 for bindir in ("bin", "Scripts"): 37 bindir = os.path.join(sys.prefix, bindir) 38 if os.path.exists(bindir): 39 if name: 40 return os.path.join(bindir, name + os.path.splitext(sys.executable)[1]) 41 else: 42 return bindir 43 44 easy.error("ERROR: Scripts directory not found in '%s'" % (sys.prefix,)) 45 sys.exit(1)
46 47
48 -def vsh(cmd, *args, **kw):
49 """ Execute a command installed into the active virtualenv. 50 """ 51 args = '" "'.join(i.replace('"', r'\"') for i in args) 52 easy.sh('"%s" "%s"' % (venv_bin(cmd), args))
53 54
55 -def install_tools(dependencies):
56 """ Install a required tool before using it, if it's missing. 57 58 Note that C{dependencies} can be a distutils requirement, 59 or a simple name from the C{tools} task configuration, or 60 a (nested) list of such requirements. 61 """ 62 tools = getattr(easy.options, "tools", {}) 63 for dependency in iterutil.flatten(dependencies): 64 dependency = tools.get(dependency, dependency) 65 try: 66 pkg_resources.require(dependency) 67 except pkg_resources.DistributionNotFound: 68 vsh("pip", "install", "-q", dependency) 69 dependency = pkg_resources.require(dependency) 70 easy.info("Installed required tool %s" % (dependency,))
71 72
73 -def task_requires(*dependencies):
74 """ A task decorator that ensures a distutils dependency (or a list thereof) is met 75 before that task is executed. 76 """ 77 def entangle(task): 78 "Decorator wrapper." 79 if not isinstance(task, tasks.Task): 80 task = tasks.Task(task) 81 82 def tool_task(*args, **kw): 83 "Install requirements, then call original task." 84 install_tools(dependencies) 85 return task_body(*args, **kw)
86 87 # Apply our wrapper to original task 88 task_body, task.func = task.func, tool_task 89 return task 90 91 return entangle 92 93
94 -def toplevel_packages():
95 """ Get package list, without sub-packages. 96 """ 97 packages = set(easy.options.setup.packages) 98 for pkg in list(packages): 99 packages -= set(p for p in packages if str(p).startswith(pkg + '.')) 100 return list(sorted(packages))
101