Skip to content

Commit

Permalink
make python3 only and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wdpypere committed Feb 22, 2023
1 parent e772ce7 commit 75e9ec8
Show file tree
Hide file tree
Showing 25 changed files with 61 additions and 92 deletions.
1 change: 0 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ node {
sh 'git clean -fxd'
}
stage('test') {
sh 'python2.7 -V'
sh 'pip3 install --ignore-installed --prefix $PWD/.vsc-tox tox'
sh 'export PATH=$PWD/.vsc-tox/bin:$PATH && export PYTHONPATH=$PWD/.vsc-tox/lib/python$(python3 -c "import sys; print(\\"%s.%s\\" % sys.version_info[:2])")/site-packages:$PYTHONPATH && tox -v -c tox.ini'
sh 'rm -r $PWD/.vsc-tox'
Expand Down
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# vsc-utils

### Build Status
- Python 2.6 : [![Build Status](https://jenkins1.ugent.be/job/vsc-utils-python26/badge/icon)](https://jenkins1.ugent.be/job/vsc-utils-python26/)
- Python 2.7 : [![Build Status](https://jenkins1.ugent.be/job/vsc-utils-python27/badge/icon)](https://jenkins1.ugent.be/job/vsc-utils-python27/)

# Description
Common tools used within our organization.
These tools live in the vsc.utils namespace, toghether with the tools from
Expand All @@ -12,8 +8,5 @@ vsc-base.
They have been split of from vsc-base because the tools here need aditional dependencies
to work correctly whereas the tools in vsc-base do not.

Originally created by the HPC team of Ghent University (http://ugent.be/hpc).

# Documentation
https://jenkins1.ugent.be/job/vsc-utils-python26/Documentation/
Originally created by the HPC team of Ghent University (https://ugent.be/hpc).

2 changes: 1 addition & 1 deletion lib/vsc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2015-2021 Ghent University
# Copyright 2015-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2015-2021 Ghent University
# Copyright 2015-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/availability.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
37 changes: 17 additions & 20 deletions lib/vsc/utils/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down Expand Up @@ -32,10 +32,9 @@
import jsonpickle
import os
import time
import pickle

from vsc.utils import fancylogger
from vsc.utils.py2vs3 import pickle, FileNotFoundErrorExc


class FileCache(object):
"""File cache with a timestamp safety.
Expand Down Expand Up @@ -106,7 +105,7 @@ def __init__(self, filename, retain_old=True, raise_unpickable=False):
finally:
g.close()

except (OSError, IOError, ValueError, FileNotFoundErrorExc) as err:
except (OSError, IOError, ValueError, FileNotFoundError) as err:
self.log.warning("Could not access the file cache at %s [%s]", self.filename, err)
self.shelf = {}
self.log.info("Cache in %s starts with an empty shelf", (self.filename,))
Expand Down Expand Up @@ -158,19 +157,17 @@ def close(self):
dirname = os.path.dirname(self.filename)
if not os.path.exists(dirname):
os.makedirs(dirname)
f = open(self.filename, 'wb')
if not f:
self.log.error('cannot open the file cache at %s for writing', self.filename)
else:
if self.retain_old:
self.shelf.update(self.new_shelf)
self.new_shelf = self.shelf

g = gzip.GzipFile(mode='wb', fileobj=f)
pickled = jsonpickle.encode(self.new_shelf)
# .encode() is required in Python 3, since we need to pass a bytestring
g.write(pickled.encode())
g.close()
f.close()

self.log.info('closing the file cache at %s', self.filename)
with open(self.filename, 'wb') as fih:
if not fih:
self.log.error('cannot open the file cache at %s for writing', self.filename)
else:
if self.retain_old:
self.shelf.update(self.new_shelf)
self.new_shelf = self.shelf

with gzip.GzipFile(mode='wb', fileobj=fih) as zipf:
pickled = jsonpickle.encode(self.new_shelf)
# .encode() is required in Python 3, since we need to pass a bytestring
zipf.write(pickled.encode())

self.log.info('closing the file cache at %s', self.filename)
2 changes: 1 addition & 1 deletion lib/vsc/utils/fs_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/lock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
8 changes: 3 additions & 5 deletions lib/vsc/utils/nagios.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# -*- encoding: utf-8 -*-
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down Expand Up @@ -40,7 +40,6 @@
@author: Andy Georges (Ghent University)
@author: Luis Fernando Muñoz Mejías (Ghent University)
"""
from __future__ import print_function

import operator
import os
Expand All @@ -52,7 +51,6 @@

from vsc.utils.cache import FileCache
from vsc.utils.fancylogger import getLogger
from vsc.utils.py2vs3 import is_string, FileNotFoundErrorExc

log = getLogger(__name__)

Expand Down Expand Up @@ -155,7 +153,7 @@ def __init__(self, nrange):
"""
self.log = getLogger(self.__class__.__name__, fname=False)

if not is_string(nrange):
if not isinstance(nrange, str):
newnrange = str(nrange)
self.log.debug("nrange %s of type %s, converting to string (%s)", str(nrange), type(nrange), newnrange)
try:
Expand Down Expand Up @@ -316,7 +314,7 @@ def cache(self, nagios_exit, nagios_message):
else:
self.log.warn("Not running as root: Cannot chown the nagios check file %s to %s",
self.filename, self.nagios_username)
except (OSError, FileNotFoundErrorExc):
except (OSError, FileNotFoundError):
self.log.raiseException("Cannot chown the nagios check file %s to the nagios user" % (self.filename))

return True
Expand Down
15 changes: 7 additions & 8 deletions lib/vsc/utils/pickle_files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down Expand Up @@ -38,10 +38,7 @@
import logging
import os
import stat


from vsc.utils.py2vs3 import pickle, FileNotFoundErrorExc

import pickle

class TimestampPickle(object):
"""Stores a timestamp in some format in a file."""
Expand All @@ -60,8 +57,9 @@ def read(self):
"""

try:
timestamp = pickle.load(open(self.filename, "rb"))
except (IOError, OSError, FileNotFoundErrorExc):
with open (self.filename, "rb") as fih:
timestamp = pickle.load(fih)
except (IOError, OSError, FileNotFoundError):
logging.exception("Failed to load timestamp pickle from filename %s.", self.filename)
return None

Expand All @@ -78,7 +76,8 @@ def write(self, timestamp):
"""

try:
pickle.dump(timestamp, open(self.filename, "wb"))
with open(self.filename, "wb") as fih:
pickle.dump(timestamp, fih)
except Exception:
logging.exception("Failed to dump timestamp %s to pickle in filename %s", timestamp, self.filename)
raise
Expand Down
7 changes: 3 additions & 4 deletions lib/vsc/utils/rest_oauth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down Expand Up @@ -28,9 +28,8 @@
was registered with the OAuth system of the web application.
"""
import jsonpickle

from vsc.utils.py2vs3 import Request, urlencode

from urllib.parse import urlencode
from urllib.request import Request

def request_access_token(opener, path, client_id, client_secret):
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/script_tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
5 changes: 2 additions & 3 deletions lib/vsc/utils/timestamp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: latin-1 -*-
#
# Copyright 2009-2021 Ghent University
# Copyright 2009-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down Expand Up @@ -36,7 +36,6 @@

from vsc.utils.cache import FileCache
from vsc.utils.dateandtime import utc
from vsc.utils.py2vs3 import is_string

LDAP_DATETIME_TIMEFORMAT = "%Y%m%d%H%M%SZ"

Expand Down Expand Up @@ -66,7 +65,7 @@ def convert_to_datetime(timestamp=None):
if isinstance(timestamp, datetime.datetime):
if timestamp.tzinfo is None:
timestamp = timestamp.replace(tzinfo=utc)
elif is_string(timestamp):
elif isinstance(timestamp, str):
if len(timestamp) == 10:
# Unix timestamp
timestamp = datetime.datetime.fromtimestamp(int(timestamp), utc)
Expand Down
21 changes: 9 additions & 12 deletions lib/vsc/utils/timestamp_pid_lockfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down Expand Up @@ -38,7 +38,6 @@
import time

from lockfile.linklockfile import LockBase, LockFailed, NotLocked, NotMyLock
from vsc.utils.py2vs3 import FileNotFoundErrorExc, FileExistsErrorExc

class LockFileReadError(Exception):
'''Exception raised when we cannot get the expected information from the lock file.'''
Expand Down Expand Up @@ -83,7 +82,7 @@ def acquire(self, timeout=None):
try:
_write_pid_timestamp_file(self.path)
logging.info('Obtained lock on timestamped pid lockfile %s', self.path)
except (OSError, FileNotFoundErrorExc, FileExistsErrorExc) as err:
except (OSError, FileNotFoundError, FileExistsError) as err:
doraise = True
if err.errno == errno.EEXIST:
## Check if the timestamp is older than the threshold
Expand Down Expand Up @@ -125,20 +124,18 @@ def _read_pid_timestamp_file(path):
Returns (pid :: Int, timestamp :: Int).
'''
try:
pidfp = open(path, 'r')
with open(path, 'r') as pidfp:
pidLine = pidfp.readline().strip()
timestampLine = pidfp.readline().strip()
pid = int(pidLine)
timestamp = int(timestampLine)
return (pid, timestamp)

except IOError as err:
if err.errno == errno.ENOENT:
return None
else:
raise LockFileReadError('Cannot get the information from the pid file.')

pidLine = pidfp.readline().strip()
timestampLine = pidfp.readline().strip()

try:
pid = int(pidLine)
timestamp = int(timestampLine)
return (pid, timestamp)
except ValueError:
raise LockFileReadError("Contents of pid file %s invalid" % (path))

Expand Down
15 changes: 1 addition & 14 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
@author: Stijn De Weirdt (Ghent University)
@author: Andy Georges (Ghent University)
"""
import sys

import vsc.install.shared_setup as shared_setup
from vsc.install.shared_setup import ag, sdw

Expand All @@ -41,20 +39,9 @@
'netifaces',
'jsonpickle',
]
if sys.version_info < (3, 0):
# jsonpickle pulls in too new and wrong deps on CentOS 7
install_requires.extend([
'jsonpickle < 1.4.0'
])

else:
install_requires.extend([
'jsonpickle'
])


PACKAGE = {
'version': '2.1.10',
'version': '2.2.0',
'author': [ag, sdw],
'maintainer': [ag, sdw],
'excluded_pkgs_rpm': ['vsc', 'vsc.utils'], # vsc is default, vsc.utils is provided by vsc-base
Expand Down
2 changes: 1 addition & 1 deletion test/00-import.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2016-2021 Ghent University
# Copyright 2016-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
2 changes: 1 addition & 1 deletion test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2016-2021 Ghent University
# Copyright 2016-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
2 changes: 1 addition & 1 deletion test/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
4 changes: 2 additions & 2 deletions test/nagios.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down Expand Up @@ -35,12 +35,12 @@
import random
import string
from pwd import getpwuid
from io import StringIO

from vsc.install.testing import TestCase

from vsc.utils.nagios import NagiosReporter, SimpleNagios
from vsc.utils.nagios import NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN
from vsc.utils.py2vs3 import StringIO


class TestNagios(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion test/nagios_results.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8
#
# Copyright 2012-2021 Ghent University
# Copyright 2012-2023 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
Expand Down
Loading

0 comments on commit 75e9ec8

Please sign in to comment.