0
|
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 |
|
|
17 |
import os
|
|
18 |
import sys
|
|
19 |
import logging
|
|
20 |
|
|
21 |
from cone.public import exceptions, plugin, utils, api
|
|
22 |
import crml_writer, crml_comparator
|
|
23 |
from crml_model import *
|
|
24 |
|
|
25 |
class CrmlImpl(plugin.ImplBase):
|
|
26 |
IMPL_TYPE_ID = 'crml'
|
|
27 |
|
|
28 |
RFS_RECORDS_LIST_VARNAME = 'crml_cenrep_rfs_records_list'
|
|
29 |
RFS_TXT_GENERATED_VARNAME = 'crml_cenrep_rfs_txt_generated'
|
|
30 |
|
|
31 |
def __init__(self, resource_ref, configuration, repository):
|
|
32 |
plugin.ImplBase.__init__(self, resource_ref, configuration)
|
|
33 |
self.resource_ref = resource_ref
|
|
34 |
self.configuration = configuration
|
|
35 |
self.logger = logging.getLogger('cone.crml(%s)' % self.resource_ref)
|
|
36 |
self.repository = repository
|
3
|
37 |
|
|
38 |
def __getstate__(self):
|
|
39 |
state = super(CrmlImpl, self).__getstate__()
|
|
40 |
state['repository'] = self.__dict__.get('repository',None)
|
|
41 |
return state
|
|
42 |
|
|
43 |
|
0
|
44 |
def generate(self, context=None):
|
|
45 |
# Quick fix
|
|
46 |
if context:
|
|
47 |
self.generation_context = context
|
3
|
48 |
|
|
49 |
# See if delta CenReps should be generated
|
|
50 |
delta_cenrep = context and context.changed_refs is not None \
|
|
51 |
and 'deltacenrep' in context.tags.get('crml', [])
|
|
52 |
|
|
53 |
changed_refs = None
|
|
54 |
if delta_cenrep:
|
|
55 |
changed_refs = context.changed_refs
|
|
56 |
|
|
57 |
# Hard-coded output for delta CenReps for now
|
|
58 |
self.output_subdir = 'deltacenreps'
|
|
59 |
self.plugin_output = ''
|
|
60 |
|
0
|
61 |
file_path = self._get_cenrep_txt_file_path()
|
|
62 |
self.logger.debug("Generating file '%s'..." % file_path)
|
|
63 |
|
|
64 |
# Generate CenRep text data and write it to the output file
|
3
|
65 |
writer = crml_writer.CrmlTxtWriter(context, self.logger)
|
|
66 |
data = writer.get_cenrep_txt_data(self.repository, changed_refs).encode('UTF-16')
|
0
|
67 |
self._write_to_file(file_path, data)
|
|
68 |
|
3
|
69 |
# Add to the generated files list
|
|
70 |
KEY = 'crml_generated_cenrep_files'
|
|
71 |
lst = context.impl_data_dict.setdefault(KEY, [])
|
|
72 |
lst.append((os.path.basename(file_path), os.path.abspath(file_path)))
|
|
73 |
|
0
|
74 |
|
|
75 |
# Collect the record for cenrep_rfs.txt generation in post_generate()
|
|
76 |
if self.generation_context is not None:
|
|
77 |
rfs_record = writer.get_cenrep_rfs_record(self.repository)
|
|
78 |
if rfs_record:
|
|
79 |
# Add the record to the dictionary
|
|
80 |
data_dict = self.generation_context.impl_data_dict
|
|
81 |
VARNAME = self.RFS_RECORDS_LIST_VARNAME
|
|
82 |
if VARNAME not in data_dict:
|
|
83 |
data_dict[VARNAME] = []
|
|
84 |
data_dict[VARNAME].append(rfs_record)
|
|
85 |
|
|
86 |
def post_generate(self, context=None):
|
|
87 |
# Quick fix
|
|
88 |
if context:
|
|
89 |
self.generation_context = context
|
|
90 |
if self._is_cenrep_rfs_txt_to_be_generated():
|
|
91 |
# Generate CenRep RFS text file if not already generated
|
|
92 |
data_dict = self.generation_context.impl_data_dict
|
|
93 |
if self.RFS_TXT_GENERATED_VARNAME not in data_dict:
|
|
94 |
rfs_records = data_dict.get(self.RFS_RECORDS_LIST_VARNAME, [])
|
|
95 |
|
|
96 |
file_path = self._get_cenrep_rfs_txt_file_path()
|
|
97 |
writer = crml_writer.CrmlTxtWriter(self.configuration, self.logger)
|
|
98 |
data = writer.get_cenrep_rfs_txt_data(rfs_records).encode('UTF-16')
|
|
99 |
self._write_to_file(file_path, data)
|
|
100 |
|
|
101 |
data_dict[self.RFS_TXT_GENERATED_VARNAME] = True
|
|
102 |
|
|
103 |
def list_output_files(self):
|
|
104 |
"""
|
|
105 |
Return a list of output files as an array.
|
|
106 |
"""
|
|
107 |
files = [self._get_cenrep_txt_file_path()]
|
|
108 |
if self._is_cenrep_rfs_txt_to_be_generated():
|
|
109 |
files.append(self._get_cenrep_rfs_txt_file_path())
|
|
110 |
return files
|
|
111 |
|
|
112 |
def get_refs(self):
|
|
113 |
if self.repository is None:
|
|
114 |
return []
|
|
115 |
else:
|
|
116 |
return self.repository.get_refs()
|
|
117 |
|
|
118 |
def get_flat_comparison_id(self):
|
|
119 |
return crml_comparator.CrmlComparator.get_flat_comparison_id(self.repository)
|
|
120 |
|
|
121 |
def get_flat_comparison_extra_data(self):
|
|
122 |
return crml_comparator.CrmlComparator.get_flat_comparison_extra_data(self.repository)
|
|
123 |
|
|
124 |
@classmethod
|
|
125 |
def get_flat_comparison_impl_type_id(cls):
|
|
126 |
return 'crml'
|
|
127 |
|
|
128 |
def flat_compare(self, other):
|
|
129 |
comparator = crml_comparator.CrmlComparator(self.resource_ref, self.repository)
|
|
130 |
return comparator.flat_compare(other.resource_ref, other.repository)
|
|
131 |
|
|
132 |
def _get_cenrep_txt_file_path(self):
|
|
133 |
"""
|
|
134 |
Return the full path to the CenRep text file generated by this implementation
|
|
135 |
"""
|
|
136 |
uid = self.repository.uid_value
|
|
137 |
if uid.startswith('0x'): uid = uid[2:]
|
|
138 |
return os.path.normpath(os.path.join(self.output, uid + '.txt'))
|
|
139 |
|
|
140 |
def _get_cenrep_rfs_txt_file_path(self):
|
|
141 |
"""
|
|
142 |
Return the full path to the CenRep RFS text file
|
|
143 |
"""
|
|
144 |
# cenrep_rfs.txt goes to a different place than the rest of
|
|
145 |
# the CenRep files, so temporarily override plugin_output
|
|
146 |
# for that purpose
|
|
147 |
orig_pluginoutput = self.plugin_output
|
|
148 |
self.plugin_output = 'private/100059C9'
|
|
149 |
rfs_txt_path = os.path.normpath(os.path.join(self.output, 'cenrep_rfs.txt'))
|
|
150 |
self.plugin_output = orig_pluginoutput
|
|
151 |
return rfs_txt_path
|
|
152 |
|
|
153 |
def _is_cenrep_rfs_txt_to_be_generated(self):
|
|
154 |
"""
|
|
155 |
Return whether the CenRep RFS text file is to be generated.
|
|
156 |
"""
|
|
157 |
if self.generation_context is None:
|
|
158 |
return False
|
|
159 |
|
|
160 |
targets = self.generation_context.tags.get('target', [])
|
|
161 |
return 'core' in targets or 'rofs2' in targets
|
|
162 |
|
|
163 |
def _write_to_file(self, file_path, data):
|
|
164 |
|
|
165 |
# Write data
|
3
|
166 |
f = self.generation_context.create_file(file_path, implementation=self)
|
0
|
167 |
try: f.write(data)
|
|
168 |
finally: f.close()
|