File: py2dsp

package info (click to toggle)
pypi2deb 4.20240727
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 200 kB
  • sloc: python: 1,088; sh: 36; makefile: 25
file content (174 lines) | stat: -rwxr-xr-x 7,921 bytes parent folder | download
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
#! /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 logging
import argparse
import asyncio
import sys
import shutil
from os import environ, getcwd, makedirs, unlink
from os.path import abspath, exists, isdir, join
from shutil import rmtree
from pypi2deb import VERSION
from pypi2deb.debianize import debianize
from pypi2deb.github import github_download
from pypi2deb.pypi import get_pypi_info, parse_pypi_info, download
from pypi2deb.tools import execute, unpack, parse_filename, pkg_name

logging.basicConfig(format='%(levelname).1s: py2dsp '
                           '%(module)s:%(lineno)d: %(message)s')
log = logging.getLogger('py2dsp')
DESCRIPTION = 'Python source package to Debian source package converter'


async def main(args):
    log.debug('args: %s', args)
    if not exists(args.root):
        makedirs(args.root)
    if exists(args.name):  # file or dir
        fpath = abspath(args.name)
        fname = fpath.rstrip('/').rsplit('/', 1)[-1]
        parsed = parse_filename(fname)
        version = parsed.get('version')
        name = parsed.get('name') or args.name
        ctx = await get_pypi_info(args.pypi_search if args.pypi_search else name)
        ctx = parse_pypi_info(ctx)
        ctx['name'], ctx['version'] = name, version
        if args.github:
            ctx['github'] = args.github
        shutil.copy(fpath, args.root)
    else:  # download from PyPI
        parsed = parse_filename(args.name)
        requested_version = parsed.get('version')
        name = parsed.get('name') or args.name
        ctx = await get_pypi_info(args.pypi_search if args.pypi_search else name, requested_version)
        ctx = parse_pypi_info(ctx)
        if not ctx:
            log.error('invalid name: %s', args.name)
            exit(1)
        #name = ctx['name']
        version = ctx['version']
        if args.github:
            # Use the version parsed from the PyPI API response to download from GitHub
            ctx['github'] = args.github
            log.debug(f"Calling github_download with {name}, {args.github}, {version}, {args.root}")
            fname = await github_download(name, args.github, version=version, destdir=args.root)
        else:
            # Use the requested version to get a richer response from the PyPI API if no version was requested
            fname = await download(name, version=requested_version, destdir=args.root)
        fpath = join(args.root, fname)

    ctx['root'] = args.root
    src_name = ctx['src_name'] = pkg_name(name)
    ctx['distribution'] = args.distribution
    ctx['debian_revision'] = args.revision
    if isdir(fpath):
        dpath = fpath
    else:
        dirname = '{}-{}'.format(src_name, version)
        log.debug(f"Unpacking {fpath}")
        dpath = unpack(fpath, args.root, dirname)

    await debianize(dpath, ctx, args.profile)
    await execute(['dpkg-buildpackage', '-S', '-us', '-uc', '-nc', '-d',
                        '-I.git', '-i.git'], dpath)
    # workaround for https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=845436
    unlink(join(dpath, 'debian/files'))
    if args.build:
        await execute(['dpkg-buildpackage', '-b'], dpath)

    if args.clean:
        rmtree(dpath)


if __name__ == '__main__':
    usage = '%(prog)s NAME [OPTIONS]'
    parser = argparse.ArgumentParser(usage=usage,
                                     description=DESCRIPTION)
    parser.add_argument('-v', '--verbose', action='store_true',
                        default=environ.get('PY2DSP_VERBOSE') == '1',
                        help='turn verbose mode on')
    parser.add_argument('-q', '--quiet', action='store_true',
                        default=environ.get('PY2DSP_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('--build', action='store_true',
                        default=environ.get('PY2DSP_BUILD', '0') == '1',
                        help='build binary package')
    parser.add_argument('--application', action='store_true',
                        default=environ.get('PY2DSP_APPLICATION', '0') == '1',
                        help='this is an application rather than module')
    parser.add_argument('--profile', action='store',
                        help='load default values from profile.json file (if available)')
    parser.add_argument('--github', '--gh',  default=None,
                        help='fetch the package from GitHub instead of PyPI')
    parser.add_argument('--pypi-search',  default=None,
                        help='specify the PyPI search term instead of the source package name')

    changelog = parser.add_argument_group('changelog', 'debian/changelog specific settings')
    changelog.add_argument('--distribution', action='store',
                           default=environ.get('PY2DSP_DISTRIBUTION', 'UNRELEASED'),
                           help='targetted Debian suite')
    changelog.add_argument('--revision', action='store',
                           default=environ.get('PY2DSP_REVISION', '0~py2deb'),
                           help='Debian changelog revision')
    changelog.add_argument('-m', '--message', action='store',
                           default=environ.get('PY2DSP_MESSAGE', 'converte0~py2deb'),
                           help='Debian changelog message')

    parser.add_argument('name', default=None, nargs='?',
                        help='Python source name or tarball')

    args = parser.parse_args()
    if not args.name and args.github:
        args.name = args.github.split('/')[-1]

    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)
    log.debug('version: {}'.format(VERSION))
    log.debug(sys.argv)

    if args.profile in ('dpmt', 'papt'):
        logging.warning("'dpmt' and 'papt' profiles have been replaced by the 'dpt' profile")
        args.profile = 'dpt'

    try:
        asyncio.run(main(args))
    except Exception as e:
        log.error(e, exc_info=args.verbose)
        exit(2)