def test_process_text_in_directory__renamed_file(self): with utils.temporary_dir(): with open("test1", "w") as f: f.write("test") def process(name, content): return "test2", "test" utils.process_text_in_directory(".", process) with open("test2", "r") as f: result = f.read() assert result == "test"
def test_process_text_in_directory__skips_binary(self): contents = b"\x9c%%%NAMESPACE%%%" with utils.temporary_dir(): with open("test", "wb") as f: f.write(contents) def process(name, content): return name, "" utils.process_text_in_directory(".", process) # assert contents were untouched with open("test", "rb") as f: result = f.read() assert contents == result
def retrieve_components( components, org_config, target: str, md_format: bool, extra_package_xml_opts: dict, namespace_tokenize: str, api_version: str, ): """Retrieve specified components from an org into a target folder. Retrieval is done using the sfdx force:source:retrieve command. Set `md_format` to True if retrieving into a folder with a package in metadata format. In this case the folder will be temporarily converted to dx format for the retrieval and then converted back. Retrievals to metadata format can also set `namespace_tokenize` to a namespace prefix to replace it with a `%%%NAMESPACE%%%` token. """ with contextlib.ExitStack() as stack: if md_format: # Create target if it doesn't exist if not os.path.exists(target): os.mkdir(target) touch(os.path.join(target, "package.xml")) # Inject namespace if namespace_tokenize: process_text_in_directory( target, functools.partial(inject_namespace, namespace=namespace_tokenize, managed=True), ) # Temporarily convert metadata format to DX format stack.enter_context(temporary_dir()) os.mkdir("target") # We need to create sfdx-project.json # so that sfdx will recognize force-app as a package directory. with open("sfdx-project.json", "w") as f: json.dump( { "packageDirectories": [{ "path": "force-app", "default": True }] }, f) sfdx( "force:mdapi:convert", log_note="Converting to DX format", args=["-r", target, "-d", "force-app"], check_return=True, ) # Construct package.xml with components to retrieve, in its own tempdir package_xml_path = stack.enter_context(temporary_dir(chdir=False)) _write_manifest(components, package_xml_path, api_version) # Retrieve specified components in DX format sfdx( "force:source:retrieve", access_token=org_config.access_token, log_note="Retrieving components", args=[ "-a", str(api_version), "-x", os.path.join(package_xml_path, "package.xml"), "-w", "5", ], capture_output=False, check_return=True, env={"SFDX_INSTANCE_URL": org_config.instance_url}, ) if md_format: # Convert back to metadata format sfdx( "force:source:convert", log_note="Converting back to metadata format", args=["-r", "force-app", "-d", target], capture_output=False, check_return=True, ) # Reinject namespace tokens if namespace_tokenize: process_text_in_directory( target, functools.partial(tokenize_namespace, namespace=namespace_tokenize), ) # Regenerate package.xml, # to avoid reformatting or losing package name/scripts package_xml_opts = { "directory": target, "api_version": api_version, **extra_package_xml_opts, } package_xml = PackageXmlGenerator(**package_xml_opts)() with open(os.path.join(target, "package.xml"), "w") as f: f.write(package_xml)