浏览代码

Move readFile from rpc.

Chris McDonough 19 年之前
父节点
当前提交
f68befd1c9
共有 3 个文件被更改,包括 45 次插入48 次删除
  1. 3 35
      rpc.py
  2. 33 0
      src/supervisor/options.py
  3. 9 13
      src/supervisor/tests.py

+ 3 - 35
rpc.py

@@ -17,6 +17,7 @@ import StringIO
 import tempfile
 import errno
 from http import NOT_DONE_YET
+from options import readFile
 
 RPC_VERSION  = 1.0
 
@@ -222,7 +223,7 @@ class SupervisorNamespaceRPCInterface:
             raise RPCError(Faults.NO_FILE)
 
         try:
-            return _readFile(logfile, offset, length)
+            return readFile(logfile, offset, length)
         except ValueError, inst:
             why = inst.args[0]
             raise RPCError(getattr(Faults, why))
@@ -552,7 +553,7 @@ class SupervisorNamespaceRPCInterface:
             raise RPCError(Faults.NO_FILE, logfile)
 
         try:
-            return _readFile(logfile, offset, length)
+            return readFile(logfile, offset, length)
         except ValueError, inst:
             why = inst.args[0]
             raise RPCError(getattr(Faults, why))
@@ -842,37 +843,4 @@ def traverse(ob, method, params):
     except TypeError:
         raise RPCError(Faults.INCORRECT_PARAMETERS)
 
-def _readFile(filename, offset, length):
-    """ Read length bytes from the file named by filename starting at
-    offset """
-
-    absoffset = abs(offset)
-    abslength = abs(length)
-
-    try:
-        f = open(filename, 'rb')
-        if absoffset != offset:
-            # negative offset returns offset bytes from tail of the file
-            if length:
-                raise ValueError('BAD_ARGUMENTS')
-            f.seek(0, 2)
-            sz = f.tell()
-            pos = int(sz - absoffset)
-            if pos < 0:
-                pos = 0
-            f.seek(pos)
-            data = f.read(absoffset)
-        else:
-            if abslength != length:
-                raise ValueError('BAD_ARGUMENTS')
-            if length == 0:
-                f.seek(offset)
-                data = f.read()
-            else:
-                sz = f.seek(offset)
-                data = f.read(length)
-    except (os.error, IOError):
-        raise ValueError('FAILED')
-
-    return data
 

+ 33 - 0
src/supervisor/options.py

@@ -968,3 +968,36 @@ class UnixStreamHTTPConnection(httplib.HTTPConnection):
 class UnixStreamHTTP(httplib.HTTP):
     _connection_class = UnixStreamHTTPConnection
 
+def readFile(filename, offset, length):
+    """ Read length bytes from the file named by filename starting at
+    offset """
+
+    absoffset = abs(offset)
+    abslength = abs(length)
+
+    try:
+        f = open(filename, 'rb')
+        if absoffset != offset:
+            # negative offset returns offset bytes from tail of the file
+            if length:
+                raise ValueError('BAD_ARGUMENTS')
+            f.seek(0, 2)
+            sz = f.tell()
+            pos = int(sz - absoffset)
+            if pos < 0:
+                pos = 0
+            f.seek(pos)
+            data = f.read(absoffset)
+        else:
+            if abslength != length:
+                raise ValueError('BAD_ARGUMENTS')
+            if length == 0:
+                f.seek(offset)
+                data = f.read()
+            else:
+                sz = f.seek(offset)
+                data = f.read(length)
+    except (os.error, IOError):
+        raise ValueError('FAILED')
+
+    return data

+ 9 - 13
src/supervisor/tests.py

@@ -169,6 +169,15 @@ command=/bin/cat
         self.assertEqual(instance.minfds, 2048)
         self.assertEqual(instance.minprocs, 300)
 
+    def test_readFile_failed(self):
+        from options import readFile
+        try:
+            readFile('/notthere', 0, 10)
+        except ValueError, inst:
+            self.assertEqual(inst.args[0], 'FAILED')
+        else:
+            raise AssertionError("Didn't raise")
+
 class TestBase(unittest.TestCase):
     def setUp(self):
         pass
@@ -702,19 +711,6 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
                                       'status':rpc.Faults.SUCCESS,
                                       'description':'OK'})
 
-    def test_readFile_failed(self):
-        from rpc import _readFile
-        supervisord = DummySupervisor()
-        interface = self._makeOne(supervisord)
-        logfile = supervisord.options.logfile
-        try:
-            _readFile('/notthere', 0, 10)
-        except ValueError, inst:
-            self.assertEqual(inst.args[0], 'FAILED')
-        else:
-            raise AssertionError("Didn't raise")
-
-
 class SystemNamespaceXMLRPCInterfaceTests(TestBase):
     def _getTargetClass(self):
         return rpc.SystemNamespaceRPCInterface