From f815d2033d464c3838f07188b8aa0ce773b6e57b Mon Sep 17 00:00:00 2001 From: Emilio Pozuelo Monfort Date: Thu, 13 Aug 2020 11:47:12 +0200 Subject: De-duplicate setup_path All the scripts in bin/ can share the definition. Also setup_paths.py calls setup_path so one just has to import that module before importing those from lib/python/. Additionally this helps some scripts work better under Python 3, as one variant of setup_paths that we had called string.rfind, which is not present there. --- bin/apt-update-file | 17 +---------------- bin/check-syntax | 17 +---------------- bin/lts-bts | 7 +------ bin/lts-cve-triage.py | 8 +------- bin/lts-needs-forward-port.py | 8 +------- bin/report-vuln | 7 +------ bin/setup_paths.py | 11 +++++++++++ bin/update-db | 17 +---------------- bin/update-nvd | 16 +--------------- 9 files changed, 19 insertions(+), 89 deletions(-) create mode 100644 bin/setup_paths.py (limited to 'bin') diff --git a/bin/apt-update-file b/bin/apt-update-file index 617eed2d99..505d23f602 100755 --- a/bin/apt-update-file +++ b/bin/apt-update-file @@ -2,24 +2,9 @@ # This script is mainly used to demo the updateFile function. from __future__ import print_function -import os -import os.path -import string import sys -def setup_paths(): - check_file = 'lib/python/debian_support.py' - path = os.getcwd() - while 1: - if os.path.exists("%s/%s" % (path, check_file)): - sys.path = [path + '/lib/python'] + sys.path - return path - idx = string.rfind(path, '/') - if idx == -1: - raise ImportError("could not setup paths") - path = path[0:idx] -root_path = setup_paths() - +import setup_paths import debian_support if len(sys.argv) != 3: diff --git a/bin/check-syntax b/bin/check-syntax index 6f7eadd831..529f378550 100755 --- a/bin/check-syntax +++ b/bin/check-syntax @@ -1,23 +1,8 @@ #!/usr/bin/python3 -import os -import os.path -import string import sys -def setup_paths(): - check_file = 'lib/python/debian_support.py' - path = os.getcwd() - while 1: - if os.path.exists("%s/%s" % (path, check_file)): - sys.path = [path + '/lib/python'] + sys.path - return path - idx = string.rfind(path, '/') - if idx == -1: - raise ImportError("could not setup paths") - path = path[0:idx] -root_path = setup_paths() - +import setup_paths import bugs import debian_support diff --git a/bin/lts-bts b/bin/lts-bts index da9365721c..d1a70b63c2 100755 --- a/bin/lts-bts +++ b/bin/lts-bts @@ -13,12 +13,7 @@ import warnings from tracker_data import TrackerData -def setup_path(): - dirname = os.path.dirname - base = dirname(dirname(os.path.realpath(sys.argv[0]))) - sys.path.insert(0, os.path.join(base, "lib", "python")) - -setup_path() +import setup_paths import config from jinja2 import Template diff --git a/bin/lts-cve-triage.py b/bin/lts-cve-triage.py index 7f106ab8e6..9a90fcd816 100755 --- a/bin/lts-cve-triage.py +++ b/bin/lts-cve-triage.py @@ -15,7 +15,6 @@ # You should have received a copy of the GNU General Public License # along with this file. If not, see . -import os import sys import argparse import collections @@ -23,12 +22,7 @@ import collections from tracker_data import TrackerData from unsupported_packages import UnsupportedPackages, LimitedSupportPackages -def setup_path(): - dirname = os.path.dirname - base = dirname(dirname(os.path.realpath(sys.argv[0]))) - sys.path.insert(0, os.path.join(base, "lib", "python")) - -setup_path() +import setup_paths import config RELEASES = { diff --git a/bin/lts-needs-forward-port.py b/bin/lts-needs-forward-port.py index ee29ef5486..06a5630f8f 100755 --- a/bin/lts-needs-forward-port.py +++ b/bin/lts-needs-forward-port.py @@ -17,17 +17,11 @@ import argparse import collections -import os import sys from tracker_data import TrackerData -def setup_path(): - dirname = os.path.dirname - base = dirname(dirname(os.path.realpath(sys.argv[0]))) - sys.path.insert(0, os.path.join(base, "lib", "python")) - -setup_path() +import setup_paths import config lts = config.get_supported_releases()[0] diff --git a/bin/report-vuln b/bin/report-vuln index ffa3ecc12f..d0dead6895 100755 --- a/bin/report-vuln +++ b/bin/report-vuln @@ -20,13 +20,8 @@ from textwrap import wrap temp_id = re.compile('(?:CVE|cve)\-[0-9]{4}-XXXX') -def setup_path(): - dirname = os.path.dirname - base = dirname(dirname(os.path.realpath(sys.argv[0]))) - sys.path.insert(0, os.path.join(base, "lib", "python")) - def description_from_list(id, pkg='', skip_entries=0): - setup_path() + import setup_paths import bugs import debian_support is_temp = temp_id.match(id) diff --git a/bin/setup_paths.py b/bin/setup_paths.py new file mode 100644 index 0000000000..d2f8194662 --- /dev/null +++ b/bin/setup_paths.py @@ -0,0 +1,11 @@ +# inserts lib/python/ into sys.path + +import os +import sys + +def setup_path(): + dirname = os.path.dirname + base = dirname(dirname(os.path.realpath(__file__))) + sys.path.insert(0, os.path.join(base, "lib", "python")) + +setup_path() diff --git a/bin/update-db b/bin/update-db index 71603d9ee1..e6da4322ed 100755 --- a/bin/update-db +++ b/bin/update-db @@ -1,24 +1,9 @@ #!/usr/bin/python3 from __future__ import print_function -import os -import os.path -import string import sys -def setup_paths(): - check_file = 'lib/python/debian_support.py' - path = os.getcwd() - while 1: - if os.path.exists("%s/%s" % (path, check_file)): - sys.path = [path + '/lib/python'] + sys.path - return path - idx = string.rfind(path, '/') - if idx == -1: - raise ImportError("could not setup paths") - path = path[0:idx] -os.chdir(setup_paths()) - +import setup_paths import bugs import debian_support import security_db diff --git a/bin/update-nvd b/bin/update-nvd index 19b1cbfcda..6573b9139f 100755 --- a/bin/update-nvd +++ b/bin/update-nvd @@ -1,23 +1,9 @@ #!/usr/bin/python3 -import os import os.path -import string import sys -def setup_paths(): - check_file = 'lib/python/debian_support.py' - path = os.getcwd() - while 1: - if os.path.exists("%s/%s" % (path, check_file)): - sys.path = [path + '/lib/python'] + sys.path - return path - idx = string.rfind(path, '/') - if idx == -1: - raise ImportError("could not setup paths") - path = path[0:idx] -setup_paths() - +import setup_paths import nvd import security_db -- cgit v1.2.3