13 # |
13 # |
14 # Description: |
14 # Description: |
15 # |
15 # |
16 #!/usr/bin/python |
16 #!/usr/bin/python |
17 |
17 |
18 import datetime, os, os.path, sys, traceback |
18 import datetime, os, os.path, sys, traceback, shutil |
19 from optparse import OptionParser |
19 from optparse import OptionParser |
20 |
20 |
21 """This script sets up S60 distribution policy files.""" |
21 """This script sets up S60 distribution policy files and optionally filters out non-distributed directories.""" |
22 |
22 |
23 class PolicyData: |
23 class PolicyData: |
24 def __init__(self): |
24 def __init__(self): |
25 self.root_policy = None |
25 self.root_policy = None |
26 self.policies = [] |
26 self.policies = [] |
|
27 self.filtered_directories = 0 |
27 self.created_policies = 0 |
28 self.created_policies = 0 |
28 self.existing_policies = 0 |
29 self.existing_policies = 0 |
29 self.warning_count = 0 |
30 self.warning_count = 0 |
30 self.start_time = datetime.datetime.now() |
31 self.start_time = datetime.datetime.now() |
31 self.stop_time = None |
32 self.stop_time = None |
33 if self.stop_time is None: |
34 if self.stop_time is None: |
34 self.stop_time = datetime.datetime.now() |
35 self.stop_time = datetime.datetime.now() |
35 msg = "Distribution policies generation time " + str(self.stop_time - self.start_time) |
36 msg = "Distribution policies generation time " + str(self.stop_time - self.start_time) |
36 msg += "\nCreated policies: " + str(self.created_policies) |
37 msg += "\nCreated policies: " + str(self.created_policies) |
37 msg += "\nExisting policies: " + str(self.existing_policies) |
38 msg += "\nExisting policies: " + str(self.existing_policies) |
|
39 msg += "\nFiltered directories: " + str(self.filtered_directories) |
38 if self.warning_count > 0: |
40 if self.warning_count > 0: |
39 msg += "\nWarnings: " + str(self.warning_count) |
41 msg += "\nWarnings: " + str(self.warning_count) |
40 return msg |
42 return msg |
41 |
43 |
42 policy_data = PolicyData() |
44 policy_data = PolicyData() |
43 |
45 |
44 def main(): |
46 def main(): |
45 parser = OptionParser(usage = "python -u %prog [options] <path>") |
47 parser = OptionParser(usage = "python -u %prog [options] <path>") |
|
48 parser.add_option("-f", "--filter", action="store_true", dest="filter", |
|
49 help="filter non-distributed directories") |
46 (opts, args) = parser.parse_args() |
50 (opts, args) = parser.parse_args() |
|
51 if len(args) == 0: |
|
52 print "Mandatory argument <path> missing" |
|
53 parser.print_help() |
|
54 sys.exit(1); |
|
55 |
47 root_path = args[0] |
56 root_path = args[0] |
48 if not os.path.isdir(root_path): |
57 if not os.path.isdir(root_path): |
49 print "ERROR: %s is not a directory" % root_path |
58 print "ERROR: %s is not a directory" % root_path |
50 sys.exit(1); |
59 sys.exit(1); |
51 |
60 |
52 try: |
61 try: |
53 global policy_data |
62 global policy_data |
54 print "Started " + str(policy_data.start_time) |
63 print "Started " + str(policy_data.start_time) |
55 parseConfigurationFile(policy_data) |
64 parseConfigurationFile(policy_data) |
56 print "Generating S60 distribution policies to", root_path |
65 print "Generating S60 distribution policies to", root_path |
|
66 if opts.filter: |
|
67 print "Removing non-contributed directories" |
57 print "Policy config:", str(policy_data.policies) |
68 print "Policy config:", str(policy_data.policies) |
58 print "Policy for root dir:", str(policy_data.root_policy) |
69 print "Policy for root dir:", str(policy_data.root_policy) |
59 writePolicies(root_path, policy_data) |
70 filterAndWritePolicies(root_path, policy_data, opts.filter) |
60 policy_data.stop_time = datetime.datetime.now() |
71 policy_data.stop_time = datetime.datetime.now() |
61 print "Finished " + str(policy_data.stop_time) |
72 print "Finished " + str(policy_data.stop_time) |
62 print str(policy_data) |
73 print str(policy_data) |
63 except: |
74 except: |
64 print "ERROR: Unexpected exception" |
75 print "ERROR: Unexpected exception" |
95 sys.exit(1) |
106 sys.exit(1) |
96 policy_data.policies += [("", default_policy)] |
107 policy_data.policies += [("", default_policy)] |
97 finally: |
108 finally: |
98 f.close() |
109 f.close() |
99 |
110 |
100 def writePolicies(root_path, policy_data): |
111 def filterAndWritePolicies(root_path, policy_data, filter): |
101 # Write policy for the root directory. |
112 # Write policy for the root directory. |
102 if (not policy_data.root_policy is None) and \ |
113 if (not policy_data.root_policy is None) and \ |
103 (policy_data.root_policy != "IGNORE"): |
114 (policy_data.root_policy != "IGNORE"): |
104 writePolicy(root_path, policy_data.root_policy) |
115 writePolicy(root_path, policy_data.root_policy) |
105 # Write policies for the subdirectories. |
116 # Write policies for the subdirectories. |
106 for root, dirs, files in os.walk(root_path): |
117 for root, dirs, files in os.walk(root_path): |
107 for dir in dirs: |
118 for dir in dirs: |
108 path = os.path.join(root, dir) |
119 path = os.path.join(root, dir) |
109 policy = getPolicyForPath(path.replace(root_path, ""), policy_data) |
120 policy = getPolicyForPath(path.replace(root_path, ""), policy_data) |
110 if policy != "IGNORE": |
121 if policy != "IGNORE": |
111 writePolicy(path, policy) |
122 if filter and policy != "7": |
|
123 print "Removing directory", path |
|
124 shutil.rmtree(path) |
|
125 policy_data.filtered_directories += 1 |
|
126 else: |
|
127 writePolicy(path, policy) |
112 |
128 |
113 def writePolicy(path, policy): |
129 def writePolicy(path, policy): |
114 global policy_data |
130 global policy_data |
115 policy_file = os.path.join(path, 'distribution.policy.s60') |
131 policy_file = os.path.join(path, 'distribution.policy.s60') |
116 |
132 |