From 665ed819319480082b8a99895b3d5706abfb9b64 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Sun, 28 Jun 2020 00:00:27 +0530 Subject: cerbero: Define a custom user agent while downloading Some places block urllib, and others block wget / curl. Set our own agent to workaround that, and so that people can contact us in case we're doing something they don't like while downloading things. --- cerbero/enums.py | 3 +++ cerbero/utils/shell.py | 11 +++++++---- setup.py | 3 ++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cerbero/enums.py b/cerbero/enums.py index 6ef5cda1..4137e027 100644 --- a/cerbero/enums.py +++ b/cerbero/enums.py @@ -17,6 +17,9 @@ # Boston, MA 02111-1307, USA. +# Safest place to define this since this file imports very few modules +CERBERO_VERSION = '1.16.2.1' + class Platform: ''' Enumeration of supported platforms ''' LINUX = 'linux' diff --git a/cerbero/utils/shell.py b/cerbero/utils/shell.py index 20247e1b..2f38c7bc 100644 --- a/cerbero/utils/shell.py +++ b/cerbero/utils/shell.py @@ -35,12 +35,13 @@ from pathlib import Path, PurePath from distutils.version import StrictVersion import cerbero.hacks -from cerbero.enums import Platform +from cerbero.enums import CERBERO_VERSION, Platform from cerbero.utils import _, system_info, to_unixpath from cerbero.utils import messages as m from cerbero.errors import FatalError +USER_AGENT = 'GStreamer Cerbero/' + CERBERO_VERSION PATCH = 'patch' TAR = 'tar' @@ -325,7 +326,7 @@ def download_wget(url, destination=None, check_cert=True, overwrite=False): @param destination: destination where the file will be saved @type destination: str ''' - cmd = "wget %s " % url + cmd = "wget --user-agent '{}' {} ".format(USER_AGENT, url) path = None if destination is not None: cmd += "-O %s " % destination @@ -366,7 +367,9 @@ def download_urllib2(url, destination=None, check_cert=True, overwrite=False): try: logging.info(destination) with open(destination, 'wb') as d: - f = urllib.request.urlopen(url, context=ctx) + req = urllib.request.Request(url) + req.add_header('User-Agent', USER_AGENT) + f = urllib.request.urlopen(req, context=ctx) d.write(f.read()) except urllib.error.HTTPError as e: if os.path.exists(destination): @@ -383,7 +386,7 @@ def download_curl(url, destination=None, check_cert=True, overwrite=False): @type destination: str ''' path = None - cmd = "curl -L --fail --retry 2 " + cmd = "curl -L --fail --retry 2 --user-agent '{}' ".format(USER_AGENT) if not check_cert: cmd += " -k " if destination is not None: diff --git a/setup.py b/setup.py index 1ce4f572..62bd8923 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ import shutil from setuptools import setup, find_packages from setuptools.command import sdist as setuptools_sdist from cerbero.utils import shell +from cerbero.enums import CERBERO_VERSION from distutils.dir_util import copy_tree import distutils.log @@ -127,7 +128,7 @@ class extended_sdist(setuptools_sdist.sdist): setup( name = "cerbero", - version = '1.16.2.1', + version = CERBERO_VERSION, author = "Andoni Morales", author_email = "amorales@fluendo.com", description = ("Multi platform build system for Open Source projects"), -- cgit v1.2.3