This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #311 from dephell/self-update
Add self upgrade command
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,8 @@ | |
'project test', | ||
'project validate', | ||
|
||
'self upgrade', | ||
|
||
'vendor download', | ||
'vendor import', | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# built-in | ||
import subprocess | ||
import sys | ||
from argparse import ArgumentParser | ||
from pathlib import Path | ||
|
||
# app | ||
from ..config import builders | ||
from ..context_tools import chdir | ||
from ..package_manager import PackageManager | ||
from .base import BaseCommand | ||
|
||
|
||
class SelfUpgradeCommand(BaseCommand): | ||
"""Upgrade DepHell to the latest version. | ||
""" | ||
|
||
@classmethod | ||
def get_parser(cls) -> ArgumentParser: | ||
parser = cls._get_default_parser() | ||
builders.build_config(parser) | ||
builders.build_output(parser) | ||
builders.build_other(parser) | ||
return parser | ||
|
||
def __call__(self) -> bool: | ||
path = Path(__file__).parent.parent.parent | ||
if (path / 'README.md').exists() and (path / '.git').exists(): | ||
return self._upgrade_repo(path=path) | ||
return self._upgrade_package() | ||
|
||
def _upgrade_repo(self, path: Path) -> bool: | ||
with chdir(path): | ||
cmd = ['git', 'pull', 'origin', 'master'] | ||
result = subprocess.run(cmd) | ||
return (result.returncode == 0) | ||
|
||
def _upgrade_package(self) -> bool: | ||
manager = PackageManager(executable=sys.executable) | ||
args = ['-U', '--upgrade-strategy=eager'] | ||
if manager.is_global: | ||
args.append('--user') | ||
code = manager.run('install', *args, 'dephell') | ||
return (code == 0) |