diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 3407bf50..ccc78cb5 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -9,7 +9,7 @@ jobs: matrix: python-version: ['2.x', '3.x'] name: Python ${{ matrix.python-version }} test - steps: + steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 @@ -20,7 +20,7 @@ jobs: - name: Base requirements for SSM run: pip install -r requirements.txt - name: Additional requirements for the unit and coverage tests - run: pip install -r requirements-test.txt + run: pip install -r requirements-test.txt - name: Pre-test set up run: | export TMPDIR=$PWD/tmp @@ -28,6 +28,6 @@ jobs: export PYTHONPATH=$PYTHONPATH:`pwd -P` cd test - name: Run unit tests - run: coverage run --branch --source=ssm,bin -m unittest discover --buffer + run: coverage run --branch --source=ssm,bin -m unittest discover --buffer - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..f80092a2 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,29 @@ +# See https://pre-commit.com for more information +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.5.0 + hooks: + # Python related checks + - id: check-ast + - id: check-builtin-literals + - id: check-docstring-first + - id: name-tests-test + name: Check unit tests start with 'test_' + args: ['--django'] + files: 'test/.*' + # Other checks + - id: check-added-large-files + - id: check-merge-conflict + - id: check-yaml + - id: end-of-file-fixer + - id: mixed-line-ending + name: Force line endings to LF + args: ['--fix=lf'] + - id: trailing-whitespace + +- repo: https://github.com/pre-commit/pygrep-hooks + rev: v1.9.0 + hooks: + - id: python-check-mock-methods + - id: python-no-eval + - id: python-no-log-warn diff --git a/LICENSE b/LICENSE index 8a8a912f..9c4d731a 100644 --- a/LICENSE +++ b/LICENSE @@ -138,4 +138,4 @@ if You agree to indemnify, defend, and hold each Contributor harmless for any li incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. -END OF TERMS AND CONDITIONS \ No newline at end of file +END OF TERMS AND CONDITIONS diff --git a/bin/apel-ssm b/bin/apel-ssm index 16e48a85..5b1e8cc1 100755 --- a/bin/apel-ssm +++ b/bin/apel-ssm @@ -74,4 +74,3 @@ case "$1" in esac exit $RETVAL - diff --git a/requirements-test.txt b/requirements-test.txt index 10f23ea6..528e7127 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -3,3 +3,4 @@ coveralls<=1.2.0 mock codecov +pre-commit diff --git a/ssm/agents.py b/ssm/agents.py index 5c1b1d90..aaef6f1c 100644 --- a/ssm/agents.py +++ b/ssm/agents.py @@ -79,7 +79,7 @@ def get_protocol(cp, log): # If the newer configuration setting 'protocol' is not set, use 'STOMP' # for backwards compatability. protocol = Ssm2.STOMP_MESSAGING - log.warn("No option set for 'protocol'. Defaulting to %s.", protocol) + log.warning("No option set for 'protocol'. Defaulting to %s.", protocol) except ValueError: log.critical("Invalid protocol '%s' set. Must be either '%s' or '%s'.", protocol, Ssm2.STOMP_MESSAGING, Ssm2.AMS_MESSAGING) @@ -315,7 +315,7 @@ def run_receiver(protocol, brokers, project, token, cp, log, dn_file): ssm.send_ping() except (NotConnectedException, AmsConnectionException) as error: - log.warn('Connection lost.') + log.warning('Connection lost.') log.debug(error) ssm.shutdown() dc.close() @@ -355,7 +355,7 @@ def get_dns(dn_file, log): elif line.strip().startswith('/'): dns.append(line.strip()) else: - log.warn('DN in incorrect format: %s', line) + log.warning('DN in incorrect format: %s', line) finally: if f is not None: f.close() diff --git a/ssm/ssm2.py b/ssm/ssm2.py index 735056b7..8f0a6ed4 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -289,7 +289,7 @@ def _handle_msg(self, text): """ if text is None or text == '': warning = 'Empty text passed to _handle_msg.' - log.warn(warning) + log.warning(warning) return None, None, warning # if not text.startswith('MIME-Version: 1.0'): # raise Ssm2Exception('Not a valid message.') @@ -313,7 +313,7 @@ def _handle_msg(self, text): if signer not in self._valid_dns: warning = 'Signer not in valid DNs list: %s' % signer - log.warn(warning) + log.warning(warning) return None, signer, warning else: log.info('Valid signer: %s', signer) @@ -335,7 +335,7 @@ def _save_msg_to_queue(self, body, empaid): # allows the msg to be reloaded if needed. body = extracted_msg - log.warn("Message rejected: %s", err_msg) + log.warning("Message rejected: %s", err_msg) name = self._rejectq.add({'body': body, 'signer': signer, @@ -474,7 +474,7 @@ def send_all(self): log.info('Found %s messages.', self._outq.count()) for msgid in self._outq: if not self._outq.lock(msgid): - log.warn('Message was locked. %s will not be sent.', msgid) + log.warning('Message was locked. %s will not be sent.', msgid) continue text = self._outq.get(msgid) @@ -514,7 +514,7 @@ def send_all(self): # Remove empty dirs and unlock msgs older than 5 min (default) self._outq.purge() except OSError as e: - log.warn('OSError raised while purging message queue: %s', e) + log.warning('OSError raised while purging message queue: %s', e) ########################################################################### # Connection handling methods @@ -563,9 +563,9 @@ def handle_connect(self): break except ConnectFailedException as e: # ConnectFailedException doesn't provide a message. - log.warn('Failed to connect to %s:%s.', host, port) + log.warning('Failed to connect to %s:%s.', host, port) except Ssm2Exception as e: - log.warn('Failed to connect to %s:%s: %s', host, port, e) + log.warning('Failed to connect to %s:%s: %s', host, port, e) if not self.connected: raise Ssm2Exception('Attempts to start the SSM failed. The system will exit.') @@ -660,7 +660,7 @@ def startup(self): f.write('\n') f.close() except IOError as e: - log.warn('Failed to create pidfile %s: %s', self._pidfile, e) + log.warning('Failed to create pidfile %s: %s', self._pidfile, e) self.handle_connect() @@ -672,7 +672,7 @@ def shutdown(self): if os.path.exists(self._pidfile): os.remove(self._pidfile) else: - log.warn('pidfile %s not found.', self._pidfile) + log.warning('pidfile %s not found.', self._pidfile) except IOError as e: - log.warn('Failed to remove pidfile %s: %e', self._pidfile, e) - log.warn('SSM may not start again until it is removed.') + log.warning('Failed to remove pidfile %s: %e', self._pidfile, e) + log.warning('SSM may not start again until it is removed.') diff --git a/test/test_agents.py b/test/test_agents.py index 1db8d01c..6837b1d1 100644 --- a/test/test_agents.py +++ b/test/test_agents.py @@ -58,7 +58,7 @@ def test_get_iffy_dns(self): f.write(dn_text) f.close() ssm.agents.get_dns(self.tf_path, self.mock_log) - self.assertEqual(self.mock_log.warn.call_count, 2) + self.assertEqual(self.mock_log.warning.call_count, 2) def tearDown(self): os.remove(self.tf_path)