Skip to content

Commit

Permalink
Show error if pip fails, delete module directory on failed install to…
Browse files Browse the repository at this point in the history
… make a later install possible
  • Loading branch information
TheLastProject committed Mar 23, 2017
1 parent 4acc2d3 commit e1120af
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Install dependencies automatically if the module provides a requirements.txt file

### Fixed
- If module installation fails, the module directory is removed, so a subsequent installation doesn't instantly fail

## [0.5] - 2017-03-22
### API changes
- Remove Action.notify_message and Action.notify_error, which are synonyms for add_message and add_error
Expand Down
50 changes: 41 additions & 9 deletions pext/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,26 @@ def _log_error(self, message: str) -> None:
else:
print(message)

def _pip_install(self, module_dir_name: str) -> None:
def _pip_install(self, module_dir_name: str) -> int:
"""Install module dependencies using pip."""
pip.main(['install',
'--upgrade',
'--target',
os.path.join(self.module_dependencies_dir, module_dir_name),
'-r',
os.path.join(self.module_dir, module_dir_name, 'requirements.txt')])
module_requirements_path = os.path.join(self.module_dir, module_dir_name, 'requirements.txt')
module_dependencies_path = os.path.join(self.module_dependencies_dir, module_dir_name)

if not os.path.isfile(module_requirements_path):
return

try:
os.mkdir(module_dependencies_path)
except OSError:
# Probably already exists, that's okay
pass

return pip.main(['install',
'--upgrade',
'--target',
module_dependencies_path,
'-r',
module_requirements_path])

def bind_logger(self, logger: Logger) -> str:
"""Connect a logger to the module manager.
Expand Down Expand Up @@ -655,12 +667,27 @@ def install_module(self, url: str, verbose=False, interactive=True) -> bool:
if verbose:
self._log_error('Failed to install {}'.format(module_name))

try:
rmtree(os.path.join(self.module_dir, dir_name))
except FileNotFoundError:
pass

return False

if verbose:
self._log('Installing dependencies for {}'.format(module_name))

self._pip_install(dir_name)
pip_exit_code = self._pip_install(dir_name)
if pip_exit_code != 0:
if verbose:
self._log_error('Failed to install dependencies for {}, error {}'.format(module_name, pip_exit_code))

try:
rmtree(os.path.join(self.module_dir, dir_name))
except FileNotFoundError:
pass

return False

if verbose:
self._log('Installed {}'.format(module_name))
Expand Down Expand Up @@ -715,7 +742,12 @@ def update_module(self, module_name: str, verbose=False) -> bool:
if verbose:
self._log('Updating dependencies for {}'.format(module_name))

self._pip_install(dir_name)
pip_exit_code = self._pip_install(dir_name)
if pip_exit_code != 0:
if verbose:
self._log_error('Failed to update dependencies for {}, error {}'.format(module_name, pip_exit_code))

return False

if verbose:
self._log('Updated {}'.format(module_name))
Expand Down

0 comments on commit e1120af

Please sign in to comment.