-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy path__init__.py
More file actions
670 lines (544 loc) · 22.3 KB
/
__init__.py
File metadata and controls
670 lines (544 loc) · 22.3 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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import contextlib
import fnmatch
import io
import math
import os
import re
import shutil
import sys
import tempfile
import textwrap
import zipfile
from datetime import datetime
from pathlib import Path
from typing import Union
import requests
import sarge
from cumulusci.core.exceptions import CumulusCIException
from .xml import ( # noqa
elementtree_parse_file,
remove_xml_element,
remove_xml_element_file,
remove_xml_element_string,
)
from .ziputils import process_text_in_zipfile # noqa
from .ziputils import zip_subfolder
CUMULUSCI_PATH = os.path.realpath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "../..")
)
META_XML_CLEAN_DIRS = ("classes/", "triggers/", "pages/", "aura/", "components/")
API_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
DATETIME_LEN = len("2018-08-07T16:00:56.000")
BREW_DEPRECATION_MSG = (
"It looks like you have installed CumulusCI using brew."
"This method of installation is no longer supported."
"Please use the following to install CumulusCI with pipx:\n"
"brew uninstall cumulusci\nbrew install pipx\npipx ensurepath\npipx install cumulusci"
)
PIP_UPDATE_CMD = "pip install --upgrade cumulusci"
PIPX_UPDATE_CMD = "pipx upgrade cumulusci"
def parse_api_datetime(value):
"""parse a datetime returned from the salesforce API.
in python 3 we should just use a strptime %z, but until then we're just going
to assert that its a fixed offset of +0000 since thats the observed behavior. getting
python 2 to support fixed offset parsing is too complicated for what we need imo."""
dt = datetime.strptime(value[0:DATETIME_LEN], API_DATE_FORMAT)
offset_str = value[DATETIME_LEN:]
assert offset_str in ["+0000", "Z"], "The Salesforce API returned a weird timezone."
return dt
def find_replace(find, replace, directory, filePattern, logger=None, max=None):
"""Recursive find/replace.
Walks through files matching `filePattern` within `directory`
and does a string substitution of `find` with `replace`.
"""
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, filePattern):
filepath = os.path.join(path, filename)
with io.open(filepath, encoding="utf-8") as f:
s = f.read()
if max:
s_updated = s.replace(find, replace, max)
else:
s_updated = s.replace(find, replace)
if s != s_updated:
if logger:
logger.info("Updating {}".format(filepath))
with io.open(filepath, "w", encoding="utf-8") as f:
f.write(s_updated)
def find_replace_regex(find, replace, directory, filePattern, logger=None):
"""Recursive find/replace using a regular expression.
Walks through files matching `filePattern` within `directory`
and does a regex substitution of `find` with `replace`.
"""
pattern = re.compile(find)
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, filePattern):
filepath = os.path.join(path, filename)
with io.open(filepath, encoding="utf-8") as f:
s = f.read()
s_updated = pattern.sub(replace, s)
if s != s_updated:
if logger:
logger.info("Updating {}".format(filepath))
with io.open(filepath, "w", encoding="utf-8") as f:
f.write(s_updated)
def find_rename(find, replace, directory, logger=None):
"""Recursive find/replace within filenames.
Walks through files within `directory`
and renames files to replace `find` with `replace`.
"""
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in files:
filepath = os.path.join(path, filename)
if logger:
logger.info("Renaming {}".format(filepath))
os.rename(filepath, os.path.join(path, filename.replace(find, replace)))
def remove_xml_element_directory(name, directory, file_pattern, logger=None):
"""Recursively walk a directory and remove XML elements"""
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, file_pattern):
filepath = os.path.join(path, filename)
remove_xml_element_file(name, filepath)
# backwards-compatibility aliases
findReplace = find_replace
findReplaceRegex = find_replace_regex
findRename = find_rename
removeXmlElement = remove_xml_element_directory
def download_extract_zip(url, target=None, subfolder=None, headers=None):
if not headers:
headers = {}
resp = requests.get(url, headers=headers)
zip_content = io.BytesIO(resp.content)
zip_file = zipfile.ZipFile(zip_content)
if subfolder:
zip_file = zip_subfolder(zip_file, subfolder)
if target:
zip_file.extractall(target)
return
return zip_file
def download_extract_github(
github_api, repo_owner, repo_name, subfolder=None, ref=None
):
return download_extract_github_from_repo(
github_api.repository(repo_owner, repo_name), subfolder, ref
)
def download_extract_github_from_repo(github_repo, subfolder=None, ref=None):
if not ref:
ref = github_repo.default_branch
zip_content = io.BytesIO()
if not github_repo.archive("zipball", zip_content, ref=ref):
raise CumulusCIException(
f"Unable to download an archive of the Git ref {ref} from "
f"{github_repo.full_name}. This can mean that the ref has "
"not been pushed to the server, that CumulusCI's credential "
"does not have permission to access it, or that your access "
"is restricted by an IP address allow list."
)
zip_file = zipfile.ZipFile(zip_content)
path = sorted(zip_file.namelist())[0]
if subfolder:
path = path + subfolder
zip_file = zip_subfolder(zip_file, path)
return zip_file
def process_text_in_directory(path, process_file):
"""Process each file in a directory using the `process_file` function.
`process_file` should be a function which accepts a filename and content as text
and returns a (possibly modified) filename and content. The file will be
updated with the new content, and renamed if necessary.
Files with content that cannot be decoded as UTF-8 will be skipped.
"""
for path, dirs, files in os.walk(path):
for orig_name in files:
orig_path = os.path.join(path, orig_name)
try:
with open(orig_path, "r", encoding="utf-8") as f:
orig_content = f.read()
except UnicodeDecodeError:
# Probably a binary file; skip it
continue
new_name, new_content = process_file(orig_name, orig_content)
new_path = os.path.join(path, new_name)
if new_name != orig_name:
os.rename(orig_path, new_path)
with open(new_path, "w", encoding="utf-8") as f:
f.write(new_content)
def inject_namespace(
name,
content,
namespace=None,
managed=None,
filename_token=None,
namespace_token=None,
namespaced_org=None,
logger=None,
):
"""Replaces %%%NAMESPACE%%% in file content and ___NAMESPACE___ in file name
with either '' if no namespace is provided or 'namespace__' if provided.
"""
# Handle namespace and filename tokens
if not filename_token:
filename_token = "___NAMESPACE___"
if not namespace_token:
namespace_token = "%%%NAMESPACE%%%"
if managed is True and namespace:
namespace_prefix = namespace + "__"
namespace_dot_prefix = namespace + "."
else:
namespace_prefix = ""
namespace_dot_prefix = ""
namespace_dot_token = "%%%NAMESPACE_DOT%%%"
# Handle tokens %%%NAMESPACED_ORG%%% and ___NAMESPACED_ORG___
namespaced_org_token = "%%%NAMESPACED_ORG%%%"
namespaced_org_file_token = "___NAMESPACED_ORG___"
namespaced_org = namespace + "__" if namespaced_org else ""
# Handle token %%%NAMESPACE_OR_C%%% for lightning components
namespace_or_c_token = "%%%NAMESPACE_OR_C%%%"
namespace_or_c = namespace if managed and namespace else "c"
# Handle token %%%NAMESPACED_ORG_OR_C%%%
namespaced_org_or_c_token = "%%%NAMESPACED_ORG_OR_C%%%"
namespaced_org_or_c = namespace if namespaced_org else "c"
orig_name = name
prev_content = content
content = content.replace(namespace_token, namespace_prefix)
if logger and content != prev_content:
logger.info(f' {name}: Replaced {namespace_token} with "{namespace_prefix}"')
prev_content = content
content = content.replace(namespace_dot_token, namespace_dot_prefix)
if logger and content != prev_content:
logger.info(
f' {name}: Replaced {namespace_dot_token} with "{namespace_dot_prefix}"'
)
prev_content = content
content = content.replace(namespace_or_c_token, namespace_or_c)
if logger and content != prev_content:
logger.info(
f' {name}: Replaced {namespace_or_c_token} with "{namespace_or_c}"'
)
if name == "package.xml":
prev_content = content
content = content.replace(filename_token, namespace_prefix)
if logger and content != prev_content:
logger.info(
f' {name}: Replaced {filename_token} with "{namespace_prefix}"'
)
# Also replace ___NAMESPACED_ORG___ tokens in package.xml
prev_content = content
content = content.replace(namespaced_org_file_token, namespaced_org)
if logger and content != prev_content:
logger.info(
f' {name}: Replaced {namespaced_org_file_token} with "{namespaced_org}"'
)
prev_content = content
content = content.replace(namespaced_org_token, namespaced_org)
if logger and content != prev_content:
logger.info(
f' {name}: Replaced {namespaced_org_token} with "{namespaced_org}"'
)
prev_content = content
content = content.replace(namespaced_org_or_c_token, namespaced_org_or_c)
if logger and content != prev_content:
logger.info(
f' {name}: Replaced {namespaced_org_or_c_token} with "{namespaced_org_or_c}"'
)
# Replace namespace token in file name
name = name.replace(filename_token, namespace_prefix)
name = name.replace(namespaced_org_file_token, namespaced_org)
if logger and name != orig_name:
logger.info(f" {orig_name}: renamed to {name}")
return name, content
def strip_namespace(name, content, namespace, logger=None):
"""Given a namespace, strips 'namespace__' from file name and content"""
namespace_prefix = "{}__".format(namespace)
lightning_namespace = "{}:".format(namespace)
orig_content = content
new_content = orig_content.replace(namespace_prefix, "")
new_content = new_content.replace(lightning_namespace, "c:")
name = name.replace(namespace_prefix, "")
if orig_content != new_content and logger:
logger.info(
" {file_name}: removed {namespace}".format(
file_name=name, namespace=namespace_prefix
)
)
return name, new_content
def tokenize_namespace(name, content, namespace, logger=None):
"""Given a namespace, replaces 'namespace__' with %%%NAMESPACE%%%
in file content and ___NAMESPACE___ in file name
"""
if not namespace:
return name, content
namespace_prefix = "{}__".format(namespace)
lightning_namespace = "{}:".format(namespace)
content = content.replace(namespace_prefix, "%%%NAMESPACE%%%")
content = content.replace(lightning_namespace, "%%%NAMESPACE_OR_C%%%")
name = name.replace(namespace_prefix, "___NAMESPACE___")
return name, content
def zip_clean_metaxml(zip_src, logger=None):
"""Given a zipfile, cleans all ``*-meta.xml`` files in the zip for
deployment by stripping all ``<packageVersions/>`` elements
"""
zip_dest = zipfile.ZipFile(io.BytesIO(), "w", zipfile.ZIP_DEFLATED)
changed = []
for name in zip_src.namelist():
content = zip_src.read(name)
if name.startswith(META_XML_CLEAN_DIRS) and name.endswith("-meta.xml"):
try:
content.decode("utf-8")
except UnicodeDecodeError:
# if we cannot decode the content, it may be binary;
# don't try and replace it.
pass
else:
clean_content = remove_xml_element_string("packageVersions", content)
if clean_content != content:
changed.append(name)
content = clean_content
zip_dest.writestr(name, content)
if changed and logger:
logger.info(
"Cleaned package versions from {} meta.xml files".format(len(changed))
)
zip_src.close()
return zip_dest
def doc_task(task_name, task_config, project_config=None, org_config=None):
"""Document a (project specific) task configuration in RST format."""
from cumulusci.core.utils import import_global
doc = []
doc.append(f".. _{task_name.replace('_', '-')}:\n")
doc.append(f"{task_name}\n==========================================\n")
doc.append(f"**Description:** {task_config.description}\n")
doc.append(f"**Class:** {task_config.class_path}\n")
task_class = import_global(task_config.class_path)
if "task_docs" in task_class.__dict__:
task_docs = textwrap.dedent(task_class.task_docs.strip("\n"))
doc.append(task_docs)
task_option_info = get_task_option_info(task_config, task_class)
doc.append("Command Syntax\n------------------------------------------\n")
command_syntax = get_command_syntax(task_name)
doc.append(command_syntax)
task_option_doc = create_task_options_doc(task_option_info)
if task_option_doc:
doc.append("Options\n------------------------------------------\n")
doc.extend(task_option_doc)
return "\n".join(doc)
def get_command_syntax(task_name):
"""Return an example command syntax string in .rst format"""
return f"``$ cci task run {task_name}``\n\n"
def get_task_option_info(task_config, task_class):
"""Gets the the following info for each option in the task
usage: example usage statement (i.e. -o name VALUE)
required: True/False
default: If a default value is present
description: Description string provided on the task option
option_type: A type string provided on the task option
Returns list of option dicts with required at the front of the map
"""
required_options = []
optional_options = []
defaults = task_config.options or {}
for name, option in list(task_class.task_options.items()):
usage = get_option_usage_string(name, option)
required = True if option.get("required") else False
default = defaults.get(name)
description = option.get("description")
option_type = option.get("type")
info = {
"usage": usage,
"name": name,
"required": required,
"default": default,
"description": description,
"option_type": option_type,
}
if required:
required_options.append(info)
else:
optional_options.append(info)
return [*required_options, *optional_options]
def get_option_usage_string(name, option):
"""Returns a usage string if one exists
else creates a usage string in the form of:
--option-name OPTIONNAME
"""
usage_str = option.get("usage")
if not usage_str:
usage_str = f"--{name} {name.replace('_','').upper()}"
return usage_str
def create_task_options_doc(task_options):
"""Generate the 'Options' section for a given tasks documentation"""
doc = []
for option in task_options:
usage_str = option.get("usage")
if usage_str:
doc.append(f"\n``{usage_str}``")
description = option.get("description")
if description:
doc.append(f"\n\t {description}")
default = option.get("default")
if default:
doc.append(f"\n\t Default: {default}")
elif option.get("required"):
doc.append("\n *Required*")
else:
doc.append("\n *Optional*")
option_type = option.get("option_type")
if option_type:
doc.append(f"\n\t Type: {option_type}")
return doc
def flow_ref_title_and_intro(intro_blurb):
return f"""Flow Reference
==========================================
\n{intro_blurb}
"""
def document_flow(flow_name, description, flow_coordinator, additional_info=None):
"""Document (project specific) flow configurations in RST format"""
doc = []
doc.append(f".. _{flow_name.replace('_', '-')}:\n")
doc.append(f"{flow_name}\n{'^' * len(flow_name)}\n")
doc.append(f"**Description:** {description}\n")
if additional_info:
doc.append(additional_info)
doc.append("**Flow Steps**\n")
doc.append(".. code-block:: console\n")
flow_step_lines = flow_coordinator.get_flow_steps(for_docs=True)
# extra indent beneath code-block and finish with pipe for extra space afterwards
flow_step_lines = [f"\t{line}" for line in flow_step_lines]
# fix when clauses
lines = []
for line in flow_step_lines:
if line.startswith("when"):
line = f"\t\t{line}"
lines.append(line)
doc.extend(lines)
return "\n".join(doc)
def package_xml_from_dict(items, api_version, package_name=None):
lines = []
# Print header
lines.append('<?xml version="1.0" encoding="UTF-8"?>')
lines.append('<Package xmlns="http://soap.sforce.com/2006/04/metadata">')
# Include package name if specified
if package_name:
lines.append(" <fullName>{}</fullName>".format(package_name))
# Print types sections
for md_type, members in sorted(items.items()):
members.sort()
lines.append(" <types>")
for member in members:
lines.append(" <members>{}</members>".format(member))
lines.append(" <name>{}</name>".format(md_type))
lines.append(" </types>")
# Print footer
lines.append(" <version>{0}</version>".format(api_version))
lines.append("</Package>")
return "\n".join(lines)
@contextlib.contextmanager
def cd(path):
"""Context manager that changes to another directory"""
if not path:
yield
return
cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(cwd)
@contextlib.contextmanager
def temporary_dir(chdir=True):
"""Context manager that creates a temporary directory and chdirs to it.
When the context manager exits it returns to the previous cwd
and deletes the temporary directory.
"""
d = tempfile.mkdtemp()
try:
with contextlib.ExitStack() as stack:
if chdir:
stack.enter_context(cd(d))
yield d
finally:
if os.path.exists(d):
try:
shutil.rmtree(d)
except Exception as e: # pragma: no cover
import logging # needs to be local or cumulusci.utils.logging gets picked up
logging.getLogger(__file__).warn(
f"Cannot remove temporary directory {d} because: {e}"
)
def touch(path):
"""Ensure a file exists."""
with open(path, "a"):
pass
def in_directory(filepath, dirpath):
"""Returns a boolean for whether filepath is contained in dirpath.
Normalizes the paths (e.g. resolving symlinks and ..)
so this is the safe way to make sure a user-configured path
is located inside the user's project repo.
"""
filepath = os.path.realpath(filepath)
dirpath = os.path.realpath(dirpath)
return filepath == dirpath or filepath.startswith(os.path.join(dirpath, ""))
def log_progress(
iterable,
logger,
batch_size=10000,
progress_message="Processing... ({})",
done_message="Done! (Total: {})",
):
"""Log progress while iterating."""
i = 0
for x in iterable:
yield x
i += 1
if not i % batch_size:
logger.info(progress_message.format(i))
logger.info(done_message.format(i))
def random_alphanumeric_underscore(length):
import secrets
# Ensure the string is the right length
byte_length = math.ceil((length * 3) / 4)
return secrets.token_urlsafe(byte_length).replace("-", "_")[:length]
def get_cci_upgrade_command():
deprecated_install_paths = ["cellar", "linuxbrew"]
for path in deprecated_install_paths:
if path in CUMULUSCI_PATH.lower():
return BREW_DEPRECATION_MSG
return PIPX_UPDATE_CMD if "pipx" in CUMULUSCI_PATH.lower() else PIP_UPDATE_CMD
def convert_to_snake_case(content):
s1 = re.sub("([^_])([A-Z][a-z]+)", r"\1_\2", content)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
def get_git_config(config_key):
p = sarge.Command(
sarge.shell_format('git config --get "{0!s}"', config_key),
stderr=sarge.Capture(buffer_size=-1),
stdout=sarge.Capture(buffer_size=-1),
shell=True,
)
p.run()
config_value = (
io.TextIOWrapper(p.stdout, encoding=sys.stdout.encoding).read().strip()
)
return config_value if config_value and not p.returncode else None
def update_tree(src: Union[str, Path], dest: Union[str, Path]):
"""
Copies files from src to dest, same as distutils.copy_tree(update=1).
Copies the entire directory tree from src to dest. If dest exists, only
copies files that are newer in src than in dest, or files that don't exist
in dest.
Args:
src (Union[str, Path]): The source directory to copy files from.
dest (Union[str, Path]): The destination directory to copy files to.
"""
src_path = Path(src)
dest_path = Path(dest)
if not dest_path.exists():
shutil.copytree(src_path, dest_path)
else:
for src_dir in src_path.rglob("*"):
if src_dir.is_file():
dest_file = dest_path / src_dir.relative_to(src_path)
if (
not dest_file.exists()
or src_dir.stat().st_mtime - dest_file.stat().st_mtime > 1
):
dest_file.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_dir, dest_file)