Refactor Hbc::Container::Dmg for clarity (#22952)

This commit is contained in:
Josh Hagins 2016-07-20 11:06:06 -04:00 committed by GitHub
parent ed4548e342
commit e41a3c5fd3
2 changed files with 192 additions and 173 deletions

View File

@ -1,3 +1,4 @@
require "set"
require "tempfile"
class Hbc::Container::Dmg < Hbc::Container::Base
@ -17,28 +18,16 @@ class Hbc::Container::Dmg < Hbc::Container::Base
def extract
mount!
assert_mounts_found
@mounts.each do |mount|
Tempfile.open(["", ".bom"]) do |bomfile|
bomfile.close
Tempfile.open(["", ".list"]) do |filelist|
filelist.write(bom_filelist_from_path(mount))
filelist.close
@command.run("/usr/bin/mkbom", args: ["-s", "-i", filelist.path, "--", bomfile.path])
@command.run("/usr/bin/ditto", args: ["--bom", bomfile.path, "--", mount, @cask.staged_path])
end
end
end
extract_mounts
ensure
eject!
end
def mount!
plist = @command.run("/usr/bin/hdiutil",
# realpath is a failsafe against unusual filenames
args: %w[mount -plist -nobrowse -readonly -noidme -mountrandom /tmp] + [Pathname.new(@path).realpath],
input: %w[y])
plist = @command.run!("/usr/bin/hdiutil",
# realpath is a failsafe against unusual filenames
args: %w[mount -plist -nobrowse -readonly -noidme -mountrandom /tmp] + [Pathname.new(@path).realpath],
input: %w[y])
.plist
@mounts = mounts_from_plist(plist)
end
@ -66,36 +55,58 @@ class Hbc::Container::Dmg < Hbc::Container::Base
private
def extract_mounts
@mounts.each(&method(:extract_mount))
end
def extract_mount(mount)
Tempfile.open(["", ".bom"]) do |bomfile|
bomfile.close
Tempfile.open(["", ".list"]) do |filelist|
filelist.write(bom_filelist_from_path(mount))
filelist.close
@command.run!("/usr/bin/mkbom", args: ["-s", "-i", filelist.path, "--", bomfile.path])
@command.run!("/usr/bin/ditto", args: ["--bom", bomfile.path, "--", mount, @cask.staged_path])
end
end
end
def bom_filelist_from_path(mount)
mountpath = Pathname.new(mount).realpath
paths = Dir.glob(mountpath.join("**", "*"), File::FNM_DOTMATCH)
.map { |path| Pathname.new(path).relative_path_from(mountpath) }
paths = paths.reject { |path|
path = mountpath.join(path.sub(%r{/.*}, ""))
# unnecessary DMG metadata
%w[
.background
.com.apple.timemachine.donotpresent
.DocumentRevisions-V100
.DS_Store
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
].include?(path.basename.to_s) ||
# symlinks to system directories (commonly to /Applications)
(path.symlink? &&
Hbc::MacOS::SYSTEM_DIRS.include?(Pathname.new(File.readlink(path))))
Dir.chdir(mount) {
Dir.glob("**/*", File::FNM_DOTMATCH).map { |path|
next if skip_path?(Pathname(path))
path == "." ? path : path.prepend("./")
}.compact.join("\n").concat("\n")
}
end
paths.map(&:to_s)
.map { |path| path.prepend(path == "." ? "" : "./").concat("\n") }
.join
def skip_path?(path)
dmg_metadata?(path) || system_dir_symlink?(path)
end
# unnecessary DMG metadata
DMG_METADATA_FILES = %w[
.background
.com.apple.timemachine.donotpresent
.DocumentRevisions-V100
.DS_Store
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
].to_set.freeze
def dmg_metadata?(path)
relative_root = path.sub(%r{/.*}, "")
DMG_METADATA_FILES.include?(relative_root.basename.to_s)
end
def system_dir_symlink?(path)
# symlinks to system directories (commonly to /Applications)
path.symlink? && Hbc::MacOS.system_dir?(path.readlink)
end
def mounts_from_plist(plist)

View File

@ -232,140 +232,148 @@ module Hbc::MacOS
"/var/spool",
"/var/spool/mail",
"/var/tmp",
].map { |x| Pathname.new(x) }.freeze
]
.map(&method(:Pathname))
.to_set
.freeze
# TODO: There should be a way to specify a containing
# directory under which nothing can be deleted.
UNDELETABLE_DIRS = Set.new [
"~/",
"~/Applications",
"~/Desktop",
"~/Documents",
"~/Downloads",
"~/Mail",
"~/Movies",
"~/Music",
"~/Music/iTunes",
"~/Music/iTunes/iTunes Music",
"~/Music/iTunes/Album Artwork",
"~/News",
"~/Pictures",
"~/Pictures/Desktops",
"~/Pictures/Photo Booth",
"~/Pictures/iChat Icons",
"~/Pictures/iPhoto Library",
"~/Public",
"~/Sites",
"~/Library",
"~/Library/.localized",
"~/Library/Accessibility",
"~/Library/Accounts",
"~/Library/Address Book Plug-Ins",
"~/Library/Application Scripts",
"~/Library/Application Support",
"~/Library/Application Support/Apple",
"~/Library/Application Support/com.apple.AssistiveControl",
"~/Library/Application Support/com.apple.QuickLook",
"~/Library/Application Support/com.apple.TCC",
"~/Library/Assistants",
"~/Library/Audio",
"~/Library/Automator",
"~/Library/Autosave Information",
"~/Library/Caches",
"~/Library/Calendars",
"~/Library/ColorPickers",
"~/Library/ColorSync",
"~/Library/Colors",
"~/Library/Components",
"~/Library/Compositions",
"~/Library/Containers",
"~/Library/Contextual Menu Items",
"~/Library/Cookies",
"~/Library/DTDs",
"~/Library/Desktop Pictures",
"~/Library/Developer",
"~/Library/Dictionaries",
"~/Library/DirectoryServices",
"~/Library/Displays",
"~/Library/Documentation",
"~/Library/Extensions",
"~/Library/Favorites",
"~/Library/FileSync",
"~/Library/Filesystems",
"~/Library/Filters",
"~/Library/FontCollections",
"~/Library/Fonts",
"~/Library/Frameworks",
"~/Library/GameKit",
"~/Library/Graphics",
"~/Library/Group Containers",
"~/Library/Icons",
"~/Library/IdentityServices",
"~/Library/Image Capture",
"~/Library/Images",
"~/Library/Input Methods",
"~/Library/Internet Plug-Ins",
"~/Library/InternetAccounts",
"~/Library/iTunes",
"~/Library/KeyBindings",
"~/Library/Keyboard Layouts",
"~/Library/Keychains",
"~/Library/LaunchAgents",
"~/Library/LaunchDaemons",
"~/Library/LocationBundles",
"~/Library/LoginPlugins",
"~/Library/Logs",
"~/Library/Mail",
"~/Library/Mail Downloads",
"~/Library/Messages",
"~/Library/Metadata",
"~/Library/Mobile Documents",
"~/Library/MonitorPanels",
"~/Library/OpenDirectory",
"~/Library/PDF Services",
"~/Library/PhonePlugins",
"~/Library/Phones",
"~/Library/PreferencePanes",
"~/Library/Preferences",
"~/Library/Printers",
"~/Library/PrivateFrameworks",
"~/Library/PubSub",
"~/Library/QuickLook",
"~/Library/QuickTime",
"~/Library/Receipts",
"~/Library/Recent Servers",
"~/Library/Recents",
"~/Library/Safari",
"~/Library/Saved Application State",
"~/Library/Screen Savers",
"~/Library/ScreenReader",
"~/Library/ScriptingAdditions",
"~/Library/ScriptingDefinitions",
"~/Library/Scripts",
"~/Library/Security",
"~/Library/Services",
"~/Library/Sounds",
"~/Library/Speech",
"~/Library/Spelling",
"~/Library/Spotlight",
"~/Library/StartupItems",
"~/Library/StickiesDatabase",
"~/Library/Sync Services",
"~/Library/SyncServices",
"~/Library/SyncedPreferences",
"~/Library/TextEncodings",
"~/Library/User Pictures",
"~/Library/Video",
"~/Library/Voices",
"~/Library/WebKit",
"~/Library/WidgetResources",
"~/Library/Widgets",
"~/Library/Workflows",
]
.map { |x| Pathname.new(x).expand_path }
.concat(SYSTEM_DIRS)
UNDELETABLE_DIRS = [
"~/",
"~/Applications",
"~/Desktop",
"~/Documents",
"~/Downloads",
"~/Mail",
"~/Movies",
"~/Music",
"~/Music/iTunes",
"~/Music/iTunes/iTunes Music",
"~/Music/iTunes/Album Artwork",
"~/News",
"~/Pictures",
"~/Pictures/Desktops",
"~/Pictures/Photo Booth",
"~/Pictures/iChat Icons",
"~/Pictures/iPhoto Library",
"~/Public",
"~/Sites",
"~/Library",
"~/Library/.localized",
"~/Library/Accessibility",
"~/Library/Accounts",
"~/Library/Address Book Plug-Ins",
"~/Library/Application Scripts",
"~/Library/Application Support",
"~/Library/Application Support/Apple",
"~/Library/Application Support/com.apple.AssistiveControl",
"~/Library/Application Support/com.apple.QuickLook",
"~/Library/Application Support/com.apple.TCC",
"~/Library/Assistants",
"~/Library/Audio",
"~/Library/Automator",
"~/Library/Autosave Information",
"~/Library/Caches",
"~/Library/Calendars",
"~/Library/ColorPickers",
"~/Library/ColorSync",
"~/Library/Colors",
"~/Library/Components",
"~/Library/Compositions",
"~/Library/Containers",
"~/Library/Contextual Menu Items",
"~/Library/Cookies",
"~/Library/DTDs",
"~/Library/Desktop Pictures",
"~/Library/Developer",
"~/Library/Dictionaries",
"~/Library/DirectoryServices",
"~/Library/Displays",
"~/Library/Documentation",
"~/Library/Extensions",
"~/Library/Favorites",
"~/Library/FileSync",
"~/Library/Filesystems",
"~/Library/Filters",
"~/Library/FontCollections",
"~/Library/Fonts",
"~/Library/Frameworks",
"~/Library/GameKit",
"~/Library/Graphics",
"~/Library/Group Containers",
"~/Library/Icons",
"~/Library/IdentityServices",
"~/Library/Image Capture",
"~/Library/Images",
"~/Library/Input Methods",
"~/Library/Internet Plug-Ins",
"~/Library/InternetAccounts",
"~/Library/iTunes",
"~/Library/KeyBindings",
"~/Library/Keyboard Layouts",
"~/Library/Keychains",
"~/Library/LaunchAgents",
"~/Library/LaunchDaemons",
"~/Library/LocationBundles",
"~/Library/LoginPlugins",
"~/Library/Logs",
"~/Library/Mail",
"~/Library/Mail Downloads",
"~/Library/Messages",
"~/Library/Metadata",
"~/Library/Mobile Documents",
"~/Library/MonitorPanels",
"~/Library/OpenDirectory",
"~/Library/PDF Services",
"~/Library/PhonePlugins",
"~/Library/Phones",
"~/Library/PreferencePanes",
"~/Library/Preferences",
"~/Library/Printers",
"~/Library/PrivateFrameworks",
"~/Library/PubSub",
"~/Library/QuickLook",
"~/Library/QuickTime",
"~/Library/Receipts",
"~/Library/Recent Servers",
"~/Library/Recents",
"~/Library/Safari",
"~/Library/Saved Application State",
"~/Library/Screen Savers",
"~/Library/ScreenReader",
"~/Library/ScriptingAdditions",
"~/Library/ScriptingDefinitions",
"~/Library/Scripts",
"~/Library/Security",
"~/Library/Services",
"~/Library/Sounds",
"~/Library/Speech",
"~/Library/Spelling",
"~/Library/Spotlight",
"~/Library/StartupItems",
"~/Library/StickiesDatabase",
"~/Library/Sync Services",
"~/Library/SyncServices",
"~/Library/SyncedPreferences",
"~/Library/TextEncodings",
"~/Library/User Pictures",
"~/Library/Video",
"~/Library/Voices",
"~/Library/WebKit",
"~/Library/WidgetResources",
"~/Library/Widgets",
"~/Library/Workflows",
]
.map { |x| Pathname(x).expand_path }
.to_set
.union(SYSTEM_DIRS)
.freeze
def system_dir?(dir)
SYSTEM_DIRS.any? { |u| File.identical?(u, dir) }
end
def undeletable?(dir)
UNDELETABLE_DIRS.any? { |u| File.identical?(u, dir) }
end