def test_store_then_upload(self, tdir): """Collects statistics while reporting is disabled: save to disk.""" with capture_stderr() as lines: stats = usagestats.Stats(tdir, optin_prompt, 'http://127.0.0.1:8000/', unique_user_id=True, version='1.0') stats.note({'mode': 'compatibility'}) stats.submit([('what', 'Ran the program'), ('mode', 'nope')], usagestats.PYTHON_VERSION) self.assertEqual(lines, [ b"", b"Uploading usage statistics is currently disabled", b"Please help us by providing anonymous usage statistics; " b"you can enable this", b"by running:", b" cool_program --enable-stats", b"If you do not want to see this message again, you can run:", b" cool_program --disable-stats", b"Nothing will be uploaded before you opt in." ]) self._get_reports(tdir, 0) with capture_stderr() as lines: stats = usagestats.Stats(tdir, optin_prompt, 'http://127.0.0.1:8000/', unique_user_id=True, version='1.0') stats.enable_reporting() stats.note({'mode': 'compatibility'}) stats.submit([('what', 'Ran the program'), ('mode', 'yep')], usagestats.PYTHON_VERSION) self._get_reports(tdir, 2) with capture_stderr() as lines: stats = usagestats.Stats(tdir, optin_prompt, 'http://127.0.0.1:8000/', unique_user_id=True, version='1.0') stats.note({'mode': 'compatibility'}) stats.submit([('what', 'Ran the program'), ('mode', 'again')], usagestats.PYTHON_VERSION) reports = self._get_reports(tdir, 3) for report, mode in zip(reports, [b'nope', b'yep', b'again']): regex_compare(report, [ br'^submitted_from:127.0.0.1$', br'^submitted_date:', br'^date:', br'^user:'******'^version:1\.0$', br'^mode:compatibility$', br'^what:Ran the program$', br'^mode:' + mode + br'$', br'^python:' ], self.fail)
def setup_usage_report(name, version): """Sets up the usagestats module. """ global _usage_report # Unpack CA certificate fd, certificate_file = Path.tempfile(prefix='rpz_stats_ca_', suffix='.pem') with certificate_file.open('wb') as fp: fp.write(usage_report_ca) os.close(fd) atexit.register(os.remove, certificate_file.path) _usage_report = usagestats.Stats( '~/.reprozip/usage_stats', usagestats.Prompt(enable='%s usage_report --enable' % name, disable='%s usage_report --disable' % name), os.environ.get('REPROZIP_USAGE_URL', 'https://reprozip-stats.poly.edu/'), version='%s %s' % (name, version), unique_user_id=True, env_var='REPROZIP_USAGE_STATS', ssl_verify=certificate_file.path) try: os.getcwd().encode('ascii') except (UnicodeEncodeError, UnicodeDecodeError): record_usage(cwd_ascii=False) else: record_usage(cwd_ascii=True)
def setup_usage_report(): """Sets up the usagestats module. """ global usage_report certificate_file = get_ca_certificate() usage_report = usagestats.Stats( '~/.vistrails/usage_stats', usagestats.Prompt( "\nUploading usage statistics is currently disabled\n" "Please help us by providing anonymous usage statistics; " "you can enable this\neither from the GUI or by using " "--enable-usage-stats\n" "If you do not want to see this message again, you can disable " "it from the GUI or with --disable-usage-stats\n" "Nothing will be uploaded before you opt in.\n"), 'https://reprozip-stats.poly.edu/', version='VisTrails %s' % vistrails_version(), unique_user_id=True, env_var='VISTRAILS_USAGE_STATS', ssl_verify=certificate_file) cwd = os.getcwd() record_usage(cwd_spaces=b' ' in cwd) try: cwd.decode('ascii') except UnicodeDecodeError: record_usage(cwd_ascii=False) else: record_usage(cwd_ascii=True)
def setup_usage_report(name, version): """Sets up the usagestats module. """ global _usage_report certificate_file = get_reprozip_ca_certificate() _usage_report = usagestats.Stats( '~/.reprozip/usage_stats', usagestats.Prompt(enable='%s usage_report --enable' % name, disable='%s usage_report --disable' % name), os.environ.get('REPROZIP_USAGE_URL', 'https://stats.reprozip.org/'), version='%s %s' % (name, version), unique_user_id=True, env_var='REPROZIP_USAGE_STATS', ssl_verify=certificate_file.path) try: os.getcwd().encode('ascii') except (UnicodeEncodeError, UnicodeDecodeError): record_usage(cwd_ascii=False) else: record_usage(cwd_ascii=True)
def test_upload_one(self, tdir): """Uploads statistics.""" with capture_stderr() as lines: stats = usagestats.Stats(tdir, optin_prompt, 'http://127.0.0.1:8000/', unique_user_id=True, version='1.0') stats.enable_reporting() stats.note({'mode': 'compatibility'}) stats.submit([('what', 'Ran the program'), ('mode', 'yep')], usagestats.PYTHON_VERSION) self.assertEqual(lines, []) reports = self._get_reports(tdir, 1) self.assertEqual(len(reports), 1) report, = reports regex_compare(report, [ br'^submitted_from:127.0.0.1$', br'^submitted_date:', br'^date:', br'^user:'******'^version:1\.0$', br'^mode:compatibility$', br'^what:Ran the program$', br'^mode:yep$', br'^python:' ], self.fail)
# See file LICENSE for full license details. import os from reprounzip.common import get_reprozip_ca_certificate import usagestats from reprounzip_qt import __version__ as version _certificate_file = get_reprozip_ca_certificate() _usage_report = usagestats.Stats( '~/.reprozip/usage_stats', usagestats.Prompt(''), os.environ.get('REPROZIP_USAGE_URL', 'https://stats.reprozip.org/'), version='%s %s' % ('reprounzip-qt', version), unique_user_id=True, env_var='REPROZIP_USAGE_STATS', ssl_verify=_certificate_file.path ) def record_usage(**kwargs): """Records some info in the current usage report. """ if _usage_report is not None: _usage_report.note(kwargs) def submit_usage_report(**kwargs): """Submits the current usage report to the usagestats server.