# HG changeset patch # User Peter Sanchez # Date 1510284713 28800 # Thu Nov 09 19:31:53 2017 -0800 # Node ID b8c7c81dca371b9d151da9b6eab9d6d5cb9fc39d # Parent 046529367db260e4ffce1ee3426891325c2b1db5 Adding support for Python3 virtual environments and DelpoyConfig json formatting diff --git a/djeploy/__init__.py b/djeploy/__init__.py --- a/djeploy/__init__.py +++ b/djeploy/__init__.py @@ -1,10 +1,12 @@ import sys -from djeploy.globals import set_env, djeploy_require, get_env, command +import json +from fabric.api import env +from .globals import set_env, djeploy_require, get_env, command __version__ = '0.2.5-dev' -__all__ = ['set_env', 'djeploy_require', 'get_env', 'command',] +__all__ = ['set_env', 'djeploy_require', 'get_env', 'command', 'DeployConfig'] default_configs = { @@ -17,6 +19,10 @@ 'releases_dirname': 'releases', # directory name to store the releases 'num_releases': 5, # number of old deploys to leave on server + # Virtual Environment options + 'venv_type': 'virtualenv', # Other option is 'python3' (uses -m venv) + 'python3_bin': 'python3', # ie, python3.6, /crazy/path/to/python3.6 + # Ops module settings 'space_options': { 'default': lambda: command.abort('No space option was provided.'), @@ -46,3 +52,54 @@ 'initd_use_pty': False, } set_env(**default_configs) + + +class DeployConfig(object): + _data = {} + + def __init__(self, config_file='fabconfig.json'): + self.config_file = config_file + self.load_config() + self._global_vars() + + def load_config(self): + self._data = json.load(open(self.config_file)) + + def _global_vars(self): + if 'global' in self._data: + self.load_global(self._data['global']) + + if 'fab' in self._data: + self.load_fab(self._data['fab']) + + if 'djeploy' in self._data: + self.load_djeploy(self._data['djeploy']) + + def load_global(self, data): + for k, v in data.items(): + setattr(self, k, v) + + def load_fab(self, data): + for k, v in data.items(): + setattr(env, k, v) + + def load_djeploy(self, data): + set_env(**data) + + def load_space(self, space): + if 'spaces' not in self._data: + raise ValueError( + 'Invalid json config provided. No spaces element.' + ) + + if space not in self._data['spaces']: + raise ValueError( + 'No {0} element in spaces.'.format(space) + ) + + # Load all space section overrides. Options are global, fab, & djeploy + for key in self._data['spaces'][space].keys(): + key_data = self._data['spaces'][space][key] + if key_data and hasattr(self, 'load_{0}'.format(key)): + func = getattr(self, 'load_{0}'.format(key)) + func(key_data) diff --git a/djeploy/deploy.py b/djeploy/deploy.py --- a/djeploy/deploy.py +++ b/djeploy/deploy.py @@ -141,8 +141,10 @@ python=None, env_dir='env'): ''' Create the virtual environment ''' - opts = get_env('release_path') + opts = get_env('release_path', 'venv_type', 'python3_bin') release_path = opts['release_path'] + venv_type = opts['venv_type'] + python3_bin = opts['python3_bin'] if extra_path is not None: release_path = os.path.join(release_path, extra_path) @@ -151,11 +153,18 @@ # Sanity check env_dir = '.' - cmd = 'virtualenv ' - if not site_pkgs: - cmd += '--no-site-packages ' - if python is not None: - cmd += '-p %s ' % python + if venv_type == 'virtualenv': + cmd = 'virtualenv ' + if not site_pkgs: + cmd += '--no-site-packages ' + if python is not None: + cmd += '-p {0} '.format(python) + else: + # Pass in custom path by using the python variable + cmd = '{0} -m venv '.format(python or python3_bin) + if site_pkgs: + cmd += '--system-site-packages ' + cmd += env_dir if not command.exists(release_path):