Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def build_c_extensions(ext_dir, args):
bdir = os.path.join(build_dir(), 'calibre-extension-objects')
cmd = [
PYTHON, 'setup.py', 'build',
'--output-dir', ext_dir, '--build-dir', bdir,
]
if args.build_only:
cmd.extend(('--only', args.build_only))
if run(*cmd, COMPILER_CWD=bdir) != 0:
print('Building of calibre C extensions failed', file=sys.stderr)
os.chdir(CALIBRE_DIR)
run_shell()
raise SystemExit('Building of calibre C extensions failed')
return ext_dir
for pat in ('PyQt5\\uic\\port_v3', ):
x = glob.glob(j(env.lib_dir, 'site-packages', pat))[0]
shutil.rmtree(x)
pyqt = j(env.lib_dir, 'site-packages', 'PyQt5')
for x in {x for x in os.listdir(pyqt) if x.endswith('.pyd')}:
if x.partition('.')[0] not in PYQT_MODULES and x != 'sip.pyd':
os.remove(j(pyqt, x))
with open(j(pyqt, '__init__.py') , 'r+b') as f:
raw = f.read()
nraw = raw.replace(b'def find_qt():', b'def find_qt():\n return # disabled for calibre')
if nraw == raw:
raise Exception('Failed to patch PyQt to disable dll directory manipulation')
f.seek(0), f.truncate(), f.write(nraw)
printf('Adding calibre sources...')
for x in glob.glob(j(CALIBRE_DIR, 'src', '*')):
if os.path.isdir(x):
if os.path.exists(os.path.join(x, '__init__.py')):
shutil.copytree(x, j(sp_dir, b(x)), ignore=shutil.ignore_patterns('*.pyc', '*.pyo'))
else:
shutil.copy(x, j(sp_dir, b(x)))
for x in (r'calibre\manual', r'calibre\plugins', 'pythonwin'):
deld = j(sp_dir, x)
if os.path.exists(deld):
shutil.rmtree(deld)
for x in os.walk(j(sp_dir, 'calibre')):
for f in x[-1]:
if not f.endswith('.py'):
os.remove(j(x[0], f))
def add_site_packages(self):
print('\nAdding site-packages')
os.makedirs(self.site_packages)
sys_path = json.loads(subprocess.check_output([
PYTHON, '-c', 'import sys, json; json.dump(sys.path, sys.stdout)']))
paths = reversed(tuple(map(abspath, [x for x in sys_path if x.startswith('/') and not x.startswith('/Library/')])))
upaths = []
for x in paths:
if x not in upaths and (x.endswith('.egg') or x.endswith('/site-packages')):
upaths.append(x)
upaths.append(join(CALIBRE_DIR, 'src'))
for x in upaths:
print('\t', x)
tdir = None
try:
if not os.path.isdir(x):
zf = zipfile.ZipFile(x)
tdir = tempfile.mkdtemp()
zf.extractall(tdir)
x = tdir
self.add_modules_from_dir(x)
self.add_packages_from_dir(x)
finally:
if tdir is not None:
shutil.rmtree(tdir)
try:
shutil.rmtree(join(self.site_packages, 'calibre', 'plugins'))
def run(*args, **extra_env):
env = os.environ.copy()
env.update(worker_env)
env.update(extra_env)
env['SW'] = PREFIX
env['LD_LIBRARY_PATH'] = LIBDIR
env['SIP_BIN'] = os.path.join(PREFIX, 'bin', 'sip')
env['QMAKE'] = os.path.join(PREFIX, 'qt', 'bin', 'qmake')
return subprocess.call(list(args), env=env, cwd=CALIBRE_DIR)
def __init__(self):
self.src_root = CALIBRE_DIR
self.base = mkdtemp('frozen-')
self.lib_dir = j(self.base, 'lib')
self.py_dir = j(self.lib_dir, 'python' + py_ver)
os.makedirs(self.py_dir)
self.bin_dir = j(self.base, 'bin')
os.mkdir(self.bin_dir)
self.SRC = j(self.src_root, 'src')
self.obj_dir = mkdtemp('launchers-')
def read_cal_file(name):
with open(os.path.join(CALIBRE_DIR, 'src', 'calibre', name), 'rb') as f:
return f.read().decode('utf-8')
for pat in ('PyQt5\\uic\\port_v3', ):
x = glob.glob(j(env.lib_dir, 'site-packages', pat))[0]
shutil.rmtree(x)
pyqt = j(env.lib_dir, 'site-packages', 'PyQt5')
for x in {x for x in os.listdir(pyqt) if x.endswith('.pyd')}:
if x.partition('.')[0] not in PYQT_MODULES and x != 'sip.pyd':
os.remove(j(pyqt, x))
with open(j(pyqt, '__init__.py') , 'r+b') as f:
raw = f.read()
nraw = raw.replace(b'def find_qt():', b'def find_qt():\n return # disabled for calibre')
if nraw == raw:
raise Exception('Failed to patch PyQt to disable dll directory manipulation')
f.seek(0), f.truncate(), f.write(nraw)
printf('Adding calibre sources...')
for x in glob.glob(j(CALIBRE_DIR, 'src', '*')):
if os.path.isdir(x):
if os.path.exists(os.path.join(x, '__init__.py')):
shutil.copytree(x, j(sp_dir, b(x)), ignore=shutil.ignore_patterns('*.pyc', '*.pyo'))
else:
shutil.copy(x, j(sp_dir, b(x)))
for x in (r'calibre\manual', r'calibre\plugins', 'pythonwin'):
deld = j(sp_dir, x)
if os.path.exists(deld):
shutil.rmtree(deld)
for x in os.walk(j(sp_dir, 'calibre')):
for f in x[-1]:
if not f.endswith('.py'):
os.remove(j(x[0], f))