Skip to content

Commit

Permalink
Merge pull request #157 from wwade/plug-workspace-project
Browse files Browse the repository at this point in the history
plugins: add plugin hook for workspaceProject
  • Loading branch information
wwade authored Feb 4, 2022
2 parents 09a4162 + 7c8cac0 commit ff1f4f1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Create new virtualenv and install an editable version of ``jobrunner``:
.. code:: console
pipenv --three install --dev
pipenv run install -e .
pipenv run pip install -e .
Autoformat the code and check linters:

Expand Down
3 changes: 2 additions & 1 deletion jobrunner/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
unlocked,
utcNow,
workspaceIdentity,
workspaceProject,
)

LOG = getLogger(__name__)
Expand Down Expand Up @@ -64,7 +65,7 @@ def __init__(self, uidx, key=None):
self._user = os.getenv('USER')
self._env = dict(os.environ)
self._workspace = workspaceIdentity()
self._proj = os.getenv('WP')
self._proj = workspaceProject()
self._rc = None
self.logfile = None
self._key = key
Expand Down
15 changes: 13 additions & 2 deletions jobrunner/plugins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import absolute_import, division, print_function

import importlib
import pkgutil
import socket
from typing import Tuple
import warnings

import jobrunner.plugin
Expand Down Expand Up @@ -46,3 +45,15 @@ def workspaceIdentity(self):
if ret:
return ret
return socket.gethostname()

def workspaceProject(self) -> Tuple[str, bool]:
"""
If the current context has a notion of a "project name", return the project
name as well as a bool True to indicate that the plugin is authoritative.
"""
for plugin in self.plugins:
if hasattr(plugin, 'workspaceProject'):
ret, ok = plugin.workspaceProject()
if ok:
return ret, ok
return "", False
1 change: 1 addition & 0 deletions jobrunner/test/info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def setJobEnv(jobInfo, newEnv):
def newJob(uidx, cmd, workspaceIdentity="WS"):
mockPlug = mock.MagicMock(plugins.Plugins)
mockPlug.workspaceIdentity.return_value = workspaceIdentity
mockPlug.workspaceProject.return_value = ("myProject", True)
utils.MOD_STATE.plugins = mockPlug
parent = mock.MagicMock(db.JobsBase)
parent.inactive = mock.MagicMock(db.DatabaseBase)
Expand Down
10 changes: 8 additions & 2 deletions jobrunner/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import collections
from contextlib import contextmanager
import datetime
Expand All @@ -12,6 +10,7 @@
import sys
import tempfile
import time
from typing import Optional

import chardet
import dateutil.tz
Expand Down Expand Up @@ -178,6 +177,13 @@ def workspaceIdentity():
return MOD_STATE.plugins.workspaceIdentity()


def workspaceProject() -> Optional[str]:
proj, ok = MOD_STATE.plugins.workspaceProject()
if ok:
return proj
return os.getenv("WP")


def _getAllowed():
allowed = (list(range(ord('a'), ord('z') + 1)) +
list(range(ord('A'), ord('Z') + 1)) +
Expand Down

0 comments on commit ff1f4f1

Please sign in to comment.