Rev 4941: Switch the apis over to using File objects rather than integers. in http://bazaar.launchpad.net/~jameinel/bzr/2.1.0rc1-set-mtime

John Arbash Meinel john at arbash-meinel.com
Wed Jan 6 17:21:21 GMT 2010


At http://bazaar.launchpad.net/~jameinel/bzr/2.1.0rc1-set-mtime

------------------------------------------------------------
revno: 4941
revision-id: john at arbash-meinel.com-20100106172057-inza3wazs24pbith
parent: john at arbash-meinel.com-20100105221312-ez1orftbr65ws6yd
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1.0rc1-set-mtime
timestamp: Wed 2010-01-06 11:20:57 -0600
message:
  Switch the apis over to using File objects rather than integers.
  
  Also, os.utime does exist (even if I couldn't find it before). We should
  probably perf-test if it is worth using the open file handles.
-------------- next part --------------
=== modified file 'bzrlib/_readdir_pyx.pyx'
--- a/bzrlib/_readdir_pyx.pyx	2010-01-05 21:38:30 +0000
+++ b/bzrlib/_readdir_pyx.pyx	2010-01-06 17:20:57 +0000
@@ -390,7 +390,7 @@
     return result
 
 
-def fset_mtime(fileno, mtime):
+def fset_mtime(f, mtime):
     """See osutils.fset_mtime."""
     cdef int fd
     cdef int retval
@@ -398,7 +398,7 @@
     cdef timeval tv[2]
     cdef stat st
 
-    fd = fileno
+    fd = f.fileno()
     d_mtime = mtime
     tv[1].tv_sec = <int>(d_mtime)
     tv[1].tv_usec = <int>((d_mtime - tv[1].tv_sec) * 1000000.0)

=== modified file 'bzrlib/_walkdirs_win32.pyx'
--- a/bzrlib/_walkdirs_win32.pyx	2010-01-05 21:15:22 +0000
+++ b/bzrlib/_walkdirs_win32.pyx	2010-01-06 17:20:57 +0000
@@ -289,7 +289,7 @@
         return dirblock
 
 
-def fset_mtime(fileno, mtime):
+def fset_mtime(f, mtime):
     """See osutils.fset_mtime."""
     cdef HANDLE the_handle
     cdef FILE_BASIC_INFO bi
@@ -297,7 +297,7 @@
     cdef int retval
 
     ft = _timestamp_to_ftime(mtime)
-    the_handle = <HANDLE>(_get_osfhandle(fileno))
+    the_handle = <HANDLE>(_get_osfhandle(f.fileno()))
     if the_handle == <HANDLE>(-1):
         raise OSError('Invalid fileno') # IOError?
     retval = GetFileInformationByHandleEx(the_handle, FileBasicInfo,

=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2010-01-05 21:15:22 +0000
+++ b/bzrlib/osutils.py	2010-01-06 17:20:57 +0000
@@ -1889,24 +1889,23 @@
     return b
 
 
-def _noop_fset_mtime(fileno, mtime):
-    """Do nothing.
-
-    This pretends to set the mtime of a file, but we were unable to import an
-    extension which would provide that functionality. So we just skip.
-
+def _utime_fset_mtime(f, mtime):
+    """Use 'os.utime' to set the mtime.
+
+    This will cause a second lookup of the path, etc, but it works.
     :seealso: fset_mtime
     """
+    os.utime(f.name, (mtime, mtime))
 
 
 _set_mtime_func = None
-def fset_mtime(fileno, mtime):
+def fset_mtime(f, mtime):
     """Set the last-modified time (mtime) for this file handle.
 
     This uses native OS functionality to set file times. As such, if extensions
     are not compiled, this function becomes a no-op.
 
-    :param fileno: The fileno for the file, usually obtained from f.fileno()
+    :param f: A File object (from open())
     :param mtime: time-since-epoch to set the mtime to. (same as time.time(),
         or st.st_mtime, etc.). This can be a floating point number, but we
         don't guarantee better than 1s resolution.
@@ -1918,17 +1917,17 @@
             try:
                 from bzrlib._walkdirs_win32 import fset_mtime
             except ImportError:
-                _set_mtime_func = _noop_fset_mtime
+                _set_mtime_func = _utime_fset_mtime
             else:
                 _set_mtime_func = fset_mtime
         else:
             try:
                 from bzrlib._readdir_pyx import fset_mtime
             except ImportError:
-                _set_mtime_func = _noop_fset_mtime
+                _set_mtime_func = _utime_fset_mtime
             else:
                 _set_mtime_func = fset_mtime
-    return _set_mtime_func(fileno, mtime)
+    return _set_mtime_func(f, mtime)
 
 
 def send_all(socket, bytes, report_activity=None):

=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py	2010-01-05 21:38:30 +0000
+++ b/bzrlib/tests/test_osutils.py	2010-01-06 17:20:57 +0000
@@ -2008,29 +2008,22 @@
 
 class TestFSetMtime(tests.TestCaseInTempDir):
 
-    def have_extension(self):
-        return (UTF8DirReaderFeature.available()
-                or test__walkdirs_win32.win32_readdir_feature.available())
-
-    def test__noop(self):
-        # The _noop_fset_mtime function doesn't change the mtime
+    def test__utime_fset_mtime(self):
         f = open('test', 'wb')
         try:
             mtime = os.fstat(f.fileno()).st_mtime
-            osutils._noop_fset_mtime(f.fileno(), time.time()-20)
+            new_mtime = mtime - 20
+            osutils._utime_fset_mtime(f, new_mtime)
         finally:
             f.close()
-        self.assertEqual(mtime, os.lstat('test').st_mtime)
+        self.assertEqual(int(new_mtime), int(os.lstat('test').st_mtime))
 
     def test_fset_mtime(self):
-        if not self.have_extension():
-            self.knownFailure('Pure python does not expose a way to set'
-                              ' the mtime of a file.')
         f = open('test', 'wb')
         new_mtime = time.time()-20.0
         try:
             mtime = os.fstat(f.fileno()).st_mtime
-            osutils.fset_mtime(f.fileno(), new_mtime)
+            osutils.fset_mtime(f, new_mtime)
         finally:
             f.close()
         self.assertNotEqual(mtime, new_mtime)

=== modified file 'bzrlib/transform.py'
--- a/bzrlib/transform.py	2010-01-05 22:13:12 +0000
+++ b/bzrlib/transform.py	2010-01-06 17:20:57 +0000
@@ -1159,7 +1159,7 @@
         """
         if self._creation_mtime is None:
             self._creation_mtime = time.time()
-        osutils.fset_mtime(f.fileno(), self._creation_mtime)
+        osutils.fset_mtime(f, self._creation_mtime)
 
     def create_hardlink(self, path, trans_id):
         """Schedule creation of a hard link"""



More information about the bazaar-commits mailing list