def fetch(self, output):
dl_path = self.get_latest_tarball()
raw_path, _ = urllib.urlretrieve(dl_path)
# The downloaded file is a gzip'd tarball.
extract_path = self._make_temp_directory("rootfetch-apple-extracted")
sh.tar("-xzv", "-f", raw_path, "-C", extract_path, strip_components=1)
# We now have a directory with all the apple files. We need to find the
# roots directory, parse out all the different formats, then generate a
# single file that has PEMs in it.
certificates_path = os.path.join(extract_path, "certificates", "roots")
for f in os.listdir(certificates_path):
full_path = os.path.join(certificates_path, f)
if not os.path.isfile(full_path):
continue
pem = self.make_pem(full_path)
output.write("# ")
output.write(f)
output.write("\n")
output.write("\n".join(pem))
output.write("\n\n")
python类tar()的实例源码
def input_dir(self):
if not os.path.exists(self.test_data_dir):
if not os.path.exists(self.test_data_tarball):
sh.wget('-P', self.test_dir, data_tarball_url)
sh.tar('zxvf', self.test_data_tarball, '-C', self.test_dir)
return os.path.join(self.test_data_dir, 'input')
def input_dir(self):
test_dir = os.path.dirname(os.path.realpath(__file__))
test_data_dir = os.path.join(test_dir, 'test_data')
test_data_tarball = os.path.join(test_dir, data_tarball)
if not os.path.exists(test_data_dir):
if not os.path.exists(test_data_tarball):
sh.wget('-P', test_dir, data_tarball_url)
sh.tar('zxvf', test_data_tarball, '-C', test_dir)
return os.path.join(test_data_dir, 'input')
def _get_project_name(archive):
"""
Get the project name from the archive.
The project name is the name of the root directory in the archive.
"""
try:
contents = tar(exclude='*/*', list=True, file=archive)
return os.path.dirname(str(contents))
except ErrorReturnCode as e:
raise CommandError('Failed to list archive - %s' % e)
def _decompress_archive(self, archive_path):
"""
Decompress the given archive into the S2E environment's projects
directory.
"""
try:
logger.info('Decompressing archive %s', archive_path)
tar(extract=True, xz=True, verbose=True, file=archive_path,
directory=self.projects_path(), _fg=True, _out=sys.stdout,
_err=sys.stderr)
except ErrorReturnCode as e:
raise CommandError('Failed to decompress project archive - %s', e)
def build_arch(self, arch):
# We don't have to actually build anything as CrystaX comes
# with the necessary modules. They are included by modifying
# the Android.mk in the jni folder.
# If the Python version to be used is not prebuilt with the CrystaX
# NDK, we do have to download it.
crystax_python_dir = join(self.ctx.ndk_dir, 'sources', 'python')
if not exists(join(crystax_python_dir, self.version)):
info(('The NDK does not have a prebuilt Python {}, trying '
'to obtain one.').format(self.version))
if self.version not in prebuilt_download_locations:
error(('No prebuilt version for Python {} could be found, '
'the built cannot continue.'))
exit(1)
with temp_directory() as td:
self.download_file(prebuilt_download_locations[self.version],
join(td, 'downloaded_python'))
shprint(sh.tar, 'xf', join(td, 'downloaded_python'),
'--directory', crystax_python_dir)
if not exists(join(crystax_python_dir, self.version)):
error(('Something went wrong, the directory at {} should '
'have been created but does not exist.').format(
join(crystax_python_dir, self.version)))
if not exists(join(
crystax_python_dir, self.version, 'libs', arch.arch)):
error(('The prebuilt Python for version {} does not contain '
'binaries for your chosen architecture "{}".').format(
self.version, arch.arch))
exit(1)
# TODO: We should have an option to build a new Python. This
# would also allow linking to openssl and sqlite from CrystaX.
dirn = self.ctx.get_python_install_dir()
ensure_dir(dirn)
# Instead of using a locally built hostpython, we use the
# user's Python for now. They must have the right version
# available. Using e.g. pyenv makes this easy.
self.ctx.hostpython = 'python{}'.format(self.version)
def extract_source(self, source, cwd):
"""
(internal) Extract the `source` into the directory `cwd`.
"""
if not source:
return
if isfile(source):
info("Extract {} into {}".format(source, cwd))
if source.endswith(".tgz") or source.endswith(".tar.gz"):
shprint(sh.tar, "-C", cwd, "-xvzf", source)
elif source.endswith(".tbz2") or source.endswith(".tar.bz2"):
shprint(sh.tar, "-C", cwd, "-xvjf", source)
elif source.endswith(".zip"):
zf = zipfile.ZipFile(source)
zf.extractall(path=cwd)
zf.close()
else:
warning(
"Error: cannot extract, unrecognized extension for {}"
.format(source))
raise Exception()
elif isdir(source):
info("Copying {} into {}".format(source, cwd))
shprint(sh.cp, '-a', source, cwd)
else:
warning(
"Error: cannot extract or copy, unrecognized path {}"
.format(source))
raise Exception()
# def get_archive_rootdir(self, filename):
# if filename.endswith(".tgz") or filename.endswith(".tar.gz") or \
# filename.endswith(".tbz2") or filename.endswith(".tar.bz2"):
# archive = tarfile.open(filename)
# root = archive.next().path.split("/")
# return root[0]
# elif filename.endswith(".zip"):
# with zipfile.ZipFile(filename) as zf:
# return dirname(zf.namelist()[0])
# else:
# print("Error: cannot detect root directory")
# print("Unrecognized extension for {}".format(filename))
# raise Exception()