##// END OF EJS Templates
rust-cpython: mark all PyLeaked methods as unsafe...
rust-cpython: mark all PyLeaked methods as unsafe Unfortunately, these methods can be abused to obtain the inner 'static reference. The simplest (pseudo-code) example is: let leaked: PyLeaked<&'static _> = shared.leak_immutable(); let static_ref: &'static _ = &*leaked.try_borrow(py)?; // PyLeakedRef::deref() tries to bound the lifetime to itself, but // the underlying data is a &'static reference, so the returned // reference can be &'static. This problem can be easily fixed by coercing the lifetime, but there are many other ways to achieve that, and there wouldn't be a generic solution: let leaked: PyLeaked<&'static [_]> = shared.leak_immutable(); let leaked_iter: PyLeaked<slice::Iter<'static, _>> = unsafe { leaked.map(|v| v.iter()) }; let static_slice: &'static [_] = leaked_iter.try_borrow(py)?.as_slice(); So basically I failed to design the safe borrowing interface. Maybe we'll instead have to add much more restricted interface on top of the unsafe PyLeaked methods? For instance, Iterator::next() could be implemented if its Item type is not &'a (where 'a may be cheated.) Anyway, this seems not an easy issue, so it's probably better to leave the current interface as unsafe, and get broader comments while upstreaming this feature.

File last commit:

r43824:cf5eaf24 default
r44689:e960c30d default
Show More
util.py
226 lines | 6.2 KiB | text/x-python | PythonLexer
Gregory Szorc
packaging: convert files to LF...
r42118 # util.py - Common packaging utility code.
#
# Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
# no-check-code because Python 3 native.
import distutils.version
import getpass
Gregory Szorc
packaging: stage installed files for Inno...
r43916 import glob
Gregory Szorc
packaging: convert files to LF...
r42118 import os
import pathlib
Gregory Szorc
packaging: always pass VERSION into Inno invocation...
r43918 import re
Gregory Szorc
packaging: stage installed files for Inno...
r43916 import shutil
Gregory Szorc
packaging: convert files to LF...
r42118 import subprocess
import tarfile
import zipfile
def extract_tar_to_directory(source: pathlib.Path, dest: pathlib.Path):
with tarfile.open(source, 'r') as tf:
tf.extractall(dest)
def extract_zip_to_directory(source: pathlib.Path, dest: pathlib.Path):
with zipfile.ZipFile(source, 'r') as zf:
zf.extractall(dest)
def find_vc_runtime_files(x64=False):
"""Finds Visual C++ Runtime DLLs to include in distribution."""
winsxs = pathlib.Path(os.environ['SYSTEMROOT']) / 'WinSxS'
prefix = 'amd64' if x64 else 'x86'
Augie Fackler
formatting: blacken the codebase...
r43346 candidates = sorted(
p
for p in os.listdir(winsxs)
if p.lower().startswith('%s_microsoft.vc90.crt_' % prefix)
)
Gregory Szorc
packaging: convert files to LF...
r42118
for p in candidates:
print('found candidate VC runtime: %s' % p)
# Take the newest version.
version = candidates[-1]
d = winsxs / version
return [
d / 'msvcm90.dll',
d / 'msvcp90.dll',
d / 'msvcr90.dll',
winsxs / 'Manifests' / ('%s.manifest' % version),
]
def windows_10_sdk_info():
"""Resolves information about the Windows 10 SDK."""
base = pathlib.Path(os.environ['ProgramFiles(x86)']) / 'Windows Kits' / '10'
if not base.is_dir():
raise Exception('unable to find Windows 10 SDK at %s' % base)
# Find the latest version.
bin_base = base / 'bin'
versions = [v for v in os.listdir(bin_base) if v.startswith('10.')]
version = sorted(versions, reverse=True)[0]
bin_version = bin_base / version
return {
'root': base,
'version': version,
'bin_root': bin_version,
'bin_x86': bin_version / 'x86',
Augie Fackler
formatting: blacken the codebase...
r43346 'bin_x64': bin_version / 'x64',
Gregory Szorc
packaging: convert files to LF...
r42118 }
def find_signtool():
"""Find signtool.exe from the Windows SDK."""
sdk = windows_10_sdk_info()
for key in ('bin_x64', 'bin_x86'):
p = sdk[key] / 'signtool.exe'
if p.exists():
return p
raise Exception('could not find signtool.exe in Windows 10 SDK')
Augie Fackler
formatting: blacken the codebase...
r43346 def sign_with_signtool(
file_path,
description,
subject_name=None,
cert_path=None,
cert_password=None,
timestamp_url=None,
):
Gregory Szorc
packaging: convert files to LF...
r42118 """Digitally sign a file with signtool.exe.
``file_path`` is file to sign.
``description`` is text that goes in the signature.
The signing certificate can be specified by ``cert_path`` or
``subject_name``. These correspond to the ``/f`` and ``/n`` arguments
to signtool.exe, respectively.
The certificate password can be specified via ``cert_password``. If
not provided, you will be prompted for the password.
``timestamp_url`` is the URL of a RFC 3161 timestamp server (``/tr``
argument to signtool.exe).
"""
if cert_path and subject_name:
raise ValueError('cannot specify both cert_path and subject_name')
while cert_path and not cert_password:
cert_password = getpass.getpass('password for %s: ' % cert_path)
args = [
Augie Fackler
formatting: blacken the codebase...
r43346 str(find_signtool()),
'sign',
Gregory Szorc
packaging: convert files to LF...
r42118 '/v',
Augie Fackler
formatting: blacken the codebase...
r43346 '/fd',
'sha256',
'/d',
description,
Gregory Szorc
packaging: convert files to LF...
r42118 ]
if cert_path:
args.extend(['/f', str(cert_path), '/p', cert_password])
elif subject_name:
args.extend(['/n', subject_name])
if timestamp_url:
args.extend(['/tr', timestamp_url, '/td', 'sha256'])
args.append(str(file_path))
print('signing %s' % file_path)
subprocess.run(args, check=True)
PRINT_PYTHON_INFO = '''
import platform; print("%s:%s" % (platform.architecture()[0], platform.python_version()))
'''.strip()
def python_exe_info(python_exe: pathlib.Path):
"""Obtain information about a Python executable."""
Matt Harbison
packaging: don't crash building wix with python3.6 and earlier...
r42259 res = subprocess.check_output([str(python_exe), '-c', PRINT_PYTHON_INFO])
Gregory Szorc
packaging: convert files to LF...
r42118
Matt Harbison
packaging: don't crash building wix with python3.6 and earlier...
r42259 arch, version = res.decode('utf-8').split(':')
Gregory Szorc
packaging: convert files to LF...
r42118
version = distutils.version.LooseVersion(version)
return {
'arch': arch,
'version': version,
'py3': version >= distutils.version.LooseVersion('3'),
}
Gregory Szorc
packaging: stage installed files for Inno...
r43916
def process_install_rules(
rules: list, source_dir: pathlib.Path, dest_dir: pathlib.Path
):
for source, dest in rules:
if '*' in source:
if not dest.endswith('/'):
raise ValueError('destination must end in / when globbing')
# We strip off the source path component before the first glob
# character to construct the relative install path.
prefix_end_index = source[: source.index('*')].rindex('/')
relative_prefix = source_dir / source[0:prefix_end_index]
for res in glob.glob(str(source_dir / source), recursive=True):
source_path = pathlib.Path(res)
if source_path.is_dir():
continue
rel_path = source_path.relative_to(relative_prefix)
dest_path = dest_dir / dest[:-1] / rel_path
dest_path.parent.mkdir(parents=True, exist_ok=True)
print('copying %s to %s' % (source_path, dest_path))
shutil.copy(source_path, dest_path)
# Simple file case.
else:
source_path = pathlib.Path(source)
if dest.endswith('/'):
dest_path = pathlib.Path(dest) / source_path.name
else:
dest_path = pathlib.Path(dest)
full_source_path = source_dir / source_path
full_dest_path = dest_dir / dest_path
full_dest_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(full_source_path, full_dest_path)
print('copying %s to %s' % (full_source_path, full_dest_path))
Gregory Szorc
packaging: always pass VERSION into Inno invocation...
r43918
def read_version_py(source_dir):
"""Read the mercurial/__version__.py file to resolve the version string."""
p = source_dir / 'mercurial' / '__version__.py'
with p.open('r', encoding='utf-8') as fh:
m = re.search('version = b"([^"]+)"', fh.read(), re.MULTILINE)
if not m:
raise Exception('could not parse %s' % p)
return m.group(1)