Skip to content

Commit d5ec2b8

Browse files
Add TUF version number, and user agent
1 parent 4863868 commit d5ec2b8

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

setup.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,26 @@
7575
from setuptools import find_packages
7676

7777

78+
def find_version(*file_paths):
79+
"""
80+
https://github.com/pypa/pip/blob/404838abcca467648180b358598c597b74d568c9/setup.py#L18-L28
81+
"""
82+
83+
version_file = read(*file_paths)
84+
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
85+
version_file, re.M)
86+
if version_match:
87+
return version_match.group(1)
88+
raise RuntimeError("Unable to find version string.")
89+
90+
7891
with open('README.md') as file_object:
7992
long_description = file_object.read()
8093

94+
8195
setup(
8296
name = 'tuf',
83-
version = '0.11.1',
97+
version = find_version("tuf", "__init__.py"),
8498
description = 'A secure updater framework for Python',
8599
long_description = long_description,
86100
long_description_content_type='text/markdown',

tuf/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "0.11.1"

tuf/download.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,15 @@ def _download_file(url, required_length, STRICT_REQUIRED_LENGTH=True):
232232

233233
try:
234234
# Attach some default headers to every Session.
235+
requests_user_agent = _session.headers['User-Agent']
236+
# Follows the RFC: https://tools.ietf.org/html/rfc7231#section-5.5.3
237+
tuf_user_agent = 'tuf/' + tuf.__version__ + ' ' + requests_user_agent
235238
_session.headers.update({
236239
# Tell the server not to compress or modify anything.
237240
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding#Directives
238241
'Accept-Encoding': 'identity',
239242
# The TUF user agent.
240-
# TODO: Attach version number, which cannot be easily found right now.
241-
'User-Agent': 'tuf'
243+
'User-Agent': tuf_user_agent
242244
})
243245

244246
# Get the requests.Response object for this URL.

0 commit comments

Comments
 (0)