Преглед изворни кода

Delete paths inside .<object-name>_content_names

When installing a gcs dep entry, record the getnames() output to
.<object_name>_content_names. When updating the object(s) of a gcs
entry, it'll clear all files related to that gcs entry including:
.<object_name>_content_names and the paths inside it,
.<object_name>_hash, and .<object_name>_is_first_class_gcs

Bug: b/324418194
Change-Id: Ieb56623ce929b715ed103af9560efcf66b46a9c4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5454646
Commit-Queue: Stephanie Kim <kimstephanie@google.com>
Reviewed-by: Joanna Wang <jojwang@chromium.org>
Stephanie Kim пре 1 година
родитељ
комит
8b40b1b381
2 измењених фајлова са 48 додато и 12 уклоњено
  1. 12 9
      gclient.py
  2. 36 3
      gclient_scm.py

+ 12 - 9
gclient.py

@@ -2582,9 +2582,9 @@ class GcsDependency(Dependency):
               self).run(revision_overrides, command, args, work_queue, options,
                         patch_refs, target_branches, skip_sync_revisions)
 
-    def WriteFilenameHash(self, sha1, hash_file):
-        with open(hash_file, 'w') as f:
-            f.write(sha1)
+    def WriteToFile(self, content, file):
+        with open(file, 'w') as f:
+            f.write(content)
             f.write('\n')
 
     def IsDownloadNeeded(self, output_dir, output_file, hash_file,
@@ -2646,8 +2646,8 @@ class GcsDependency(Dependency):
                                    or gcs_file_name)
 
         # Remove any forward slashes and drop any extensions
-        hash_name = self.object_name.replace('/', '_').split('.')[0]
-        hash_file = os.path.join(output_dir, f'.{hash_name}_hash')
+        file_prefix = self.object_name.replace('/', '_').split('.')[0]
+        hash_file = os.path.join(output_dir, f'.{file_prefix}_hash')
         migration_toggle_file = os.path.join(
             output_dir,
             download_from_google_storage.construct_migration_file_name(
@@ -2722,11 +2722,14 @@ class GcsDependency(Dependency):
                                                     possible_top_level_dirs)
                 if not is_valid_tar:
                     raise Exception('tarfile contains invalid entries')
+
+                tar_content_file = os.path.join(
+                    output_dir, f'.{file_prefix}_content_names')
+                self.WriteToFile(json.dumps(tar.getnames()), tar_content_file)
+
                 tar.extractall(path=output_dir)
-        self.WriteFilenameHash(calculated_sha256sum, hash_file)
-        with open(migration_toggle_file, 'w') as f:
-            f.write(str(1))
-            f.write('\n')
+        self.WriteToFile(calculated_sha256sum, hash_file)
+        self.WriteToFile(str(1), migration_toggle_file)
 
     #override
     def GetScmName(self):

+ 36 - 3
gclient_scm.py

@@ -6,6 +6,7 @@
 import collections
 import contextlib
 import errno
+import glob
 import json
 import logging
 import os
@@ -2004,13 +2005,45 @@ class GcsRoot(object):
                 full_path = os.path.join(self.root_dir, path)
                 if (len(resolved_objects) != len(parsed_objects)
                         and os.path.exists(full_path)):
-                    gclient_utils.rmtree(full_path)
+                    self.clobber_tar_content_names(full_path)
+                    self.clobber_hash_files(full_path)
+                    self.clobber_migration_files(full_path)
+
+    def clobber_tar_content_names(self, entry_directory):
+        """Delete paths written in .*_content_names files"""
+        content_names_files = glob.glob(
+            os.path.join(entry_directory, '.*_content_names'))
+        for file in content_names_files:
+            with open(file, 'r') as f:
+                names = json.loads(f.read().strip())
+                for name in names:
+                    name_path = os.path.join(entry_directory, name)
+                    if os.path.isdir(
+                            name_path) or not os.path.exists(name_path):
+                        continue
+                    os.remove(os.path.join(entry_directory, name))
+            os.remove(file)
+
+    def clobber_hash_files(self, entry_directory):
+        files = glob.glob(os.path.join(entry_directory, '.*_hash'))
+        for f in files:
+            os.remove(f)
+
+    def clobber_migration_files(self, entry_directory):
+        files = glob.glob(os.path.join(entry_directory,
+                                       '.*_is_first_class_gcs'))
+        for f in files:
+            os.remove(f)
 
     def clobber(self):
-        """Remove all dep path directories and clear .gcs_entries"""
+        """Remove all dep path gcs items and clear .gcs_entries"""
         for _, objects_dict in self._gcs_entries.items():
             for dep_path, _ in objects_dict.items():
-                gclient_utils.rmtree(os.path.join(self.root_dir, dep_path))
+                full_path = os.path.join(self.root_dir, dep_path)
+                self.clobber_tar_content_names(full_path)
+                self.clobber_hash_files(full_path)
+                self.clobber_migration_files(full_path)
+
         if os.path.exists(self._gcs_entries_file):
             os.remove(self._gcs_entries_file)
         with self._mutator_lock: