| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai |
| 3 |
|
| 4 |
|
| 5 |
__license__ = 'GPL v3' |
| 6 |
__copyright__ = '2009, Kovid Goyal <[email protected]>' |
| 7 |
__docformat__ = 'restructuredtext en' |
| 8 |
|
| 9 |
import os |
| 10 |
import re |
| 11 |
import sys |
| 12 |
|
| 13 |
src_base = os.path.dirname(os.path.abspath(__file__)) |
| 14 |
|
| 15 |
|
| 16 |
def check_version_info(): |
| 17 |
with open(os.path.join(src_base, 'pyproject.toml')) as f: |
| 18 |
raw = f.read() |
| 19 |
m = re.search(r'''^requires-python\s*=\s*['"](.+?)['"]''', raw, flags=re.MULTILINE) |
| 20 |
assert m is not None |
| 21 |
minver = m.group(1) |
| 22 |
m = re.match(r'(>=?)(\d+)\.(\d+)', minver) |
| 23 |
assert m is not None |
| 24 |
q = int(m.group(2)), int(m.group(3)) |
| 25 |
if m.group(1) == '>=': |
| 26 |
is_ok = sys.version_info >= q |
| 27 |
else: |
| 28 |
is_ok = sys.version_info > q |
| 29 |
if not is_ok: |
| 30 |
sys.exit(f'calibre requires Python {minver}. Current Python version: {".".join(map(str, sys.version_info[:3]))}') |
| 31 |
|
| 32 |
|
| 33 |
check_version_info() |
| 34 |
|
| 35 |
sys.path.insert(0, src_base) |
| 36 |
|
| 37 |
from setup import commands, get_warnings, prints |
| 38 |
|
| 39 |
|
| 40 |
def option_parser(): |
| 41 |
import optparse |
| 42 |
|
| 43 |
parser = optparse.OptionParser() |
| 44 |
parser.add_option('-c', '--clean', default=False, action='store_true', help=('Instead of running the command delete all files generated by the command')) |
| 45 |
parser.add_option('--clean-backups', default=False, action='store_true', help='Delete all backup files from the source tree') |
| 46 |
parser.add_option('--clean-all', default=False, action='store_true', help='Delete all machine generated files from the source tree') |
| 47 |
return parser |
| 48 |
|
| 49 |
|
| 50 |
def clean_backups(): |
| 51 |
for root, _, files in os.walk('.'): |
| 52 |
for name in files: |
| 53 |
for t in ('.pyc', '.pyo', '~', '.swp', '.swo'): |
| 54 |
if name.endswith(t): |
| 55 |
os.remove(os.path.join(root, name)) |
| 56 |
|
| 57 |
|
| 58 |
def main(args=sys.argv): |
| 59 |
if len(args) == 1 or args[1] in ('-h', '--help'): |
| 60 |
print('Usage: python', args[0], 'command', '[options]') |
| 61 |
print('\nWhere command is one of:') |
| 62 |
print() |
| 63 |
for x in sorted(commands.__all__): |
| 64 |
print(f'{x:20} -', end=' ') |
| 65 |
c = getattr(commands, x) |
| 66 |
desc = getattr(c, 'short_description', c.description) |
| 67 |
print(desc) |
| 68 |
|
| 69 |
print('\nTo get help on a particular command, run:') |
| 70 |
print('\tpython', args[0], 'command -h') |
| 71 |
return 1 |
| 72 |
|
| 73 |
command = args[1] |
| 74 |
if command not in commands.__all__: |
| 75 |
print(command, 'is not a recognized command.') |
| 76 |
print('Valid commands:', ', '.join(commands.__all__)) |
| 77 |
return 1 |
| 78 |
|
| 79 |
command = getattr(commands, command) |
| 80 |
|
| 81 |
parser = option_parser() |
| 82 |
command.add_all_options(parser) |
| 83 |
usage = f'Usage: python setup.py {args[1]} [options]\n\n' + command.description |
| 84 |
if command.usage_help: |
| 85 |
usage += '\n\n' + command.usage_help |
| 86 |
parser.set_usage(usage) |
| 87 |
|
| 88 |
opts, args = parser.parse_args(args) |
| 89 |
opts.cli_args = args[2:] |
| 90 |
|
| 91 |
if opts.clean_backups: |
| 92 |
clean_backups() |
| 93 |
|
| 94 |
if opts.clean: |
| 95 |
prints('Cleaning', args[1]) |
| 96 |
command.clean() |
| 97 |
return 0 |
| 98 |
|
| 99 |
if opts.clean_all: |
| 100 |
for cmd in commands.__all__: |
| 101 |
prints('Cleaning', cmd) |
| 102 |
getattr(commands, cmd).clean() |
| 103 |
return 0 |
| 104 |
|
| 105 |
command.run_all(opts) |
| 106 |
|
| 107 |
warnings = get_warnings() |
| 108 |
if warnings: |
| 109 |
print() |
| 110 |
prints('There were', len(warnings), 'warning(s):') |
| 111 |
print() |
| 112 |
for args, kwargs in warnings: |
| 113 |
prints('*', *args, **kwargs) |
| 114 |
print() |
| 115 |
|
| 116 |
return 0 |
| 117 |
|
| 118 |
|
| 119 |
if __name__ == '__main__': |
| 120 |
sys.exit(main()) |