/
pbds.py
300 lines (255 loc) · 10.1 KB
/
pbds.py
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
290
291
292
293
294
295
296
297
298
299
300
# -*- encoding: utf-8 -*-
# PKGBUILDer v4.3.2
# An AUR helper (and library) in Python 3.
# Copyright © 2011-2023, Chris Warrick.
# See /LICENSE for licensing information.
"""
PKGBUILDer Data Storage.
:Copyright: © 2011-2023, Chris Warrick.
:License: BSD (see /LICENSE).
"""
from . import _, __version__
import pkgbuilder
import pkgbuilder.ui
import sys
import os
import logging
import subprocess
import pycman
import pkg_resources
import configparser
__all__ = ('PBDS',)
class PBDS(object):
"""PKGBUILDer Data Storage."""
# For fancy-schmancy messages stolen from makepkg.
colors = {
'all_off': '\x1b[1;0m',
'bold': '\x1b[1;1m',
'blue': '\x1b[1;1m\x1b[1;34m',
'green': '\x1b[1;1m\x1b[1;32m',
'red': '\x1b[1;1m\x1b[1;31m',
'yellow': '\x1b[1;1m\x1b[1;33m'
}
pacman = False
fetch = False
validation = True
depcheck = True
pkginst = True
clean = False
pgpcheck = True
confirm = True
deepclone = False
depcheck = True
vcsupgrade = False
colors_status = True
# TRANSLATORS: see makepkg.
inttext = _('Aborted by user! Exiting...')
# TRANSLATORS: see pacman.
wrapperinttext = _('Interrupt signal received\n')
edit_pkgbuild = False
# STUFF NOT TO BE CHANGED BY HUMAN BEINGS. EVER.
mp1 = '=='
mp2 = ' '
debug = False
console = None
_pyc = None
hassudo = os.path.exists('/usr/bin/sudo')
uid = os.geteuid()
# Creating the configuration/log stuff...
confhome = os.getenv('XDG_CONFIG_HOME')
if confhome is None:
confhome = os.path.expanduser('~/.config/')
kwdir = os.path.join(confhome, 'kwpolska')
confdir = os.path.join(kwdir, 'pkgbuilder')
confpath = os.path.join(confdir, 'pkgbuilder.ini')
if not os.path.exists(confhome):
os.mkdir(confhome)
if not os.path.exists(kwdir):
os.mkdir(kwdir)
if not os.path.exists(confdir):
os.mkdir(confdir)
if not os.path.exists(confpath):
with open(confpath, 'wb') as fh:
fh.write(pkg_resources.resource_string(
'pkgbuilder', 'data/pkgbuilder.ini.skel'))
# Configuration file support
config = configparser.ConfigParser()
config.read_string(pkg_resources.resource_string(
'pkgbuilder', 'data/pkgbuilder.ini.skel').decode('utf-8'))
config.read([confpath], encoding='utf-8')
if os.getenv('PACMAN') is None:
paccommand = config.get('extras', 'paccommand', fallback='pacman')
else:
paccommand = os.getenv('PACMAN')
# Language changing
language = config.get('PKGBUILDer', 'language')
if language != 'auto':
pkgbuilder.T = pkgbuilder.gettext.translation(
'pkgbuilder', '/usr/share/locale', languages=[language],
fallback='C')
pkgbuilder.G = pkgbuilder.T.gettext
logging.basicConfig(format='%(asctime)-15s [%(levelname)-7s] '
':%(name)-10s: %(message)s',
filename=os.path.join(confdir, 'pkgbuilder.log'),
level=logging.DEBUG)
log = logging.getLogger('pkgbuilder')
log.info('*** PKGBUILDer v' + __version__)
def get_setting(self, name, config_section, config_option,
positive, negative):
"""Get the value of a setting, based on config file and arguments.
:param str name: name of setting, used for error on conflict
:param bool config_section: configuration file section
:param bool config_option: configuration file option
:param bool positive: positive argument
:param bool negative: negative argument
:return: the value requested by the user, defaults to config
:rtype: bool
"""
config = s = self.config.getboolean(config_section, config_option)
if positive:
s = True
elif negative:
s = False
if positive and negative:
print(
_("warning: conflicting values for setting {0}, "
"using {1}").format(name, config))
s = config
return s
def _pycreload(self):
"""Reload pycman, without UI fanciness."""
self._pyc = pycman.config.init_with_config('/etc/pacman.conf')
def pycreload(self):
"""Reload pycman."""
msg = _('Initializing pacman access...')
with pkgbuilder.ui.Throbber(msg, printback=False):
self._pyc = pycman.config.init_with_config('/etc/pacman.conf')
sys.stdout.write('\r' + ((len(msg) + 4) * ' ') + '\r')
@property
def pyc(self):
"""Return a pycman handle, initializing one if necessary."""
if not self._pyc:
self.pycreload()
return self._pyc
def run_command(self, args, prepend=None, asonearg=False):
"""
Run a command.
.. note:: Accepts only one command. ``shell=False``, for safety.
asonearg is for ``su -c`` and most people don’t need nor want
it.
.. note:: since version 2.1.6.2, ``args`` must be a list.
"""
if prepend is None:
prepend = []
if asonearg:
return subprocess.call(prepend + [' '.join(args)])
else:
return subprocess.call(prepend + args)
def sudo(self, args):
"""
Run as root.
Uses ``sudo`` if present, ``su -c`` otherwise, nothing if already
running as root.
.. note:: Accepts only one command. `shell=False`, for safety.
"""
if self.uid != 0:
if self.hassudo:
return self.run_command(args, prepend=['sudo'])
else:
return self.run_command(args, prepend=['su', '-c'],
asonearg=True)
else:
return self.run_command(args)
def root_crash(self):
"""Crash if running as root."""
if self.uid == 0:
self.log.error('running as root, crashing')
self.fancy_error(_('Running as root is not allowed as it can '
'cause catastrophic damage to your system!'))
self.fancy_error(_('Please restart PKGBUILDer as a regular user.'))
sys.exit(255)
def debugmode(self, nochange=False):
"""Print all the logged messages to stderr."""
if not self.debug:
self.console = logging.StreamHandler()
self.console.setLevel(logging.DEBUG)
self.console.setFormatter(logging.Formatter('[%(levelname)-7s] '
':%(name)-10s: %(message)s'))
logging.getLogger('').addHandler(self.console)
self.debug = True
self.mp1 = self.mp2 = 'pb'
elif self.debug and nochange:
pass
else:
logging.getLogger('').removeHandler(self.console)
self.debug = False
self.mp1 = '=='
self.mp2 = ' '
def colorson(self):
"""Enable colors."""
self.colors = {
'all_off': '\x1b[1;0m',
'bold': '\x1b[1;1m',
'blue': '\x1b[1;1m\x1b[1;34m',
'green': '\x1b[1;1m\x1b[1;32m',
'red': '\x1b[1;1m\x1b[1;31m',
'yellow': '\x1b[1;1m\x1b[1;33m'
}
def colorsoff(self):
"""Disable colors."""
self.colors = {
'all_off': '',
'bold': '',
'blue': '',
'green': '',
'red': '',
'yellow': ''
}
def fancy_msg(self, text):
"""Display main messages."""
sys.stderr.write(self.colors['green'] + self.mp1 + '>' +
self.colors['all_off'] +
self.colors['bold'] + ' ' + text +
self.colors['all_off'] + '\n')
self.log.info('({0:<20}) {1}'.format('fancy_msg', text))
def fancy_msg_prompt(self, text):
"""Display main messages that ask a question."""
sys.stderr.write(self.colors['green'] + self.mp1 + '>' +
self.colors['all_off'] +
self.colors['bold'] + ' ' + text +
self.colors['all_off'])
sys.stderr.flush()
self.log.info('({0:<20}) {1} \n'.format('fancy_msg', text))
return input()
def fancy_msg2(self, text):
"""Display sub-messages."""
sys.stderr.write(self.colors['blue'] + self.mp2 + '->' +
self.colors['all_off'] +
self.colors['bold'] + ' ' + text +
self.colors['all_off'] + '\n')
self.log.info('({0:<20}) {1}'.format('fancy_msg2', text))
def fancy_warning(self, text):
"""Display warning messages."""
sys.stderr.write(self.colors['yellow'] + self.mp1 + '> ' +
_('WARNING:') + self.colors['all_off'] +
self.colors['bold'] + ' ' + text +
self.colors['all_off'] + '\n')
self.log.warning('({0:<20}) {1}'.format('fancy_warning', text))
def fancy_warning2(self, text):
"""Display warning sub-messages."""
sys.stderr.write(self.colors['yellow'] + self.mp2 + '->' +
self.colors['all_off'] + self.colors['bold'] + ' ' +
text + self.colors['all_off'] + '\n')
self.log.warning('({0:<20}) {1}'.format('fancy_warning2', text))
def fancy_error(self, text):
"""Display error messages."""
sys.stderr.write(self.colors['red'] + self.mp1 + '> ' + _('ERROR:') +
self.colors['all_off'] + self.colors['bold'] + ' ' +
text + self.colors['all_off'] + '\n')
self.log.error('({0:<20}) {1}'.format('fancy_error', text))
def fancy_error2(self, text):
"""Display error sub-messages."""
sys.stderr.write(self.colors['red'] + self.mp2 + '->' +
self.colors['all_off'] + self.colors['bold'] + ' ' +
text + self.colors['all_off'] + '\n')
self.log.error('({0:<20}) {1}'.format('fancy_error2', text))