chiark / gitweb /
New project: amalgamate /etc/services from little bits.
authormdw <mdw>
Fri, 29 Apr 2005 20:22:56 +0000 (20:22 +0000)
committermdw <mdw>
Fri, 29 Apr 2005 20:22:56 +0000 (20:22 +0000)
debian/README [new file with mode: 0644]
debian/changelog [new file with mode: 0644]
debian/control [new file with mode: 0644]
debian/copyright [new file with mode: 0644]
debian/postinst [new file with mode: 0755]
debian/prerm [new file with mode: 0755]
debian/rules [new file with mode: 0755]
merge-services [new file with mode: 0755]
merge-services.1 [new file with mode: 0644]
update-services [new file with mode: 0755]
update-services.8 [new file with mode: 0644]

diff --git a/debian/README b/debian/README
new file mode 100644 (file)
index 0000000..c0bd13c
--- /dev/null
@@ -0,0 +1,5 @@
+This is a quick hack because I'm fed up of not having all the entries I
+want in my /etc/services file.  Now I can drop my local mods in
+/etc/services.d and run update-services when they change.  Easy.
+
+-- [mdw]
diff --git a/debian/changelog b/debian/changelog
new file mode 100644 (file)
index 0000000..e65de0b
--- /dev/null
@@ -0,0 +1,6 @@
+services (1.0.0) unstable; urgency=low
+
+  * Shiny and new!
+
+ -- Mark Wooding <mdw@nsict.org>  Fri, 29 Apr 2005 20:26:47 +0100
+
diff --git a/debian/control b/debian/control
new file mode 100644 (file)
index 0000000..1d45814
--- /dev/null
@@ -0,0 +1,11 @@
+Source: services
+Section: net
+Priority: extra
+Build-Depends: python (>= 2.3), debhelper (>= 4.2.32)
+Maintainer: Mark Wooding <mdw@nsict.org>
+Standards-Version: 3.1.1
+
+Package: services
+Architecture: all
+Depends: ${python:Depends}
+Description: Amalgamate services tables into one big /etc/services file.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644 (file)
index 0000000..4806b54
--- /dev/null
@@ -0,0 +1,15 @@
+This package is copyright (c) 2003 Mark Wooding.
+
+This package is free software; you can redistribute it and/or modify it under
+the terms of the GNU Library General Public License as published by the
+Free Software Foundation; either version 2 of the License, or (at your
+option) any later version.
+
+This package 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 Library General Public License for
+more details.
+
+You should have a copy of the GNU Library General Public License in
+/usr/share/common-licenses/LGPL-2; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
diff --git a/debian/postinst b/debian/postinst
new file mode 100755 (executable)
index 0000000..b82ebd7
--- /dev/null
@@ -0,0 +1,11 @@
+#! /bin/sh -e
+
+dpkg-divert --add \
+  --package services \
+  --rename \
+  --divert /etc/services.d/netbase \
+  /etc/services
+
+/usr/sbin/update-services
+
+#DEBHELPER#
diff --git a/debian/prerm b/debian/prerm
new file mode 100755 (executable)
index 0000000..7cf12c4
--- /dev/null
@@ -0,0 +1,7 @@
+#! /bin/sh -e
+
+rm -f /etc/services
+dpkg-divert --remove \
+  --package services \
+  --divert /etc/services.d/netbase \
+  /etc/services
diff --git a/debian/rules b/debian/rules
new file mode 100755 (executable)
index 0000000..790b521
--- /dev/null
@@ -0,0 +1,53 @@
+#! /usr/bin/make -f
+
+export DH_COMPAT = 4
+
+build:
+       touch build
+
+clean:
+       dh_clean
+       rm -f build
+
+install: build
+       dh_clean
+       mkdir -p debian/services/etc/services.d 
+       mkdir -p debian/services/usr/bin
+       mkdir -p debian/services/usr/share/man/man1
+       mkdir -p debian/services/usr/share/man/man8
+       mkdir -p debian/services/usr/sbin
+       mkdir -p debian/services/usr/share/doc/services
+       install -m755 merge-services debian/services/usr/bin
+       install -m755 update-services debian/services/usr/sbin
+       install -m644 merge-services.1 debian/services/usr/share/man/man1
+       install -m644 update-services.8 debian/services/usr/share/man/man8
+       install -m644 README /usr/share/doc/services
+
+binary-indep:
+       dh_testdir -i
+       dh_testroot -i
+       dh_compress -i
+       dh_installdocs -i
+       dh_python -i
+       dh_fixperms -i
+       dh_gencontrol -i
+       dh_installdeb -i
+       dh_md5sums -i
+       dh_builddeb -i
+binary-arch:
+
+binary: binary-indep binary-arch
+
+source:
+       rm -rf =deb=
+       pkg=`sed '1s!^\([^ ]*\) .*$$!\1!;2q' debian/changelog`; \
+       ver=`sed '1s!^.* (\([0-9.]*\)) .*$$!\1!;2q' debian/changelog`; \
+       mkdir -p =deb=/$$pkg-$$ver/debian
+       cp update-services update-services.8 merge-services \
+         merge-services.1 =deb=/*/
+       cp debian/README debian/changelog debian/control debian/copyright \
+         debian/postinst debian/prerm debian/rules =deb=/*/debian
+       d=`pwd`; cd ..; dpkg-source -i -b $$d/=deb=/*
+       rm -rf =deb=
+
+.PHONY: binary binary-arch binary-indep install clean
diff --git a/merge-services b/merge-services
new file mode 100755 (executable)
index 0000000..2929bc8
--- /dev/null
@@ -0,0 +1,94 @@
+#! /usr/bin/python
+
+import sre as re
+import os
+from sys import argv, stderr
+
+class struct (object):
+  def __init__(me, **kw):
+    me.__dict__.update(kw)
+  def __repr__(me):
+    r = '%s(' % me.__class__.__name__
+    sep = ''
+    for k in me.__dict__:
+      r += sep + '%s=%r' % (k, me.__dict__[k])
+      sep = ', '
+    r += ')'
+    return r
+
+class service (struct):
+  def __str__(me):
+    return '%(name)s:%(port)d/%(proto)s' % me.__dict__
+  def key(me):
+    return me.port, me.proto, me.name
+  def parse(string):
+    m = re.match(r'''^ (\S+) \s+ (\d+)/(\S+) \s*
+                     ([^#\s] [^#]* [^#\s] | [^#\s])? \s*
+                     (?: \# \s* (\S .* \S | \S |) )? \s* $''',
+                 string, re.VERBOSE)
+    if not m:
+      raise 'Bad service line %r' % string
+    me = service(name = m.group(1),
+                 port = int(m.group(2)),
+                 proto = m.group(3),
+                 aliases = m.group(4) and m.group(4).split() or [],
+                 comment = m.group(5))
+    return me
+  parse = staticmethod(parse)
+
+class servicetab (object):
+  def __init__(me):
+    me.tab = {}
+  def _insert(me, serv):
+    tab = me.tab
+    changep = False
+    if serv.key() not in tab:
+      tab[serv.key()] = serv
+      changep = True
+    else:
+      s = tab[serv.key()]
+      d = {}
+      for a in s.aliases:
+        d[a] = 1
+      #warnp = True
+      dd = {}
+      for a in serv.aliases:
+        dd[a] = 1
+        if a not in d:
+          #if warnp:
+          #  print >>stderr, 'Merging aliases for %s' % s
+          #  warnp = False
+          s.aliases += [a]
+          changep = True
+      #for a in s.aliases:
+      #  if a not in dd:
+      #    if warnp:
+      #      print >>stderr, 'Merging aliases for %s' % s
+      #      warnp = False
+    return changep
+  def scan(me, file):
+    changep = False
+    for l in open(file):
+      if re.match(r'^\s*(\#.*)?$', l):
+        continue
+      serv = service.parse(l)
+      if me._insert(serv):
+        changep = True
+    if not changep:
+      print >>stderr, 'File `%s\' redundant' % file
+    return changep
+  def write(me):
+    kk = me.tab.values()
+    kk.sort(lambda x, y: cmp(x.key(), y.key()))
+    print '## services file [generated]'
+    print
+    for s in kk:
+      print '%-20s %9s %s' % (s.name,
+                              '%d/%s' % (s.port, s.proto),
+                              ' '.join(s.aliases))
+
+if 'running_under_emacs_p' not in globals():
+  t = servicetab()
+  for f in argv[1:]:
+    t.scan(f)
+    t.write()
diff --git a/merge-services.1 b/merge-services.1
new file mode 100644 (file)
index 0000000..b3a2c97
--- /dev/null
@@ -0,0 +1,31 @@
+.TH merge-services 1 "29 April 2005"
+.SH NAME
+merge-services \- merge entries from several /etc/services files
+.SH SYNOPSIS
+.B merge-services
+.I file
+\&...
+.SH DESCRIPTION
+The
+.B merge-services
+command reads the services files given on the command line and writes a
+merged file to its standard output.
+.PP
+A services entry consists of five parts: a
+.IR name ,
+a
+.IR "port number" ,
+a
+.IR protocol ,
+a list of
+.IR aliases ,
+and a
+.IR comment .
+The merging process discards all comments.  Two entries match if their
+names, ports and protocols are all equal: in this case, the output
+contains a single entry listing the union of both sets of aliases.  In
+all other cases, entries are left alone.
+.SH BUGS
+It's very stupid.
+.SH AUTHOR
+Mark Wooding, <mdw@nsict.org>
diff --git a/update-services b/update-services
new file mode 100755 (executable)
index 0000000..54964c5
--- /dev/null
@@ -0,0 +1,5 @@
+#! /bin/sh
+
+set -e
+merge-services /etc/services.d/*[!#~] "$@" >/etc/services.new
+mv /etc/services.new /etc/services
diff --git a/update-services.8 b/update-services.8
new file mode 100644 (file)
index 0000000..fc9ce44
--- /dev/null
@@ -0,0 +1,24 @@
+.TH update-services 8 "29 April 2005"
+.SH NAME
+update-services \- update the /etc/services file
+.SH SYNOPSIS
+.B update-services
+.IR [ file
+\&...]
+.SH DESCRIPTION
+The
+.B update-services
+merges the contents of the files in 
+.B /etc/services.d
+and any given on the command line, and writes the result to
+.BR /etc/services .
+.PP
+To save Emacs users from going mental, it ignores files whose names end
+in
+.RB ` # '
+or
+.RB ` ~ '.
+.SH SEE ALSO
+.BR merge-services (1).
+.SH AUTHOR
+Mark Wooding, <mdw@nsict.org>