Replace 'tap' to 'spaces' to make gbs build succeed
[platform/upstream/hplip.git] / pkservice.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 # Author: Don Welch
21 #
22
23 __version__ = '1.0'
24 __mod__ = 'hp-pkservice'
25 __title__ = 'Policy Kit Service'
26 __doc__ = ""
27
28 # Std Lib
29 import sys
30 import getopt
31 import time
32 import os.path
33 import re
34 import os
35 import gzip
36
37 # Local
38 from base.g import *
39 from base import utils, module
40
41 USAGE = [ (__doc__, "", "name", True),
42           ("Usage: %s [MODE] [OPTIONS]" % __mod__, "", "summary", True),
43           utils.USAGE_MODE,
44           utils.USAGE_INTERACTIVE_MODE,
45           utils.USAGE_LANGUAGE,
46           utils.USAGE_OPTIONS,
47           utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
48           utils.USAGE_HELP,
49           utils.USAGE_SPACE,
50         ]
51
52 mod = module.Module(__mod__, __title__, __version__, __doc__, USAGE,
53                     (INTERACTIVE_MODE, ), run_as_root_ok=True)
54
55 mod.setUsage(module.USAGE_FLAG_NONE,
56     extra_options=[utils.USAGE_SPACE,
57     ("[OPTIONS] (General)", "", "header", False),
58     ("PolicyKit version:", "-v<version> or --version=<version>", "option", False)])
59
60 opts, device_uri, printer_name, mode, ui_toolkit, loc = \
61     mod.parseStdOpts('v:', ["version="])
62
63 user_pkit_version = None
64
65 for o, a in opts:
66     if o in ('-v', '--version'):
67         try:
68             user_pkit_version = int(a)
69         except:
70             log.error("-v or --version require an integer argument")
71             sys.exit(1)
72         if user_pkit_version < 0 or user_pkit_version > 1:
73             log.error("invalid PolicyKit version...use 0 or 1")
74             sys.exit(1)
75
76 PKIT = utils.to_bool(sys_conf.get('configure', 'policy-kit'))
77 if PKIT:
78     try:
79         from base.pkit import *
80         pkit_version = policykit_version()
81         if not user_pkit_version is None:
82             pkit_version = user_pkit_version
83         try:
84             from dbus.mainloop.glib import DBusGMainLoop
85         except ImportError:
86             log.error("PolicyKit requires dbus")
87             sys.exit(1)
88     except:
89         log.error("Unable to load pkit...is HPLIP installed?")
90         sys.exit(1)
91 else:
92     log.error("PolicyKit support not installed")
93     sys.exit(1)
94
95 DBusGMainLoop(set_as_default=True)
96
97 if not os.geteuid() == 0:
98     log.error("You must be root to run this utility.")
99     sys.exit(1)
100
101 log.debug("using PolicyKit version %d" % pkit_version)
102
103 try:
104     BackendService().run(pkit_version)
105 except dbus.DBusException, ex:
106     log.error("Unable to start service (%s)" % ex)