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

Commit

Permalink
Merge branch 'test-commands'
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Apr 5, 2019
2 parents b716f77 + e51c577 commit bd7e008
Show file tree
Hide file tree
Showing 37 changed files with 522 additions and 207 deletions.
10 changes: 7 additions & 3 deletions dephell/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
from typing import Optional

# app
from ..config import config
from ..config import config, Config
from .helpers import getitem


class BaseCommand:
logger = getLogger('dephell.commands')

def __init__(self, argv):
def __init__(self, argv, config: Config = None):
parser = self.get_parser()
self.args = parser.parse_args(argv)
self.config = self.get_config(self.args)

if config is None:
self.config = self.get_config(self.args)
else:
self.config = config

@classmethod
def get_parser(cls):
Expand Down
4 changes: 3 additions & 1 deletion dephell/commands/build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# built-in
from argparse import ArgumentParser
from pathlib import Path

# app
from ..config import builders
Expand Down Expand Up @@ -57,13 +58,14 @@ def __call__(self):
self.logger.info('merged')

# dump
project_path = Path(self.config['project'])
for to_format, to_path in DUMPERS:
if to_format == self.config['from']['format']:
continue
self.logger.info('dumping...', extra=dict(format=to_format))
dumper = CONVERTERS[to_format]
dumper.dump(
path=to_path,
path=project_path.joinpath(to_path),
reqs=Requirement.from_graph(resolver.graph, lock=False),
project=resolver.graph.metainfo,
)
Expand Down
2 changes: 1 addition & 1 deletion dephell/commands/deps_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_parser(cls):
return parser

def __call__(self) -> bool:
loader_config = self.config.get('to', self.config['from'])
loader_config = self.config.get('to') or self.config['from']
self.logger.info('get dependencies', extra=dict(
format=loader_config['format'],
path=loader_config['path'],
Expand Down
22 changes: 13 additions & 9 deletions dephell/commands/deps_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ def get_parser(cls):
return parser

def __call__(self):
loader_config = self.config.get('to', self.config['from'])
loader = CONVERTERS[loader_config['format']]
if loader.lock:
self.logger.info('get dependencies from lockfile', extra=dict(
format=loader_config['format'],
path=loader_config['path'],
))
root = loader.load(path=loader_config['path'])
else:
root = None

loader_config = self.config.get('to') or self.config.get('from')
if loader_config is not None:
loader = CONVERTERS[loader_config['format']]
if loader.lock:
self.logger.info('get dependencies from lockfile', extra=dict(
format=loader_config['format'],
path=loader_config['path'],
))
root = loader.load(path=loader_config['path'])

if root is None:
venvs = VEnvs(path=self.config['venv'])
venv = venvs.get(Path(self.config['project']), env=self.config.env)
if venv.exists():
Expand Down
8 changes: 5 additions & 3 deletions dephell/commands/generate_editorconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ def get_parser(cls):
return parser

def __call__(self):
project_path = Path(self.config['project'])

matched = []
non_matched = list(RULES)
for path in Path().iterdir():
for path in project_path.iterdir():
for i, (match, _rule) in enumerate(non_matched):
if fnmatch(path.name, match):
matched.append(non_matched.pop(i))
break

matched = ['[{}]\n{}'.format(match, '\n'.join(rule)) for match, rule in matched]
text = HEADER + '\n\n'.join(matched)
Path('.editorconfig').write_text(text)
text = HEADER + '\n\n'.join(matched) + '\n'
(project_path / '.editorconfig').write_text(text)
self.logger.info('editorconfig generated')
return True
2 changes: 1 addition & 1 deletion dephell/commands/generate_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ def __call__(self):
year=datetime.now().year,
name=getuser().title(),
))
Path('LICENSE').write_text(text)
(Path(self.config['project']) / 'LICENSE').write_text(text)
self.logger.info('license generated', extra=dict(license=license.name))
return True
9 changes: 5 additions & 4 deletions dephell/commands/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ def get_python(config: Config) -> Python:
return pythons.get_best(python)

# defined in dependency file
loader = CONVERTERS[config['from']['format']]
root = loader.load(path=config['from']['path'])
if root.python:
return pythons.get_by_spec(root.python)
if 'from' in config:
loader = CONVERTERS[config['from']['format']]
root = loader.load(path=config['from']['path'])
if root.python:
return pythons.get_by_spec(root.python)

return pythons.current
36 changes: 36 additions & 0 deletions dephell/context_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# built-in
import os
from contextlib import contextmanager


@contextmanager
def chdir(path):
"""Context manager for changing dir and restoring previous workdir after exit.
"""
curdir = os.getcwd()

path = str(path)
if not os.path.exists(path):
os.makedirs(path)

os.chdir(path)
try:
yield
finally:
os.chdir(curdir)


@contextmanager
def nullcontext(value=None):
yield value


@contextmanager
def env_var(key, value):
old_value = os.environ.get(key)
os.environ[key] = value
yield
if old_value is None:
del os.environ[key]
else:
os.environ[key] = old_value
4 changes: 2 additions & 2 deletions dephell/controllers/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..config import config
from .conflict import analize_conflict
from ..models import RootDependency
from ..utils import nullcontext
from ..context_tools import nullcontext


logger = getLogger('dephell.resolver')
Expand Down Expand Up @@ -72,7 +72,7 @@ def unapply(self, dep, *, force: bool = True, soft: bool = False) -> None:

def resolve(self, debug: bool = False, level: Optional[int] = None) -> bool:
if config['silent']:
spinner = nullcontext(type('Mock', [], {}))
spinner = nullcontext(type('Mock', (), {}))
else:
spinner = yaspin(text="resolving...")

Expand Down
2 changes: 1 addition & 1 deletion dephell/converters/setuppy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# app
from ..controllers import DependencyMaker, Readme
from ..models import Author, EntryPoint, RootDependency
from ..utils import chdir
from ..context_tools import chdir
from .base import BaseConverter

try:
Expand Down
3 changes: 2 additions & 1 deletion dephell/repositories/git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from ...config import config
from ...models.git_release import GitRelease
from ...models.release import Release
from ...utils import cached_property, chdir
from ...utils import cached_property
from ...context_tools import chdir
from ..base import Interface


Expand Down
23 changes: 0 additions & 23 deletions dephell/utils.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
# built-in
import os
import platform
from contextlib import contextmanager
from itertools import product


@contextmanager
def chdir(path):
"""Context manager for changing dir and restoring previous workdir after exit.
"""
curdir = os.getcwd()

path = str(path)
if not os.path.exists(path):
os.makedirs(path)

os.chdir(path)
try:
yield
finally:
os.chdir(curdir)


@contextmanager
def nullcontext(value=None):
yield value


def lazy_product(*all_groups):
slices = [[] for _ in range(len(all_groups))]
all_groups = [iter(groups) for groups in all_groups]
Expand Down
4 changes: 2 additions & 2 deletions dephell/venvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def ensure_directories(self, env_dir):

@attr.s()
class VEnv:
path = attr.ib(type=Path, convert=Path)
path = attr.ib(type=Path, converter=Path)

project = attr.ib(type=str, default=None)
env = attr.ib(type=str, default=None)
Expand Down Expand Up @@ -132,7 +132,7 @@ def clone(self, path: Path) -> 'VEnv':

@attr.s()
class VEnvs:
path = attr.ib(type=Path, convert=Path)
path = attr.ib(type=Path, converter=Path)

@cached_property
def current(self) -> Optional[VEnv]:
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import shutil
from pathlib import Path
import pytest


@pytest.fixture()
def temp_path(tmp_path: Path):
for path in tmp_path.iterdir():
if path.is_file():
path.unlink()
else:
shutil.rmtree(str(path))
yield tmp_path
51 changes: 0 additions & 51 deletions tests/requirements/egg-info/dephell/cache.py

This file was deleted.

30 changes: 0 additions & 30 deletions tests/requirements/egg-info/dephell/cli.py

This file was deleted.

6 changes: 0 additions & 6 deletions tests/requirements/egg-info/dephell/exceptions.py

This file was deleted.

0 comments on commit bd7e008

Please sign in to comment.