From 4bb569c16dc8de02c736f46f99bccfa3b74cc75e Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 25 Jul 2017 03:27:49 -0700 Subject: [PATCH] Refactoring --- dvc/command/base.py | 4 +++ dvc/command/lock.py | 5 +--- dvc/command/target.py | 59 ++++++++++++++++++++++--------------------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/dvc/command/base.py b/dvc/command/base.py index 51a8e8ce3..abff5dd7a 100644 --- a/dvc/command/base.py +++ b/dvc/command/base.py @@ -94,6 +94,10 @@ class CmdBase(object): parser.add_argument('--jobs', '-j', type=int, default=cpu_count(), help='Number of jobs to run simultaneously.') + @staticmethod + def set_reset_flag(parser, short_name, long_name, message): + parser.add_argument(short_name, long_name, action='store_true', default=False, help=message) + def process_common_args(self): if self._parsed_args.quiet and not self._parsed_args.verbose: Logger.be_quiet() diff --git a/dvc/command/lock.py b/dvc/command/lock.py index 587245b02..a90c41d33 100644 --- a/dvc/command/lock.py +++ b/dvc/command/lock.py @@ -10,10 +10,7 @@ class CmdLock(CmdBase): def define_args(self, parser): self.set_no_git_actions(parser) - - parser.add_argument('-u', '--unlock', action='store_true', default=False, - help='Unlock data item - enable reproduction.') - + self.set_reset_flag('-u', '--unlock', 'Unlock data item - enable reproduction.') parser.add_argument('files', metavar='', help='Data items to lock or unlock.', nargs='*') pass diff --git a/dvc/command/target.py b/dvc/command/target.py index df021b8db..d57e65509 100644 --- a/dvc/command/target.py +++ b/dvc/command/target.py @@ -11,46 +11,31 @@ class CmdTarget(CmdBase): def define_args(self, parser): self.set_no_git_actions(parser) - - parser.add_argument('-u', '--unset', action='store_true', default=False, - help='Reset target.') - + self.set_reset_flag('-u', '--unset', 'Reset target.') parser.add_argument('target', metavar='', nargs='?', help='Target data item.') pass def run(self): with DvcLock(self.is_locker, self.git): - return self.change_target(self.parsed_args.target, self.parsed_args.unset) + target = self.parsed_args.target + unset = self.parsed_args.unset - def change_target(self, target, unset): - if target and unset: - Logger.error('Unable to set target {} and use --unset in a single command'.format(target, unset)) - return 1 + if target and unset: + Logger.error('Unable to set target {} and use --unset in a single command'.format(target)) + return 1 - if not target and not unset: - Logger.error('Target is not defined') - return 1 + if not target and not unset: + Logger.error('Target is not defined') + return 1 - target_conf_file_path = self.settings.path_factory.path(self.settings.config.target_file) + target_conf_file_path = self.settings.path_factory.path(self.settings.config.target_file).relative + return self.change_target(target, unset, target_conf_file_path) + def change_target(self, target, unset, target_conf_file_path): if unset: - if not os.path.exists(target_conf_file_path.relative): - Logger.error('Target conf file {} does not exists'.format( - target_conf_file_path.relative)) - return 1 - - if not os.path.isfile(target_conf_file_path.relative): - Logger.error('Target conf file {} exists but it is not a regular file'.format( - target_conf_file_path.relative)) - return 1 - - if open(target_conf_file_path.relative).read() == '': - return 0 - - os.remove(target_conf_file_path.relative) - open(target_conf_file_path.relative, 'a').close() + return self.unset_target(target_conf_file_path) else: - with open(target_conf_file_path.relative, 'w') as fd: + with open(target_conf_file_path, 'w') as fd: target_data_item = self.settings.path_factory.existing_data_item(target) fd.write(target_data_item.data.dvc) @@ -60,6 +45,22 @@ class CmdTarget(CmdBase): msg = 'DVC target unset' return self.commit_if_needed(msg) + @staticmethod + def unset_target(target_conf_file_path): + if not os.path.exists(target_conf_file_path): + Logger.error('Target conf file {} does not exists'.format( + target_conf_file_path)) + return 1 + if not os.path.isfile(target_conf_file_path): + Logger.error('Target conf file {} exists but it is not a regular file'.format( + target_conf_file_path)) + return 1 + if open(target_conf_file_path).read() == '': + return 0 + os.remove(target_conf_file_path) + open(target_conf_file_path, 'a').close() + return 0 + if __name__ == '__main__': Runtime.run(CmdTarget, False)