stub out things to get the beatles xbox site up and running
[moon.git] / class / tuning / SecurityAttributes / merge.cs
blob6ba341eb3867a17c3ec6319456220bfd6fd1d6cc
1 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
21 using System;
22 using System.Collections.Generic;
23 using System.IO;
24 using System.Text;
26 using Moonlight.SecurityModel;
28 class Program {
30 static List<string> additions = new List<string> ();
31 static List<string> removals = new List<string> ();
32 static List<string> always = new List<string> ();
34 private static List<string> Merge ()
36 List<string> lines = new List<string> ();
38 foreach (string add in additions) {
39 // check if this addition was 'manually' overidden
40 if (removals.Contains (add))
41 continue;
43 if (!lines.Contains (add))
44 lines.Add (add);
47 foreach (string add in always) {
48 // add '!' into the list (if they were not already there)
49 if (!lines.Contains (add))
50 lines.Add (add);
53 // simplify
55 for (int i = lines.Count - 1; i >= 0; i--) {
56 string line = lines [i];
57 // if this is a method...
58 bool critical_method = line.StartsWith ("SC-M: ");
59 bool safe_method = line.StartsWith ("SSC-M: ");
60 if (critical_method || safe_method) {
61 // then check if the type is already marked
62 int start = line.IndexOf (' ', 7) + 1;
63 int length = line.IndexOf ("::", start) - start;
64 string type = line.Substring (start, length);
66 if (safe_method) {
67 // if it's a safe method then it's not worth having it inside a SSC type
68 if (lines.Contains ("SSC-T: " + type))
69 lines.RemoveAt (i);
72 // if it's a critical (or safe) method then it's not worth having inside a SC type
73 if (lines.Contains ("SC-T: " + type))
74 lines.RemoveAt (i);
78 lines.Sort ();
79 return lines;
83 // 1. read all files for an assembly
84 // 2. copy all lines that start with a '+'
85 // 3. remove (from copy) all lines that start with a '-'
86 // 4. add (to copy) all lines that start with a '!'
87 // 5. sort
88 // 6. save
90 static void ProcessLine (string line)
92 if (String.IsNullOrEmpty (line))
93 return;
95 switch (line [0]) {
96 case '#':
97 // don't copy comments into the merged files
98 break;
99 case '!':
100 always.Add (line.Substring (1));
101 break;
102 case '+':
103 additions.Add (line.Substring (1));
104 break;
105 case '-':
106 removals.Add (line.Substring (1));
107 break;
108 default:
109 // if the line is not empty (spaces, tabs) then display an error
110 if (line.Trim ().Length > 0)
111 Console.WriteLine ("\t\tUnknown command '{0}'.", line [0]);
112 break;
116 static void ProcessFile (string file)
118 Console.Write ("\tfile '{0}' ", file);
119 if (!File.Exists (file)) {
120 Console.WriteLine ("not found");
121 return;
124 using (StreamReader sr = new StreamReader (file)) {
125 while (!sr.EndOfStream)
126 ProcessLine (sr.ReadLine ());
128 Console.WriteLine ("processed");
131 static void Process (string assembly, string input, string output)
133 // read (add) all autogenerated [SecurityCritical] (from detect-sc)
134 string filename = Path.Combine (Path.Combine (input, "automatic"), assembly + ".auto.sc");
135 ProcessFile (filename);
137 // read (add) all autogenerated [SecuritySafeCritical] (from detect-ssc)
138 filename = Path.Combine (Path.Combine (input, "automatic"), assembly + ".auto.ssc");
139 ProcessFile (filename);
141 // read (add / remove) all manual overrides
142 filename = Path.Combine (Path.Combine (input, "overrides"), assembly + ".manual");
143 ProcessFile (filename);
145 // read (add) all "required for compatibility" [SecurityCritical] (from find-sc)
146 filename = Path.Combine (Path.Combine (input, "compatibility"), assembly + ".compat.sc");
147 ProcessFile (filename);
149 // make it easier for humans
150 List<string> lines = Merge ();
152 filename = Path.Combine (output, assembly + ".secattr");
153 using (StreamWriter sw = new StreamWriter (filename)) {
154 foreach (string line in lines)
155 sw.WriteLine (line);
158 additions.Clear ();
159 removals.Clear ();
160 always.Clear ();
163 static int Main (string [] args)
165 if (args.Length < 3) {
166 Console.WriteLine ("Usage: merge input-dir output-dir assembly.secattr");
167 return 1;
170 string input = args [0];
171 string output = args [1];
172 string secattr = args [2];
174 if (!secattr.EndsWith (".secattr")) {
175 Console.WriteLine ("Usage: merge input-dir output-dir assembly.secattr");
176 return 1;
179 string assembly = secattr.Substring (0, secattr.Length - ".secattr".Length);
181 if (Array.IndexOf (PlatformCode.Assemblies, assembly) == -1) {
182 Console.WriteLine ("asssembly {0} is not a platform code assembly", assembly);
183 return 1;
186 // {input}/compatibility/"assembly".compat.sc find-sc only !
187 // {input}/automatic/"assembly".auto.sc detect-sc only +
188 // {input}/automatic/"assembly".auto.ssc detect-ssc only +
189 // {input}/overrides/"assembly".manual (manual) + or -
190 // into
191 // {output}/"assembly".secattr
193 Process (assembly, input, output);
195 return 0;