1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
#! /usr/bin/python3
# vim: et ts=4 sw=4
# Copyright © 2015-2018 Piotr Ożarowski <piotr@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import argparse
import asyncio
import logging
import sys
try:
from asyncio import JoinableQueue as Queue # Python 3.4
except ImportError: # Python 3.5
from asyncio import Queue
from os import environ, getcwd, makedirs
from os.path import exists, join
from pypi2deb import VERSION
from pypi2deb.debianize import debianize
from pypi2deb.pypi import list_packages
from pypi2deb.pypi import get_pypi_info, parse_pypi_info, download
from pypi2deb.tools import unpack, pkg_name, execute
logging.basicConfig(format='%(levelname).1s: pypi2debian '
'%(module)s:%(lineno)d: %(message)s')
log = logging.getLogger('pypi2debian')
DESCRIPTION = 'Python Package Index to Debian repository converter'
class Converter:
def __init__(self, args, loop=None):
self.args = args
self.loop = loop or asyncio.get_event_loop()
self.queue = Queue(loop=self.loop)
self.build_src_queue = Queue(loop=self.loop)
self.build_bin_queue = Queue(loop=self.loop)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
self.loop.run_until_complete(self.run())
# self.loop.close()
async def run(self):
stats_worker = asyncio.Task(self.stats_worker(), loop=self.loop)
workers = [asyncio.Task(self.worker(), loop=self.loop)
for _ in range(int(self.args.jobs))]
build_src_workers = [asyncio.Task(self.build_src_worker(), loop=self.loop)
for _ in range(int(self.args.src_jobs))]
build_bin_workers = [asyncio.Task(self.build_bin_worker(), loop=self.loop)
for _ in range(int(self.args.bin_jobs))]
try:
await self.queue.join()
await self.build_src_queue.join()
await self.build_bin_queue.join()
finally:
stats_worker.cancel()
for w in workers:
w.cancel()
for w in build_src_workers:
w.cancel()
for w in build_bin_workers:
w.cancel()
def convert(self, name, version):
self.queue.put_nowait((name, version))
def build_src(self, name, version, ctx):
self.build_src_queue.put_nowait((name, version, ctx))
def build_bin(self, name, version, ctx):
self.build_bin_queue.put_nowait((name, version, ctx))
async def stats_worker(self):
while True:
await asyncio.sleep(10)
log.info('* pending conversion jobs: %s, src jobs: %s, build jobs: %s',
self.queue.qsize(), self.build_src_queue.qsize(),
self.build_bin_queue.qsize())
async def worker(self):
args = self.args
while True:
name, version = await self.queue.get()
try:
try:
ctx = await get_pypi_info(name, version)
ctx = parse_pypi_info(ctx)
except Exception as err:
log.error('%s %s: cannot load details from PyPI: %r', name, version, err)
continue
if not ctx:
log.error('%s %s: cannot find details on PyPI', name, version)
continue
if not version:
version = ctx['version']
ctx['src_name'] = pkg_name(name)
ctx['debian_revision'] = '0~pypi2deb'
dsc_path = join(args.root, '{src_name}_{version}-{debian_revision}.dsc'.format(**ctx))
ctx['dsc'] = dsc_path # could be used in build step
if exists(dsc_path):
log.debug('%s %s: skipping - dsc file already exists', name, version)
continue
if args.no_pypy:
ctx['interpreters'].discard('pypy')
if args.pypy:
ctx['interpreters'] = ctx['interpreters'] & {'pypy'}
if args.python3:
ctx['interpreters'] = ctx['interpreters'] & {'python3'}
if not ctx['interpreters']:
log.debug('%s %s: no matching interpreter is supported', name, version)
continue
try:
fname = await download(name, version, destdir=args.root)
except Exception as err:
log.error('%s %s: cannot download from PyPI: %r', name, version, err)
continue
fpath = join(args.root, fname)
ctx['root'] = args.root
dirname = '{}-{}'.format(ctx['src_name'], version)
try:
dpath = unpack(fpath, args.root, dirname)
except Exception as err:
log.error('%s %s: cannot unpack sources: %r', name, version, err)
continue
ctx['src_dir'] = dpath
# debianize sources
try:
await debianize(dpath, ctx, args.profile)
except Exception as err:
log.warn('%s %s: conversion failed with: %r', name, version, err)
continue
# create Debian source package
if args.build_src_cmd:
self.build_src(name, version, ctx)
except Exception as err:
log.error('conversion failure (%s %s)', name, version, exc_info=True)
finally:
self.queue.task_done()
async def build_src_worker(self):
args = self.args
while True:
name, version, ctx = await self.build_src_queue.get()
try:
command = args.build_src_cmd.format(**ctx)
log_path = join(args.root, '{src_name}_{version}-{debian_revision}_source.log'.format(**ctx))
res = await execute(command, ctx['src_dir'], log_output=log_path)
if res != 0:
log.error('%s %s: creating source package failed with return code %d',
name, version, res)
elif args.build_cmd:
# build the package - separate queue, usually one build at a time
self.build_bin(name, version, ctx)
except Exception as err:
log.error('%s %s: creating source package failed with: %r',
name, version, err)
self.build_src_queue.task_done()
async def build_bin_worker(self):
args = self.args
while True:
name, version, ctx = await self.build_bin_queue.get()
try:
command = args.build_cmd.format(**ctx)
log_path = join(args.root, '{src_name}_{version}-{debian_revision}_build.log'.format(**ctx))
res = await execute(command, ctx['src_dir'], log_output=log_path)
if res != 0:
log.error('%s %s: building binary failed with return code %d',
name, version, res)
except Exception as err:
log.error('%s %s: building binary package failed with: %r',
name, version, err, exc_info=True)
self.build_bin_queue.task_done()
if __name__ == '__main__':
usage = '%(prog)s [OPTIONS]'
parser = argparse.ArgumentParser(usage=usage,
description=DESCRIPTION)
parser.add_argument('-v', '--verbose', action='store_true',
default=environ.get('PYPI2DEB_VERBOSE') == '1',
help='turn verbose mode on')
parser.add_argument('-q', '--quiet', action='store_true',
default=environ.get('PYPI2DEB_QUIET') == '1',
help='be quiet')
parser.add_argument('--version', action='version',
version='%(prog)s {}'.format(VERSION))
parser.add_argument('--root', action='store', metavar='DIR',
default=environ.get('DESTDIR',
join(getcwd(), 'result')),
help='destination directory [default: ./result]')
parser.add_argument('--clean', action='store_true',
default=environ.get('PY2DSP_CLEAN', '0') == '1',
help='remove name-version directory after creating source package')
parser.add_argument('--profile', action='store',
help='load default values from profile.json file (if available)')
filters = parser.add_argument_group('filters')
filters.add_argument('-c', '--classifiers', action='append', metavar='TAG',
default=[], help='tag used to select packages for conversion'
' (can be passed several times)')
filters.add_argument('--python3', action='store_true',
default=environ.get('PYPI2DEB_PYTHON3') == '1',
help='limit to Python 3.X packages only')
# filters.add_argument('--pypy', action='store_true',
# default=environ.get('PYPI2DEB_PYPY') == '1',
# help='limit to PyPy packages only')
# filters.add_argument('--no-pypy', action='store_true',
# default=environ.get('PYPI2DEB_NO_PYPY') == '1',
# help='do not generate pypy- packages')
commands = parser.add_argument_group('commands', 'override commands')
commands.add_argument('--build-src-cmd', action='store', metavar='COMMAND',
help='command to build source package',
default='dpkg-buildpackage -S -uc -us -nc -d -I.git -i.git')
commands.add_argument('--build-cmd', action='store', metavar='COMMAND',
help='build command, none by default')
jobs = parser.add_argument_group('jobs')
jobs.add_argument('-j', '--jobs', default=8, metavar='INT',
help='number of conversion jobs to run simultaneously')
jobs.add_argument('--src-jobs', default=4, metavar='INT',
help='number of source package build jobs to run simultaneously')
jobs.add_argument('--bin-jobs', default=1, metavar='INT',
help='number of binary build jobs to run simultaneously')
args = parser.parse_args()
args.pypy = False
args.no_pypy = True
if args.verbose:
logging.getLogger('pypi2deb').setLevel(logging.DEBUG)
log.setLevel(logging.DEBUG)
elif args.quiet:
logging.getLogger('pypi2deb').setLevel(logging.ERROR)
log.setLevel(logging.ERROR)
else:
logging.getLogger('pypi2deb').setLevel(logging.INFO)
log.setLevel(logging.INFO)
if args.profile in ('dpmt', 'papt'):
logging.warning("'dpmt' and 'papt' profiles have been replaced by the 'dpt' profile")
args.profile = 'dpt'
if args.python3:
args.classifiers.append('Programming Language :: Python :: 3')
elif args.pypy:
args.classifiers.append('Programming Language :: Python :: Implementation :: PyPy')
log.debug('version: {}'.format(VERSION))
log.debug(sys.argv)
log.debug('args: %s', args)
if not exists(args.root):
makedirs(args.root)
packages = list_packages(args.classifiers)
with Converter(args) as converter:
for name, version in packages.items():
converter.convert(name, version)
|