[Python-modules-commits] [python-rpaths] 01/03: New upstream version 0.6

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Tue Apr 18 10:42:49 UTC 2017


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch master
in repository python-rpaths.

commit 16d88d5db79eabaab33f6779fe643a01cfd2d1b9
Author: Ghislain Antony Vaillant <ghisvail at gmail.com>
Date:   Tue Apr 18 10:55:51 2017 +0100

    New upstream version 0.6
---
 CHANGELOG.md                             |  54 +++++
 LICENSE.txt                              | 191 ++++++++++++++++
 MANIFEST.in                              |   7 +
 PKG-INFO                                 | 102 +++++++++
 README.rst                               |  82 +++++++
 setup.cfg                                |   7 +
 setup.py                                 |  34 +++
 tests/__init__.py                        |   0
 tests/__main__.py                        |  33 +++
 tests/test_reporting.py                  | 164 ++++++++++++++
 tests/utils.py                           |  78 +++++++
 usagestats.egg-info/PKG-INFO             | 102 +++++++++
 usagestats.egg-info/SOURCES.txt          |  17 ++
 usagestats.egg-info/dependency_links.txt |   1 +
 usagestats.egg-info/requires.txt         |   1 +
 usagestats.egg-info/top_level.txt        |   1 +
 usagestats.py                            | 378 +++++++++++++++++++++++++++++++
 wsgi/usagestats_server.py                | 101 +++++++++
 18 files changed, 1353 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..415eb56
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,54 @@
+Changelog
+=========
+
+0.6 (2017-04-15)
+----------------
+
+Bugfixes:
+* Check environment variable again before submitting
+* Don't log full report on INFO (make that DEBUG)
+
+0.5 (2016-03-04)
+----------------
+
+Bugfixes:
+* Always use 3 digits for milliseconds in WSGI server (makes sure they are in order)
+
+Features:
+* Introduce `read_config()` and `write_config()` methods for overloading with your app's specific config system
+* `enableable` and `disableable` now can be used to set status of buttons in an interface
+
+0.4 (2015-07-07)
+----------------
+
+Bugfixes:
+* Correctly handle prompt=None
+* Explicit ValueError if reusing already-submitted report
+
+0.3 (2014-12-18)
+----------------
+
+Bugfixes:
+* Don't fail if `PYTHON_USAGE_STATS` is not set
+* Major rewrite of the enabled/disabled statuses
+* Keep reports if we can't connect
+* Reports 'version' argument to Stats
+
+Features:
+* Adds 'ssl_verify' argument, for custom SSL CA
+* Makes submission faster (max 5 reports at a time, 1s timeout)
+
+0.2 (2014-10-28)
+----------------
+
+Bugfixes:
+* Don't submit reports when disabled!
+* Show prompt
+
+Features:
+* Reporting can be disabled by an environment variable (useful for tests)
+
+0.1 (2014-10-28)
+----------------
+
+First version. Client can submit info, WSGI server example.
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..37ec93a
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,191 @@
+Apache License
+Version 2.0, January 2004
+http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+"License" shall mean the terms and conditions for use, reproduction, and
+distribution as defined by Sections 1 through 9 of this document.
+
+"Licensor" shall mean the copyright owner or entity authorized by the copyright
+owner that is granting the License.
+
+"Legal Entity" shall mean the union of the acting entity and all other entities
+that control, are controlled by, or are under common control with that entity.
+For the purposes of this definition, "control" means (i) the power, direct or
+indirect, to cause the direction or management of such entity, whether by
+contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
+outstanding shares, or (iii) beneficial ownership of such entity.
+
+"You" (or "Your") shall mean an individual or Legal Entity exercising
+permissions granted by this License.
+
+"Source" form shall mean the preferred form for making modifications, including
+but not limited to software source code, documentation source, and configuration
+files.
+
+"Object" form shall mean any form resulting from mechanical transformation or
+translation of a Source form, including but not limited to compiled object code,
+generated documentation, and conversions to other media types.
+
+"Work" shall mean the work of authorship, whether in Source or Object form, made
+available under the License, as indicated by a copyright notice that is included
+in or attached to the work (an example is provided in the Appendix below).
+
+"Derivative Works" shall mean any work, whether in Source or Object form, that
+is based on (or derived from) the Work and for which the editorial revisions,
+annotations, elaborations, or other modifications represent, as a whole, an
+original work of authorship. For the purposes of this License, Derivative Works
+shall not include works that remain separable from, or merely link (or bind by
+name) to the interfaces of, the Work and Derivative Works thereof.
+
+"Contribution" shall mean any work of authorship, including the original version
+of the Work and any modifications or additions to that Work or Derivative Works
+thereof, that is intentionally submitted to Licensor for inclusion in the Work
+by the copyright owner or by an individual or Legal Entity authorized to submit
+on behalf of the copyright owner. For the purposes of this definition,
+"submitted" means any form of electronic, verbal, or written communication sent
+to the Licensor or its representatives, including but not limited to
+communication on electronic mailing lists, source code control systems, and
+issue tracking systems that are managed by, or on behalf of, the Licensor for
+the purpose of discussing and improving the Work, but excluding communication
+that is conspicuously marked or otherwise designated in writing by the copyright
+owner as "Not a Contribution."
+
+"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
+of whom a Contribution has been received by Licensor and subsequently
+incorporated within the Work.
+
+2. Grant of Copyright License.
+
+Subject to the terms and conditions of this License, each Contributor hereby
+grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
+irrevocable copyright license to reproduce, prepare Derivative Works of,
+publicly display, publicly perform, sublicense, and distribute the Work and such
+Derivative Works in Source or Object form.
+
+3. Grant of Patent License.
+
+Subject to the terms and conditions of this License, each Contributor hereby
+grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
+irrevocable (except as stated in this section) patent license to make, have
+made, use, offer to sell, sell, import, and otherwise transfer the Work, where
+such license applies only to those patent claims licensable by such Contributor
+that are necessarily infringed by their Contribution(s) alone or by combination
+of their Contribution(s) with the Work to which such Contribution(s) was
+submitted. If You institute patent litigation against any entity (including a
+cross-claim or counterclaim in a lawsuit) alleging that the Work or a
+Contribution incorporated within the Work constitutes direct or contributory
+patent infringement, then any patent licenses granted to You under this License
+for that Work shall terminate as of the date such litigation is filed.
+
+4. Redistribution.
+
+You may reproduce and distribute copies of the Work or Derivative Works thereof
+in any medium, with or without modifications, and in Source or Object form,
+provided that You meet the following conditions:
+
+You must give any other recipients of the Work or Derivative Works a copy of
+this License; and
+You must cause any modified files to carry prominent notices stating that You
+changed the files; and
+You must retain, in the Source form of any Derivative Works that You distribute,
+all copyright, patent, trademark, and attribution notices from the Source form
+of the Work, excluding those notices that do not pertain to any part of the
+Derivative Works; and
+If the Work includes a "NOTICE" text file as part of its distribution, then any
+Derivative Works that You distribute must include a readable copy of the
+attribution notices contained within such NOTICE file, excluding those notices
+that do not pertain to any part of the Derivative Works, in at least one of the
+following places: within a NOTICE text file distributed as part of the
+Derivative Works; within the Source form or documentation, if provided along
+with the Derivative Works; or, within a display generated by the Derivative
+Works, if and wherever such third-party notices normally appear. The contents of
+the NOTICE file are for informational purposes only and do not modify the
+License. You may add Your own attribution notices within Derivative Works that
+You distribute, alongside or as an addendum to the NOTICE text from the Work,
+provided that such additional attribution notices cannot be construed as
+modifying the License.
+You may add Your own copyright statement to Your modifications and may provide
+additional or different license terms and conditions for use, reproduction, or
+distribution of Your modifications, or for any such Derivative Works as a whole,
+provided Your use, reproduction, and distribution of the Work otherwise complies
+with the conditions stated in this License.
+
+5. Submission of Contributions.
+
+Unless You explicitly state otherwise, any Contribution intentionally submitted
+for inclusion in the Work by You to the Licensor shall be under the terms and
+conditions of this License, without any additional terms or conditions.
+Notwithstanding the above, nothing herein shall supersede or modify the terms of
+any separate license agreement you may have executed with Licensor regarding
+such Contributions.
+
+6. Trademarks.
+
+This License does not grant permission to use the trade names, trademarks,
+service marks, or product names of the Licensor, except as required for
+reasonable and customary use in describing the origin of the Work and
+reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty.
+
+Unless required by applicable law or agreed to in writing, Licensor provides the
+Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
+including, without limitation, any warranties or conditions of TITLE,
+NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
+solely responsible for determining the appropriateness of using or
+redistributing the Work and assume any risks associated with Your exercise of
+permissions under this License.
+
+8. Limitation of Liability.
+
+In no event and under no legal theory, whether in tort (including negligence),
+contract, or otherwise, unless required by applicable law (such as deliberate
+and grossly negligent acts) or agreed to in writing, shall any Contributor be
+liable to You for damages, including any direct, indirect, special, incidental,
+or consequential damages of any character arising as a result of this License or
+out of the use or inability to use the Work (including but not limited to
+damages for loss of goodwill, work stoppage, computer failure or malfunction, or
+any and all other commercial damages or losses), even if such Contributor has
+been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability.
+
+While redistributing the Work or Derivative Works thereof, You may choose to
+offer, and charge a fee for, acceptance of support, warranty, indemnity, or
+other liability obligations and/or rights consistent with this License. However,
+in accepting such obligations, You may act only on Your own behalf and on Your
+sole responsibility, not on behalf of any other Contributor, and only if You
+agree to indemnify, defend, and hold each Contributor harmless for any liability
+incurred by, or claims asserted against, such Contributor by reason of your
+accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work
+
+To apply the Apache License to your work, attach the following boilerplate
+notice, with the fields enclosed by brackets "[]" replaced with your own
+identifying information. (Don't include the brackets!) The text should be
+enclosed in the appropriate comment syntax for the file format. We also
+recommend that a file or class name and description of purpose be included on
+the same "printed page" as the copyright notice for easier identification within
+third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..886e729
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,7 @@
+include README.rst
+include LICENSE.txt
+include CHANGELOG.md
+graft tests
+include wsgi/*.py
+
+global-exclude *.py[co]
diff --git a/PKG-INFO b/PKG-INFO
new file mode 100644
index 0000000..2981842
--- /dev/null
+++ b/PKG-INFO
@@ -0,0 +1,102 @@
+Metadata-Version: 1.1
+Name: usagestats
+Version: 0.6
+Summary: Anonymous usage statistics collecter
+Home-page: https://github.com/remram44/usagestats
+Author: Remi Rampin
+Author-email: remirampin at gmail.com
+License: Apache License 2.0
+Description: Usage statistics collector
+        ==========================
+        
+        This package is meant to easily get usage statistics from the users of your
+        program.
+        
+        Statistics will be collected but won't be uploaded until the user opts in. A
+        message will be printed on stderr asking the user to explicitly opt in or opt
+        out.
+        
+        Usage
+        -----
+        
+        You can easily collect information from your program by adding usagestats to
+        your project's requirements and using the library. Here is an example::
+        
+            import usagestats
+            import sys
+        
+        
+            optin_prompt = usagestats.Prompt(enable='cool_program --enable-stats',
+                                             disable='cool_program --disable-stats')
+        
+            # Location where to store stats
+            # Also allocates a unique ID for the user
+            # The version is important, since the information you log (or the format)
+            # might change in later versions of your program
+            stats = usagestats.Stats('~/.myprog/usage_stats',
+                                     optin_prompt,
+                                     'https://usagestats.example.org/',
+                                     unique_user_id=True,
+                                     version='0.1')
+        
+        
+            def main():
+                if len(sys.argv) < 2:
+                    pass
+                elif sys.argv.get(1) == '--enable-stats':
+                    stats.enable_reporting()
+                    sys.exit(0)
+                elif sys.argv.get(1) == '--disable-stats':
+                    stats.disable_reporting()
+                    sys.exit(0)
+        
+                if sys.version_info < (3,):
+                    # Stores some info, will be reported when submit() is called
+                    stats.note({'mode': 'compatibility'})
+        
+                # Report things
+                stats.submit(
+                    # Dictionary containing the info
+                    {'what': 'Ran the program'},
+                    # Flags making usagestats insert more details
+                    usagestats.OPERATING_SYSTEM,  # Operating system/distribution
+                    usagestats.PYTHON_VERSION,    # Python version info
+                    usagestats.SESSION_TIME,      # Time since Stats object was created
+                )
+        
+        
+            if __name__ == '__main__':
+                main()
+        
+        `submit()` will, by default, store the info in the specified directory. Nothing
+        will be reported until the user opts in; a message will simply be printed to
+        stderr::
+        
+            Uploading usage statistics is currently DISABLED
+            Please help us by providing anonymous usage statistics; you can enable this
+            by running:
+                cool_program --enable-stats
+            If you do not want to see this message again, you can run:
+                cool_program --disable-stats
+            Nothing will be uploaded before you opt in.
+        
+        Server
+        ------
+        
+        To collect the reports, any server will do; the reports are uploaded via POST
+        as a LF-separated list of ``key:value`` pairs. A simple script for mod_wsgi is
+        included; it writes each report to a separate file. Writing your own
+        implementation in your language of choice (PHP, Java) with your own backend
+        should be fairly straightforward.
+        
+Keywords: server,log,logging,usage,stats,statistics,collection,report
+Platform: UNKNOWN
+Classifier: Development Status :: 5 - Production/Stable
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: Apache Software License
+Classifier: Programming Language :: Python
+Classifier: Topic :: Internet
+Classifier: Topic :: Internet :: Log Analysis
+Classifier: Topic :: Software Development
+Classifier: Topic :: System :: Logging
+Classifier: Topic :: Utilities
diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..ec36a74
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,82 @@
+Usage statistics collector
+==========================
+
+This package is meant to easily get usage statistics from the users of your
+program.
+
+Statistics will be collected but won't be uploaded until the user opts in. A
+message will be printed on stderr asking the user to explicitly opt in or opt
+out.
+
+Usage
+-----
+
+You can easily collect information from your program by adding usagestats to
+your project's requirements and using the library. Here is an example::
+
+    import usagestats
+    import sys
+
+
+    optin_prompt = usagestats.Prompt(enable='cool_program --enable-stats',
+                                     disable='cool_program --disable-stats')
+
+    # Location where to store stats
+    # Also allocates a unique ID for the user
+    # The version is important, since the information you log (or the format)
+    # might change in later versions of your program
+    stats = usagestats.Stats('~/.myprog/usage_stats',
+                             optin_prompt,
+                             'https://usagestats.example.org/',
+                             unique_user_id=True,
+                             version='0.1')
+
+
+    def main():
+        if len(sys.argv) < 2:
+            pass
+        elif sys.argv.get(1) == '--enable-stats':
+            stats.enable_reporting()
+            sys.exit(0)
+        elif sys.argv.get(1) == '--disable-stats':
+            stats.disable_reporting()
+            sys.exit(0)
+
+        if sys.version_info < (3,):
+            # Stores some info, will be reported when submit() is called
+            stats.note({'mode': 'compatibility'})
+
+        # Report things
+        stats.submit(
+            # Dictionary containing the info
+            {'what': 'Ran the program'},
+            # Flags making usagestats insert more details
+            usagestats.OPERATING_SYSTEM,  # Operating system/distribution
+            usagestats.PYTHON_VERSION,    # Python version info
+            usagestats.SESSION_TIME,      # Time since Stats object was created
+        )
+
+
+    if __name__ == '__main__':
+        main()
+
+`submit()` will, by default, store the info in the specified directory. Nothing
+will be reported until the user opts in; a message will simply be printed to
+stderr::
+
+    Uploading usage statistics is currently DISABLED
+    Please help us by providing anonymous usage statistics; you can enable this
+    by running:
+        cool_program --enable-stats
+    If you do not want to see this message again, you can run:
+        cool_program --disable-stats
+    Nothing will be uploaded before you opt in.
+
+Server
+------
+
+To collect the reports, any server will do; the reports are uploaded via POST
+as a LF-separated list of ``key:value`` pairs. A simple script for mod_wsgi is
+included; it writes each report to a separate file. Writing your own
+implementation in your language of choice (PHP, Java) with your own backend
+should be fairly straightforward.
diff --git a/setup.cfg b/setup.cfg
new file mode 100644
index 0000000..adf5ed7
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,7 @@
+[bdist_wheel]
+universal = 1
+
+[egg_info]
+tag_build = 
+tag_date = 0
+
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..232d849
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,34 @@
+import os
+from setuptools import setup
+
+
+# pip workaround
+os.chdir(os.path.abspath(os.path.dirname(__file__)))
+
+
+with open('README.rst') as fp:
+    description = fp.read()
+setup(name='usagestats',
+      version='0.6',
+      py_modules=['usagestats'],
+      description="Anonymous usage statistics collecter",
+      install_requires=['requests'],
+      author="Remi Rampin",
+      author_email='remirampin at gmail.com',
+      maintainer="Remi Rampin",
+      maintainer_email='remirampin at gmail.com',
+      url='https://github.com/remram44/usagestats',
+      long_description=description,
+      license='Apache License 2.0',
+      keywords=['server', 'log', 'logging', 'usage', 'stats', 'statistics',
+                'collection', 'report'],
+      classifiers=[
+          'Development Status :: 5 - Production/Stable',
+          'Intended Audience :: Developers',
+          'License :: OSI Approved :: Apache Software License',
+          'Programming Language :: Python',
+          'Topic :: Internet',
+          'Topic :: Internet :: Log Analysis',
+          'Topic :: Software Development',
+          'Topic :: System :: Logging',
+          'Topic :: Utilities'])
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/__main__.py b/tests/__main__.py
new file mode 100644
index 0000000..e57f4e1
--- /dev/null
+++ b/tests/__main__.py
@@ -0,0 +1,33 @@
+import locale
+import os
+import sys
+
+try:
+    import unittest2 as unittest
+    sys.modules['unittest'] = unittest
+except ImportError:
+    import unittest
+
+
+top_level = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
+start_dir = os.path.join(top_level, 'tests')
+if top_level not in sys.path:
+    sys.path.insert(0, top_level)
+sys.path.append(start_dir)
+
+
+class Program(unittest.TestProgram):
+    def createTests(self):
+        if self.testNames is None:
+            self.test = self.testLoader.discover(
+                    start_dir=os.path.dirname(os.path.abspath(__file__)),
+                    pattern='test_*.py')
+        else:
+            self.test = self.testLoader.loadTestsFromNames(self.testNames)
+
+
+if __name__ == '__main__':
+    # Locale
+    locale.setlocale(locale.LC_ALL, '')
+
+    Program()
diff --git a/tests/test_reporting.py b/tests/test_reporting.py
new file mode 100644
index 0000000..1a94501
--- /dev/null
+++ b/tests/test_reporting.py
@@ -0,0 +1,164 @@
+import functools
+import os
+import shutil
+import signal
+import subprocess
+import sys
+import tempfile
+import time
+import unittest
+
+import usagestats
+
+from tests.utils import capture_stderr, regex_compare
+
+
+optin_prompt = usagestats.Prompt(enable='cool_program --enable-stats',
+                                 disable='cool_program --disable-stats')
+
+
+def temp_recv_dir(func):
+    @functools.wraps(func)
+    def wrapper(self):
+        for name in os.listdir(self._recv_dir):
+            os.remove(os.path.join(self._recv_dir, name))
+        tdir = tempfile.mkdtemp(prefix='usagestats_tests_send_')
+        try:
+            os.environ['HOME'] = tdir
+            func(self, tdir=tdir)
+        finally:
+            shutil.rmtree(tdir)
+    return wrapper
+
+
+class TestReporting(unittest.TestCase):
+    @classmethod
+    def setUpClass(cls):
+        if 'PYTHON_USAGE_STATS' in os.environ:
+            del os.environ['PYTHON_USAGE_STATS']
+        cls._recv_dir = tempfile.mkdtemp(prefix='usagestats_tests_server_')
+        os.environ['USAGESTATS_SERVER_USE_WERKZEUG'] = 'yes'
+        cls._wsgi_process = subprocess.Popen(
+                [sys.executable,
+                 os.path.abspath(os.path.join(os.path.dirname(__file__),
+                                              os.pardir,
+                                              'wsgi',
+                                              'usagestats_server.py'))],
+                cwd=cls._recv_dir)
+        time.sleep(2)
+
+    @classmethod
+    def tearDownClass(cls):
+        cls._wsgi_process.send_signal(signal.SIGTERM)
+        cls._wsgi_process.wait()
+        shutil.rmtree(cls._recv_dir)
+
+    def _get_reports(self, tdir, expected=0):
+        # Give .5s to the server to process the request and write a file
+        time.sleep(0.5)
+        lst = list(os.listdir(self._recv_dir))
+        # This cheats a little by waiting a bit more (max 5s) if the expected
+        # number of files is not there
+        nb = 1
+        while nb < 10 and len(lst) < expected:
+            time.sleep(0.5)
+            nb += 1
+            lst = list(os.listdir(self._recv_dir))
+        self.assertEqual(len(lst), expected)
+        results = []
+        for name in sorted(lst):
+            with open(os.path.join(self._recv_dir, name), 'rb') as fp:
+                results.append(fp.read())
+        return results
+
+    @temp_recv_dir
+    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:',
+                           br'^version:1\.0$',
+                           br'^mode:compatibility$',
+                           br'^what:Ran the program$',
+                           br'^mode:' + mode + br'$',
+                           br'^python:'],
+                          self.fail)
+
+    @temp_recv_dir
+    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:',
+                       br'^version:1\.0$',
+                       br'^mode:compatibility$',
+                       br'^what:Ran the program$',
+                       br'^mode:yep$',
+                       br'^python:'],
+                      self.fail)
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644
index 0000000..49305db
--- /dev/null
+++ b/tests/utils.py
@@ -0,0 +1,78 @@
+import contextlib
+import sys
+import re
+
+
+if sys.version_info < (3,):
+    from itertools import izip_longest as zip_longest
+else:
+    from itertools import zip_longest
+
+
+class FakeStream(object):
+    def __init__(self):
+        self.written = []
+
+    def write(self, s):
+        if isinstance(s, bytes):
+            self.written.append(s)
+        else:
+            self.written.append(s.encode('utf-8'))
+
+    def getvalue(self):
+        value = b''.join(self.written)
+        self.written = [value]
+        return value
+
+
+ at contextlib.contextmanager
+def capture_stream(stream):
+    lines = []
+    old = getattr(sys, stream)
+    sio = FakeStream()
+    setattr(sys, stream, sio)
+    try:
+        yield lines
+    finally:
+        setattr(sys, stream, old)
+        lines.extend(sio.getvalue().split(b'\n'))
+        if lines and not lines[-1]:
+            del lines[-1]
+
+
+ at contextlib.contextmanager
+def capture_stdout():
+    with capture_stream('stdout') as lines:
+        yield lines
+
+
+ at contextlib.contextmanager
+def capture_stderr():
+    with capture_stream('stderr') as lines:
+        yield lines
+
+
+def _fail(msg):
+    raise AssertionError(msg)
+
+
+def regex_compare(actual, expected, fail=_fail):
+    if isinstance(actual, bytes):
+        actual = actual.splitlines()
+        if actual and not actual[-1]:
+            actual = actual[:-1]
+    elif not isinstance(actual, (list, tuple)):
+        raise TypeError
+
+    try:
+        for a, e in zip_longest(actual, expected):
+            if e is None:
+                fail("Unexpected line %r" % a)
+            elif a is None:
+                fail("Missing line: expected %r" % a)
+            else:
+                if not re.search(e, a):
+                    fail("%r != %r" % (a, e))
+    except Exception:
+        print("Tested output: %r" % (actual,))
+        raise
diff --git a/usagestats.egg-info/PKG-INFO b/usagestats.egg-info/PKG-INFO
new file mode 100644
index 0000000..2981842
--- /dev/null
+++ b/usagestats.egg-info/PKG-INFO
@@ -0,0 +1,102 @@
+Metadata-Version: 1.1
+Name: usagestats
+Version: 0.6
+Summary: Anonymous usage statistics collecter
+Home-page: https://github.com/remram44/usagestats
+Author: Remi Rampin
+Author-email: remirampin at gmail.com
+License: Apache License 2.0
+Description: Usage statistics collector
+        ==========================
+        
+        This package is meant to easily get usage statistics from the users of your
+        program.
+        
+        Statistics will be collected but won't be uploaded until the user opts in. A
+        message will be printed on stderr asking the user to explicitly opt in or opt
+        out.
+        
+        Usage
+        -----
+        
+        You can easily collect information from your program by adding usagestats to
+        your project's requirements and using the library. Here is an example::
+        
+            import usagestats
+            import sys
+        
+        
+            optin_prompt = usagestats.Prompt(enable='cool_program --enable-stats',
+                                             disable='cool_program --disable-stats')
+        
+            # Location where to store stats
+            # Also allocates a unique ID for the user
+            # The version is important, since the information you log (or the format)
+            # might change in later versions of your program
+            stats = usagestats.Stats('~/.myprog/usage_stats',
+                                     optin_prompt,
+                                     'https://usagestats.example.org/',
+                                     unique_user_id=True,
+                                     version='0.1')
+        
+        
+            def main():
+                if len(sys.argv) < 2:
+                    pass
+                elif sys.argv.get(1) == '--enable-stats':
+                    stats.enable_reporting()
+                    sys.exit(0)
+                elif sys.argv.get(1) == '--disable-stats':
+                    stats.disable_reporting()
+                    sys.exit(0)
+        
+                if sys.version_info < (3,):
+                    # Stores some info, will be reported when submit() is called
+                    stats.note({'mode': 'compatibility'})
+        
+                # Report things
+                stats.submit(
+                    # Dictionary containing the info
+                    {'what': 'Ran the program'},
+                    # Flags making usagestats insert more details
+                    usagestats.OPERATING_SYSTEM,  # Operating system/distribution
+                    usagestats.PYTHON_VERSION,    # Python version info
+                    usagestats.SESSION_TIME,      # Time since Stats object was created
+                )
+        
+        
+            if __name__ == '__main__':
+                main()
+        
+        `submit()` will, by default, store the info in the specified directory. Nothing
+        will be reported until the user opts in; a message will simply be printed to
+        stderr::
+        
+            Uploading usage statistics is currently DISABLED
+            Please help us by providing anonymous usage statistics; you can enable this
+            by running:
+                cool_program --enable-stats
+            If you do not want to see this message again, you can run:
+                cool_program --disable-stats
+            Nothing will be uploaded before you opt in.
+        
+        Server
+        ------
+        
+        To collect the reports, any server will do; the reports are uploaded via POST
+        as a LF-separated list of ``key:value`` pairs. A simple script for mod_wsgi is
+        included; it writes each report to a separate file. Writing your own
+        implementation in your language of choice (PHP, Java) with your own backend
+        should be fairly straightforward.
+        
+Keywords: server,log,logging,usage,stats,statistics,collection,report
+Platform: UNKNOWN
+Classifier: Development Status :: 5 - Production/Stable
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: Apache Software License
+Classifier: Programming Language :: Python
+Classifier: Topic :: Internet
+Classifier: Topic :: Internet :: Log Analysis
+Classifier: Topic :: Software Development
+Classifier: Topic :: System :: Logging
+Classifier: Topic :: Utilities
diff --git a/usagestats.egg-info/SOURCES.txt b/usagestats.egg-info/SOURCES.txt
new file mode 100644
index 0000000..f2e5782
--- /dev/null
+++ b/usagestats.egg-info/SOURCES.txt
@@ -0,0 +1,17 @@
+CHANGELOG.md
+LICENSE.txt
+MANIFEST.in
+README.rst
+setup.cfg
+setup.py
+usagestats.py
+tests/__init__.py
+tests/__main__.py
+tests/test_reporting.py
+tests/utils.py
+usagestats.egg-info/PKG-INFO
+usagestats.egg-info/SOURCES.txt
+usagestats.egg-info/dependency_links.txt
+usagestats.egg-info/requires.txt
+usagestats.egg-info/top_level.txt
+wsgi/usagestats_server.py
\ No newline at end of file
diff --git a/usagestats.egg-info/dependency_links.txt b/usagestats.egg-info/dependency_links.txt
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/usagestats.egg-info/dependency_links.txt
@@ -0,0 +1 @@
+
diff --git a/usagestats.egg-info/requires.txt b/usagestats.egg-info/requires.txt
new file mode 100644
index 0000000..f229360
--- /dev/null
+++ b/usagestats.egg-info/requires.txt
@@ -0,0 +1 @@
+requests
diff --git a/usagestats.egg-info/top_level.txt b/usagestats.egg-info/top_level.txt
new file mode 100644
index 0000000..4bb8584
--- /dev/null
+++ b/usagestats.egg-info/top_level.txt
@@ -0,0 +1 @@
+usagestats
diff --git a/usagestats.py b/usagestats.py
new file mode 100644
index 0000000..1ce1bae
--- /dev/null
+++ b/usagestats.py
@@ -0,0 +1,378 @@
... 485 lines suppressed ...

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-rpaths.git



More information about the Python-modules-commits mailing list