summaryrefslogtreecommitdiff
blob: efdb9755c7737de46a3aaeeebcff5220c8197176 (plain)
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
#!/usr/bin/python

"""Default settings"""

import argparse
import os
import sys
if sys.hexversion < 0x3000000:
	from io import open
import re
import glob

import portage
from portage import _encodings, _unicode_encode

if sys.version_info[0] >= 3:
	_unicode = str
else:
	_unicode = unicode  # noqa

portage_root = _unicode(portage.root)

DEFAULTS = {
		'DEFAULT_LD_FILE': os.path.join(portage_root, 'etc/ld.so.conf'),
		'DEFAULT_ENV_FILE': os.path.join(portage_root, 'etc/profile.env'),
		'REVDEP_CONFDIR': os.path.join(portage_root, 'etc/revdep-rebuild/'),
		'PKG_DIR': os.path.join(portage_root, 'var/db/pkg/'),
		'DEFAULT_TMP_DIR': os.path.join(portage_root, '/tmp/revdep-rebuild' if os.getgid() else '/var/cache/revdep-rebuild'), #cache default location

		# number of maximum allowed files to be parsed at once
		'CMD_MAX_ARGS': 1000,

		'PRETEND': False,     #pretend only
		'EXACT': False,      #exact package version
		#if program should use temporary files from previous run
		'USE_TMP_FILES': True,

		'VERBOSITY': 1,

		'quiet': False,
		'nocolor': False,
		'library': set(),
		'no-progress': False,
		'debug': False,
		'no-ld-path': False,
		'no-order': False,
		'pass_through_options': [],
		'stdout': sys.stdout,
		'stdin': sys.stdin,
		'stderr': sys.stderr
		}


def parse_options():
	"""Parses the command line options an sets settings accordingly"""

	# TODO: Verify: options: no-ld-path, no-order, no-progress
	#are not appliable
	from .rebuild import VERSION, APP_NAME
	settings = DEFAULTS.copy()

	parser = argparse.ArgumentParser(
		description='Broken reverse dependency rebuilder, python implementation.',
		epilog='Calls emerge, options after -- are ignored by %s '
				'and passed directly to emerge.' % APP_NAME,
		add_help=False
		)

	parser.add_argument('-h', '--help',
					 action='help',
					 help='Print this usage and exit')
	parser.add_argument('-V', '--version',
					 action='version',
					 help='Show version informations',
					 version='%(prog)s ' + VERSION)

	parser.add_argument('-i', '--ignore',
					 action='store_true',
					 help='Ignore temporary files from previous runs '
						'(also won\'t create any)')

	parser.add_argument('-L', '--library',
					 action='append',
					 help='Unconditionally emerge existing packages that use '
						'the library with NAME. NAME can be a full path, full '
						'or partial name')
	parser.add_argument('-l', '--no-ld-path',
					 action='store_true',
					 help='Do not set LD_LIBRARY_PATH')
	parser.add_argument('-o', '--no-order',
					 action='store_true',
					 help='Do not check the build order '
						'(Saves time, but may cause breakage.)')
	parser.add_argument('-p', '--pretend',
					 action='store_true',
					 help='Do a trial run without actually emerging anything '
						'(also passed to emerge command)')

	parser.add_argument('-C', '--nocolor',
					 action='store_true',
					 help='Turn off colored output')
	parser.add_argument('-q', '--quiet',
					 action='store_true',
					 help='Be less verbose (also passed to emerge command)')
	parser.add_argument('-v', '--verbose',
					 action='store_true',
					 help='Be more verbose (also passed to emerge command)')
	parser.add_argument('-d', '--debug',
					 action='store_true',
					 help='Print debug informations')

	parser.add_argument('portage_options', nargs='*')

	args = parser.parse_args()
	settings['VERBOSITY'] = 3 if args.debug else 2 if args.verbose else 0 if args.quiet else 1
	settings['quiet'] = args.quiet
	settings['PRETEND'] = args.pretend
	settings['nocolor'] = args.nocolor
	if args.library:
		settings['library'] = set(settings['library']) | set(args.library)
	settings['USE_TMP_FILES'] = not args.ignore
	settings['pass_through_options'] = list(settings['pass_through_options']) + args.portage_options

	return settings


def _parse_dirs_to_set(dir_str):
	'''Changes space-delimited directory list into set with them
	'''
	_ret = set()
	for search in dir_str.split():
		if search == '-*':
			break
		_ret.update(glob.glob(search))
	return _ret


def parse_revdep_config(revdep_confdir):
	''' Parses all files under and returns
		tuple of: (masked_dirs, masked_files, search_dirs)'''

	search_dirs = os.environ.get('SEARCH_DIRS', '')
	masked_dirs = os.environ.get('SEARCH_DIRS_MASK', '')
	masked_files = os.environ.get('LD_LIBRARY_MASK', '')

	for _file in os.listdir(revdep_confdir):
		for line in open(_unicode_encode(os.path.join(revdep_confdir, _file),
				encoding=_encodings['fs']), encoding=_encodings['content']):
			line = line.strip()
			#first check for comment, we do not want to regex all lines
			if not line.startswith('#'):
				match = re.match(r'LD_LIBRARY_MASK=\"([^"]+)"', line)
				if match is not None:
					masked_files += ' ' + match.group(1)
					continue
				match = re.match(r'SEARCH_DIRS_MASK=\"([^"]+)"', line)
				if match is not None:
					masked_dirs += ' ' + match.group(1)
					continue
				match = re.match(r'SEARCH_DIRS="([^"]+)"', line)
				if match is not None:
					search_dirs += ' ' + match.group(1)
					continue

	masked_files = set(masked_files.split(' '))
	masked_dirs = _parse_dirs_to_set(masked_dirs)
	search_dirs = _parse_dirs_to_set(search_dirs)

	return (masked_dirs, masked_files, search_dirs)