|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 #!/usr/bin/python |
|
17 |
|
18 import datetime, os, os.path, sys, traceback |
|
19 from optparse import OptionParser |
|
20 |
|
21 """This script sets up S60 distribution policy files.""" |
|
22 |
|
23 class PolicyData: |
|
24 def __init__(self): |
|
25 self.root_policy = None |
|
26 self.policies = [] |
|
27 self.created_policies = 0 |
|
28 self.existing_policies = 0 |
|
29 self.warning_count = 0 |
|
30 self.start_time = datetime.datetime.now() |
|
31 self.stop_time = None |
|
32 def __str__(self): |
|
33 if self.stop_time is None: |
|
34 self.stop_time = datetime.datetime.now() |
|
35 msg = "Distribution policies generation time " + str(self.stop_time - self.start_time) |
|
36 msg += "\nCreated policies: " + str(self.created_policies) |
|
37 msg += "\nExisting policies: " + str(self.existing_policies) |
|
38 if self.warning_count > 0: |
|
39 msg += "\nWarnings: " + str(self.warning_count) |
|
40 return msg |
|
41 |
|
42 policy_data = PolicyData() |
|
43 |
|
44 def main(): |
|
45 parser = OptionParser(usage = "python -u %prog [options] <path>") |
|
46 (opts, args) = parser.parse_args() |
|
47 root_path = args[0] |
|
48 if not os.path.isdir(root_path): |
|
49 print "ERROR: %s is not a directory" % root_path |
|
50 sys.exit(1); |
|
51 |
|
52 try: |
|
53 global policy_data |
|
54 print "Started " + str(policy_data.start_time) |
|
55 parseConfigurationFile(policy_data) |
|
56 print "Generating S60 distribution policies to", root_path |
|
57 print "Policy config:", str(policy_data.policies) |
|
58 print "Policy for root dir:", str(policy_data.root_policy) |
|
59 writePolicies(root_path, policy_data) |
|
60 policy_data.stop_time = datetime.datetime.now() |
|
61 print "Finished " + str(policy_data.stop_time) |
|
62 print str(policy_data) |
|
63 except: |
|
64 print "ERROR: Unexpected exception" |
|
65 traceback.print_exc() |
|
66 print str(policy_data) |
|
67 sys.exit(1) |
|
68 |
|
69 def parseConfigurationFile(policy_data): |
|
70 config_file = "distribution.policy.s60.configuration.txt" |
|
71 f = open(config_file) |
|
72 try: |
|
73 default_policy = None |
|
74 line_count = 0; |
|
75 for line in f.readlines(): |
|
76 line_count += 1 |
|
77 line = line.strip() |
|
78 if len(line) == 0 or line.find("#") == 0: |
|
79 continue |
|
80 tokens = line.split(':') |
|
81 if len(tokens) != 2: |
|
82 print "ERROR: Invalid line", line_count, "at file", config_file |
|
83 sys.exit(1) |
|
84 dir = tokens[0].strip() |
|
85 dir = dir.replace("/", os.sep) |
|
86 policy = tokens[1].strip() |
|
87 if dir == "DEFAULT_POLICY": |
|
88 default_policy = policy |
|
89 elif dir == "ROOT_POLICY": |
|
90 policy_data.root_policy = policy |
|
91 else: |
|
92 policy_data.policies += [(dir, policy)] |
|
93 if default_policy is None: |
|
94 print "ERROR: Default policy not defined at", config_file |
|
95 sys.exit(1) |
|
96 policy_data.policies += [("", default_policy)] |
|
97 finally: |
|
98 f.close() |
|
99 |
|
100 def writePolicies(root_path, policy_data): |
|
101 # Write policy for the root directory. |
|
102 if (not policy_data.root_policy is None) and \ |
|
103 (policy_data.root_policy != "IGNORE"): |
|
104 writePolicy(root_path, policy_data.root_policy) |
|
105 # Write policies for the subdirectories. |
|
106 for root, dirs, files in os.walk(root_path): |
|
107 for dir in dirs: |
|
108 path = os.path.join(root, dir) |
|
109 policy = getPolicyForPath(path.replace(root_path, ""), policy_data) |
|
110 if policy != "IGNORE": |
|
111 writePolicy(path, policy) |
|
112 |
|
113 def writePolicy(path, policy): |
|
114 global policy_data |
|
115 policy_file = os.path.join(path, 'distribution.policy.s60') |
|
116 |
|
117 if os.path.exists(policy_file): |
|
118 existing_policy = readPolicy(policy_file) |
|
119 if policy == existing_policy: |
|
120 print "Policy %s already exists in %s" % (existing_policy, policy_file) |
|
121 else: |
|
122 print "WARNING: Policy %s (should be %s) already exists in %s" % (existing_policy, policy, policy_file) |
|
123 policy_data.warning_count += 1 |
|
124 policy_data.existing_policies += 1 |
|
125 return |
|
126 |
|
127 f = open(policy_file, "w+") |
|
128 try: |
|
129 print "Writing policy", policy, "to", policy_file |
|
130 f.write(policy) |
|
131 policy_data.created_policies += 1 |
|
132 except IOError: |
|
133 print "ERROR: Writing policy to", policy_file, "failed" |
|
134 sys.exit(1) |
|
135 finally: |
|
136 f.close |
|
137 |
|
138 def readPolicy(policy_file): |
|
139 policy = None |
|
140 f = open(policy_file) |
|
141 try: |
|
142 for line in f.readlines(): |
|
143 # Take the last line from the policy. |
|
144 policy = line.strip() |
|
145 finally: |
|
146 f.close |
|
147 return policy |
|
148 |
|
149 def getPolicyForPath(target_path, policy_data): |
|
150 for path, policy in policy_data.policies: |
|
151 match_path = target_path |
|
152 if path.find("ROOT" + os.sep) == 0: |
|
153 # Add keyword ROOT to target_path so that only |
|
154 # paths in root directory will match. |
|
155 match_path = "ROOT" + target_path |
|
156 if match_path.find(path + os.sep) != -1 or \ |
|
157 match_path.find(path, -len(path)) != -1: |
|
158 return policy |
|
159 return None |
|
160 |
|
161 if __name__ == "__main__": |
|
162 main() |