Chameleon

Chameleon Svn Source Tree

Root/branches/zenith432/package/Scripts.templates/CleanOptions/clean_bootplist.pl

  • Property svn:executable set to *
1#!/usr/bin/perl
2
3use strict;
4use YAML::Syck;
5
6our $target_volume;
7our $config_file;
8our $plist_filepath;
9
10
11
12our $yaml_file="@YAML_FILE@";
13
14if ($#ARGV < 1) {
15 print stderr "A target volume and the plist file are needed\n";
16} else {
17 $target_volume=$ARGV[0];
18 $config_file=$ARGV[1];
19}
20
21$plist_filepath = "${target_volume}/Extra/${config_file}";
22if ( -f "$plist_filepath" ) {
23 main("$yaml_file");
24}
25
26sub _do_cmd {
27 my ($cmd, $key, $value) = @_;
28 my $out;
29
30 $key =~ s/([\s])/\\$1/g; # Escape characters in key
31 $value =~ s/([\s"])/\\$1/g; # Escape characters in value (space & ")
32 my $plistbuddy_command="$cmd :$key $value";
33
34 open ( OUTPUT, "-|", '/usr/libexec/PlistBuddy', "-c", "$plistbuddy_command", "$plist_filepath" );
35 my $exit_code = $?;
36 chomp($out = <OUTPUT>);
37 close OUTPUT;
38 return $out;
39}
40
41sub get_boot_plist_option {
42 my ($option) = @_;
43 return _do_cmd( "Print", "$option");
44}
45
46sub delete_boot_plist_option {
47 my ($option) = @_;
48 return _do_cmd( "Delete", "$option");
49}
50
51sub delete_boot_plist_text_option {
52 my ($option, $values) = @_;
53 my $current_value = get_boot_plist_option "$option";
54 if ( $current_value ne "") {
55 foreach my $recognized_value (@{$values}) {
56 if ($recognized_value eq $current_value) {
57 return _do_cmd( "Delete", "$option");
58 }
59 }
60 }
61 return "";
62}
63
64sub delete_boot_plist_list_option {
65 my ($option, $values) = @_;
66 my $current_value = get_boot_plist_option "$option";
67 if ( $current_value ne "") {
68 my %count;
69 my @new_list;
70 foreach my $value (@{$values}) {
71 $count{$value} = 1;
72 }
73 foreach my $value (split(/\s+/,$current_value)) {
74 if ( not exists $count{$value} ) {
75 push @new_list, $value;
76 }
77 }
78 return _do_cmd( "Set", $option, join(' ',@new_list) );
79 }
80 return "";
81}
82
83sub main() {
84 # Remove all options that installer can managed
85 my ($yaml_file) = @_;
86 my $hash_ref = LoadFile($yaml_file) or die "Can't open yaml file\n";
87 foreach my $option ( keys %{$hash_ref} ) {
88 my $type = $hash_ref->{$option}->{type};
89 if ( $type =~ /^bool$/i ) {
90 delete_boot_plist_option($option);
91 } elsif ( $type =~ /^text$/i ) {
92 delete_boot_plist_text_option( $option,
93 $hash_ref->{$option}->{values} );
94 } elsif ( $type =~ /^list$/i ) {
95 delete_boot_plist_list_option( $option,
96 $hash_ref->{$option}->{values} );
97 }
98 }
99}
100

Archive Download this file

Revision: HEAD