Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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:
if chdir is True:
with cd(d):
yield d
else:
yield d
finally:
if os.path.exists(d):
shutil.rmtree(d)
def _get_package_zip(self, path):
# Build the zip file
zip_bytes = io.BytesIO()
zipf = zipfile.ZipFile(zip_bytes, "w", zipfile.ZIP_DEFLATED)
with cd(path):
for file_to_package in self._get_files_to_package():
zipf.write(file_to_package)
zipf.close()
zipf_processed = self._process_zip_file(zipfile.ZipFile(zip_bytes))
fp = zipf_processed.fp
zipf_processed.close()
return base64.b64encode(fp.getvalue()).decode("utf-8")
def _exclude_files(self, path):
with cd(path):
for path in self.options["exclude"]:
if os.path.isdir(path):
shutil.rmtree(path)
elif os.path.isfile(path):
os.remove(path)
continue
self.logger.info(
"Zipping {} to add to staticresources".format(bundle_relpath)
)
# Add resource-meta.xml file
meta_name = "{}.resource-meta.xml".format(name)
meta_path = os.path.join(path, meta_name)
with open(meta_path, "rb") as f:
zip_dest.writestr("staticresources/{}".format(meta_name), f.read())
# Add bundle
zip_path = os.path.join("staticresources", "{}.resource".format(name))
with open(zip_path, "wb") as bundle_fp:
bundle_zip = zipfile.ZipFile(bundle_fp, "w", zipfile.ZIP_DEFLATED)
with cd(bundle_path):
for resource_file in self._get_static_resource_files():
bundle_zip.write(resource_file)
bundle_zip.close()
zip_dest.write(zip_path)
bundles.append(name)
# Update package.xml
tree = ET.fromstring(package_xml)
section = tree.find(".//sf:types[sf:name='StaticResource']", self.namespaces)
if section is None:
section = ET.Element("{{{}}}types".format(self.namespaces["sf"]))
name = ET.Element("{{{}}}name".format(self.namespaces["sf"]))
section.append(name)
name.text = "StaticResource"
tree.find(".//sf:types[last()]", self.namespaces).addnext(section)
for name in bundles:
namespaced_org=self.options["namespaced_org"],
logger=self.logger,
)
if dependency.get("namespace_strip"):
self.logger.info(
"Removing namespace prefix {}__ from all files and filenames".format(
"{}__".format(dependency["namespace_strip"])
)
)
package_zip = zip_strip_namespace(
package_zip, namespace=dependency["namespace_strip"], logger=self.logger
)
with temporary_dir() as path:
with cd(path):
package_zip.extractall(path)
package_config = {
"name": "{repo_owner}/{repo_name} {subfolder}".format(**dependency),
"version_name": "{repo_owner}/{repo_name} {subfolder} - ".format(
**dependency
)
+ "{{ version }}",
"package_type": "unlocked",
"path": os.path.join(path),
# FIXME: Ideally we'd do this without a namespace but that causes package creation errors
"namespace": self.package_config.get("namespace"),
}
package_id = self._get_or_create_package(package_config)
self.request_id = self._create_version_request(
package_id, package_config
)
def _add_files_to_package(self, package_zip, path):
with cd(path):
for file_to_package in self._get_files_to_package():
package_zip.write(file_to_package)
def __call__(self):
# If sentry is configured, initialize sentry for error capture
self.project_config.init_sentry()
if self.salesforce_task and not self.org_config:
raise TaskRequiresSalesforceOrg(
"This task requires a salesforce org. "
"Use org default to set a default org "
"or pass the org name with the --org option"
)
self._update_credentials()
self._init_task()
with stacked_task(self):
self.working_path = os.getcwd()
with cd(self.project_config.repo_root):
try:
self._log_begin()
self.result = self._run_task()
return self.return_values
except Exception as e:
self._process_exception(e)
raise