Skip to content

Commit

Permalink
Upgrade file to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
lauft committed Mar 8, 2025
1 parent cdfe7bd commit d80f1b5
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions test/basetest/hooks.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
from __future__ import division

import errno
import os
import shutil
import stat
from sys import stderr

try:
import simplejson as json
except ImportError:
import json
import shutil
import json

from datetime import datetime
from .utils import DEFAULT_HOOK_PATH
from .exceptions import HookError
from test.basetest.exceptions import HookError


class InvalidJSON(object):
class InvalidJSON:
"""
Object representing the original unparsed JSON string and the JSON error
"""
Expand All @@ -30,15 +20,12 @@ def json_decoder(string):
Attempt to decode a JSON string and in case of error return an
InvalidJSON object
"""
decoder = json.JSONDecoder().decode

try:
return decoder(string)
except ValueError as e:
return json.loads(string)
except json.JSONDecodeError as e:
return InvalidJSON(string, str(e))


class Hooks(object):
class Hooks:
"""
Abstraction to help interact with hooks (add, remove) during tests and
keep track of which are active.
Expand Down Expand Up @@ -153,13 +140,13 @@ def clear(self):
shutil.rmtree(self.hookdir)
except OSError as e:
# If the hookdir folder doesn't exist, no harm done and keep going
if e.errno != errno.ENOENT:
if e.errno != 2: # 2 is the errno for ENOENT
raise

os.mkdir(self.hookdir)


class Hook(object):
class Hook:
"""
Represents a hook script and provides methods to enable/disable hooks
"""
Expand Down Expand Up @@ -258,14 +245,13 @@ def _check_hook_type(self):
if self.hookname.startswith(hooktype):
break
else:
stderr.write("WARNING: {0} is not a valid hook type. "
"It will not be triggered\n".format(self.hookname))
print("WARNING: {0} is not a valid hook type. It will not be triggered".format(self.hookname), file=os.sys.stderr)

def _remove_file(self, file):
try:
os.remove(file)
except OSError as e:
if e.errno == errno.ENOENT:
if e.errno == 2: # 2 is the errno for ENOENT
raise HookError("Hook with name {0} was not found on hooks/ folder".format(file))
else:
raise
Expand Down

0 comments on commit d80f1b5

Please sign in to comment.