| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPLv3 Copyright: 2009, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
|
| 4 |
|
| 5 |
import glob |
| 6 |
import os |
| 7 |
import shutil |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
import tempfile |
| 11 |
import textwrap |
| 12 |
import time |
| 13 |
|
| 14 |
from setup import Command, __appname__, __version__, basenames, functions, isbsd, ishaiku, islinux, modules |
| 15 |
|
| 16 |
HEADER = '''\ |
| 17 |
#!/usr/bin/env python{py_major_version} |
| 18 |
|
| 19 |
""" |
| 20 |
This is the standard runscript for all of calibre's tools. |
| 21 |
Do not modify it unless you know what you are doing. |
| 22 |
""" |
| 23 |
|
| 24 |
import sys, os |
| 25 |
|
| 26 |
path = os.environ.get('CALIBRE_PYTHON_PATH', {path!r}) |
| 27 |
if path not in sys.path: |
| 28 |
sys.path.insert(0, path) |
| 29 |
|
| 30 |
sys.resources_location = os.environ.get('CALIBRE_RESOURCES_PATH', {resources!r}) |
| 31 |
sys.extensions_location = os.environ.get('CALIBRE_EXTENSIONS_PATH', {extensions!r}) |
| 32 |
sys.executables_location = os.environ.get('CALIBRE_EXECUTABLES_PATH', {executables!r}) |
| 33 |
sys.system_plugins_location = {system_plugins_loc!r} |
| 34 |
|
| 35 |
''' |
| 36 |
|
| 37 |
TEMPLATE = ( |
| 38 |
HEADER |
| 39 |
+ ''' |
| 40 |
from {module} import {func!s} |
| 41 |
sys.exit({func!s}()) |
| 42 |
''' |
| 43 |
) |
| 44 |
|
| 45 |
COMPLETE_TEMPLATE = ( |
| 46 |
HEADER |
| 47 |
+ ''' |
| 48 |
sys.path.insert(0, os.path.join(path, 'calibre', 'utils')) |
| 49 |
import complete |
| 50 |
sys.path = sys.path[1:] |
| 51 |
|
| 52 |
sys.exit(complete.main()) |
| 53 |
''' |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
class Develop(Command): |
| 58 |
description = textwrap.dedent('''\ |
| 59 |
Setup a development environment for calibre. |
| 60 |
This allows you to run calibre directly from the source tree. |
| 61 |
Binaries will be installed in <prefix>/bin where <prefix> is |
| 62 |
the prefix of your python installation. This can be controlled |
| 63 |
via the --prefix option. |
| 64 |
''') |
| 65 |
short_description = 'Setup a development environment for calibre' |
| 66 |
MODE = 0o755 |
| 67 |
drop_privileges_for_subcommands = True |
| 68 |
|
| 69 |
sub_commands = [ |
| 70 |
'build', |
| 71 |
'resources', |
| 72 |
'iso639', |
| 73 |
'iso3166', |
| 74 |
'gui', |
| 75 |
] |
| 76 |
|
| 77 |
def add_postinstall_options(self, parser): |
| 78 |
parser.add_option('--make-errors-fatal', action='store_true', default=False, dest='fatal_errors', help='If set die on post install errors.') |
| 79 |
parser.add_option( |
| 80 |
'--no-postinstall', |
| 81 |
action='store_false', |
| 82 |
dest='postinstall', |
| 83 |
default=True, |
| 84 |
help="Don't run post install actions like creating MAN pages, setting" + ' up desktop integration and so on', |
| 85 |
) |
| 86 |
|
| 87 |
def add_options(self, parser): |
| 88 |
parser.add_option('--prefix', help='Binaries will be installed in <prefix>/bin') |
| 89 |
parser.add_option('--system-plugins-location', help='Path to a directory from which the installed calibre will load plugins') |
| 90 |
self.add_postinstall_options(parser) |
| 91 |
|
| 92 |
def consolidate_paths(self): |
| 93 |
opts = self.opts |
| 94 |
if not opts.prefix: |
| 95 |
opts.prefix = sys.prefix |
| 96 |
for x in ('prefix', 'libdir', 'bindir', 'sharedir', 'staging_root', 'staging_libdir', 'staging_bindir', 'staging_sharedir'): |
| 97 |
o = getattr(opts, x, None) |
| 98 |
if o: |
| 99 |
setattr(opts, x, os.path.abspath(o)) |
| 100 |
self.libdir = getattr(opts, 'libdir', None) |
| 101 |
if self.libdir is None: |
| 102 |
self.libdir = self.j(opts.prefix, 'lib') |
| 103 |
self.bindir = getattr(opts, 'bindir', None) |
| 104 |
if self.bindir is None: |
| 105 |
self.bindir = self.j(opts.prefix, 'bin') |
| 106 |
self.sharedir = getattr(opts, 'sharedir', None) |
| 107 |
if self.sharedir is None: |
| 108 |
self.sharedir = self.j(opts.prefix, 'share') |
| 109 |
if not getattr(opts, 'staging_root', None): |
| 110 |
opts.staging_root = opts.prefix |
| 111 |
self.staging_libdir = getattr(opts, 'staging_libdir', None) |
| 112 |
if self.staging_libdir is None: |
| 113 |
self.staging_libdir = opts.staging_libdir = self.j(opts.staging_root, 'lib') |
| 114 |
self.staging_bindir = getattr(opts, 'staging_bindir', None) |
| 115 |
if self.staging_bindir is None: |
| 116 |
self.staging_bindir = opts.staging_bindir = self.j(opts.staging_root, 'bin') |
| 117 |
self.staging_sharedir = getattr(opts, 'staging_sharedir', None) |
| 118 |
if self.staging_sharedir is None: |
| 119 |
self.staging_sharedir = opts.staging_sharedir = self.j(opts.staging_root, 'share') |
| 120 |
|
| 121 |
self.staging_libdir = opts.staging_libdir = self.j(self.staging_libdir, 'calibre') |
| 122 |
self.staging_sharedir = opts.staging_sharedir = self.j(self.staging_sharedir, 'calibre') |
| 123 |
self.system_plugins_loc = opts.system_plugins_location |
| 124 |
|
| 125 |
if self.__class__.__name__ == 'Develop': |
| 126 |
self.libdir = self.SRC |
| 127 |
self.sharedir = self.RESOURCES |
| 128 |
else: |
| 129 |
self.libdir = self.j(self.libdir, 'calibre') |
| 130 |
self.sharedir = self.j(self.sharedir, 'calibre') |
| 131 |
self.info('INSTALL paths:') |
| 132 |
self.info('\tLIB:', self.staging_libdir) |
| 133 |
self.info('\tSHARE:', self.staging_sharedir) |
| 134 |
|
| 135 |
def pre_sub_commands(self, opts): |
| 136 |
if not (islinux or isbsd or ishaiku): |
| 137 |
self.info( |
| 138 |
'\nSetting up a source based development environment is only ' |
| 139 |
'supported on linux. On other platforms, see the User Manual' |
| 140 |
' for help with setting up a development environment.' |
| 141 |
) |
| 142 |
raise SystemExit(1) |
| 143 |
# Ensure any config files created as a side effect of importing calibre |
| 144 |
# during the build process are in /tmp |
| 145 |
os.environ['CALIBRE_CONFIG_DIRECTORY'] = os.environ.get('CALIBRE_CONFIG_DIRECTORY', '/tmp/calibre-install-config') |
| 146 |
|
| 147 |
def run(self, opts): |
| 148 |
self.manifest = [] |
| 149 |
self.opts = opts |
| 150 |
self.consolidate_paths() |
| 151 |
self.install_files() |
| 152 |
self.write_templates() |
| 153 |
self.install_env_module() |
| 154 |
self.run_postinstall() |
| 155 |
self.success() |
| 156 |
|
| 157 |
def install_env_module(self): |
| 158 |
import sysconfig |
| 159 |
|
| 160 |
libdir = os.path.join( |
| 161 |
self.opts.staging_root, |
| 162 |
sysconfig.get_config_var('PLATLIBDIR') or 'lib', |
| 163 |
os.path.basename(sysconfig.get_config_var('DESTLIB') or sysconfig.get_config_var('LIBDEST') or f'python{sysconfig.get_python_version()}'), |
| 164 |
'site-packages', |
| 165 |
) |
| 166 |
try: |
| 167 |
if not os.path.exists(libdir): |
| 168 |
os.makedirs(libdir) |
| 169 |
except OSError: |
| 170 |
self.warn('Cannot install calibre environment module to: ' + libdir) |
| 171 |
else: |
| 172 |
path = os.path.join(libdir, 'init_calibre.py') |
| 173 |
self.info('Installing calibre environment module: ' + path) |
| 174 |
with open(path, 'wb') as f: |
| 175 |
f.write(HEADER.format(**self.template_args()).encode('utf-8')) |
| 176 |
self.manifest.append(path) |
| 177 |
|
| 178 |
def install_files(self): |
| 179 |
pass |
| 180 |
|
| 181 |
def run_postinstall(self): |
| 182 |
if self.opts.postinstall: |
| 183 |
from calibre.linux import PostInstall |
| 184 |
|
| 185 |
PostInstall(self.opts, info=self.info, warn=self.warn, manifest=self.manifest) |
| 186 |
|
| 187 |
def success(self): |
| 188 |
self.info('\nDevelopment environment successfully setup') |
| 189 |
|
| 190 |
def write_templates(self): |
| 191 |
for typ in ('console', 'gui'): |
| 192 |
for name, mod, func in zip(basenames[typ], modules[typ], functions[typ]): |
| 193 |
self.write_template(name, mod, func) |
| 194 |
|
| 195 |
def template_args(self): |
| 196 |
return { |
| 197 |
'py_major_version': sys.version_info.major, |
| 198 |
'path': self.libdir, |
| 199 |
'resources': self.sharedir, |
| 200 |
'executables': self.bindir, |
| 201 |
'extensions': self.j(self.libdir, 'calibre', 'plugins'), |
| 202 |
'system_plugins_loc': self.system_plugins_loc, |
| 203 |
} |
| 204 |
|
| 205 |
def write_template(self, name, mod, func): |
| 206 |
template = COMPLETE_TEMPLATE if name == 'calibre-complete' else TEMPLATE |
| 207 |
args = self.template_args() |
| 208 |
args['module'] = mod |
| 209 |
args['func'] = func |
| 210 |
script = template.format(**args) |
| 211 |
path = self.j(self.staging_bindir, name) |
| 212 |
if not os.path.exists(self.staging_bindir): |
| 213 |
os.makedirs(self.staging_bindir) |
| 214 |
self.info('Installing binary:', path) |
| 215 |
if os.path.lexists(path) and not os.path.exists(path): |
| 216 |
os.remove(path) |
| 217 |
with open(path, 'wb') as f: |
| 218 |
f.write(script.encode('utf-8')) |
| 219 |
os.chmod(path, self.MODE) |
| 220 |
self.manifest.append(path) |
| 221 |
|
| 222 |
|
| 223 |
class Install(Develop): |
| 224 |
description = textwrap.dedent('''\ |
| 225 |
Install calibre to your system. By default, calibre |
| 226 |
is installed to <prefix>/bin, <prefix>/lib/calibre, |
| 227 |
<prefix>/share/calibre. These can all be controlled via options. |
| 228 |
|
| 229 |
The default <prefix> is the prefix of your python installation. |
| 230 |
|
| 231 |
The .desktop, .mime and icon files are installed using XDG. The |
| 232 |
location they are installed to can be controlled by setting |
| 233 |
the environment variables: |
| 234 |
XDG_DATA_DIRS=/usr/share equivalent |
| 235 |
XDG_UTILS_INSTALL_MODE=system |
| 236 |
For staged installs this will be automatically set to: |
| 237 |
<staging_root>/share |
| 238 |
''') |
| 239 |
short_description = 'Install calibre from source' |
| 240 |
|
| 241 |
sub_commands = ['build', 'gui'] |
| 242 |
|
| 243 |
def add_options(self, parser): |
| 244 |
parser.add_option('--prefix', help='Installation prefix.') |
| 245 |
parser.add_option('--libdir', help='Where to put calibre library files. Default is <prefix>/lib') |
| 246 |
parser.add_option('--bindir', help='Where to put the calibre binaries. Default is <prefix>/bin') |
| 247 |
parser.add_option('--sharedir', help='Where to put the calibre data files. Default is <prefix>/share') |
| 248 |
parser.add_option( |
| 249 |
'--staging-root', |
| 250 |
'--root', |
| 251 |
default=None, |
| 252 |
help=( |
| 253 |
'Use a different installation root (mainly for packaging).' |
| 254 |
' The prefix option controls the paths written into ' |
| 255 |
'the launcher scripts. This option controls the prefix ' |
| 256 |
'to which the install will actually copy files. By default ' |
| 257 |
'it is set to the value of --prefix.' |
| 258 |
), |
| 259 |
) |
| 260 |
parser.add_option('--staging-libdir', help='Where to put calibre library files. Default is <root>/lib') |
| 261 |
parser.add_option('--staging-bindir', help='Where to put the calibre binaries. Default is <root>/bin') |
| 262 |
parser.add_option('--staging-sharedir', help='Where to put the calibre data files. Default is <root>/share') |
| 263 |
parser.add_option('--system-plugins-location', help='Path to a directory from which the installed calibre will load plugins') |
| 264 |
self.add_postinstall_options(parser) |
| 265 |
|
| 266 |
def install_files(self): |
| 267 |
dest = self.staging_libdir |
| 268 |
if os.path.exists(dest): |
| 269 |
shutil.rmtree(dest) |
| 270 |
self.info('Installing code to', dest) |
| 271 |
self.manifest.append(dest) |
| 272 |
for x in os.walk(self.SRC): |
| 273 |
reldir = os.path.relpath(x[0], self.SRC) |
| 274 |
destdir = os.path.join(dest, reldir) |
| 275 |
for f in x[-1]: |
| 276 |
if os.path.splitext(f)[1] in ('.py', '.so'): |
| 277 |
if not os.path.exists(destdir): |
| 278 |
os.makedirs(destdir) |
| 279 |
shutil.copy2(self.j(x[0], f), destdir) |
| 280 |
dest = self.staging_sharedir |
| 281 |
if os.path.exists(dest): |
| 282 |
shutil.rmtree(dest) |
| 283 |
self.info('Installing resources to', dest) |
| 284 |
shutil.copytree(self.RESOURCES, dest, symlinks=True) |
| 285 |
self.manifest.append(dest) |
| 286 |
|
| 287 |
def success(self): |
| 288 |
self.info('\n\ncalibre successfully installed. You can start it by running the command calibre') |
| 289 |
|
| 290 |
|
| 291 |
class Sdist(Command): |
| 292 |
description = 'Create a source distribution' |
| 293 |
DEST = os.path.join('dist', f'{__appname__}-{__version__}.tar.xz') |
| 294 |
|
| 295 |
def run(self, opts): |
| 296 |
if not self.e(self.d(self.DEST)): |
| 297 |
os.makedirs(self.d(self.DEST)) |
| 298 |
with tempfile.TemporaryDirectory() as tdir: |
| 299 |
tdir = self.j(tdir, f'calibre-{__version__}') |
| 300 |
self.run_with_tdir(tdir) |
| 301 |
|
| 302 |
def run_with_tdir(self, tdir): |
| 303 |
self.info('\tRunning git export...') |
| 304 |
os.mkdir(tdir) |
| 305 |
subprocess.check_call('git archive HEAD | tar -x -C ' + tdir, shell=True) |
| 306 |
for x in open('.gitignore'): |
| 307 |
if not x.startswith('/resources/'): |
| 308 |
continue |
| 309 |
x = x[1:] |
| 310 |
p = x.strip().replace('/', os.sep) |
| 311 |
for p in glob.glob(p): |
| 312 |
d = self.j(tdir, os.path.dirname(p)) |
| 313 |
if not self.e(d): |
| 314 |
os.makedirs(d) |
| 315 |
if os.path.isdir(p): |
| 316 |
shutil.copytree(p, self.j(tdir, p)) |
| 317 |
else: |
| 318 |
shutil.copy2(p, d) |
| 319 |
for x in os.walk(os.path.join(self.SRC, 'calibre')): |
| 320 |
for f in x[-1]: |
| 321 |
if not f.endswith('_ui.py'): |
| 322 |
continue |
| 323 |
f = os.path.join(x[0], f) |
| 324 |
f = os.path.relpath(f) |
| 325 |
dest = os.path.join(tdir, self.d(f)) |
| 326 |
shutil.copy2(f, dest) |
| 327 |
|
| 328 |
tbase = self.j(self.d(self.SRC), 'translations') |
| 329 |
for x in ('iso_639', 'calibre'): |
| 330 |
destdir = self.j(tdir, 'translations', x) |
| 331 |
if not os.path.exists(destdir): |
| 332 |
os.makedirs(destdir) |
| 333 |
for y in glob.glob(self.j(tbase, x, '*.po')) + glob.glob(self.j(tbase, x, '*.pot')): |
| 334 |
dest = self.j(destdir, self.b(y)) |
| 335 |
if not os.path.exists(dest): |
| 336 |
shutil.copy2(y, dest) |
| 337 |
shutil.copytree(self.j(tbase, 'manual'), self.j(tdir, 'translations', 'manual')) |
| 338 |
self.add_man_pages(self.j(tdir, 'man-pages')) |
| 339 |
|
| 340 |
self.info('\tCreating tarfile...') |
| 341 |
dest = self.DEST.rpartition('.')[0] |
| 342 |
shutil.rmtree(os.path.join(tdir, '.github')) |
| 343 |
subprocess.check_call(['tar', '--mtime=now', '-cf', self.a(dest), f'calibre-{__version__}'], cwd=self.d(tdir)) |
| 344 |
self.info('\tCompressing tarfile...') |
| 345 |
if os.path.exists(self.a(self.DEST)): |
| 346 |
os.remove(self.a(self.DEST)) |
| 347 |
subprocess.check_call(['xz', '-9', self.a(dest)]) |
| 348 |
|
| 349 |
def add_man_pages(self, dest): |
| 350 |
from setup.commands import man_pages |
| 351 |
|
| 352 |
man_pages.build_man_pages(dest) |
| 353 |
|
| 354 |
def clean(self): |
| 355 |
if os.path.exists(self.DEST): |
| 356 |
os.remove(self.DEST) |
| 357 |
|
| 358 |
|
| 359 |
class Bootstrap(Command): |
| 360 |
description = 'Bootstrap a fresh checkout of calibre from git to a state where it can be installed. Requires various development tools/libraries/headers' |
| 361 |
TRANSLATIONS_REPO = 'kovidgoyal/calibre-translations' |
| 362 |
sub_commands = 'build iso639 iso3166 translations gui resources cacerts recent_uas'.split() |
| 363 |
|
| 364 |
def add_options(self, parser): |
| 365 |
parser.add_option( |
| 366 |
'--ephemeral', |
| 367 |
default=False, |
| 368 |
action='store_true', |
| 369 |
help='Do not download all history for the translations. Speeds up first time download but subsequent downloads will be slower.', |
| 370 |
) |
| 371 |
parser.add_option( |
| 372 |
'--path-to-translations', help='Path to existing out-of-tree translations checkout. Use this to avoid downloading translations at all.' |
| 373 |
) |
| 374 |
|
| 375 |
def pre_sub_commands(self, opts): |
| 376 |
tdir = self.j(self.d(self.SRC), 'translations') |
| 377 |
clone_cmd = ['git', 'clone', f'https://github.com/{self.TRANSLATIONS_REPO}.git', 'translations'] |
| 378 |
if opts.path_to_translations: |
| 379 |
if os.path.exists(tdir): |
| 380 |
shutil.rmtree(tdir) |
| 381 |
shutil.copytree(opts.path_to_translations, tdir) |
| 382 |
# Change permissions for the top-level folder |
| 383 |
os.chmod(tdir, 0o755) |
| 384 |
for root, dirs, files in os.walk(tdir): |
| 385 |
# set perms on sub-directories |
| 386 |
for momo in dirs: |
| 387 |
os.chmod(os.path.join(root, momo), 0o755) |
| 388 |
# set perms on files |
| 389 |
for momo in files: |
| 390 |
os.chmod(os.path.join(root, momo), 0o644) |
| 391 |
|
| 392 |
elif opts.ephemeral: |
| 393 |
if os.path.exists(tdir): |
| 394 |
shutil.rmtree(tdir) |
| 395 |
|
| 396 |
st = time.time() |
| 397 |
clone_cmd.insert(2, '--depth=1') |
| 398 |
subprocess.check_call(clone_cmd, cwd=self.d(self.SRC)) |
| 399 |
print(f'Downloaded translations in {int(time.time() - st)} seconds') |
| 400 |
elif os.path.exists(tdir): |
| 401 |
subprocess.check_call(['git', 'pull'], cwd=tdir) |
| 402 |
else: |
| 403 |
subprocess.check_call(clone_cmd, cwd=self.d(self.SRC)) |
| 404 |
|
| 405 |
def run(self, opts): |
| 406 |
self.info(f'\n\nAll done! You should now be able to run "{sys.executable} setup.py install" to install calibre') |