add command: refactor

This commit is contained in:
pared
2019-02-27 13:48:40 +01:00
parent 2214f6b65d
commit df1201a508
+46 -26
View File
@@ -1,45 +1,65 @@
import os
from dvc.stage import Stage
def add(self, fname, recursive=False, no_commit=False):
from dvc.stage import Stage
def add(repo, fname, recursive=False, no_commit=False):
fnames = _collect_filenames_to_add(repo, fname, recursive)
fnames = []
stages = _create_stagefiles(repo, fnames, no_commit)
repo.check_dag(repo.stages() + stages)
for stage in stages:
stage.dump()
repo.remind_to_git_add()
return stages
def _collect_filenames_to_add(repo, fname, recursive):
if recursive and os.path.isdir(fname):
for root, dirs, files in os.walk(str(fname)):
for f in files:
path = os.path.join(root, f)
if Stage.is_stage_file(path):
continue
if os.path.basename(path) == self.scm.ignore_file:
continue
if self.scm.is_tracked(path):
continue
fnames.append(path)
fnames = _collect_valid_filenames_from_directory(repo, fname)
else:
fnames = [fname]
return fnames
def _collect_valid_filenames_from_directory(repo, fname):
fnames = []
for file_path in _file_paths(fname):
if _is_valid_file_to_add(file_path, repo):
fnames.append(file_path)
return fnames
def _file_paths(directory):
for root, _, files in os.walk(directory):
for f in files:
yield os.path.join(root, f)
def _is_valid_file_to_add(file_path, repo):
if Stage.is_stage_file(file_path):
return False
if os.path.basename(file_path) == repo.scm.ignore_file:
return False
if repo.scm.is_tracked(file_path):
return False
return True
def _create_stagefiles(repo, fnames, no_commit):
stages = []
self.files_to_git_add = []
with self.state:
with repo.state:
for f in fnames:
stage = Stage.create(repo=self, outs=[f], add=True)
stage = Stage.create(repo=repo, outs=[f], add=True)
if stage is None:
stages.append(stage)
continue
stage.save()
if not no_commit:
stage.commit()
stages.append(stage)
self.check_dag(self.stages() + stages)
for stage in stages:
if stage is not None:
stage.dump()
self.remind_to_git_add()
return stages