Repository: iterative/dvc

Issue:
`dvc init` prints analytics warning even if has been disabled
**When** a new repository is initialized and the `core.analytics` has been set to `False` either globally or system-wide
**Then** a the analytics warning message is still printed
**Causing** a bit of irritation and actually checking the code what's really going on 

Seems like the `_welcome_message` function in `dvc/repo/init.py` has no check if analytics is still enabled.

---
**DVC version**: 0.51.2
**Method of installation**: `pip`



Discussion:

Patch:
diff --git a/dvc/analytics.py b/dvc/analytics.py
index d9805bfd1..6643db768 100644
--- a/dvc/analytics.py
+++ b/dvc/analytics.py
@@ -213,10 +213,20 @@ class Analytics(object):
         return core.get(Config.SECTION_CORE_ANALYTICS, True)
 
     @staticmethod
-    def _is_enabled(cmd=None):
+    def _get_current_config():
         from dvc.config import Config
         from dvc.repo import Repo
         from dvc.exceptions import NotDvcRepoError
+
+        try:
+            dvc_dir = Repo.find_dvc_dir()
+            config = Config(dvc_dir)
+        except NotDvcRepoError:
+            config = Config(validate=False)
+        return config
+
+    @staticmethod
+    def is_enabled(cmd=None):
         from dvc.command.daemon import CmdDaemonBase
 
         if os.getenv("DVC_TEST"):
@@ -225,17 +235,13 @@ class Analytics(object):
         if isinstance(cmd, CmdDaemonBase):
             return False
 
-        if cmd is None or not hasattr(cmd, "config"):
-            try:
-                dvc_dir = Repo.find_dvc_dir()
-                config = Config(dvc_dir)
-                assert config is not None
-            except NotDvcRepoError:
-                config = Config(validate=False)
-                assert config is not None
-        else:
-            config = cmd.config
-            assert config is not None
+        config = (
+            Analytics._get_current_config()
+            if cmd is None or not hasattr(cmd, "config")
+            else cmd.config
+        )
+
+        assert config is not None
 
         enabled = Analytics._is_enabled_config(config)
         logger.debug(
@@ -253,7 +259,7 @@ class Analytics(object):
         """
         from dvc.daemon import daemon
 
-        if not Analytics._is_enabled(cmd):
+        if not Analytics.is_enabled(cmd):
             return
 
         analytics = Analytics()
@@ -264,7 +270,7 @@ class Analytics(object):
         """Collect and send analytics."""
         import requests
 
-        if not self._is_enabled():
+        if not self.is_enabled():
             return
 
         self.collect()
diff --git a/dvc/output/base.py b/dvc/output/base.py
index 65439e83b..fbbac18b9 100644
--- a/dvc/output/base.py
+++ b/dvc/output/base.py
@@ -123,6 +123,13 @@ class OutputBase(object):
     def is_in_repo(self):
         return False
 
+    @property
+    def use_scm_ignore(self):
+        if not self.is_in_repo:
+            return False
+
+        return self.use_cache or self.stage.is_repo_import
+
     @property
     def cache(self):
         return getattr(self.repo.cache, self.scheme)
@@ -210,6 +217,12 @@ class OutputBase(object):
         if self.is_empty:
             logger.warning("'{}' is empty.".format(self))
 
+        if self.use_scm_ignore:
+            if self.repo.scm.is_tracked(self.fspath):
+                raise OutputAlreadyTrackedError(self)
+
+            self.repo.scm.ignore(self.fspath)
+
         if not self.use_cache:
             self.info = self.remote.save_info(self.path_info)
             if self.metric:
@@ -230,13 +243,6 @@ class OutputBase(object):
             )
             return
 
-        if self.is_in_repo:
-            if self.repo.scm.is_tracked(self.fspath):
-                raise OutputAlreadyTrackedError(self)
-
-            if self.use_cache:
-                self.repo.scm.ignore(self.fspath)
-
         self.info = self.remote.save_info(self.path_info)
 
     def commit(self):
@@ -296,11 +302,11 @@ class OutputBase(object):
         if self.scheme != "local":
             return
 
-        if ignore_remove and self.use_cache and self.is_in_repo:
+        if ignore_remove and self.use_scm_ignore:
             self.repo.scm.ignore_remove(self.fspath)
 
     def move(self, out):
-        if self.scheme == "local" and self.use_cache and self.is_in_repo:
+        if self.scheme == "local" and self.use_scm_ignore:
             self.repo.scm.ignore_remove(self.fspath)
 
         self.remote.move(self.path_info, out.path_info)
@@ -309,7 +315,7 @@ class OutputBase(object):
         self.save()
         self.commit()
 
-        if self.scheme == "local" and self.use_cache and self.is_in_repo:
+        if self.scheme == "local" and self.use_scm_ignore:
             self.repo.scm.ignore(self.fspath)
 
     def get_files_number(self):
diff --git a/dvc/repo/init.py b/dvc/repo/init.py
index 6fefbad8f..19dc20d1b 100644
--- a/dvc/repo/init.py
+++ b/dvc/repo/init.py
@@ -7,21 +7,23 @@ from dvc.scm import SCM, NoSCM
 from dvc.config import Config
 from dvc.exceptions import InitError
 from dvc.utils import boxify, relpath, remove
+from dvc.analytics import Analytics
 
 logger = logging.getLogger(__name__)
 
 
 def _welcome_message():
-    logger.info(
-        boxify(
-            "DVC has enabled anonymous aggregate usage analytics.\n"
-            "Read the analytics documentation (and how to opt-out) here:\n"
-            "{blue}https://dvc.org/doc/user-guide/analytics{nc}".format(
-                blue=colorama.Fore.BLUE, nc=colorama.Fore.RESET
-            ),
-            border_color="red",
+    if Analytics.is_enabled():
+        logger.info(
+            boxify(
+                "DVC has enabled anonymous aggregate usage analytics.\n"
+                "Read the analytics documentation (and how to opt-out) here:\n"
+                "{blue}https://dvc.org/doc/user-guide/analytics{nc}".format(
+                    blue=colorama.Fore.BLUE, nc=colorama.Fore.RESET
+                ),
+                border_color="red",
+            )
         )
-    )
 
     msg = (
         "{yellow}What's next?{nc}\n"


Test patch:
diff --git a/tests/func/test_import.py b/tests/func/test_import.py
index b91dccfc4..b097f9845 100644
--- a/tests/func/test_import.py
+++ b/tests/func/test_import.py
@@ -4,7 +4,7 @@ import filecmp
 from tests.utils import trees_equal
 
 
-def test_import(repo_dir, dvc_repo, erepo):
+def test_import(repo_dir, git, dvc_repo, erepo):
     src = erepo.FOO
     dst = erepo.FOO + "_imported"
 
@@ -13,9 +13,10 @@ def test_import(repo_dir, dvc_repo, erepo):
     assert os.path.exists(dst)
     assert os.path.isfile(dst)
     assert filecmp.cmp(repo_dir.FOO, dst, shallow=False)
+    assert git.git.check_ignore(dst)
 
 
-def test_import_dir(repo_dir, dvc_repo, erepo):
+def test_import_dir(repo_dir, git, dvc_repo, erepo):
     src = erepo.DATA_DIR
     dst = erepo.DATA_DIR + "_imported"
 
@@ -24,9 +25,10 @@ def test_import_dir(repo_dir, dvc_repo, erepo):
     assert os.path.exists(dst)
     assert os.path.isdir(dst)
     trees_equal(src, dst)
+    assert git.git.check_ignore(dst)
 
 
-def test_import_rev(repo_dir, dvc_repo, erepo):
+def test_import_rev(repo_dir, git, dvc_repo, erepo):
     src = "version"
     dst = src
 
@@ -36,3 +38,4 @@ def test_import_rev(repo_dir, dvc_repo, erepo):
     assert os.path.isfile(dst)
     with open(dst, "r+") as fobj:
         assert fobj.read() == "branch"
+    assert git.git.check_ignore(dst)