Skip to content

Commit 2a036ed

Browse files
various python: clean up sys.path hacks
The reason that the files in test/verify/ don't need to do sys.path hacks is because parent.py has been symlinked into that directory. Do the same with the two other directories which contain scripts which need such hacks, allowing a substantial cleanup. Make some minor code adjustments to use the variables defined in parent.py instead of the home-brew versions. Add a somewhat dubious cwd= parameter to the `git grep` subprocess invocation in tools/url-check. This is strictly correct (since we want to check the base directory, regardless of the current directory when the tool was invoked) but we're really doing as a convenient way to avoid a message about an unused import.
1 parent 2d0ec23 commit 2a036ed

File tree

5 files changed

+11
-25
lines changed

5 files changed

+11
-25
lines changed

test/containers/check-bastion

+2-9
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,11 @@
1919

2020

2121
import os
22-
import sys
23-
24-
test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
25-
sys.path.append(test_dir)
26-
27-
from verify import parent
28-
parent # pyflakes
29-
22+
from parent import TEST_DIR
3023
from testlib import *
3124
from testvm import VirtMachine
3225

33-
AUTH_KEY = os.path.join(test_dir, "containers/files/enc_rsa")
26+
AUTH_KEY = os.path.join(TEST_DIR, "containers/files/enc_rsa")
3427
PUB_AUTH_KEY = "{}.pub".format(AUTH_KEY)
3528

3629

test/containers/parent.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../common/parent.py

test/containers/run-tests

+5-11
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,11 @@ import imp
2222
import os
2323
import string
2424
import unittest
25+
from parent import TEST_DIR, BASE_DIR
26+
import testlib
2527

26-
sys.dont_write_bytecode = True
27-
28-
base_dir = os.path.dirname(os.path.realpath(__file__))
29-
testdir = os.path.dirname(base_dir)
30-
sys.path.append(testdir)
3128

32-
from verify import parent
33-
parent # pyflakes
34-
35-
from common import testlib
29+
sys.dont_write_bytecode = True
3630

3731

3832
def check_valid(filename):
@@ -47,7 +41,7 @@ def run(opts):
4741
# Now actually load the tests, any modules that start with "check-*"
4842
loader = unittest.TestLoader()
4943
suite = unittest.TestSuite()
50-
for filename in glob.glob(os.path.join(base_dir,
44+
for filename in glob.glob(os.path.join(TEST_DIR, "containers",
5145
"check-{0}*".format(opts.container))):
5246
name = check_valid(filename)
5347
if not name or not os.path.isfile(filename):
@@ -68,7 +62,7 @@ def main():
6862
if not opts.container:
6963
opts.container = 'bastion'
7064

71-
if not os.path.isdir(os.path.abspath(os.path.join(testdir, "..", "containers", opts.container))):
65+
if not os.path.isdir(os.path.abspath(os.path.join(BASE_DIR, "containers", opts.container))):
7266
sys.stderr.write("Unable to run tests for unknown container {0}.\n".format(opts.container))
7367
exit(1)
7468

tools/parent.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../test/common/parent.py

tools/urls-check

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
1919

2020

21-
import os
22-
from os.path import dirname, abspath
2321
import sys
2422
import time
2523
import argparse
2624
import subprocess
2725
import urllib
2826
from urllib.request import urlopen, Request
29-
30-
sys.path.append(os.path.join(dirname(dirname(abspath(__file__))), "bots"))
27+
from parent import BASE_DIR
3128
import task
3229

3330
DAYS = 7
@@ -87,7 +84,7 @@ def main():
8784

8885
def check_urls(verbose):
8986
command = r'git grep -IEho "(https?)://[-a-zA-Z0-9@:%_\+.~#?&=/]+" -- pkg ":!*.svg" | sort -u'
90-
urls = subprocess.check_output(command, shell=True, universal_newlines=True).split("\n")
87+
urls = subprocess.check_output(command, shell=True, universal_newlines=True, cwd=BASE_DIR).split("\n")
9188
redirects = []
9289
failed = []
9390
for url in urls:

0 commit comments

Comments
 (0)