Permalink
Browse files

rhnlib for Location-Aware Support (rhnlib-2.1.4-17.el4_8.1)

  • Loading branch information...
1 parent 4ead9b3 commit 9136ec9451784128039cbffa1885a15b12afdce5 @ubeck ubeck committed Feb 26, 2012
Showing with 231 additions and 113 deletions.
  1. +1 −1 rhn/SSL.py
  2. +8 −24 rhn/SmartIO.py
  3. +18 −27 rhn/UserDictCase.py
  4. +1 −1 rhn/__init__.py
  5. +4 −19 rhn/connections.py
  6. +12 −3 rhn/nonblocking.py
  7. +110 −0 rhn/rhnLockfile.py
  8. +57 −21 rhn/rpclib.py
  9. +20 −17 rhn/transports.py
View
@@ -6,7 +6,7 @@
#
# Author: Mihai Ibanescu <misa@redhat.com>
-# $Id: SSL.py 137099 2008-02-19 17:27:26Z pkilambi $
+# $Id: SSL.py 191145 2010-03-01 10:21:24Z msuchy $
"""
rhn.SSL builds an abstraction on top of the objects provided by pyOpenSSL
View
@@ -1,11 +1,12 @@
+#/usr/bin/env python
#
# Smart IO class
#
# Copyright (c) 2002-2005 Red Hat, Inc.
#
# Author: Mihai Ibanescu <misa@redhat.com>
-# $Id: SmartIO.py 98118 2006-07-19 19:07:30Z jbowes $
+# $Id: SmartIO.py 191145 2010-03-01 10:21:24Z msuchy $
"""
This module implements the SmartIO class
"""
@@ -58,27 +59,10 @@ def __getattr__(self, name):
# Creates a temporary file and passes back its file descriptor
def _tempfile(tmpdir='/tmp'):
- # Try to create the file a couple of times
- filename = "%s/_rhn_transports-%d" % (tmpdir, os.getpid())
- for i in range(5):
- try:
- fd = os.open(filename, os.O_CREAT | os.O_RDWR | os.O_EXCL)
- except OSError, e:
- if e.errno == 17:
- # File exists
- filename = "%s-%.5f" % (filename, time.time() % 10)
- continue
- # Another error, raise it
- raise
- else:
- break
- else:
- # Failed to get the temp file
- raise IOError, "Could not create temporary file"
-
- # To make sure it's a temp file, unlink it
- os.unlink(filename)
+ import tempfile
+ (fd, fname) = tempfile.mkstemp(prefix="_rhn_transports-%d-" \
+ % os.getpid(), dir=tmpdir)
+ # tempfile, unlink it
+ os.unlink(fname)
+ return os.fdopen(fd, "wb+")
- # Convert the file descriptor into a stream
- f = os.fdopen(fd, "wb+")
- return f
View
@@ -4,7 +4,9 @@
# Copyright (c) 2001-2005, Red Hat Inc.
# All rights reserved.
#
-# $Id: UserDictCase.py 102336 2006-09-14 18:51:39Z jbowes $
+# $Id: UserDictCase.py 191145 2010-03-01 10:21:24Z msuchy $
+
+import string
from types import StringType
from UserDict import UserDict
@@ -14,47 +16,36 @@ class UserDictCase(UserDict):
def __init__(self, data = None):
self.kcase = {}
UserDict.__init__(self, data)
-
- def __lower_string(self, key):
- """ Return the lower() of key if it is a string. """
- if isinstance(key, StringType):
- return key.lower()
- else:
- return key
-
# some methods used to make the class work as a dictionary
def __setitem__(self, key, value):
- lkey = self.__lower_string(key)
+ lkey = key
+ if isinstance(key, StringType):
+ lkey = string.lower(key)
self.data[lkey] = value
self.kcase[lkey] = key
-
def __getitem__(self, key):
- key = self.__lower_string(key)
+ if isinstance(key, StringType):
+ key = string.lower(key)
if not self.data.has_key(key):
return None
return self.data[key]
-
- get = __getitem__
-
def __delitem__(self, key):
- key = self.__lower_string(key)
+ if isinstance(key, StringType):
+ key = string.lower(key)
del self.data[key]
del self.kcase[key]
-
+ get = __getitem__
def keys(self):
return self.kcase.values()
-
def items(self):
return self.get_hash().items()
-
def has_key(self, key):
- key = self.__lower_string(key)
+ if isinstance(key, StringType):
+ key = string.lower(key)
return self.data.has_key(key)
-
def clear(self):
self.data.clear()
self.kcase.clear()
-
# return this data as a real hash
def get_hash(self):
return reduce(lambda a, (ik, v), hc=self.kcase:
@@ -63,19 +54,19 @@ def get_hash(self):
# return the data for marshalling
def __getstate__(self):
return self.get_hash()
-
# we need a setstate because of the __getstate__ presence screws up deepcopy
def __setstate__(self, state):
self.__init__(state)
-
# get a dictionary out of this instance ({}.update doesn't get instances)
def dict(self):
return self.get_hash()
-
def update(self, dict):
for (k, v) in dict.items():
- self[k] = v
-
+ lk = k
+ if isinstance(k, StringType):
+ lk = string.lower(k)
+ self.data[lk] = v
+ self.kcase[lk] = k
# Expose an iterator. This would normally fail if there is no iter()
# function defined - but __iter__ will never be called on python 1.5.2
def __iter__(self):
View
@@ -3,7 +3,7 @@
#
# Copyright (c) 2005 Red Hat, Inc.
#
-# $Id: __init__.py 89042 2005-07-05 22:05:02Z wregglej $
+# $Id: __init__.py 191145 2010-03-01 10:21:24Z msuchy $
"""
rhn - A collection of modules used by Red Hat Network
"""
View
@@ -6,31 +6,16 @@
#
# Author: Mihai Ibanescu <misa@redhat.com>
-# $Id: connections.py 116568 2007-05-21 23:30:08Z pkilambi $
+# $Id: connections.py 191145 2010-03-01 10:21:24Z msuchy $
import sys
import string
import SSL
import nonblocking
-# Testing which version of python we run
-if hasattr(sys, "version_info"):
- # python 2.2 or newer
- import httplib
-else:
- # Older version, with incompatible httplib; import the patched one
- import _httplib
- httplib = _httplib
-
-# Testing which version of python we run
-if hasattr(sys, "version_info"):
- # python 2.2 or newer
- import xmlrpclib
-else:
- # Older version, with incompatible httplib; import the patched one
- import _internal_xmlrpclib
- xmlrpclib = _internal_xmlrpclib
+import httplib
+import xmlrpclib
# Import into the local namespace some httplib-related names
_CS_REQ_SENT = httplib._CS_REQ_SENT
@@ -89,7 +74,7 @@ def __init__(self, host, port=None):
self._cb_ex = []
self._cb_user_data = None
self._cb_callback = None
- self._user_agent = "rhn.connections $Revision: 116568 $ (python)"
+ self._user_agent = "rhn.connections $Revision: 191145 $ (python)"
def set_callback(self, rs, ws, ex, user_data, callback):
# XXX check the params
View
@@ -1,17 +1,26 @@
+#!/usr/bin/env python
#
#
#
-# $Id: nonblocking.py 102302 2006-09-14 14:25:34Z jbowes $
+# $Id: nonblocking.py 191145 2010-03-01 10:21:24Z msuchy $
import select
import fcntl
+# Testing which version of python we run
+import sys
+if hasattr(sys, "version_info"):
+ # python 2.2 or newer; FCNTL is deprecated
+ FCNTL = fcntl
+else:
+ # Older version, with valid FCNTL
+ import FCNTL
class NonBlockingFile:
def __init__(self, fd):
# Keep a copy of the file descriptor
self.fd = fd
- fcntl.fcntl(self.fd.fileno(), fcntl.F_SETFL, fcntl.O_NDELAY | fcntl.FNDELAY)
+ fcntl.fcntl(self.fd.fileno(), FCNTL.F_SETFL, FCNTL.O_NDELAY | FCNTL.FNDELAY)
# Set the callback-related stuff
self.read_fd_set = []
self.write_fd_set = []
@@ -24,7 +33,7 @@ def set_callback(self, read_fd_set, write_fd_set, exc_fd_set,
self.read_fd_set = read_fd_set
# Make the objects non-blocking
for f in self.read_fd_set:
- fcntl.fcntl(f.fileno(), fcntl.F_SETFL, fcntl.O_NDELAY | fcntl.FNDELAY)
+ fcntl.fcntl(f.fileno(), FCNTL.F_SETFL, FCNTL.O_NDELAY | FCNTL.FNDELAY)
self.write_fd_set = write_fd_set
self.exc_fd_set = exc_fd_set
View
@@ -0,0 +1,110 @@
+#
+# Copyright (c) 2008--2010 Red Hat, Inc.
+#
+# This software is licensed to you under the GNU General Public License,
+# version 2 (GPLv2). There is NO WARRANTY for this software, express or
+# implied, including the implied warranties of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
+# along with this software; if not, see
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
+#
+# Red Hat trademarks are not licensed under GPLv2. No permission is
+# granted to use or replicate Red Hat trademarks that are incorporated
+# in this software or its documentation.
+#
+
+import os
+import sys
+import fcntl
+from errno import EWOULDBLOCK, EEXIST
+import fcntl
+
+class LockfileLockedException(Exception):
+ """thrown ONLY when pid file is locked."""
+ pass
+
+class Lockfile:
+
+ """class that provides simple access to a PID-style lockfile.
+
+ methods: __init__(lockfile), acquire(), and release()
+ NOTE: currently acquires upon init
+ The *.pid file will be acquired, or an LockfileLockedException is raised.
+ """
+
+ def __init__(self, lockfile, pid=None):
+ """create (if need be), and acquire lock on lockfile
+
+ lockfile example: '/var/run/up2date.pid'
+ """
+
+ # cleanup the path and assign it.
+ self.lockfile = os.path.abspath(
+ os.path.expanduser(
+ os.path.expandvars(lockfile)))
+
+ self.pid = pid
+ if not self.pid:
+ self.pid = os.getpid()
+
+ # create the directory structure
+ dirname = os.path.dirname(self.lockfile)
+ if not os.path.exists(dirname):
+ try:
+ os.makedirs(dirname)
+ except OSError, e:
+ if hasattr(e, 'errno') and e.errno == EEXIST:
+ # race condition... dirname exists now.
+ pass
+ else:
+ raise
+
+ # open the file -- non-destructive read-write, unless it needs
+ # to be created XXX: potential race condition upon create?
+ self.f = os.open(self.lockfile, os.O_RDWR|os.O_CREAT|os.O_SYNC)
+ self.acquire()
+
+ def acquire(self):
+ """acquire the lock; else raise LockfileLockedException."""
+
+ try:
+ fcntl.flock(self.f, fcntl.LOCK_EX|fcntl.LOCK_NB)
+ except IOError, e:
+ if e.errno == EWOULDBLOCK:
+ raise LockfileLockedException(
+ "cannot acquire lock on %s." % self.lockfile)
+ else:
+ raise
+ # unlock upon exit
+ fcntl.fcntl(self.f, fcntl.F_SETFD, 1)
+ # truncate and write the pid
+ os.ftruncate(self.f, 0)
+ os.write(self.f, str(self.pid) + '\n')
+
+ def release(self):
+ # Remove the lock file
+ os.unlink(self.lockfile)
+ fcntl.flock(self.f, fcntl.LOCK_UN)
+ os.close(self.f)
+
+
+def main():
+ """test code"""
+
+ try:
+ L = Lockfile('./test.pid')
+ except LockfileLockedException, e:
+ sys.stderr.write("%s\n" % e)
+ sys.exit(-1)
+ else:
+ print "lock acquired "
+ print "...sleeping for 10 seconds"
+ import time
+ time.sleep(10)
+ L.release()
+ print "lock released "
+
+if __name__ == '__main__':
+ # test code
+ sys.exit(main() or 0)
+
Oops, something went wrong.

0 comments on commit 9136ec9

Please sign in to comment.