Refactoring

This commit is contained in:
Dmitry Petrov
2017-07-25 03:27:49 -07:00
parent 8a02c12042
commit 4bb569c16d
3 changed files with 35 additions and 33 deletions
+4
View File
@@ -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()
+1 -4
View File
@@ -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
+30 -29
View File
@@ -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)