def collect_and_send_report(args=None, return_code=None): """ Collect information from the runtime/environment and the command being executed into a report and send it over the network. To prevent analytics from blocking the execution of the main thread, sending the report is done in a separate process. The inter-process communication happens through a file containing the report as a JSON, where the _collector_ generates it and the _sender_ removes it after sending it. """ import tempfile from dvc.daemon import daemon report = {} # Include command execution information on the report only when available. if args and hasattr(args, "func"): report.update({"cmd_class": args.func.__name__}) if return_code is not None: report.update({"cmd_return_code": return_code}) with tempfile.NamedTemporaryFile(delete=False, mode="w") as fobj: json.dump(report, fobj) daemon(["analytics", fobj.name])
def fetch(self, detach=True): from dvc.daemon import daemon if detach: daemon(["updater"]) return self._with_lock(self._get_latest_version, "fetching")
def send_cmd(cmd, args, ret): """Collect and send analytics for CLI command. Args: args (list): parsed args for the CLI command. ret (int): return value of the CLI command. """ from dvc.daemon import daemon if not Analytics.is_enabled(cmd): return analytics = Analytics() analytics.collect_cmd(args, ret) daemon(["analytics", analytics.dump()])
def test(self, mock_windows, mock_posix): daemon.daemon(["updater"]) if os.name == "nt": mock_posix.assert_not_called() mock_windows.assert_called() args, kwargs = mock_windows.call_args else: mock_windows.assert_not_called() mock_posix.assert_called() args, kwargs = mock_posix.call_args env = args[1] self.assertTrue("PYTHONPATH" in env.keys()) file_path = os.path.abspath(inspect.stack()[0][1]) file_dir = os.path.dirname(file_path) test_dir = os.path.dirname(file_dir) dvc_dir = os.path.dirname(test_dir) self.assertEqual(env["PYTHONPATH"], dvc_dir)
def test_daemon(mocker): mock_windows = mocker.patch("dvc.daemon._spawn_windows") mock_posix = mocker.patch("dvc.daemon._spawn_posix") daemon.daemon(["updater"]) if os.name == "nt": mock_posix.assert_not_called() mock_windows.assert_called() args = mock_windows.call_args[0] else: mock_windows.assert_not_called() mock_posix.assert_called() args = mock_posix.call_args[0] env = args[1] assert "PYTHONPATH" in env.keys() file_path = os.path.abspath(inspect.stack()[0][1]) file_dir = os.path.dirname(file_path) test_dir = os.path.dirname(file_dir) dvc_dir = os.path.dirname(test_dir) assert env["PYTHONPATH"] == dvc_dir assert env[daemon.DVC_DAEMON] == "1"
def test_no_recursive_spawn(): with mock.patch.dict(os.environ, {daemon.DVC_DAEMON: "1"}): with mock.patch("dvc.daemon._spawn") as mock_spawn: daemon.daemon(["updater"]) mock_spawn.assert_not_called()
def test_no_recursive_spawn(mocker): mocker.patch.dict(os.environ, {daemon.DVC_DAEMON: "1"}) mock_spawn = mocker.patch("dvc.daemon._spawn") daemon.daemon(["updater"]) mock_spawn.assert_not_called()