2007-04-10 04:47:37 +02:00
|
|
|
"""Various utility functions"""
|
2007-06-24 14:43:48 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2006-10-15 01:08:44 +02:00
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library 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
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2006-06-13 21:18:29 +02:00
|
|
|
import time
|
|
|
|
import sha
|
|
|
|
import random
|
|
|
|
import binascii
|
2006-06-15 17:18:33 +02:00
|
|
|
import string
|
2006-12-01 23:23:58 +01:00
|
|
|
import os
|
|
|
|
from ConfigParser import ConfigParser
|
|
|
|
from ConfigParser import NoOptionError
|
2006-08-17 10:32:59 +02:00
|
|
|
|
2006-09-25 15:45:24 +02:00
|
|
|
def printable_hash(in_hash):
|
2006-12-04 20:12:24 +01:00
|
|
|
"""Convert binary hash data into printable characters."""
|
|
|
|
printable = ""
|
|
|
|
for char in in_hash:
|
|
|
|
printable = printable + binascii.b2a_hex(char)
|
|
|
|
return printable
|
2006-06-13 21:18:29 +02:00
|
|
|
|
|
|
|
def _sha_data(data):
|
2006-12-04 20:12:24 +01:00
|
|
|
"""sha1 hash some bytes."""
|
|
|
|
sha_hash = sha.new()
|
|
|
|
sha_hash.update(data)
|
|
|
|
return sha_hash.digest()
|
2006-06-13 21:18:29 +02:00
|
|
|
|
|
|
|
def unique_id(data = ''):
|
2007-04-10 04:47:37 +02:00
|
|
|
"""Generate a likely-unique ID for whatever purpose
|
|
|
|
|
|
|
|
data -- suffix appended to working data before hashing
|
|
|
|
|
|
|
|
Returns a 40-character string with hexidecimal digits
|
|
|
|
representing an SHA hash of the time, a random digit
|
|
|
|
within a constrained range and the data passed.
|
|
|
|
|
|
|
|
Note: these are *not* crypotographically secure or
|
|
|
|
globally unique identifiers. While they are likely
|
|
|
|
to be unique-enough, no attempt is made to make
|
|
|
|
perfectly unique values.
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
data_string = "%s%s%s" % (time.time(), random.randint(10000, 100000), data)
|
|
|
|
return printable_hash(_sha_data(data_string))
|
2006-06-14 20:42:44 +02:00
|
|
|
|
|
|
|
|
2006-06-22 20:05:38 +02:00
|
|
|
ACTIVITY_ID_LEN = 40
|
2006-06-14 20:42:44 +02:00
|
|
|
|
|
|
|
def is_hex(s):
|
2006-12-04 20:12:24 +01:00
|
|
|
return s.strip(string.hexdigits) == ''
|
2006-06-14 20:42:44 +02:00
|
|
|
|
2006-06-22 20:05:38 +02:00
|
|
|
def validate_activity_id(actid):
|
2006-12-04 20:12:24 +01:00
|
|
|
"""Validate an activity ID."""
|
2007-04-10 04:47:37 +02:00
|
|
|
if not isinstance(actid, (str,unicode)):
|
2006-12-04 20:12:24 +01:00
|
|
|
return False
|
|
|
|
if len(actid) != ACTIVITY_ID_LEN:
|
|
|
|
return False
|
|
|
|
if not is_hex(actid):
|
|
|
|
return False
|
|
|
|
return True
|
2006-06-14 20:42:44 +02:00
|
|
|
|
2006-12-01 23:23:58 +01:00
|
|
|
class _ServiceParser(ConfigParser):
|
2006-12-04 20:12:24 +01:00
|
|
|
def optionxform(self, option):
|
|
|
|
return option
|
2006-12-01 23:23:58 +01:00
|
|
|
|
|
|
|
def write_service(name, bin, path):
|
2007-04-10 04:47:37 +02:00
|
|
|
"""Write a D-BUS service definition file
|
|
|
|
|
|
|
|
These are written by the bundleregistry when
|
|
|
|
a new activity is registered. They bind a
|
|
|
|
D-BUS bus-name with an executable which is
|
|
|
|
to provide the named service.
|
|
|
|
|
|
|
|
name -- D-BUS service name, must be a valid
|
|
|
|
filename/D-BUS name
|
|
|
|
bin -- executable providing named service
|
|
|
|
path -- directory into which to write the
|
|
|
|
name.service file
|
|
|
|
|
|
|
|
The service files themselves are written using
|
|
|
|
the _ServiceParser class, which is a subclass
|
|
|
|
of the standard ConfigParser class.
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
service_cp = _ServiceParser()
|
|
|
|
section = 'D-BUS Service'
|
|
|
|
service_cp.add_section(section)
|
|
|
|
service_cp.set(section, 'Name', name)
|
|
|
|
service_cp.set(section, 'Exec', bin)
|
2006-12-01 23:23:58 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
dest_filename = os.path.join(path, name + '.service')
|
|
|
|
fileobject = open(dest_filename, 'w')
|
|
|
|
service_cp.write(fileobject)
|
|
|
|
fileobject.close()
|