forked from p1otr/pypi2deb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy2dsp
executable file
·151 lines (135 loc) · 6.31 KB
/
py2dsp
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
#! /usr/bin/python3
# vim: et ts=4 sw=4
# Copyright © 2015-2018 Piotr Ożarowski <[email protected]>
#
# 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
from os import environ, getcwd, makedirs
from os.path import abspath, exists, isdir, join
from shutil import rmtree
from pypi2deb import VERSION
from pypi2deb.debianize import debianize
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'
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 = yield from get_pypi_info(name)
ctx = parse_pypi_info(ctx)
else: # download from PyPI
parsed = parse_filename(args.name)
version = parsed.get('version')
name = parsed.get('name') or args.name
ctx = yield from get_pypi_info(name, version)
ctx = parse_pypi_info(ctx)
if not ctx:
log.error('invalid name: %s', args.name)
exit(1)
name = ctx['name']
version = ctx['version']
fname = yield from download(name, version=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
if isdir(fpath):
dpath = fpath
else:
dirname = '{}-{}'.format(src_name, version)
dpath = unpack(fpath, args.root, dirname)
yield from debianize(dpath, ctx, args.profile)
yield from execute(['dpkg-buildpackage', '-S', '-us', '-uc', '-nc', '-d',
'-I.git', '-i.git'], dpath)
if args.build:
yield from 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')
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('--profile', action='store',
help='load default values from profile.json file (if available)')
parser.add_argument('name', default=None,
help='Python source name or tarball')
args = parser.parse_args()
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)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main(args))
except Exception as e:
log.error(e, exc_info=args.verbose)
exit(2)
finally:
loop.close()