Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gw: fix config and systemd example generation #137

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion documentation/gw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The following commands will set up a gateway instance named ``mygw`` on a Linux

# generate a simple configuration file
sudo python -m p4p.gw --example-config /etc/pvagw/mygw.conf
# generate a systemd unit file to support the gateway
# generate a systemd unit file template to support the gateway
sudo python -m p4p.gw --example-systemd \
/etc/systemd/system/[email protected]
# start the gateway
Expand Down
17 changes: 12 additions & 5 deletions src/p4p/gw.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,21 +745,28 @@ def main(args=None):
args = getargs().parse_args(args)

catfile = None
I = '%i'
conf = '/etc/pvagw/%i.conf'
if args.example_config:
catfile = 'example.conf'
I = os.path.splitext(os.path.basename(args.config))[0]
conf = os.path.abspath(args.config)
elif args.example_systemd:
catfile = '[email protected]'
inst = os.path.splitext(os.path.basename(args.config))[0].split('@')
if len(inst) == 2 and inst[1]:
I = inst[1]
conf = '/etc/pvagw/{0}.conf'.format(I)

if catfile is not None:
if args.config=='-':
O = sys.stdout
I = '%i'
conf = '/etc/pvagw/%i.conf'
print('# eg. save as /etc/systemd/system/[email protected]', file=sys.stderr)
if args.example_config:
print('# eg. save as /etc/pvagw/<instance>.conf', file=sys.stderr)
elif args.example_systemd:
print('# eg. save as /etc/systemd/system/[email protected]', file=sys.stderr)
else:
O = open(args.config, 'w')
I = os.path.splitext(os.path.basename(args.config))[0]
conf = os.path.abspath(args.config)

pythonpath=os.environ.get('PYTHONPATH','').split(os.pathsep)
modroot = os.path.dirname(os.path.dirname(__file__)) # directory containing p4p/gw.py
Expand Down
27 changes: 20 additions & 7 deletions src/p4p/test/test_gw.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,28 @@ def test_config(self):
content = F.read()
self.assertRegex(content, '"statusprefix"')

def test_systemd(self):
with NamedTemporaryFile() as F:
def test_systemd_instance(self):
with NamedTemporaryFile(suffix='@blah.service') as F:
try:
main(['--example-systemd', F.name])
except SystemExit as e:
self.assertEqual(e.code, 0)

F.seek(0)
content = F.read()
self.assertRegex(content, '-m p4p.gw /etc/pvagw/blah.conf')
self.assertRegex(content, 'multi-user.target')

def test_systemd_template(self):
with NamedTemporaryFile(suffix='@.service') as F:
try:
main(['--example-systemd', F.name])
except SystemExit as e:
self.assertEqual(e.code, 0)

F.seek(0)
content = F.read()
self.assertRegex(content, '-m p4p.gw /etc/pvagw/%i.conf')
self.assertRegex(content, 'multi-user.target')

class TestGC(RefTestCase):
Expand Down Expand Up @@ -212,7 +225,7 @@ def test_monitor(self):
Q2 = Queue(maxsize=4)

with self._ds_client.monitor('pv:ro', Q1.put, notify_disconnect=True):

self.assertIsInstance(Q1.get(timeout=self.timeout), Disconnected)
self.assertEqual(42, Q1.get(timeout=self.timeout))

Expand Down Expand Up @@ -283,7 +296,7 @@ def put(pv, op):
_log.debug("Exit setUp")

def setUpGW(self, usconfig):
cfile = self._cfile = NamedTemporaryFile('w+')
cfile = self._cfile = NamedTemporaryFile(mode='w+')
json.dump({
'version':2,
'clients':[{
Expand Down Expand Up @@ -461,7 +474,7 @@ class TestHighLevelChained(TestHighLevel):

def setUpGW(self, usconfig):
# First GW, connected to upstream server
cfile = self._cfile = NamedTemporaryFile('w+')
cfile = self._cfile = NamedTemporaryFile(mode='w+')
json.dump({
'version':2,
'clients':[{
Expand Down Expand Up @@ -497,7 +510,7 @@ def setUpGW(self, usconfig):
gw1config = self._app1.servers[u'server1_0'].conf()

# Second GW, connected to first
cfile = self._cfile = NamedTemporaryFile('w+')
cfile = self._cfile = NamedTemporaryFile(mode='w+')
json.dump({
'version':2,
'clients':[{
Expand Down Expand Up @@ -622,7 +635,7 @@ def log(self):
return self._log.read()

def write(self, content):
F = NamedTemporaryFile('w+')
F = NamedTemporaryFile(mode='w+')
self._files.append(F)
F.write(content)
F.flush()
Expand Down
9 changes: 7 additions & 2 deletions src/p4p/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,14 @@ class RegularNamedTemporaryFile(object):
"""Like tempfile.NamedTemporaryFile which doesn't use O_TEMPORARY on windows
"""
def __init__(self, *args, **kws):
fd, self.name = tempfile.mkstemp()
mkstemp_kws = {k: v for k, v in kws.items()
if k in ['suffix', 'prefix', 'dir']}
fd, self.name = tempfile.mkstemp(**mkstemp_kws)
try:
self.file = os.fdopen(fd, *args, **kws)
if 'mode' in kws:
self.file = os.fdopen(fd, kws['mode'])
else:
self.file = os.fdopen(fd)
self.read = self.file.read
self.write = self.file.write
self.flush = self.file.flush
Expand Down