Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version as an importable variable and remove dependency on pkg_resources #566

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ Our team will trigger the release pipeline.
- Commit the changes to the repository in a release branch and open a PR. Do not merge yet.

### Release
1. Bump the version in [`setup.py`](setup.py) and push it to your changelog PR. [Example](https://github.com/DataDog/datadogpy/pull/495/files#diff-2eeaed663bd0d25b7e608891384b7298)
1. Bump the version in [`datadog/version.py`](datadog/version.py) and push it to your changelog PR. [Example](https://github.com/DataDog/datadogpy/pull/495/files#diff-2eeaed663bd0d25b7e608891384b7298)
1. Merge the PR to master.
1. Create the release on GitHub. [Example](https://github.com/DataDog/datadogpy/releases/tag/v0.33.0)
1. Checkout the tag created at the previous step.
1. Run `ddev release build .` and `ddev release upload --sdist . `.
- Make sure that both an `sdist` and a [universal wheel](https://packaging.python.org/guides/distributing-packages-using-setuptools/#universal-wheels) have been uploaded to [PyPI](https://pypi.python.org/pypi/datadog/).
1. Bump the version again in `setup.py` to a dev version (e.g. `0.34.0` -> `0.35.0.dev`), open a PR and merge it to master.
1. Bump the version again in `datadog/version.py` to a dev version (e.g. `0.34.0` -> `0.35.0.dev`), open a PR and merge it to master.
5 changes: 1 addition & 4 deletions datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
from datadog.dogstatsd import DogStatsd, statsd # noqa
from datadog.threadstats import ThreadStats, datadog_lambda_wrapper, lambda_metric # noqa
from datadog.util.compat import iteritems, NullHandler, text
from datadog.util.config import get_version
from datadog.util.hostname import get_hostname


__version__ = get_version()
from datadog.version import __version__ # noqa

# Loggers
logging.getLogger('datadog.api').addHandler(NullHandler())
Expand Down
5 changes: 2 additions & 3 deletions datadog/dogshell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import argparse

# datadog
from datadog import initialize
from datadog import initialize, __version__
from datadog.dogshell.comment import CommentClient
from datadog.dogshell.common import DogshellConfig
from datadog.dogshell.dashboard_list import DashboardListClient
Expand All @@ -26,7 +26,6 @@
from datadog.dogshell.tag import TagClient
from datadog.dogshell.timeboard import TimeboardClient
from datadog.dogshell.dashboard import DashboardClient
from datadog.util.config import get_version


def main():
Expand All @@ -53,7 +52,7 @@ def main():
parser.add_argument('--timeout', help="time to wait in seconds before timing"
" out an API call (default 10)", default=10, type=int)
parser.add_argument('-v', '--version', help='Dog API version', action='version',
version='%(prog)s {0}'.format(get_version()))
version='%(prog)s {0}'.format(__version__))

config = DogshellConfig()

Expand Down
5 changes: 2 additions & 3 deletions datadog/dogshell/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
import warnings

# datadog
from datadog import initialize, api
from datadog.util.config import get_version
from datadog import initialize, api, __version__
from datadog.util.compat import is_p3k


Expand Down Expand Up @@ -258,7 +257,7 @@ def parse_options(raw_args=None):
quotes to prevent python executing as soon as there is a space in your command. \n \nNOTICE: In \
normal mode, the whole stderr is printed before stdout, in flush_live mode they will be mixed but \
there is not guarantee that messages sent by the command on both stderr and stdout are printed in \
the order they were sent.", version="%prog {0}".format(get_version()), option_class=DogwrapOption)
the order they were sent.", version="%prog {0}".format(__version__), option_class=DogwrapOption)

parser.add_option('-n', '--name', action='store', type='string', help="the name of the event \
as it should appear on your Datadog stream")
Expand Down
8 changes: 2 additions & 6 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from datadog.dogstatsd.context import TimedContextManagerDecorator
from datadog.dogstatsd.route import get_default_route
from datadog.util.compat import text
from datadog.util.config import get_pkg_version
from datadog.util.format import normalize_tags
from datadog.version import __version__

# Logging
log = logging.getLogger('datadog.dogstatsd')
Expand Down Expand Up @@ -163,13 +163,9 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, max_buffer_size=50, nam
self.default_sample_rate = default_sample_rate

# init telemetry version
try:
client_version = get_pkg_version()
except Exception:
client_version = u"unknown"
self._client_tags = [
"client:py",
"client_version:{}".format(client_version),
"client_version:{}".format(__version__),
"client_transport:{}".format(transport),
]
self._reset_telementry()
Expand Down
5 changes: 0 additions & 5 deletions datadog/util/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ class NullHandler(Handler):
def emit(self, record):
pass

try:
import pkg_resources as pkg
except ImportError:
pkg = None # type: ignore


def _is_py_version_higher_than(major, minor=0):
"""
Expand Down
25 changes: 8 additions & 17 deletions datadog/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import sys

# datadog
from datadog.util.compat import configparser, StringIO, is_p3k, pkg
from datadog.util.compat import configparser, StringIO, is_p3k
from datadog.version import __version__

# CONSTANTS
DATADOG_CONF = "datadog.conf"
Expand Down Expand Up @@ -135,26 +136,16 @@ def get_config(cfg_path=None, options=None):
def get_pkg_version():
"""
Resolve `datadog` package version.
"""
if not pkg:
return u"unknown"

dist = pkg.get_distribution("datadog")
# Normalize case for Windows systems
dist_loc = os.path.normcase(dist.location)
here = os.path.normcase(__file__)
if not here.startswith(dist_loc):
# not installed, but there is another version that *is*
raise pkg.DistributionNotFound

return dist.version
Deprecated: use `datadog.__version__` directly instead
"""
return __version__


def get_version():
"""
Resolve `datadog` package version.

Deprecated: use `datadog.__version__` directly instead
"""
try:
return get_pkg_version()
except pkg.DistributionNotFound:
return u"Please install `datadog` with setup.py"
return __version__
1 change: 1 addition & 0 deletions datadog/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.37.0.dev"
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def get_readme_md_contents():
long_description = f.read()
return long_description

version = {}
with open("datadog/version.py") as fp:
exec(fp.read(), version)

install_reqs = ["decorator>=3.3.2", "requests>=2.6.0"]

Expand All @@ -25,7 +28,7 @@ def get_readme_md_contents():

setup(
name="datadog",
version="0.37.0.dev",
version=version["__version__"],
install_requires=install_reqs,
tests_require=["pytest", "mock", "freezegun"],
packages=["datadog", "datadog.api", "datadog.dogstatsd", "datadog.threadstats", "datadog.util", "datadog.dogshell"],
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

# datadog
from datadog import initialize, statsd
from datadog import __version__ as version
from datadog.dogstatsd.base import DogStatsd
from datadog.dogstatsd.context import TimedContextManagerDecorator
from datadog.util.compat import is_higher_py35, is_p3k
from datadog.util.config import get_version
from tests.util.contextmanagers import preserve_environment_variable, EnvVars
from tests.unit.dogstatsd.fixtures import load_fixtures

Expand Down Expand Up @@ -78,7 +78,6 @@ def send(self, payload):


def telemetry_metrics(metrics=1, events=0, service_checks=0, bytes_sent=0, bytes_dropped=0, packets_sent=0, packets_dropped=0, transport="udp", tags=""):
version = get_version()
tags = "," + tags if tags else ""

return "\ndatadog.dogstatsd.client.metrics:{}|c|#client:py,client_version:{},client_transport:{}{}\n".format(metrics, version, transport, tags) \
Expand Down