migrate-dbus-scripts.py
[dbuscron] / migrate-dbus-scripts.py
1 #!/usr/bin/python
2 from __future__ import with_statement
3 import os, sys
4
5 if '-h' in sys.argv or '--help' in sys.argv:
6     print '''
7 %(name)s [ dbus-scripts-dir ] [ dbuscrontab-dir ]
8
9     Convert all files from dbus-scripts-dir from
10     dbus-scripts config format to dbuscron config
11     format and put them under the same names into
12     dbuscrontab-dir.
13
14     If omitted, dbus-scripts-dir defaults to
15     `/etc/dbus-scripts.d', dbuscrontab-dir
16     defaults to `/etc/dbuscrontab.d'.
17 ''' % dict(name=sys.argv[0])
18     os._exit(0)
19
20 try:
21     dbus_scripts_dir = sys.argv[1]
22 except IndexError:
23     dbus_scripts_dir = '/etc/dbus-scripts.d'
24
25 try:
26     dbuscron_dir = sys.argv[2]
27 except IndexError:
28     dbuscron_dir = '/etc/dbuscrontab.d'
29
30 for fn in os.listdir(dbus_scripts_dir):
31     fnam = os.path.join(dbus_scripts_dir, fn)
32     if not os.path.isfile(fnam):
33         continue
34
35     fout = os.path.join(dbuscron_dir, fn)
36
37     with open(fnam, 'rb') as f:
38         with open(fout, 'wb') as o:
39             for line in f:
40                 line = line.strip()
41                 if not line or line.startswith('#'):
42                     continue
43
44                 cmd, src, dest, iface, meth, args = line.split(' ', 5)
45                 args = args.replace(' ',';')
46                 opts = dict(
47                         cmd=cmd,
48                         src=src,
49                         dest=dest,
50                         iface=iface,
51                         meth=meth,
52                         args=args)
53                 # bus type sender interface path member destination args command
54                 print >> o, 'S signal,method_call %(src)s %(iface)s * %(meth)s %(dest)s %(args)s %(cmd)s' % opts
55