aboutsummaryrefslogtreecommitdiff
blob: 06cce516462cb2e8b99fb3673837027d3b076685 (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
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
#!/usr/bin/env python

import os
import sys

# roverlay modules will be imported later

HIDE_EXCEPTIONS = False

class DIE ( object ):
	NOP          =  os.EX_OK
	ERR          =  1
	BAD_USAGE    =  os.EX_USAGE
	USAGE        =  os.EX_USAGE
	ARG          =  9
	CONFIG       =  os.EX_CONFIG
	OV_CREATE    =  20
	SYNC         =  30
	CMD_LEFTOVER =  90
	IMPORT       =  91
	UNKNOWN      =  95
	INTERRUPT    = 130

	@staticmethod
	def die ( msg=None, code=None ):
		code = DIE.ERR if code is None else code
		if msg is not None:
			sys.stderr.write ( msg + "\n" )
#		else:
#			sys.stderr.write ( "died.\n" )
		sys.exit ( code )
	# --- end of die (...) ---

# --- DIE: exit codes ---
die = DIE.die


if __name__ not in ( '__main__', 'roverlay' ):
	die ( "Please don't import this script...", DIE.BAD_USAGE )


# get args
# imports roverlay.argutil (deleted when done)


try:
	import roverlay.argutil
except ImportError:
	if HIDE_EXCEPTIONS:
		die ( "Cannot import roverlay modules!", DIE.IMPORT )
	else:
		raise

COMMAND_DESCRIPTION = {
	'sync'           : 'sync repos',
	'create'         : 'create the overlay '
	                    '(implies sync, override with --nosync)',
	'depres_console' : 'run an interactive depres console (highly experimental)',
	'depres'         : 'this is an alias to \'depres_console\'',
	'nop'            : 'does nothing',
}

commands, config_file, additional_config, extra_opts = \
	roverlay.argutil.parse_argv (
		command_map=COMMAND_DESCRIPTION,
		default_config_file="R-overlay.conf"
	)

OPTION = extra_opts.get

del roverlay.argutil

# -- load config

# imports: roverlay, roverlay.config.entryutil (if --help-config)

try:
	import roverlay
except ImportError:
	if HIDE_EXCEPTIONS:
		die ( "Cannot import roverlay modules!", DIE.IMPORT )
	else:
		raise

try:
	conf = roverlay.load_config_file (
		config_file,
		extraconf=additional_config
	)
	del config_file, additional_config
except:
	if HIDE_EXCEPTIONS:
		die ( "Cannot load config file {!r}.".format ( config_file ), DIE.CONFIG )
	else:
		raise

if OPTION ( 'list_config' ):
	try:
		from roverlay.config.entryutil import list_entries
		print ( "== main config file ==\n" )
		print ( list_entries() )
	except:
		raise
		die ( "Cannot list config entries!" )

	EXIT_AFTER_CONFIG = True

if OPTION ( 'print_config' ):
	try:
		conf.visualize ( into=sys.stdout )
	except:
		die ( "Cannot print config!" )
	EXIT_AFTER_CONFIG = True


if 'EXIT_AFTER_CONFIG' in locals() and EXIT_AFTER_CONFIG:
	sys.exit ( os.EX_OK )


# -- determine commands to run
# (TODO) could replace this section when adding more actions
# imports roverlay.remote, roverlay.overlay.creator

actions = set ( filter ( lambda x : x != 'nop', commands ) )

if 'sync' in actions and OPTION ( 'nosync' ):
	die ( "sync command blocked by --nosync opt.", DIE.ARG )

del commands


if not actions:
	# this happens if a command is nop
	die ( "Nothing to do!", DIE.NOP )

if 'depres_console' in actions or 'depres' in actions:
	if len ( actions ) != 1:
		die ( "depres_console cannot be run with other commands!", DIE.USAGE )

	try:
		from roverlay.depres.simpledeprule.console import DepResConsole
		con = DepResConsole()
		con.run()
		sys.exit ( os.EX_OK )
	except ImportError:
		if HIDE_EXCEPTIONS:
			die ( "Cannot import depres console!", DIE.IMPORT )
		else:
			raise
	except:
		if HIDE_EXCEPTIONS:
			die ( "Exiting on console error!", DIE.ERR )
		else:
			raise



# -- import roverlay modules

try:
	from roverlay.remote          import RepoList
	from roverlay.overlay.creator import OverlayCreator
except ImportError:
	if HIDE_EXCEPTIONS:
		die ( "Cannot import roverlay modules!", DIE.IMPORT )
	else:
		raise



# -- run methods (and some vars)
# imports: nothing

actions_done = set()
set_action_done = actions_done.add

def optionally ( call, option, *args, **kw ):
	if OPTION ( option ):
		return call ( *args, **kw )
# --- end of optionally (...) ---

#repo_list = None
#overlay   = None
def run_sync():
	if "sync" in actions_done: return
	try:
		# set up the repo list
		global repo_list
		repo_list = RepoList (
			sync_enabled   = not OPTION ( 'nosync' ),
			force_distroot = OPTION ( 'force_distroot' )
		)

		## extra_opts->distdir ... TODO
		if 'distdirs' in extra_opts:
			repo_list.add_distdirs ( OPTION ( 'distdirs' ) )
		else:
			# default repo list
			repo_list.load()

		## this runs _nosync() or _sync(), depending on extra_opts->nosync
		repo_list.sync()

		set_action_done ( "sync" )

	except KeyboardInterrupt:
		die ( "Interrupted", DIE.INTERRUPT )
	except:
		if HIDE_EXCEPTIONS:
				die (
					"nosync() failed!" if OPTION ( "nosync" ) \
						else "sync() failed!",
					DIE.SYNC
				)
		else:
			raise
# --- end of run_sync() ---

def run_overlay_create():
	if "create" in actions_done: return
	#run_sync()
	try:
		global overlay
		overlay = OverlayCreator (
			skip_manifest=OPTION ( 'skip_manifest' ),
			allow_write=OPTION ( 'write_overlay' )
		)

		repo_list.add_packages ( overlay.add_package )

		overlay.run ( close_when_done=True )

		optionally ( overlay.write_overlay, 'write_overlay' )
		optionally ( overlay.show_overlay,  'show_overlay'  )
		if OPTION ( 'print_stats' ): print ( "\n" + overlay.stats_str() )

		set_action_done ( "create" )

	except KeyboardInterrupt:
		die ( "Interrupted", DIE.INTERRUPT )
	except:
		if HIDE_EXCEPTIONS:
			die ( "Overlay creation failed.", DIE.OV_CREATE )
		else:
			raise
	finally:
		if 'overlay' in locals() and not overlay.closed:
			# This is important 'cause it unblocks remaining ebuild creation
			# jobs/threads, specifically waiting EbuildJobChannels in depres.
			# It also writes the deps_unresolved file
			overlay.close()
# --- end of run_overlay_create() ---

# -- run

# always run sync 'cause commands = {create,sync} and create implies (no)sync
run_sync()

if 'create' in actions: run_overlay_create()

if len ( actions ) > len ( actions_done ):
	die (
		"Some actions (out of {!r}) could not be performed!".format ( actions ),
		DIE.CMD_LEFTOVER
	)