123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import os
- import sys
- from pathlib import Path
- def find_files_by_extension(root_dir, extension):
- """
- Locates all files with a specific extension within a directory and its subdirectories.
- Args:
- root_dir (str): The absolute path to the root directory to search.
- extension (str): The file extension to look for (e.g., '.unity', '.prefab').
- The dot is required.
- Returns:
- list: A list of absolute paths to the found files.
- """
- if not extension.startswith('.'):
- print("Error: Extension must start with a dot (e.g., '.txt').", file=sys.stderr)
- return []
-
- found_files = []
- for root, _, files in os.walk(root_dir):
- for file in files:
- if file.endswith(extension):
- found_files.append(os.path.join(root, file))
- return found_files
- def replicate_directory_structure(source_root, target_root):
- """
- Copies a directory tree from a source to a target location without copying the files.
- Args:
- source_root (str): The absolute path of the source directory.
- target_root (str): The absolute path of the destination directory.
- """
- source_root = Path(source_root)
- target_root = Path(target_root)
- if not source_root.is_dir():
- print(f"Error: Source path '{source_root}' is not a valid directory.", file=sys.stderr)
- return
- for dirpath, _, _ in os.walk(source_root):
- # Create a relative path from the source root
- relative_path = Path(dirpath).relative_to(source_root)
- # Join it with the target root to get the new directory path
- target_path = target_root / relative_path
- # Create the directory in the target, ignoring if it already exists
- target_path.mkdir(parents=True, exist_ok=True)
- def create_guid_to_path_map(root_dir):
- """
- Creates a dictionary mapping GUIDs to their corresponding asset file paths,
- scanning both the Assets and Packages directories and ignoring folders.
- """
- guid_map = {}
- scan_dirs = [
- os.path.join(root_dir, 'Assets'),
- os.path.join(root_dir, 'Packages'),
- os.path.join(root_dir, 'Library', 'PackageCache')
- ]
- for directory in scan_dirs:
- if not os.path.isdir(directory):
- continue
-
- meta_files = find_files_by_extension(directory, '.meta')
- for meta_file_path in meta_files:
- asset_path = meta_file_path[:-5]
- guid = None
- is_folder = False
- try:
- with open(meta_file_path, 'r', encoding='utf-8') as f:
- for line in f:
- stripped_line = line.strip()
- if stripped_line.startswith('guid:'):
- guid = stripped_line.split(':')[1].strip()
- if stripped_line == 'folderAsset: yes':
- is_folder = True
- break
-
- if is_folder:
- continue
- if guid and guid not in guid_map:
- guid_map[guid] = asset_path
- except Exception as e:
- print(f"Warning: Could not process meta file {meta_file_path}. {e}", file=sys.stderr)
- return guid_map
- if __name__ == '__main__':
- # Example usage for testing the module directly.
- # This requires a temporary directory structure to be set up.
-
- # Create a dummy structure for testing
- test_source_dir = Path('./tmp_source_for_testing')
- test_target_dir = Path('./tmp_target_for_testing')
-
- try:
- print("Setting up test directory structure...")
- (test_source_dir / 'scenes').mkdir(parents=True, exist_ok=True)
- (test_source_dir / 'prefabs' / 'characters').mkdir(parents=True, exist_ok=True)
- (test_source_dir / 'scenes' / 'level1.unity').touch()
- (test_source_dir / 'scenes' / 'level1.unity.meta').touch()
- (test_source_dir / 'prefabs' / 'player.prefab').touch()
-
- print("\n--- Testing find_files_by_extension ---")
- unity_files = find_files_by_extension(str(test_source_dir), '.unity')
- print(f"Found .unity files: {unity_files}")
- assert len(unity_files) == 1
-
- prefab_files = find_files_by_extension(str(test_source_dir), '.prefab')
- print(f"Found .prefab files: {prefab_files}")
- assert len(prefab_files) == 1
- print("\n--- Testing replicate_directory_structure ---")
- replicate_directory_structure(str(test_source_dir), str(test_target_dir))
-
- print("Checking replicated structure:")
- assert test_target_dir.exists()
- assert (test_target_dir / 'scenes').exists()
- assert (test_target_dir / 'prefabs' / 'characters').exists()
- assert not (test_target_dir / 'scenes' / 'level1.unity').exists() # File should not be copied
- print("Replicated structure seems correct.")
- finally:
- # Clean up test directories
- import shutil
- if test_source_dir.exists():
- shutil.rmtree(test_source_dir)
- if test_target_dir.exists():
- shutil.rmtree(test_target_dir)
- print("\nCleaned up test directories.")
|