Skip to content

Commit

Permalink
Extract run() to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dmpetrov committed Mar 26, 2017
1 parent c011baf commit 9fd1fb5
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 47 deletions.
9 changes: 2 additions & 7 deletions dvc/cmd_data_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import os
from shutil import copyfile
import re
Expand All @@ -10,6 +9,7 @@
from dvc.logger import Logger
from dvc.exceptions import NeatLynxException
from dvc.state_file import StateFile
from dvc.utils import run


class DataImportError(NeatLynxException):
Expand Down Expand Up @@ -138,10 +138,5 @@ def download_file(from_url, to_file):
f.write(chunk)
return


if __name__ == '__main__':
try:
sys.exit(CmdDataImport().run())
except NeatLynxException as e:
Logger.error(e)
sys.exit(1)
run(CmdDataImport())
8 changes: 2 additions & 6 deletions dvc/cmd_data_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dvc.cmd_base import CmdBase
from dvc.exceptions import NeatLynxException
from dvc.logger import Logger
from dvc.utils import run


class DataRemoveError(NeatLynxException):
Expand Down Expand Up @@ -129,9 +130,4 @@ def remove_dir_file_by_file(self, target):


if __name__ == '__main__':
import sys
try:
sys.exit(CmdDataRemove().run())
except NeatLynxException as e:
Logger.error(e)
sys.exit(1)
run(CmdDataRemove())
8 changes: 2 additions & 6 deletions dvc/cmd_data_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dvc.cmd_base import CmdBase
from dvc.logger import Logger
from dvc.exceptions import NeatLynxException
from dvc.utils import run


class DataSyncError(NeatLynxException):
Expand Down Expand Up @@ -113,9 +114,4 @@ def sync_to_cloud(self, data_path):


if __name__ == '__main__':
import sys
try:
sys.exit(CmdDataSync().run())
except NeatLynxException as e:
Logger.error(e)
sys.exit(1)
run(CmdDataSync())
9 changes: 2 additions & 7 deletions dvc/cmd_init.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import sys
from pathlib import Path

from dvc.cmd_base import CmdBase
from dvc.logger import Logger
from dvc.config import Config
from dvc.exceptions import NeatLynxException
from dvc.utils import run


class InitError(NeatLynxException):
Expand Down Expand Up @@ -114,10 +114,5 @@ def modify_gitignore(self, cache_dir_name):

Logger.printing('Directory {} was added to .gitignore file'.format(cache_dir_name))


if __name__ == '__main__':
try:
CmdInit().run()
except NeatLynxException as e:
Logger.error(e)
sys.exit(1)
run(CmdInit())
8 changes: 2 additions & 6 deletions dvc/cmd_repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dvc.exceptions import NeatLynxException
from dvc.path.data_path import NotInDataDirError
from dvc.state_file import StateFile
from dvc.utils import run


class ReproError(NeatLynxException):
Expand Down Expand Up @@ -166,9 +167,4 @@ def reproduce(self, force=False):
return self.reproduce_data_file()

if __name__ == '__main__':
import sys
try:
sys.exit(CmdRepro().run())
except NeatLynxException as e:
Logger.error(e)
sys.exit(1)
run(CmdRepro())
9 changes: 2 additions & 7 deletions dvc/cmd_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dvc.exceptions import NeatLynxException
from dvc.repository_change import RepositoryChange
from dvc.state_file import StateFile
from dvc.utils import run


class RunError(NeatLynxException):
Expand Down Expand Up @@ -174,10 +175,4 @@ def get_data_files_from_args(self, argv):


if __name__ == '__main__':
import sys

try:
sys.exit(CmdRun().run())
except NeatLynxException as e:
Logger.error(e)
sys.exit(1)
run(CmdRun())
14 changes: 13 additions & 1 deletion dvc/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from dvc.exceptions import NeatLynxException
from dvc.logger import Logger


def cached_property(f):
def get(self):
Expand All @@ -11,4 +14,13 @@ def get(self):
x = self._property_cache[f] = f(self)
return x

return property(get)
return property(get)


def run(cmd):
import sys
try:
sys.exit(cmd.run())
except NeatLynxException as e:
Logger.error(e)
sys.exit(1)
9 changes: 2 additions & 7 deletions tests/test_cmd_data_remove.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from unittest import TestCase
import os
import shutil

from dvc.cmd_data_remove import CmdDataRemove, DataRemoveError
from dvc.config import ConfigI
from dvc.path.data_path import NotInDataDirError, DataFilePathError, DataPath
from dvc.git_wrapper import GitWrapperI
from dvc.path.path_factory import PathFactory
from tests.basic_env import BasicEnvironment, DirHierarchyEnvironment
from dvc.path.data_path import DataFilePathError
from tests.basic_env import DirHierarchyEnvironment


class TestCmdDataRemove(DirHierarchyEnvironment):
Expand Down

0 comments on commit 9fd1fb5

Please sign in to comment.