6
from gettext import gettext as _
8
from twisted.internet import defer
9
from twisted.internet import protocol
10
from twisted.internet import reactor
11
from twisted.python import log
13
import accomplishments
14
from accomplishments import config
15
from accomplishments import exceptions
18
gettext.textdomain('accomplishments-daemon')
22
return config.__version__
25
class NullHandler(logging.Handler):
28
def emit(self, record):
32
def set_up_logging(opts):
33
# add a handler to prevent basicConfig
34
root = logging.getLogger()
35
null_handler = NullHandler()
36
root.addHandler(null_handler)
38
formatter = logging.Formatter(
39
"%(levelname)s:%(name)s: %(funcName)s() '%(message)s'")
41
logger = logging.getLogger('accomplishments-daemon')
42
logger_sh = logging.StreamHandler()
43
logger_sh.setFormatter(formatter)
44
logger.addHandler(logger_sh)
46
# Set the logging level to show debug messages.
48
logger.setLevel(logging.DEBUG)
49
logger.debug('logging enabled')
51
logger.setLevel(logging.DEBUG)
55
"""Retrieve accomplishments-daemon data path
57
This path is by default <accomplishments_daemon_lib_path>/../data/ in trunk
58
and /usr/share/ubuntu-accomplishments-daemon in an installed version but this path
59
is specified at installation time.
62
# Get pathname absolute or relative.
64
os.path.dirname(accomplishments.__path__[0]),
65
config.__ubuntu_accomplishments_daemon_data_directory__)
66
abs_data_path = os.path.abspath(path)
67
if not os.path.exists(abs_data_path):
68
msg = "Could not find the project data directory."
69
raise exceptions.PathNotFound(msg)
73
def get_data_file(*path_segments):
74
"""Get the full path to a data file.
76
Returns the path to a file underneath the data directory (as defined by
77
`get_data_path`). Equivalent to os.path.join(get_data_path(),
80
return os.path.join(get_data_path(), *path_segments)
84
"""Support for command line options"""
85
parser = optparse.OptionParser(version="%%prog %s" % get_version())
87
"-v", "--verbose", action="count", dest="verbose",
88
help=_("Show debug messages (-vv debugs accomplishments_daemon_lib also)"))
90
"-c", "--clear-trophies", action="count", dest="clear",
91
help=_("Clear your trophies collection"))
92
(options, args) = parser.parse_args()
96
class SubprocessReturnCodeProtocol(protocol.ProcessProtocol):
99
def __init__(self, command=""):
100
self.command = command
102
def connectionMade(self):
103
self.returnCodeDeferred = defer.Deferred()
105
def processEnded(self, reason):
106
self.returnCodeDeferred.callback(reason.value.exitCode)
108
def outReceived(self, data):
109
log.msg("Got process results: %s" % data)
111
def errReceived(self, data):
112
log.err("Got non-zero exit code for process: %s" % (
113
" ".join(self.command),))
117
def import_gpg_key(pub_key):
120
cmd = ["gpg", "--import", pub_key]
121
gpg = SubprocessReturnCodeProtocol(cmd)
122
gpg.deferred = defer.Deferred()
123
process = reactor.spawnProcess(gpg, cmd[0], cmd, env=None)