← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jtv/maas/file-age-helpers into lp:maas

 

Jeroen T. Vermeulen has proposed merging lp:~jtv/maas/file-age-helpers into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jtv/maas/file-age-helpers/+merge/111365

These helpers are needed in another branch I'm working on, and which is getting too big otherwise.


Jeroen
-- 
https://code.launchpad.net/~jtv/maas/file-age-helpers/+merge/111365
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jtv/maas/file-age-helpers into lp:maas.
=== modified file 'src/maas/tests/test_maas_import_pxe_files.py'
--- src/maas/tests/test_maas_import_pxe_files.py	2012-06-20 16:56:42 +0000
+++ src/maas/tests/test_maas_import_pxe_files.py	2012-06-21 09:27:26 +0000
@@ -13,11 +13,14 @@
 __all__ = []
 
 import os
-from stat import ST_MTIME
 from subprocess import check_call
 
 from maastesting.factory import factory
 from maastesting.testcase import TestCase
+from maastesting.utils import (
+    age_file,
+    get_write_time,
+    )
 from testtools.matchers import (
     Contains,
     FileContains,
@@ -37,14 +40,9 @@
         return infile.read()
 
 
-def get_write_time(path):
-    """Return last modification time of file at `path`."""
-    return os.stat(path)[ST_MTIME]
-
-
 def backdate(path):
     """Set the last modification time for the file at `path` to the past."""
-    os.utime(path, (99999, 99999))
+    age_file(path, 9999999)
 
 
 def compose_download_dir(archive, arch, release):

=== modified file 'src/maasserver/tests/test_filestorage.py'
--- src/maasserver/tests/test_filestorage.py	2012-05-21 04:33:24 +0000
+++ src/maasserver/tests/test_filestorage.py	2012-06-21 09:27:26 +0000
@@ -21,6 +21,7 @@
 from maasserver.models import FileStorage
 from maasserver.testing.factory import factory
 from maasserver.testing.testcase import TestCase
+from maastesting.utils import age_file
 from testtools.matchers import (
     GreaterThan,
     LessThan,
@@ -66,18 +67,6 @@
         text = "%s %s" % (including_text, factory.getRandomString())
         return text.encode('ascii')
 
-    def age_file(self, path, seconds=None):
-        """Make the file at `path` look like it hasn't been touched recently.
-
-        Decrements the file's mtime by a bit over a day.
-        """
-        if seconds is None:
-            seconds = FileStorage.objects.grace_time + 1
-        stat_result = os.stat(path)
-        atime = stat_result.st_atime
-        mtime = stat_result.st_mtime
-        os.utime(path, (atime, mtime - seconds))
-
     def test_get_existing_storage_returns_None_if_none_found(self):
         nonexistent_file = factory.getRandomString()
         self.assertIsNone(
@@ -190,7 +179,7 @@
         path = factory.make_file(
             location=self.make_upload_dir(), name=filename,
             contents=self.make_data())
-        self.age_file(path, FileStorage.objects.grace_time - 60)
+        age_file(path, FileStorage.objects.grace_time - 60)
         self.assertFalse(
             FileStorage.objects.is_old(self.get_media_path(filename)))
 
@@ -199,7 +188,7 @@
         path = factory.make_file(
             location=self.make_upload_dir(), name=filename,
             contents=self.make_data())
-        self.age_file(path, FileStorage.objects.grace_time + 1)
+        age_file(path, FileStorage.objects.grace_time + 1)
         self.assertTrue(
             FileStorage.objects.is_old(self.get_media_path(filename)))
 
@@ -208,7 +197,7 @@
         path = factory.make_file(
             location=self.make_upload_dir(), name=filename,
             contents=self.make_data())
-        self.age_file(path)
+        age_file(path, FileStorage.objects.grace_time + 1)
         FileStorage.objects.collect_garbage()
         self.assertFalse(
             FileStorage.storage.exists(self.get_media_path(filename)))
@@ -233,7 +222,7 @@
     def test_collect_garbage_leaves_referenced_files_alone(self):
         self.make_upload_dir()
         storage = factory.make_file_storage()
-        self.age_file(storage.data.path)
+        age_file(storage.data.path, FileStorage.objects.grace_time + 1)
         FileStorage.objects.collect_garbage()
         self.assertTrue(FileStorage.storage.exists(storage.data.name))
 

=== modified file 'src/maastesting/utils.py'
--- src/maastesting/utils.py	2012-05-17 15:50:08 +0000
+++ src/maastesting/utils.py	2012-06-21 09:27:26 +0000
@@ -11,14 +11,18 @@
 
 __metaclass__ = type
 __all__ = [
+    "age_file",
     "content_from_file",
     "extract_word_list",
+    "get_write_time",
     "preexec_fn",
     "retries",
     ]
 
+import os
 import re
 import signal
+from stat import ST_MTIME
 from time import (
     sleep,
     time,
@@ -28,6 +32,19 @@
 from testtools.content_type import UTF8_TEXT
 
 
+def age_file(path, seconds):
+    """Backdate a file's modification time so that it looks older."""
+    stat_result = os.stat(path)
+    atime = stat_result.st_atime
+    mtime = stat_result.st_mtime
+    os.utime(path, (atime, mtime - seconds))
+
+
+def get_write_time(path):
+    """Return last modification time of file at `path`."""
+    return os.stat(path)[ST_MTIME]
+
+
 def content_from_file(path):
     """Alternative to testtools' version.