Guest User

Untitled

a guest
Jun 18th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'Logger'
  4. require 'optparse'
  5. require_relative 'identity-manager.rb'
  6.  
  7. $logger = Logger.new(STDOUT)
  8. $logger.level = Logger::ERROR
  9. $logger.formatter = proc do |severity, datetime, progname, message|
  10. "#{message}\n"
  11. end
  12.  
  13. def find_frameworks_in_app(app_bundle_path)
  14. fail "Error accessing the specified app bundle" unless File.directory? app_bundle_path
  15. frameworks_folder_path = File.join(app_bundle_path, "Contents", "Frameworks")
  16. fail "Error accessing frameworks folder" unless File.directory? frameworks_folder_path
  17. frameworks_to_return = []
  18.  
  19. Dir.glob("#{frameworks_folder_path}/*.framework").each do |first_level_framework_path|
  20. frameworks = find_recursive_framework_paths(first_level_framework_path)
  21. frameworks_to_return += frameworks if frameworks
  22. end
  23.  
  24. $logger.info "Found Frameworks: #{frameworks_to_return}"
  25. frameworks_to_return
  26. end
  27.  
  28. def find_recursive_framework_paths(framework_bundle_path)
  29. $logger.info "Looking for internal frameworks at #{framework_bundle_path}"
  30. framework_paths_to_return = []
  31. fail "Error accessing frameworking bundle path" unless File.directory? framework_bundle_path
  32.  
  33. frameworks_folder_path = File.join(framework_bundle_path, "Frameworks")
  34. if File.directory? frameworks_folder_path
  35. Dir.glob("#{frameworks_folder_path}/*.framework").each do |framework_path|
  36. internal_frameworks = find_recursive_framework_paths(framework_path)
  37. framework_paths_to_return += internal_frameworks if internal_frameworks
  38. end
  39. end
  40.  
  41. framework_paths_to_return << framework_bundle_path
  42. end
  43.  
  44. def find_matching_signing_identity(identity_search_string = nil, team_identifier = nil, exact_match = true)
  45. $logger.info "Searching for identity external #{identity_search_string}, team identifier #{team_identifier}, exact match #{exact_match}"
  46.  
  47. identity_manager = IdentityManager.new
  48. identity_manager.logger = $logger
  49.  
  50. # First, search for identities without any specified search string or team identifier
  51. signing_identities = identity_manager.find_identities
  52.  
  53. if signing_identities == nil || signing_identities.count == 0
  54. # No valid identities at all on this machine. Quit.
  55. fail "No valid signing identities found on this machine."
  56. elsif signing_identities.count == 1
  57. # There is exactly one signing identity. We should check whether it matches, or if we can return it anyway because we're not looking for an exact match.
  58. only_identity = signing_identities.first
  59. identity_matches_search_string = identity_search_string == nil || identity.full_name_string.contains?(identity_search_string)
  60. identity_matches_team_identifier = team_identifier == nil || identity.team_identifier == team_identifier
  61.  
  62. if (identity_matches_search_string && identity_matches_team_identifier) || exact_match == false
  63. # We're good. Either we don't need an exact matches, or we have found one with this specific identity.
  64. signing_identities.first
  65. else
  66. # Only one signing identity, and it doesn't match. Quit.
  67. fail "The only identity on this system: #{only_identity.full_name_string} doesn't match the specified search string or team identifier!"
  68. end
  69. else
  70. # More than one signing idenitty. We need to run a search to narrow them down. If we find a match, return that.
  71. signing_identities = identity_manager.find_identities(identity_search_string, team_identifier)
  72. fail "Can't find any signing identities for the specified search string (#{identity_search_string}) and team identifier (#{team_identifier})!" unless signing_identities && signing_identities.count > 0
  73. fail "Can't disambiguate identities based on search string (#{identity_search_string}) and team identifier (#{team_identifier}). Found #{signing_identities.count} identities! #{signing_identities.collect { |identity| identity.full_name_string }}" unless signing_identities.count == 1
  74. signing_identities.first
  75. end
  76. end
  77.  
  78. options = {}
  79. options[:verbose] = false
  80. options[:match_exactly] = true
  81.  
  82. OptionParser.new do |opts|
  83. opts.banner = "Usage: #{opts.program_name} [options] path"
  84.  
  85. opts.on("-v", "--[no-]verbose", "Run verbosely. False by default.") do |v|
  86. options[:verbose] = v
  87. end
  88.  
  89. opts.on("-t", "--team-identifier IDENTIFIER", "Match identity by team identifier") do |team_identifier|
  90. options[:team_identifier] = team_identifier
  91. end
  92.  
  93. opts.on("-m", "--[no-]exact-match", "Match exact identity. Error if we can't disambiguate. True by default.") do |match_exactly|
  94. options[:match_exactly] = match_exactly
  95. end
  96.  
  97. opts.on("-s", "--search-string STRING", "Match identity by search string") do |search_string|
  98. options[:search_string] = search_string
  99. end
  100. end.parse!
  101.  
  102. fail "Target app not specified." if ARGV.count == 0
  103. fail "Problem parsing arguments. Expected 1 argument (target app path) after prasing. Found #{ARGV.count}. #{ARGV}" if ARGV.count > 1
  104.  
  105. target_app_path = ARGV.first
  106. fail "Invalid target app specified: #{target_app_path}." if not File.directory? target_app_path
  107.  
  108. if options[:verbose] == true
  109. $logger.level= Logger::INFO
  110. end
  111.  
  112. framework_paths = find_frameworks_in_app(target_app_path)
  113. signing_identity = find_matching_signing_identity(options[:search_string], options[:team_identifier], options[:exact_match])
  114. fail "Coundn't find identity to sign with!" unless signing_identity
  115. framework_paths.each do |framework_path|
  116. signing_identity.sign_bundle(framework_path)
  117. end
Advertisement
Add Comment
Please, Sign In to add comment