Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #311 from dephell/self-update
Browse files Browse the repository at this point in the history
Add self upgrade command
  • Loading branch information
orsinium committed Nov 19, 2019
2 parents 6138969 + 0f75e1a commit 0973341
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dephell/commands/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
'project test',
'project validate',

'self upgrade',

'vendor download',
'vendor import',

Expand Down
44 changes: 44 additions & 0 deletions dephell/commands/self_upgrade.py
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)

0 comments on commit 0973341

Please sign in to comment.