Skip to content

Commit e04b1ce

Browse files
authoredJul 5, 2024··
feat(telemetry): Remove consent confirmation prompt for kedro-telemetry (#744)
* Removed consent confirmation Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * Updated tests Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * Fixed ruff Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * Fixed docstring Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * Removed debug output Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * Dummy commit Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * Dummy commit Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * Updated huggingface-hub version Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * Updated huggingface-hub version Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> * CI fix Signed-off-by: Elena Khaustova <ymax70rus@gmail.com> --------- Signed-off-by: Elena Khaustova <ymax70rus@gmail.com>
1 parent 4a2b8e5 commit e04b1ce

File tree

2 files changed

+13
-80
lines changed

2 files changed

+13
-80
lines changed
 

‎kedro-telemetry/kedro_telemetry/plugin.py

+10-38
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from pathlib import Path
1414
from typing import Any
1515

16-
import click
1716
import requests
1817
import toml
1918
import yaml
@@ -347,14 +346,17 @@ def _send_heap_event(
347346

348347

349348
def _check_for_telemetry_consent(project_path: Path) -> bool:
349+
"""
350+
Use telemetry consent from ".telemetry" file if it exists and has a valid format.
351+
Telemetry is considered as opt-in otherwise.
352+
"""
350353
telemetry_file_path = project_path / ".telemetry"
351-
if not telemetry_file_path.exists():
352-
return _confirm_consent(telemetry_file_path)
353-
with open(telemetry_file_path, encoding="utf-8") as telemetry_file:
354-
telemetry = yaml.safe_load(telemetry_file)
355-
if _is_valid_syntax(telemetry):
356-
return telemetry["consent"]
357-
return _confirm_consent(telemetry_file_path)
354+
if telemetry_file_path.exists():
355+
with open(telemetry_file_path, encoding="utf-8") as telemetry_file:
356+
telemetry = yaml.safe_load(telemetry_file)
357+
if _is_valid_syntax(telemetry):
358+
return telemetry["consent"]
359+
return True
358360

359361

360362
def _is_valid_syntax(telemetry: Any) -> bool:
@@ -363,35 +365,5 @@ def _is_valid_syntax(telemetry: Any) -> bool:
363365
)
364366

365367

366-
def _confirm_consent(telemetry_file_path: Path) -> bool:
367-
try:
368-
with telemetry_file_path.open("w") as telemetry_file:
369-
confirm_msg = (
370-
"As an open-source project, we collect usage analytics. \n"
371-
"We cannot see nor store information contained in "
372-
"a Kedro project. \nYou can find out more by reading our "
373-
"privacy notice: \n"
374-
"https://github.com/kedro-org/kedro-plugins/tree/main/kedro-telemetry#"
375-
"privacy-notice \n"
376-
"Do you opt into usage analytics? "
377-
)
378-
if click.confirm(confirm_msg):
379-
yaml.dump({"consent": True}, telemetry_file)
380-
click.secho("You have opted into product usage analytics.", fg="green")
381-
return True
382-
click.secho(
383-
"You have opted out of product usage analytics, so none will be collected.",
384-
fg="green",
385-
)
386-
yaml.dump({"consent": False}, telemetry_file)
387-
return False
388-
except Exception as exc:
389-
logger.warning(
390-
"Failed to confirm consent. No data was sent to Heap. Exception: %s",
391-
exc,
392-
)
393-
return False
394-
395-
396368
cli_hooks = KedroTelemetryCLIHooks()
397369
project_hooks = KedroTelemetryProjectHooks()

‎kedro-telemetry/tests/test_plugin.py

+3-42
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
KedroTelemetryCLIHooks,
1818
KedroTelemetryProjectHooks,
1919
_check_for_telemetry_consent,
20-
_confirm_consent,
2120
_is_known_ci_env,
2221
)
2322

@@ -371,8 +370,6 @@ def test_check_for_telemetry_consent_given(self, mocker, fake_metadata):
371370
with open(telemetry_file_path, "w", encoding="utf-8") as telemetry_file:
372371
yaml.dump({"consent": True}, telemetry_file)
373372

374-
mock_create_file = mocker.patch("kedro_telemetry.plugin._confirm_consent")
375-
mock_create_file.assert_not_called()
376373
assert _check_for_telemetry_consent(fake_metadata.project_path)
377374

378375
def test_check_for_telemetry_consent_not_given(self, mocker, fake_metadata):
@@ -381,29 +378,16 @@ def test_check_for_telemetry_consent_not_given(self, mocker, fake_metadata):
381378
with open(telemetry_file_path, "w", encoding="utf-8") as telemetry_file:
382379
yaml.dump({"consent": False}, telemetry_file)
383380

384-
mock_create_file = mocker.patch("kedro_telemetry.plugin._confirm_consent")
385-
mock_create_file.assert_not_called()
386381
assert not _check_for_telemetry_consent(fake_metadata.project_path)
387382

388383
def test_check_for_telemetry_consent_empty_file(self, mocker, fake_metadata):
389384
Path(fake_metadata.project_path, "conf").mkdir(parents=True)
390385
telemetry_file_path = fake_metadata.project_path / ".telemetry"
391-
mock_create_file = mocker.patch(
392-
"kedro_telemetry.plugin._confirm_consent", return_value=True
393-
)
394-
395-
assert _check_for_telemetry_consent(fake_metadata.project_path)
396-
mock_create_file.assert_called_once_with(telemetry_file_path)
397386

398-
def test_check_for_telemetry_no_consent_empty_file(self, mocker, fake_metadata):
399-
Path(fake_metadata.project_path, "conf").mkdir(parents=True)
400-
telemetry_file_path = fake_metadata.project_path / ".telemetry"
401-
mock_create_file = mocker.patch(
402-
"kedro_telemetry.plugin._confirm_consent", return_value=False
403-
)
387+
with open(telemetry_file_path, "w", encoding="utf-8") as telemetry_file:
388+
yaml.dump({}, telemetry_file)
404389

405-
assert not _check_for_telemetry_consent(fake_metadata.project_path)
406-
mock_create_file.assert_called_once_with(telemetry_file_path)
390+
assert _check_for_telemetry_consent(fake_metadata.project_path)
407391

408392
def test_check_for_telemetry_consent_file_no_consent_field(
409393
self, mocker, fake_metadata
@@ -413,37 +397,14 @@ def test_check_for_telemetry_consent_file_no_consent_field(
413397
with open(telemetry_file_path, "w", encoding="utf8") as telemetry_file:
414398
yaml.dump({"nonsense": "bla"}, telemetry_file)
415399

416-
mock_create_file = mocker.patch(
417-
"kedro_telemetry.plugin._confirm_consent", return_value=True
418-
)
419-
420400
assert _check_for_telemetry_consent(fake_metadata.project_path)
421-
mock_create_file.assert_called_once_with(telemetry_file_path)
422401

423402
def test_check_for_telemetry_consent_file_invalid_yaml(self, mocker, fake_metadata):
424403
Path(fake_metadata.project_path, "conf").mkdir(parents=True)
425404
telemetry_file_path = fake_metadata.project_path / ".telemetry"
426405
telemetry_file_path.write_text("invalid_ yaml")
427406

428-
mock_create_file = mocker.patch(
429-
"kedro_telemetry.plugin._confirm_consent", return_value=True
430-
)
431-
432407
assert _check_for_telemetry_consent(fake_metadata.project_path)
433-
mock_create_file.assert_called_once_with(telemetry_file_path)
434-
435-
def test_confirm_consent_yaml_dump_error(self, mocker, fake_metadata, caplog):
436-
Path(fake_metadata.project_path, "conf").mkdir(parents=True)
437-
telemetry_file_path = fake_metadata.project_path / ".telemetry"
438-
mocker.patch("yaml.dump", side_efyfect=Exception)
439-
440-
assert not _confirm_consent(telemetry_file_path)
441-
442-
msg = (
443-
"Failed to confirm consent. No data was sent to Heap. Exception: "
444-
"pytest: reading from stdin while output is captured! Consider using `-s`."
445-
)
446-
assert msg in caplog.messages[-1]
447408

448409
@mark.parametrize(
449410
"env_vars,result",

0 commit comments

Comments
 (0)
Please sign in to comment.