Refactor: More f-string for bypy scripts
This commit is contained in:
parent
74921c1373
commit
74e70d2548
@ -26,7 +26,7 @@ def initialize_constants():
|
|||||||
kitty_constants = {}
|
kitty_constants = {}
|
||||||
src = read_src_file('constants.py')
|
src = read_src_file('constants.py')
|
||||||
nv = re.search(r'Version\((\d+), (\d+), (\d+)\)', src)
|
nv = re.search(r'Version\((\d+), (\d+), (\d+)\)', src)
|
||||||
kitty_constants['version'] = '%s.%s.%s' % (nv.group(1), nv.group(2), nv.group(3))
|
kitty_constants['version'] = f'{nv.group(1)}.{nv.group(2)}.{nv.group(3)}'
|
||||||
kitty_constants['appname'] = re.search(
|
kitty_constants['appname'] = re.search(
|
||||||
r'appname: str\s+=\s+(u{0,1})[\'"]([^\'"]+)[\'"]', src
|
r'appname: str\s+=\s+(u{0,1})[\'"]([^\'"]+)[\'"]', src
|
||||||
).group(2)
|
).group(2)
|
||||||
|
|||||||
@ -38,7 +38,7 @@ def binary_includes():
|
|||||||
'ncursesw', 'readline', 'brotlicommon', 'brotlienc', 'brotlidec'
|
'ncursesw', 'readline', 'brotlicommon', 'brotlienc', 'brotlidec'
|
||||||
))) + (
|
))) + (
|
||||||
get_dll_path('bz2', 2), get_dll_path('ssl', 2), get_dll_path('crypto', 2),
|
get_dll_path('bz2', 2), get_dll_path('ssl', 2), get_dll_path('crypto', 2),
|
||||||
get_dll_path('python' + py_ver, 2),
|
get_dll_path(f'python{py_ver}', 2),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ class Env:
|
|||||||
def __init__(self, package_dir):
|
def __init__(self, package_dir):
|
||||||
self.base = package_dir
|
self.base = package_dir
|
||||||
self.lib_dir = j(self.base, 'lib')
|
self.lib_dir = j(self.base, 'lib')
|
||||||
self.py_dir = j(self.lib_dir, 'python' + py_ver)
|
self.py_dir = j(self.lib_dir, f'python{py_ver}')
|
||||||
os.makedirs(self.py_dir)
|
os.makedirs(self.py_dir)
|
||||||
self.bin_dir = j(self.base, 'bin')
|
self.bin_dir = j(self.base, 'bin')
|
||||||
self.obj_dir = mkdtemp('launchers-')
|
self.obj_dir = mkdtemp('launchers-')
|
||||||
@ -107,7 +107,7 @@ def add_ca_certs(env):
|
|||||||
|
|
||||||
def copy_python(env):
|
def copy_python(env):
|
||||||
print('Copying python...')
|
print('Copying python...')
|
||||||
srcdir = j(PREFIX, 'lib/python' + py_ver)
|
srcdir = j(PREFIX, f'lib/python{py_ver}')
|
||||||
|
|
||||||
for x in os.listdir(srcdir):
|
for x in os.listdir(srcdir):
|
||||||
y = j(srcdir, x)
|
y = j(srcdir, x)
|
||||||
@ -187,11 +187,11 @@ def strip_files(files, argv_max=(256 * 1024)):
|
|||||||
|
|
||||||
|
|
||||||
def strip_binaries(files):
|
def strip_binaries(files):
|
||||||
print('Stripping %d files...' % len(files))
|
print(f'Stripping {len(files)} files...')
|
||||||
before = sum(os.path.getsize(x) for x in files)
|
before = sum(os.path.getsize(x) for x in files)
|
||||||
strip_files(files)
|
strip_files(files)
|
||||||
after = sum(os.path.getsize(x) for x in files)
|
after = sum(os.path.getsize(x) for x in files)
|
||||||
print('Stripped %.1f MB' % ((before - after) / (1024 * 1024.)))
|
print('Stripped {:.1f} MB'.format((before - after) / (1024 * 1024.)))
|
||||||
|
|
||||||
|
|
||||||
def create_tarfile(env, compression_level='9'):
|
def create_tarfile(env, compression_level='9'):
|
||||||
@ -203,7 +203,7 @@ def create_tarfile(env, compression_level='9'):
|
|||||||
if err.errno != errno.ENOENT:
|
if err.errno != errno.ENOENT:
|
||||||
raise
|
raise
|
||||||
os.mkdir(base)
|
os.mkdir(base)
|
||||||
dist = os.path.join(base, '%s-%s-%s.tar' % (kitty_constants['appname'], kitty_constants['version'], arch))
|
dist = os.path.join(base, f'{kitty_constants["appname"]}-{kitty_constants["version"]}-{arch}.tar')
|
||||||
with tarfile.open(dist, mode='w', format=tarfile.PAX_FORMAT) as tf:
|
with tarfile.open(dist, mode='w', format=tarfile.PAX_FORMAT) as tf:
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
os.chdir(env.base)
|
os.chdir(env.base)
|
||||||
@ -213,13 +213,13 @@ def create_tarfile(env, compression_level='9'):
|
|||||||
finally:
|
finally:
|
||||||
os.chdir(cwd)
|
os.chdir(cwd)
|
||||||
print('Compressing archive...')
|
print('Compressing archive...')
|
||||||
ans = dist.rpartition('.')[0] + '.txz'
|
ans = f'{dist.rpartition(".")[0]}.txz'
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
subprocess.check_call(['xz', '--threads=0', '-f', '-' + compression_level, dist])
|
subprocess.check_call(['xz', '--threads=0', '-f', f'-{compression_level}', dist])
|
||||||
secs = time.time() - start_time
|
secs = time.time() - start_time
|
||||||
print('Compressed in %d minutes %d seconds' % (secs // 60, secs % 60))
|
print('Compressed in {} minutes {} seconds'.format(secs // 60, secs % 60))
|
||||||
os.rename(dist + '.xz', ans)
|
os.rename(f'{dist}.xz', ans)
|
||||||
print('Archive %s created: %.2f MB' % (
|
print('Archive {} created: {:.2f} MB'.format(
|
||||||
os.path.basename(ans), os.stat(ans).st_size / (1024.**2)))
|
os.path.basename(ans), os.stat(ans).st_size / (1024.**2)))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -90,7 +90,7 @@ def strip_files(files, argv_max=(256 * 1024)):
|
|||||||
def files_in(folder):
|
def files_in(folder):
|
||||||
for record in os.walk(folder):
|
for record in os.walk(folder):
|
||||||
for f in record[-1]:
|
for f in record[-1]:
|
||||||
yield os.path.join(record[0], f)
|
yield join(record[0], f)
|
||||||
|
|
||||||
|
|
||||||
def expand_dirs(items, exclude=lambda x: x.endswith('.so')):
|
def expand_dirs(items, exclude=lambda x: x.endswith('.so')):
|
||||||
@ -103,7 +103,7 @@ def expand_dirs(items, exclude=lambda x: x.endswith('.so')):
|
|||||||
|
|
||||||
|
|
||||||
def do_sign(app_dir):
|
def do_sign(app_dir):
|
||||||
with current_dir(os.path.join(app_dir, 'Contents')):
|
with current_dir(join(app_dir, 'Contents')):
|
||||||
# Sign all .so files
|
# Sign all .so files
|
||||||
so_files = {x for x in files_in('.') if x.endswith('.so')}
|
so_files = {x for x in files_in('.') if x.endswith('.so')}
|
||||||
codesign(so_files)
|
codesign(so_files)
|
||||||
@ -153,7 +153,7 @@ class Freeze(object):
|
|||||||
self.to_strip = []
|
self.to_strip = []
|
||||||
self.warnings = []
|
self.warnings = []
|
||||||
self.py_ver = py_ver
|
self.py_ver = py_ver
|
||||||
self.python_stdlib = join(self.resources_dir, 'Python', 'lib', 'python' + self.py_ver)
|
self.python_stdlib = join(self.resources_dir, 'Python', 'lib', f'python{self.py_ver}')
|
||||||
self.site_packages = self.python_stdlib # hack to avoid needing to add site-packages to path
|
self.site_packages = self.python_stdlib # hack to avoid needing to add site-packages to path
|
||||||
self.obj_dir = mkdtemp('launchers-')
|
self.obj_dir = mkdtemp('launchers-')
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ class Freeze(object):
|
|||||||
self.run_tests()
|
self.run_tests()
|
||||||
# self.run_shell()
|
# self.run_shell()
|
||||||
|
|
||||||
ret = self.makedmg(self.build_dir, APPNAME + '-' + VERSION)
|
ret = self.makedmg(self.build_dir, f'{APPNAME}-{VERSION}')
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@ -186,7 +186,7 @@ class Freeze(object):
|
|||||||
print('\nDownloading CA certs...')
|
print('\nDownloading CA certs...')
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
cdata = urlopen(kitty_constants['cacerts_url']).read()
|
cdata = urlopen(kitty_constants['cacerts_url']).read()
|
||||||
dest = os.path.join(self.contents_dir, 'Resources', 'cacert.pem')
|
dest = join(self.contents_dir, 'Resources', 'cacert.pem')
|
||||||
with open(dest, 'wb') as f:
|
with open(dest, 'wb') as f:
|
||||||
f.write(cdata)
|
f.write(cdata)
|
||||||
|
|
||||||
@ -197,7 +197,7 @@ class Freeze(object):
|
|||||||
|
|
||||||
@flush
|
@flush
|
||||||
def run_tests(self):
|
def run_tests(self):
|
||||||
iv['run_tests'](os.path.join(self.contents_dir, 'MacOS', 'kitty'))
|
iv['run_tests'](join(self.contents_dir, 'MacOS', 'kitty'))
|
||||||
|
|
||||||
@flush
|
@flush
|
||||||
def set_id(self, path_to_lib, new_id):
|
def set_id(self, path_to_lib, new_id):
|
||||||
@ -222,10 +222,10 @@ class Freeze(object):
|
|||||||
@flush
|
@flush
|
||||||
def get_local_dependencies(self, path_to_lib):
|
def get_local_dependencies(self, path_to_lib):
|
||||||
for x, is_id in self.get_dependencies(path_to_lib):
|
for x, is_id in self.get_dependencies(path_to_lib):
|
||||||
for y in (PREFIX + '/lib/', PREFIX + '/python/Python.framework/', '@rpath/'):
|
for y in (f'{PREFIX}/lib/', f'{PREFIX}/python/Python.framework/', '@rpath/'):
|
||||||
if x.startswith(y):
|
if x.startswith(y):
|
||||||
if y == PREFIX + '/python/Python.framework/':
|
if y == f'{PREFIX}/python/Python.framework/':
|
||||||
y = PREFIX + '/python/'
|
y = f'{PREFIX}/python/'
|
||||||
yield x, x[len(y):], is_id
|
yield x, x[len(y):], is_id
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -239,7 +239,7 @@ class Freeze(object):
|
|||||||
self.to_strip.append(path_to_lib)
|
self.to_strip.append(path_to_lib)
|
||||||
old_mode = flipwritable(path_to_lib)
|
old_mode = flipwritable(path_to_lib)
|
||||||
for dep, bname, is_id in self.get_local_dependencies(path_to_lib):
|
for dep, bname, is_id in self.get_local_dependencies(path_to_lib):
|
||||||
ndep = self.FID + '/' + bname
|
ndep = f'{self.FID}/{bname}'
|
||||||
self.change_dep(dep, ndep, is_id, path_to_lib)
|
self.change_dep(dep, ndep, is_id, path_to_lib)
|
||||||
ldeps = list(self.get_local_dependencies(path_to_lib))
|
ldeps = list(self.get_local_dependencies(path_to_lib))
|
||||||
if ldeps:
|
if ldeps:
|
||||||
@ -252,7 +252,7 @@ class Freeze(object):
|
|||||||
@flush
|
@flush
|
||||||
def add_python_framework(self):
|
def add_python_framework(self):
|
||||||
print('\nAdding Python framework')
|
print('\nAdding Python framework')
|
||||||
src = join(PREFIX + '/python', 'Python.framework')
|
src = join(f'{PREFIX}/python', 'Python.framework')
|
||||||
x = join(self.frameworks_dir, 'Python.framework')
|
x = join(self.frameworks_dir, 'Python.framework')
|
||||||
curr = os.path.realpath(join(src, 'Versions', 'Current'))
|
curr = os.path.realpath(join(src, 'Versions', 'Current'))
|
||||||
currd = join(x, 'Versions', basename(curr))
|
currd = join(x, 'Versions', basename(curr))
|
||||||
@ -262,12 +262,12 @@ class Freeze(object):
|
|||||||
shutil.copy2(join(curr, 'Python'), currd)
|
shutil.copy2(join(curr, 'Python'), currd)
|
||||||
self.set_id(
|
self.set_id(
|
||||||
join(currd, 'Python'),
|
join(currd, 'Python'),
|
||||||
self.FID + '/Python.framework/Versions/%s/Python' % basename(curr))
|
f'{self.FID}/Python.framework/Versions/{basename(curr)}/Python')
|
||||||
# The following is needed for codesign
|
# The following is needed for codesign
|
||||||
with current_dir(x):
|
with current_dir(x):
|
||||||
os.symlink(basename(curr), 'Versions/Current')
|
os.symlink(basename(curr), 'Versions/Current')
|
||||||
for y in ('Python', 'Resources'):
|
for y in ('Python', 'Resources'):
|
||||||
os.symlink('Versions/Current/%s' % y, y)
|
os.symlink(f'Versions/Current/{y}', y)
|
||||||
|
|
||||||
@flush
|
@flush
|
||||||
def install_dylib(self, path, set_id=True):
|
def install_dylib(self, path, set_id=True):
|
||||||
@ -275,7 +275,7 @@ class Freeze(object):
|
|||||||
if set_id:
|
if set_id:
|
||||||
self.set_id(
|
self.set_id(
|
||||||
join(self.frameworks_dir, basename(path)),
|
join(self.frameworks_dir, basename(path)),
|
||||||
self.FID + '/' + basename(path))
|
f'{self.FID}/{basename(path)}')
|
||||||
self.fix_dependencies_in_lib(join(self.frameworks_dir, basename(path)))
|
self.fix_dependencies_in_lib(join(self.frameworks_dir, basename(path)))
|
||||||
|
|
||||||
@flush
|
@flush
|
||||||
@ -291,11 +291,11 @@ class Freeze(object):
|
|||||||
'rsync.2',
|
'rsync.2',
|
||||||
):
|
):
|
||||||
print('\nAdding', x)
|
print('\nAdding', x)
|
||||||
x = 'lib%s.dylib' % x
|
x = f'lib{x}.dylib'
|
||||||
src = join(PREFIX, 'lib', x)
|
src = join(PREFIX, 'lib', x)
|
||||||
shutil.copy2(src, self.frameworks_dir)
|
shutil.copy2(src, self.frameworks_dir)
|
||||||
dest = join(self.frameworks_dir, x)
|
dest = join(self.frameworks_dir, x)
|
||||||
self.set_id(dest, self.FID + '/' + x)
|
self.set_id(dest, f'{self.FID}/{x}')
|
||||||
self.fix_dependencies_in_lib(dest)
|
self.fix_dependencies_in_lib(dest)
|
||||||
|
|
||||||
@flush
|
@flush
|
||||||
@ -321,7 +321,7 @@ class Freeze(object):
|
|||||||
@flush
|
@flush
|
||||||
def add_stdlib(self):
|
def add_stdlib(self):
|
||||||
print('\nAdding python stdlib')
|
print('\nAdding python stdlib')
|
||||||
src = PREFIX + '/python/Python.framework/Versions/Current/lib/python' + self.py_ver
|
src = f'{PREFIX}/python/Python.framework/Versions/Current/lib/python{self.py_ver}'
|
||||||
dest = self.python_stdlib
|
dest = self.python_stdlib
|
||||||
if not os.path.exists(dest):
|
if not os.path.exists(dest):
|
||||||
os.makedirs(dest)
|
os.makedirs(dest)
|
||||||
@ -345,19 +345,19 @@ class Freeze(object):
|
|||||||
kitty_dir = join(self.resources_dir, 'kitty')
|
kitty_dir = join(self.resources_dir, 'kitty')
|
||||||
bases = ('kitty', 'kittens', 'kitty_tests')
|
bases = ('kitty', 'kittens', 'kitty_tests')
|
||||||
for x in bases:
|
for x in bases:
|
||||||
dest = os.path.join(self.python_stdlib, x)
|
dest = join(self.python_stdlib, x)
|
||||||
os.rename(os.path.join(kitty_dir, x), dest)
|
os.rename(join(kitty_dir, x), dest)
|
||||||
if x == 'kitty':
|
if x == 'kitty':
|
||||||
shutil.rmtree(os.path.join(dest, 'launcher'))
|
shutil.rmtree(join(dest, 'launcher'))
|
||||||
os.rename(os.path.join(kitty_dir, '__main__.py'), os.path.join(self.python_stdlib, 'kitty_main.py'))
|
os.rename(join(kitty_dir, '__main__.py'), join(self.python_stdlib, 'kitty_main.py'))
|
||||||
shutil.rmtree(os.path.join(kitty_dir, '__pycache__'))
|
shutil.rmtree(join(kitty_dir, '__pycache__'))
|
||||||
pdir = os.path.join(dirname(self.python_stdlib), 'kitty-extensions')
|
pdir = join(dirname(self.python_stdlib), 'kitty-extensions')
|
||||||
os.mkdir(pdir)
|
os.mkdir(pdir)
|
||||||
print('Extracting extension modules from', self.python_stdlib, 'to', pdir)
|
print('Extracting extension modules from', self.python_stdlib, 'to', pdir)
|
||||||
ext_map = extract_extension_modules(self.python_stdlib, pdir)
|
ext_map = extract_extension_modules(self.python_stdlib, pdir)
|
||||||
shutil.copy(os.path.join(os.path.dirname(self_dir), 'site.py'), os.path.join(self.python_stdlib, 'site.py'))
|
shutil.copy(join(os.path.dirname(self_dir), 'site.py'), join(self.python_stdlib, 'site.py'))
|
||||||
for x in bases:
|
for x in bases:
|
||||||
iv['sanitize_source_folder'](os.path.join(self.python_stdlib, x))
|
iv['sanitize_source_folder'](join(self.python_stdlib, x))
|
||||||
self.compile_py_modules()
|
self.compile_py_modules()
|
||||||
freeze_python(self.python_stdlib, pdir, self.obj_dir, ext_map, develop_mode_env_var='KITTY_DEVELOP_FROM', remove_pyc_files=True)
|
freeze_python(self.python_stdlib, pdir, self.obj_dir, ext_map, develop_mode_env_var='KITTY_DEVELOP_FROM', remove_pyc_files=True)
|
||||||
iv['build_frozen_launcher']([path_to_freeze_dir(), self.obj_dir])
|
iv['build_frozen_launcher']([path_to_freeze_dir(), self.obj_dir])
|
||||||
@ -434,23 +434,23 @@ class Freeze(object):
|
|||||||
''' Copy a directory d into a dmg named volname '''
|
''' Copy a directory d into a dmg named volname '''
|
||||||
print('\nMaking dmg...')
|
print('\nMaking dmg...')
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
destdir = os.path.join(SW, 'dist')
|
destdir = join(SW, 'dist')
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(destdir)
|
shutil.rmtree(destdir)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
os.mkdir(destdir)
|
os.mkdir(destdir)
|
||||||
dmg = os.path.join(destdir, volname + '.dmg')
|
dmg = join(destdir, f'{volname}.dmg')
|
||||||
if os.path.exists(dmg):
|
if os.path.exists(dmg):
|
||||||
os.unlink(dmg)
|
os.unlink(dmg)
|
||||||
tdir = tempfile.mkdtemp()
|
tdir = tempfile.mkdtemp()
|
||||||
appdir = os.path.join(tdir, os.path.basename(d))
|
appdir = join(tdir, os.path.basename(d))
|
||||||
shutil.copytree(d, appdir, symlinks=True)
|
shutil.copytree(d, appdir, symlinks=True)
|
||||||
if self.sign_installers:
|
if self.sign_installers:
|
||||||
with timeit() as times:
|
with timeit() as times:
|
||||||
sign_app(appdir, self.notarize)
|
sign_app(appdir, self.notarize)
|
||||||
print('Signing completed in %d minutes %d seconds' % tuple(times))
|
print('Signing completed in {} minutes {} seconds'.format(*times))
|
||||||
os.symlink('/Applications', os.path.join(tdir, 'Applications'))
|
os.symlink('/Applications', join(tdir, 'Applications'))
|
||||||
size_in_mb = int(
|
size_in_mb = int(
|
||||||
subprocess.check_output(['du', '-s', '-k', tdir]).decode('utf-8')
|
subprocess.check_output(['du', '-s', '-k', tdir]).decode('utf-8')
|
||||||
.split()[0]) / 1024.
|
.split()[0]) / 1024.
|
||||||
@ -466,10 +466,10 @@ class Freeze(object):
|
|||||||
print('\nCreating dmg...')
|
print('\nCreating dmg...')
|
||||||
with timeit() as times:
|
with timeit() as times:
|
||||||
subprocess.check_call(cmd + [dmg])
|
subprocess.check_call(cmd + [dmg])
|
||||||
print('dmg created in %d minutes and %d seconds' % tuple(times))
|
print('dmg created in {} minutes and {} seconds'.format(*times))
|
||||||
shutil.rmtree(tdir)
|
shutil.rmtree(tdir)
|
||||||
size = os.stat(dmg).st_size / (1024 * 1024.)
|
size = os.stat(dmg).st_size / (1024 * 1024.)
|
||||||
print('\nInstaller size: %.2fMB\n' % size)
|
print(f'\nInstaller size: {size:.2f}MB\n')
|
||||||
return dmg
|
return dmg
|
||||||
|
|
||||||
|
|
||||||
@ -477,7 +477,7 @@ def main():
|
|||||||
args = globals()['args']
|
args = globals()['args']
|
||||||
ext_dir = globals()['ext_dir']
|
ext_dir = globals()['ext_dir']
|
||||||
Freeze(
|
Freeze(
|
||||||
os.path.join(ext_dir, kitty_constants['appname'] + '.app'),
|
join(ext_dir, f'{kitty_constants["appname"]}.app'),
|
||||||
dont_strip=args.dont_strip,
|
dont_strip=args.dont_strip,
|
||||||
sign_installers=args.sign_installers,
|
sign_installers=args.sign_installers,
|
||||||
notarize=args.notarize,
|
notarize=args.notarize,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user