-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathtransforms.py
More file actions
532 lines (425 loc) · 18.5 KB
/
transforms.py
File metadata and controls
532 lines (425 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import abc
import functools
import io
import os
import re
import shutil
import typing as T
import zipfile
from pathlib import Path
from zipfile import ZipFile
from lxml import etree as ET
from pydantic.v1 import BaseModel, root_validator
from cumulusci.core.dependencies.utils import TaskContext
from cumulusci.core.enums import StrEnum
from cumulusci.core.exceptions import CumulusCIException, TaskOptionsError
from cumulusci.tasks.metadata.package import RemoveSourceComponents
from cumulusci.utils import (
cd,
inject_namespace,
strip_namespace,
temporary_dir,
tokenize_namespace,
zip_clean_metaxml,
)
from cumulusci.utils.xml import metadata_tree
from cumulusci.utils.ziputils import process_text_in_zipfile
class SourceTransform(abc.ABC):
"""Abstract base class for a transformation applied to a Metadata API deployment package"""
options_model: T.Optional[T.Type[BaseModel]]
identifier: str
def __init__(self):
...
@abc.abstractmethod
def process(self, zf: ZipFile, context: TaskContext) -> ZipFile:
...
class SourceTransformSpec(BaseModel):
transform: str
options: T.Optional[dict]
def parsed_options(self) -> T.Optional[BaseModel]:
transform_cls = get_available_transforms()[self.transform]
if transform_cls.options_model:
return transform_cls.options_model.parse_obj(self.options or {})
return None
@root_validator
def validate_spec(cls, values):
transform = values.get("transform")
if transform not in get_available_transforms():
raise ValueError(f"Transform {transform} is not valid")
transform_cls = get_available_transforms()[transform]
if transform_cls.options_model:
transform_cls.options_model.parse_obj(values.get("options"))
return values
def as_transform(self) -> SourceTransform:
transform_cls = get_available_transforms()[self.transform]
if transform_cls.options_model:
return transform_cls(self.parsed_options()) # type: ignore
else:
return transform_cls()
class SourceTransformList(BaseModel):
__root__: T.List[SourceTransformSpec]
@root_validator(pre=True)
def validate_spec_list(cls, values):
values["__root__"] = [
{"transform": s} if isinstance(s, str) else s for s in values["__root__"]
]
return values
def as_transforms(self) -> T.List[SourceTransform]:
return [
get_available_transforms()[t]() if isinstance(t, str) else t.as_transform()
for t in self.__root__
]
class NamespaceInjectionOptions(BaseModel):
namespace_tokenize: T.Optional[str]
namespace_inject: T.Optional[str]
namespace_strip: T.Optional[str]
unmanaged: bool = True
namespaced_org: bool = False
class NamespaceInjectionTransform(SourceTransform):
"""Source transform that applies namespace injection, stripping, and tokenization."""
options_model = NamespaceInjectionOptions
options: NamespaceInjectionOptions
identifier = "inject_namespace"
def __init__(self, options: NamespaceInjectionOptions):
self.options = options
def process(self, zf: ZipFile, context: TaskContext) -> ZipFile:
if self.options.namespace_tokenize:
context.logger.info(
f"Tokenizing namespace prefix {self.options.namespace_tokenize}__"
)
zf = process_text_in_zipfile(
zf,
functools.partial(
tokenize_namespace,
namespace=self.options.namespace_tokenize,
logger=context.logger,
),
)
if self.options.namespace_inject:
managed = not self.options.unmanaged
if managed:
context.logger.info(
"Replacing namespace tokens from metadata with namespace prefix "
f"{self.options.namespace_inject}__"
)
else:
context.logger.info(
"Stripping namespace tokens from metadata for unmanaged deployment"
)
zf = process_text_in_zipfile(
zf,
functools.partial(
inject_namespace,
namespace=self.options.namespace_inject,
managed=managed,
namespaced_org=self.options.namespaced_org,
logger=context.logger,
),
)
if self.options.namespace_strip:
context.logger.info("Stripping namespace tokens from metadata")
zf = process_text_in_zipfile(
zf,
functools.partial(
strip_namespace,
namespace=self.options.namespace_strip,
logger=context.logger,
),
)
return zf
class RemoveFeatureParametersTransform(SourceTransform):
"""Source transform that removes Feature Parameters. Intended for use on Unlocked Package builds."""
options_model = None
identifier = "remove_feature_parameters"
def process(self, zf: ZipFile, context: TaskContext) -> ZipFile:
package_xml = None
zip_dest = ZipFile(io.BytesIO(), "w", zipfile.ZIP_DEFLATED)
for name in zf.namelist():
if name == "package.xml":
package_xml = zf.open(name)
elif name.startswith("featureParameters/"):
# skip feature parameters
context.logger.info(
f"Skipping {name} because Feature Parameters are omitted."
)
else:
content = zf.read(name)
zip_dest.writestr(name, content)
# Remove from package.xml
if package_xml is not None:
package = metadata_tree.parse(package_xml)
for mdtype in (
"FeatureParameterInteger",
"FeatureParameterBoolean",
"FeatureParameterDate",
):
section = package.find("types", name=mdtype)
if section is not None:
package.remove(section)
package_xml = package.tostring(xml_declaration=True)
zip_dest.writestr("package.xml", package_xml)
return zip_dest
class CleanMetaXMLTransform(SourceTransform):
"""Source transform that cleans *-meta.xml files of references to specific package versions."""
options_model = None
identifier = "clean_meta_xml"
def process(self, zf: ZipFile, context: TaskContext) -> ZipFile:
context.logger.info(
"Cleaning meta.xml files of packageVersion elements for deploy"
)
return zip_clean_metaxml(zf)
class BundleStaticResourcesOptions(BaseModel):
static_resource_path: str
class BundleStaticResourcesTransform(SourceTransform):
"""Source transform that zips static resource content from an external path"""
options_model = BundleStaticResourcesOptions
options: BundleStaticResourcesOptions
identifier = "bundle_static_resources"
def __init__(self, options: BundleStaticResourcesOptions):
self.options = options
def process(self, zf: ZipFile, context: TaskContext) -> ZipFile:
path = os.path.realpath(self.options.static_resource_path)
# Copy existing files to new zipfile
zip_dest = zipfile.ZipFile(io.BytesIO(), "w", zipfile.ZIP_DEFLATED)
package_xml = None
for name in zf.namelist():
if name == "package.xml":
package_xml = zf.open(name)
else:
content = zf.read(name)
zip_dest.writestr(name, content)
if not package_xml:
raise Exception("No package.xml found; cannot zip Static Resources")
# Build static resource bundles and add to package
with temporary_dir():
os.mkdir("staticresources")
bundles = []
for name in os.listdir(path):
bundle_relpath = os.path.join(self.options.static_resource_path, name)
bundle_path = os.path.join(path, name)
if not os.path.isdir(bundle_path):
continue
context.logger.info(
f"Zipping {bundle_relpath} to add to staticresources"
)
# Add resource-meta.xml file
meta_name = f"{name}.resource-meta.xml"
meta_path = os.path.join(path, meta_name)
with open(meta_path, "rb") as f:
zip_dest.writestr(f"staticresources/{meta_name}", f.read())
# Add bundle
zip_path = os.path.join("staticresources", f"{name}.resource")
with open(zip_path, "wb") as bundle_fp:
bundle_zip = zipfile.ZipFile(bundle_fp, "w", zipfile.ZIP_DEFLATED)
with cd(bundle_path):
for root, _, files in os.walk("."):
for f in files:
resource_file = os.path.join(root, f)
bundle_zip.write(resource_file)
bundle_zip.close()
zip_dest.write(zip_path)
bundles.append(name)
# Update package.xml
package = metadata_tree.parse(package_xml)
sections = package.findall("types", name="StaticResource")
section = sections[0] if sections else None
if not section:
section = package.append("types")
section.append("name", text="StaticResource")
for name in sorted(bundles):
section.insert_before(section.find("name"), tag="members", text=name)
package_xml = package.tostring(xml_declaration=True)
zip_dest.writestr("package.xml", package_xml)
return zip_dest
class FindReplaceBaseSpec(BaseModel, abc.ABC):
find: T.Optional[str]
xpath: T.Optional[str]
paths: T.Optional[T.List[Path]] = None
@root_validator
def validate_find_xpath(cls, values):
findVal = values.get("find")
xpathVal = values.get("xpath")
if (findVal == "" or findVal is None) and (xpathVal is None or xpathVal == ""):
raise ValueError(
"Input is not valid. Please pass either find or xpath paramter."
)
if (
findVal != ""
and findVal is not None
and xpathVal != ""
and xpathVal is not None
):
raise ValueError(
"Input is not valid. Please pass either find or xpath paramter not both."
)
return values
@abc.abstractmethod
def get_replace_string(self, context: TaskContext) -> str:
...
class FindReplaceSpec(FindReplaceBaseSpec):
replace: str
def get_replace_string(self, context: TaskContext) -> str:
return self.replace
class FindReplaceEnvSpec(FindReplaceBaseSpec):
replace_env: str
def get_replace_string(self, context: TaskContext) -> str:
try:
return os.environ[self.replace_env]
except KeyError:
raise TaskOptionsError(
f"Transform {FindReplaceTransform.identifier} could not get replacement value from environment variable {self.replace_env}"
)
class FindReplaceIdAPI(StrEnum):
REST = "rest"
TOOLING = "tooling"
class FindReplaceIdSpec(FindReplaceBaseSpec):
replace_record_id_query: str
api: FindReplaceIdAPI = FindReplaceIdAPI.REST
def get_replace_string(self, context: TaskContext) -> str:
org = context.org_config
if self.api is FindReplaceIdAPI.REST:
results = org.salesforce_client.query(self.replace_record_id_query)
else:
results = org.tooling.query(self.replace_record_id_query)
if results["totalSize"] != 1:
raise CumulusCIException(
f"The find-replace query {self.replace_record_id_query} returned {results['totalSize']} results. Exactly 1 result is required"
)
try:
record_id = results["records"][0]["Id"]
except KeyError:
raise CumulusCIException(
"Results from the replace_record_id_query did not include an 'Id'. Please ensure the 'Id' field is included in your query's SELECT clause."
)
return record_id
class FindReplaceCurrentUserSpec(FindReplaceBaseSpec):
inject_username: bool
def get_replace_string(self, context: TaskContext) -> str:
if not self.inject_username: # pragma: no cover
self.logger.warning(
"The inject_username value for the find_replace transform is set to False. Skipping transform."
)
return self.find
return context.org_config.username
class FindReplaceOrgUrlSpec(FindReplaceBaseSpec):
inject_org_url: bool
def get_replace_string(self, context: TaskContext) -> str:
if not self.inject_org_url: # pragma: no cover
self.logger.warning(
"The inject_org_url value for the find_replace transform is set to False. Skipping transform."
)
return self.find
return context.org_config.instance_url
class FindReplaceTransformOptions(BaseModel):
patterns: T.List[
T.Union[
FindReplaceSpec,
FindReplaceEnvSpec,
FindReplaceIdSpec,
FindReplaceCurrentUserSpec,
FindReplaceOrgUrlSpec,
]
]
class FindReplaceTransform(SourceTransform):
"""Source transform that applies one or more find-and-replace patterns."""
options_model = FindReplaceTransformOptions
options: FindReplaceTransformOptions
identifier = "find_replace"
def __init__(self, options: FindReplaceTransformOptions):
self.options = options
def process(self, zf: ZipFile, context: TaskContext) -> ZipFile:
# To handle xpath with namespaces, without
def transform_xpath(expression):
predicate_pattern = re.compile(r"\[.*?\]")
parts = expression.split("/")
transformed_parts = []
for part in parts:
if part:
predicates = predicate_pattern.findall(part)
tag = predicate_pattern.sub("", part)
transformed_part = '/*[local-name()="' + tag + '"]'
for predicate in predicates:
transformed_part += predicate
transformed_parts.append(transformed_part)
transformed_expression = "".join(transformed_parts)
return transformed_expression
def process_file(filename: str, content: str) -> T.Tuple[str, str]:
path = Path(filename)
for spec in self.options.patterns:
if not spec.paths or any(
parent in path.parents for parent in spec.paths
):
try:
# See if the content is an xml file
content_bytes = content.encode("utf-8")
root = ET.fromstring(content_bytes)
# See if content has an xml declaration
has_xml_declaration = content.strip().startswith("<?xml")
# If find, we do not want to modify the tags in xml file, only the content
if spec.find:
stack = [root]
while stack:
element = stack.pop()
if element.text and spec.find in element.text:
element.text = element.text.replace(
spec.find, spec.get_replace_string(context)
)
stack.extend(element)
# Modify the element given by xpath
elif spec.xpath:
transformed_xpath = transform_xpath(spec.xpath)
elements_to_replace = root.xpath(transformed_xpath)
for element in elements_to_replace:
element.text = spec.get_replace_string(context)
# Add xml declaration back to file, if it initally had xml declaration
content = ET.tostring(
root, encoding="utf-8", xml_declaration=has_xml_declaration
).decode("utf-8")
except ET.XMLSyntaxError:
if spec.find:
content = content.replace(
spec.find, spec.get_replace_string(context)
)
else:
continue
except ET.XPathError as e:
raise ET.XPathError(
f"An exception of type {type(e).__name__} occurred: {e} \nKindly check the xpath given"
)
return (filename, content)
return process_text_in_zipfile(zf, process_file)
class StripUnwantedComponentsOptions(BaseModel):
package_xml: str
class StripUnwantedComponentTransform(SourceTransform):
options_model = StripUnwantedComponentsOptions
options: StripUnwantedComponentsOptions
identifier = "strip_unwanted_components"
def __init__(self, options: StripUnwantedComponentsOptions):
self.options = options
def process(self, zf: ZipFile, context: TaskContext) -> ZipFile:
package_xml_path = os.path.abspath(os.path.expanduser(self.options.package_xml))
zip_dest = zipfile.ZipFile(io.BytesIO(), "w", zipfile.ZIP_DEFLATED)
with temporary_dir():
zf.extractall()
RemoveSourceComponents(
os.getcwd(), package_xml_path, api_version=None, logger=context.logger
)()
shutil.copy(package_xml_path, "package.xml")
for root, _, files in os.walk("."):
for f in files:
file = os.path.join(root, f)
zip_dest.write(file)
return zip_dest
def get_available_transforms() -> T.Dict[str, T.Type[SourceTransform]]:
"""Get a mapping of identifiers (usable in cumulusci.yml) to transform classes"""
return {
cls.identifier: cls
for cls in [
CleanMetaXMLTransform,
NamespaceInjectionTransform,
RemoveFeatureParametersTransform,
BundleStaticResourcesTransform,
FindReplaceTransform,
StripUnwantedComponentTransform,
]
}