← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~cjwatson/launchpad/safer-release-file-creation into lp:launchpad

 

Colin Watson has proposed merging lp:~cjwatson/launchpad/safer-release-file-creation into lp:launchpad.

Commit message:
Create containing directories when writing Release files.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/safer-release-file-creation/+merge/290416

Create containing directories when writing Release files.  This can matter in --careful-release mode.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~cjwatson/launchpad/safer-release-file-creation into lp:launchpad.
=== modified file 'lib/lp/archivepublisher/publishing.py'
--- lib/lp/archivepublisher/publishing.py	2016-03-23 12:33:52 +0000
+++ lib/lp/archivepublisher/publishing.py	2016-03-30 09:24:46 +0000
@@ -65,6 +65,7 @@
 from lp.services.database.interfaces import IStore
 from lp.services.features import getFeatureFlag
 from lp.services.librarian.client import LibrarianClient
+from lp.services.osutils import open_for_writing
 from lp.services.utils import file_exists
 from lp.soyuz.enums import (
     ArchivePurpose,
@@ -818,10 +819,8 @@
         :param release_data: A `debian.deb822.Release` object to write
             to the filesystem.
         """
-        location = os.path.join(self._config.distsroot, suite)
-        if not file_exists(location):
-            os.makedirs(location)
-        with open(os.path.join(location, "Release"), "w") as release_file:
+        release_path = os.path.join(self._config.distsroot, suite, "Release")
+        with open_for_writing(release_path, "w") as release_file:
             release_data.dump(release_file, "utf-8")
 
     def _syncTimestamps(self, suite, all_files):
@@ -970,8 +969,8 @@
         release_file["Label"] = self._getLabel()
         release_file["Architecture"] = arch_name
 
-        with open(os.path.join(suite_dir,
-                               component, arch_path, "Release"), "w") as f:
+        release_path = os.path.join(suite_dir, component, arch_path, "Release")
+        with open_for_writing(release_path, "w") as f:
             release_file.dump(f, "utf-8")
 
     def _writeSuiteSource(self, distroseries, pocket, component,

=== modified file 'lib/lp/archivepublisher/tests/test_publisher.py'
--- lib/lp/archivepublisher/tests/test_publisher.py	2016-03-11 11:45:56 +0000
+++ lib/lp/archivepublisher/tests/test_publisher.py	2016-03-30 09:24:46 +0000
@@ -58,6 +58,7 @@
     BufferLogger,
     DevNullLogger,
     )
+from lp.services.osutils import open_for_writing
 from lp.services.utils import file_exists
 from lp.soyuz.enums import (
     ArchivePurpose,
@@ -1908,6 +1909,27 @@
                 os.stat(os.path.join(dep11_path, name)).st_mtime,
                 LessThan(now - 59))
 
+    def testReleaseFileWritingCreatesDirectories(self):
+        # Writing Release files creates directories as needed.
+        publisher = Publisher(
+            self.logger, self.config, self.disk_pool,
+            self.ubuntutest.main_archive)
+
+        self.getPubSource()
+        # Create the top-level Release file so that careful Release
+        # republication is allowed.
+        release_path = os.path.join(
+            self.config.distsroot, 'breezy-autotest', 'Release')
+        with open_for_writing(release_path, 'w'):
+            pass
+
+        publisher.D_writeReleaseFiles(True)
+
+        source_release = os.path.join(
+            self.config.distsroot, 'breezy-autotest', 'main', 'source',
+            'Release')
+        self.assertTrue(file_exists(source_release))
+
     def testCreateSeriesAliasesNoAlias(self):
         """createSeriesAliases has nothing to do by default."""
         publisher = Publisher(


Follow ups