Commit 9e19097c authored by Jelmer Vernooij's avatar Jelmer Vernooij

Add support for systemd-service-file-pidfile-refers-to-var-run.

Closes: #917565
parent 74b2e304
......@@ -2,6 +2,8 @@ lintian-brush (0.10) UNRELEASED; urgency=medium
* Add a (inefficient) --dry-run option. Closes: #915977
* Add --update-changelog option to force updating of changelog.
* Add support for systemd-service-file-pidfile-refers-to-var-run.
Closes: #917565
-- Jelmer Vernooij <jelmer@debian.org> Sat, 15 Dec 2018 17:18:50 +0000
......
......@@ -73,6 +73,9 @@ Lintian-Tags: public-upstream-key-not-minimal
Fix-Script: quilt-series-without-trailing-newline.sh
Lintian-Tags: quilt-series-without-trailing-newline
Fix-Script: systemd-service-file-pidfile-refers-to-var-run.py
Lintian-Tags: systemd-service-file-pidfile-refers-to-var-run
Fix-Script: transitional-package-should-be-oldlibs-optional.py
Lintian-Tags: transitional-package-should-be-oldlibs-optional
......
#!/usr/bin/python3
from lintian_brush.systemd import update_service
def replace_var_run(section, name, value):
if section == b"Service" and name == b"PIDFile":
return value.replace(b"/var/run/", b"/run/")
return value
update_service(replace_var_run)
print("Replace /var/run with /run for the Service PIDFile.")
print("Fixed-Lintian-Tags: systemd-service-file-pidfile-refers-to-var-run")
#!/usr/bin/python3
# Copyright (C) 2018 Jelmer Vernooij
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Utility functions for dealing with systemd files."""
import os
import re
def systemd_service_files(path='debian'):
"""List paths to systemd service files."""
for name in os.listdir(path):
if name.endswith('.service'):
yield os.path.join(path, name)
def update_service_file(path, cb):
"""Update a service file.
Args:
path: Path to the service file
cb: Callback called with a config.ConfigObj object
"""
lines = []
changed = False
section = None
with open(path, 'rb') as f:
for l in f.readlines():
g = re.match(b'^\\[(.*)\\]$', l)
if g:
section = g.group(1)
lines.append(l)
continue
g = re.match(b'^([A-Za-z0-9]+)=(.*)$', l)
if g:
name = g.group(1)
oldval = g.group(2)
newval = cb(section, name, oldval)
if oldval != newval:
changed = True
lines.append(name+b'='+newval+b'\n')
continue
lines.append(l)
if not changed:
return
with open(path, 'wb') as f:
f.writelines(lines)
def update_service(cb):
"""Update all service files.
Args:
cb: Callback called with a config.ConfigObj object
"""
for path in systemd_service_files():
update_service_file(path, cb)
......@@ -22,6 +22,7 @@ def test_suite():
names = [
'control',
'run',
'systemd',
]
module_names = [__name__ + '.test_' + name for name in names]
module_names.append(__name__ + ".fixers.test_suite")
......
#!/usr/bin/python
# Copyright (C) 2018 Jelmer Vernooij
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for lintian_brush.systemd."""
from breezy.tests import (
TestCaseWithTransport,
)
from lintian_brush.systemd import (
systemd_service_files,
update_service_file,
update_service,
)
class UpdateServiceFilesTests(TestCaseWithTransport):
def setUp(self):
super(UpdateServiceFilesTests, self).setUp()
self.build_tree_contents([
('debian/', ),
('debian/other', ''),
('debian/apcupsd.service', """\
[Unit]
# network-online is really needed, otherwise there are problems with snmp
# -> 865620
After=network-online.target
Description=UPS power management daemon
Documentation=man:apcupsd(8)
""")])
def test_systemd_service_files(self):
self.assertEqual(
['debian/apcupsd.service'],
list(systemd_service_files('debian')))
def test_update_service_file(self):
def cb(section, name, value):
if section == b"Unit" and name == b"Description":
return b"Not really a UPS pmd"
return value
update_service_file('debian/apcupsd.service', cb)
self.assertFileEqual("""\
[Unit]
# network-online is really needed, otherwise there are problems with snmp
# -> 865620
After=network-online.target
Description=Not really a UPS pmd
Documentation=man:apcupsd(8)
""", 'debian/apcupsd.service')
def test_update_service(self):
def cb(section, name, value):
if section == b"Unit" and name == b"Description":
return b"Not really a UPS pmd"
return value
update_service(cb)
self.assertFileEqual("""\
[Unit]
# network-online is really needed, otherwise there are problems with snmp
# -> 865620
After=network-online.target
Description=Not really a UPS pmd
Documentation=man:apcupsd(8)
""", 'debian/apcupsd.service')
[Unit]
# network-online is really needed, otherwise there are problems with snmp
# -> 865620
After=network-online.target
Description=UPS power management daemon
Documentation=man:apcupsd(8)
[Service]
ExecStartPre=/lib/apcupsd/prestart
ExecStart=/sbin/apcupsd
Type=forking
KillMode=process
PIDFile=/var/run/apcupsd.pid
[Install]
WantedBy=multi-user.target
Replace /var/run with /run for the Service PIDFile.
Fixed-Lintian-Tags: systemd-service-file-pidfile-refers-to-var-run
[Unit]
# network-online is really needed, otherwise there are problems with snmp
# -> 865620
After=network-online.target
Description=UPS power management daemon
Documentation=man:apcupsd(8)
[Service]
ExecStartPre=/lib/apcupsd/prestart
ExecStart=/sbin/apcupsd
Type=forking
KillMode=process
PIDFile=/run/apcupsd.pid
[Install]
WantedBy=multi-user.target
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment