diff --git a/dvc/project.py b/dvc/project.py index 74403171a..95399e1d5 100644 --- a/dvc/project.py +++ b/dvc/project.py @@ -53,7 +53,7 @@ class Project(object): self.cloud = DataCloud(self, config=self.config.config) self.updater = Updater(self.dvc_dir) - self._files_to_git_add = [] + self.files_to_git_add = [] self._ignore() @@ -84,14 +84,14 @@ class Project(object): return os.path.join(root_dir, Project.DVC_DIR) def _remind_to_git_add(self): - if not self._files_to_git_add: + if not self.files_to_git_add: return logger.info('\n' 'To track the changes with git run:\n' '\n' '\tgit add {files}' - .format(files=' '.join(self._files_to_git_add))) + .format(files=' '.join(self.files_to_git_add))) @staticmethod def init(root_dir=os.curdir, no_scm=False, force=False): @@ -143,8 +143,8 @@ class Project(object): scm.add([config.config_file]) - if scm.ignore_file(): - scm.add([os.path.join(dvc_dir, scm.ignore_file())]) + if scm.ignore_file: + scm.add([os.path.join(dvc_dir, scm.ignore_file)]) logger.info('\nYou can now commit the changes to git.\n') proj._welcome_message() @@ -197,7 +197,7 @@ class Project(object): path = os.path.join(root, f) if Stage.is_stage_file(path): continue - if os.path.basename(path) == self.scm.ignore_file(): + if os.path.basename(path) == self.scm.ignore_file: continue if self.scm.is_tracked(path): continue @@ -206,7 +206,7 @@ class Project(object): fnames = [fname] stages = [] - self._files_to_git_add = [] + self.files_to_git_add = [] with self.state: for f in fnames: stage = Stage.create(project=self, @@ -386,7 +386,7 @@ class Project(object): self._check_dag(self.stages() + [stage]) - self._files_to_git_add = [] + self.files_to_git_add = [] with self.state: if not no_exec: stage.run() @@ -410,7 +410,7 @@ class Project(object): self._check_dag(self.stages() + [stage]) - self._files_to_git_add = [] + self.files_to_git_add = [] with self.state: stage.run() @@ -474,7 +474,7 @@ class Project(object): else: targets.append(target) - self._files_to_git_add = [] + self.files_to_git_add = [] ret = [] with self.state: diff --git a/dvc/scm/__init__.py b/dvc/scm/__init__.py new file mode 100644 index 000000000..14b66c44a --- /dev/null +++ b/dvc/scm/__init__.py @@ -0,0 +1,22 @@ +"""Manages source control systems(e.g. Git).""" + +from dvc.scm.base import Base +from dvc.scm.git import Git + + +def SCM(root_dir, project=None): # pylint: disable=invalid-name + """Returns SCM instance that corresponds to a project at the specified + path. + + Args: + root_dir (str): path to a root directory of the project. + project (dvc.project.Project): dvc project instance that root_dir + belongs to. + + Returns: + dvc.scm.base.Base: SCM instance. + """ + if Git.is_repo(root_dir) or Git.is_submodule(root_dir): + return Git(root_dir, project=project) + + return Base(root_dir, project=project) diff --git a/dvc/scm/base.py b/dvc/scm/base.py new file mode 100644 index 000000000..d00a11e5e --- /dev/null +++ b/dvc/scm/base.py @@ -0,0 +1,142 @@ +"""Manages source control systems(e.g. Git) in dvc.""" + +import os + +from dvc.exceptions import DvcException + + +class SCMError(DvcException): + """Base class for source control management errors.""" + + +class FileNotInRepoError(DvcException): + """Thrown when trying to find .gitignore for a file that is not in a scm + repository. + """ + + +class Base(object): + """Base class for source control management driver implementations.""" + def __init__(self, root_dir=os.curdir, project=None): + self.project = project + self.root_dir = root_dir + + def __repr__(self): + return "{class_name}: '{directory}'".format( + class_name=type(self).__name__, + directory=self.dir + ) + + @property + def dir(self): + """Path to a directory with SCM specific information.""" + return None + + @staticmethod + def is_repo(root_dir): # pylint: disable=unused-argument + """Returns whether or not root_dir is a valid SCM repository.""" + return True + + @staticmethod + def is_submodule(root_dir): # pylint: disable=unused-argument + """Returns whether or not root_dir is a valid SCM repository + submodule. + """ + return True + + def ignore(self, path): # pylint: disable=unused-argument + """Makes SCM ignore a specified path.""" + + def ignore_remove(self, path): # pylint: disable=unused-argument + """Makes SCM stop ignoring a specified path.""" + + @property + def ignore_file(self): + """Filename for a file that contains ignored paths for this SCM.""" + + def ignore_list(self, p_list): + """Makes SCM ignore all paths specified in a list.""" + return [self.ignore(path) for path in p_list] + + def add(self, paths): + """Makes SCM start tracking every path from a specified list of paths. + """ + + def commit(self, msg): + """Makes SCM create a commit.""" + + def checkout(self, branch, create_new=False): + """Makes SCM checkout a branch.""" + + def branch(self, branch): + """Makes SCM create a branch with a specified name.""" + + def tag(self, tag): + """Makes SCM create a tag with a specified name.""" + + def brancher(self, + branches=None, + all_branches=False, + tags=None, + all_tags=False): + """Generator that iterates over specified revisions. + + Args: + branches (list): a list of branches to iterate over. + all_branches (bool): iterate over all available branches. + tags (list): a list of tags to iterate over. + all_tags (bool): iterate over all available tags. + + Yields: + str: the current revision. + """ + if not branches and not all_branches \ + and not tags and not all_tags: + yield '' + return + + saved = self.active_branch() + revs = [] + + if all_branches: + branches = self.list_branches() + + if all_tags: + tags = self.list_tags() + + if branches is None: + revs.extend([saved]) + else: + revs.extend(branches) + + if tags is not None: + revs.extend(tags) + + for rev in revs: + self.checkout(rev) + yield rev + + self.checkout(saved) + + def untracked_files(self): # pylint: disable=no-self-use + """Returns a list of untracked files.""" + return [] + + def is_tracked(self, path): # pylint: disable=no-self-use, unused-argument + """Returns whether or not a specified path is tracked.""" + return False + + def active_branch(self): # pylint: disable=no-self-use + """Returns current branch in the project.""" + return '' + + def list_branches(self): # pylint: disable=no-self-use + """Returns a list of available branches in the project.""" + return [] + + def list_tags(self): # pylint: disable=no-self-use + """Returns a list of available tags in the project.""" + return [] + + def install(self): + """Adds dvc commands to SCM hooks for the project.""" diff --git a/dvc/scm.py b/dvc/scm/git.py similarity index 60% rename from dvc/scm.py rename to dvc/scm/git.py index b903d3905..63bb35137 100644 --- a/dvc/scm.py +++ b/dvc/scm/git.py @@ -1,121 +1,18 @@ +"""Manages Git.""" + +# NOTE: need this one because python 2 would re-import this module +# (called git.py) instead of importing gitpython's git module. +from __future__ import absolute_import + import os import dvc.logger as logger -from dvc.exceptions import DvcException from dvc.utils import fix_env - - -class SCMError(DvcException): - pass - - -class FileNotInRepoError(DvcException): - pass - - -class Base(object): - def __init__(self, root_dir=os.curdir, project=None): - self.project = project - self.root_dir = root_dir - - def __repr__(self): - return "{class_name}: '{directory}'".format( - class_name=type(self).__name__, - directory=self.dir - ) - - @property - def dir(self): - return None - - @staticmethod - def is_repo(root_dir): - return True - - @staticmethod - def is_submodule(root_dir): - return True - - def ignore(self, path): - pass - - def ignore_remove(self, path): - pass - - def ignore_file(self): - pass - - def ignore_list(self, p_list): - return [self.ignore(path) for path in p_list] - - def add(self, paths): - pass - - def commit(self, msg): - pass - - def checkout(self, branch): - pass - - def branch(self, branch): - pass - - def tag(self, tag): - pass - - def brancher(self, - branches=None, - all_branches=False, - tags=None, - all_tags=False): - if not branches and not all_branches \ - and not tags and not all_tags: - yield '' - return - - saved = self.active_branch() - revs = [] - - if all_branches: - branches = self.list_branches() - - if all_tags: - tags = self.list_tags() - - if branches is None: - revs.extend([saved]) - else: - revs.extend(branches) - - if tags is not None: - revs.extend(tags) - - for rev in revs: - self.checkout(rev) - yield rev - - self.checkout(saved) - - def untracked_files(self): - pass - - def is_tracked(self, path): - pass - - def active_branch(self): - pass - - def list_branches(self): - pass - - def list_tags(self): - pass - - def install(self): - pass +from dvc.scm.base import Base, SCMError, FileNotInRepoError class Git(Base): + """Class for managing Git.""" GITIGNORE = '.gitignore' GIT_DIR = '.git' @@ -133,8 +30,8 @@ class Git(Base): # NOTE: fixing LD_LIBRARY_PATH for binary built by PyInstaller. # http://pyinstaller.readthedocs.io/en/stable/runtime-information.html env = fix_env(None) - lp = env.get('LD_LIBRARY_PATH', None) - self.repo.git.update_environment(LD_LIBRARY_PATH=lp) + libpath = env.get('LD_LIBRARY_PATH', None) + self.repo.git.update_environment(LD_LIBRARY_PATH=libpath) @staticmethod def is_repo(root_dir): @@ -152,6 +49,7 @@ class Git(Base): def dir(self): return self.repo.git_dir + @property def ignore_file(self): return self.GITIGNORE @@ -174,7 +72,7 @@ class Git(Base): ignore_list = open(gitignore, 'r').readlines() filtered = list(filter(lambda x: x.strip() == entry.strip(), ignore_list)) - if len(filtered) != 0: + if filtered: return msg = "Adding '{}' to '{}'.".format(os.path.relpath(path), @@ -182,14 +80,14 @@ class Git(Base): logger.info(msg) content = entry - if len(ignore_list) > 0: + if ignore_list: content = '\n' + content - with open(gitignore, 'a') as fd: - fd.write(content) + with open(gitignore, 'a') as fobj: + fobj.write(content) if self.project is not None: - self.project._files_to_git_add.append(os.path.relpath(gitignore)) + self.project.files_to_git_add.append(os.path.relpath(gitignore)) def ignore_remove(self, path): entry, gitignore = self._get_gitignore(path) @@ -197,16 +95,16 @@ class Git(Base): if not os.path.exists(gitignore): return - with open(gitignore, 'r') as fd: - lines = fd.readlines() + with open(gitignore, 'r') as fobj: + lines = fobj.readlines() filtered = list(filter(lambda x: x.strip() != entry.strip(), lines)) - with open(gitignore, 'w') as fd: - fd.writelines(filtered) + with open(gitignore, 'w') as fobj: + fobj.writelines(filtered) if self.project is not None: - self.project._files_to_git_add.append(os.path.relpath(gitignore)) + self.project.files_to_git_add.append(os.path.relpath(gitignore)) def add(self, paths): # NOTE: GitPython is not currently able to handle index version >= 3. @@ -263,17 +161,10 @@ class Git(Base): if os.path.isfile(hook): msg = "git hook '{}' already exists." raise SCMError(msg.format(os.path.relpath(hook))) - with open(hook, 'w+') as fd: - fd.write('#!/bin/sh\nexec dvc {}\n'.format(cmd)) + with open(hook, 'w+') as fobj: + fobj.write('#!/bin/sh\nexec dvc {}\n'.format(cmd)) os.chmod(hook, 0o777) def install(self): self._install_hook('post-checkout', 'checkout') self._install_hook('pre-commit', 'status') - - -def SCM(root_dir, no_scm=False, project=None): - if Git.is_repo(root_dir) or Git.is_submodule(root_dir): - return Git(root_dir, project=project) - - return Base(root_dir, project=project) diff --git a/dvc/stage.py b/dvc/stage.py index b7e98a554..95060c6b6 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -458,7 +458,7 @@ class Stage(object): with open(fname, 'w') as fd: yaml.safe_dump(self.dumpd(), fd, default_flow_style=False) - self.project._files_to_git_add.append(os.path.relpath(fname)) + self.project.files_to_git_add.append(os.path.relpath(fname)) def _compute_md5(self): from dvc.output.local import OutputLOCAL