From 21e0a7d3bdd70a4ef5e9fbf56c358c274ffe043f Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 25 Jul 2017 03:51:05 -0700 Subject: [PATCH] Refactoring 2 and small issues --- dvc/command/lock.py | 2 +- dvc/command/target.py | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/dvc/command/lock.py b/dvc/command/lock.py index a90c41d33..f946d49ab 100644 --- a/dvc/command/lock.py +++ b/dvc/command/lock.py @@ -10,7 +10,7 @@ class CmdLock(CmdBase): def define_args(self, parser): self.set_no_git_actions(parser) - self.set_reset_flag('-u', '--unlock', 'Unlock data item - enable reproduction.') + CmdLock.set_reset_flag(parser, '-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 d57e65509..fdefa93da 100644 --- a/dvc/command/target.py +++ b/dvc/command/target.py @@ -11,7 +11,7 @@ class CmdTarget(CmdBase): def define_args(self, parser): self.set_no_git_actions(parser) - self.set_reset_flag('-u', '--unset', 'Reset target.') + CmdTarget.set_reset_flag(parser, '-u', '--unset', 'Reset target.') parser.add_argument('target', metavar='', nargs='?', help='Target data item.') pass @@ -35,18 +35,17 @@ class CmdTarget(CmdBase): if unset: return self.unset_target(target_conf_file_path) else: + target_dvc_path = self.settings.path_factory.existing_data_item(target).data.dvc + if os.path.exists(target_conf_file_path): + with open(target_conf_file_path) as fd: + if fd.read() == target_dvc_path: + return 0 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) + fd.write(target_dvc_path) - if target: - msg = 'DVC target: {}'.format(target) - else: - msg = 'DVC target unset' - return self.commit_if_needed(msg) + return self.commit_if_needed('DVC target: {}'.format(target)) - @staticmethod - def unset_target(target_conf_file_path): + def unset_target(self, 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)) @@ -57,9 +56,11 @@ class CmdTarget(CmdBase): 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 + + return self.commit_if_needed('DVC target unset') if __name__ == '__main__':