aboutsummaryrefslogtreecommitdiff
blob: 52f25ed9255c0faeb1d9dbb77384e25bd6d72555 (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
#!/usr/bin/python
#
# Simple program to rsync from various 3rd party ebuild servers. 
#
#
# Copyright (c) 2004, Matthew Schick <matt@breakmygentoo.net> (original author)
# Copyright (c) 2004, Gentoo Technologies, Inc
# Copyright (c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
#
# Distributed under the terms of the GNU General Public License v2

__author__ = "Matt Schick <matt@breakmygentoo.net>, Karl Trygve Kalleberg <karltk@gentoo.org>"
__email__ = "karltk@gentoo.org"
__version__ = "0.1.0"
__productname__ = "gensync"
__description__ = "Gentoo Overlay Sync Tool"


import sys
import os
import fileinput

sys.path.insert(0, "/usr/lib/portage/pym")
import portage

try:
	from portage.output import *
except ImportError:
	from output import *

class ConfigDefaults:
	def __init__(self):
		self.rsync_timeout = 0
		self.base_overlay = "/usr/local/overlays"
		self.confdir = '/etc/gensync'
		self._init_from_file()
		self._setup_rsync()
	def _init_from_file(self):
		try:
			ins = open("/etc/gensync/gensync.conf")
			for x in ins.readlines():
				# Skip line if it's a comment or not well-formed
				if x.find("=") == -1 or \
				       (len(x) and x[0] == "#"):
					continue
				(attrib, value) = x.split('=')
				attrib = attrib.strip().strip('"')
				value = value.strip().strip('"')
				if attrib == "rsync_timeout":
					self.rsync_timeout = value
				elif attrib == "base_overlay":
					self.base_overlay = value
		except:
			pass

	def _setup_rsync(self):
		if self.rsync_timeout == 0:
			try:
				self.rsync_timeout = portage.settings["RSYNC_TIMEOUT"]
			except:
				self.rsync_timeout = 180
		self.rsync_command = "/usr/bin/rsync " + \
				     "-rlptDvz --progress " + \
				     "--delete --delete-after " + \
				     "--timeout=" + str(self.rsync_timeout) + " " \
				     "--exclude='distfiles/*' " + \
				     "--exclude='local/*' " + \
				     "--exclude='packages/*' "

Config = ConfigDefaults()

class SyncSource:
	"""Contains all details about an upstream rsync source."""
	def __init__(self,filename):
		self.id = "#unset#"
		self.name = "#unset#"
		self.url = "#unset#"
		self.overlay = "#unset#"
		self._init_from_file(filename)

		if self.overlay == "#unset#":
			self.overlay = Config.base_overlay + "/" + self.id
		elif len(self.overlay) and self.overlay[0] != "/":
			self.overlay = Config.base_overlay + "/" + self.overlay
		elif len(self.overlay) and self.overlay[0] == "/":
			pass
		else:
			print "Malformed overlay, '" + self.overlay + "'"
			sys.exit(-2)

	def _init_from_file(self, filename):
		"""Loads configuration settings from a .syncsource file."""
		ins = open(filename)
		for x in ins.readlines():
			# Skip line if it's a comment or not well-formed
			if x.find("=") == -1 or \
			       (len(x) and x[0] == "#"):
				continue
			(attrib, value) = x.split("=")
			attrib = attrib.strip().strip('"')
			value = value.strip().strip('"')
			if attrib == "id":
				self.id = value
			elif attrib == "description":
				self.name = value
			elif attrib == "rsync":
				self.url = value
			elif attrib == "overlay":
				self.overlay = value
	def perform_sync(self):
		"""Syncs with upstream source."""
		cmd = Config.rsync_command + self.url +"/* " + self.overlay
		exitcode = portage.spawn(cmd, portage.settings, free=1)

	def dump(self,ous=sys.stdout):
		ous.write("id     =\"%s\"\n" % (self.id) + \
			  "name   =\"%s\"\n" % (self.name) + \
			  "rsync  =\"%s\"\n" % (self.url) + \
			  "overlay=\"%s\"\n\n" % (self.overlay))

class SyncSourceManager:
	"""Holds information on all known upstream rsync sources."""
	def __init__(self):
		self.sources = []
		self._load_source_info()
	def _load_source_info(self):
		for x in os.listdir(Config.confdir):
			if x.rfind(".syncsource") > 0 and \
				x.endswith(".syncsource"):
				ss = SyncSource(Config.confdir + "/" + x)
				self.sources.append(ss)
	def list_sources(self):
		for x in self.sources:
			x.dump()
	def get_sync_source(self, source_id):
		for x in self.sources:
			if x.id == source_id:
				return x
		return None
	def get_all_sync_sources(self):
		return self.sources
		
def print_version():
	print __productname__ + "(" + __version__ + ") - " + \
		  __description__
	print "Author(s): " + __author__

def print_usage():
	print white("Usage: ") + turquoise(__productname__) + \
	      yellow(" <options> ") + green("repo-id-1 repo-id-2 ...")
	print "where " + yellow("<options>") + " is one of"
	print yellow(" -a, --all-sources") + "  - sync all know sources"
	print yellow(" -l, --list-sources") + " - list known rsync sources"
	print yellow(" -C, --no-color") + "      - turn off colours"
	print yellow(" -h, --help") + "         - this help screen"
	print yellow(" -V, --version") + "      - display version info"


def parse_args(cliargs):

	cmd = "sync-sources"
	args = []
	options = []
	
	for x in cliargs:
		if x in ["-V", "--version"]:
			cmd = "print-version"
			break
		elif x in ["-h", "--help"]:
			cmd = "print-usage"
			break
		elif x in ["-C", "--no-color"]:
			options.append("nocolor")
			break
		elif x in ["-l", "--list-sources"]:
			cmd = "list-sources"
		elif x in ["-a", "--all-sources"]:
			cmd = "all-sources"
		else:
			args.append(x)
	return (cmd, args, options)

def main():
	
	# Setup colors, as per system defaults
	if (not sys.stdout.isatty()) or \
	       (portage.settings["NOCOLOR"] in ["yes","true"]):
		nocolor()

	# Parse arguments
	(cmd, args, options) = parse_args(sys.argv[1:])

	# Turn off color, if specified by cmdline switch
	if "nocolor" in options:
		nocolor()
		
	# Initialise sync source manager
	ssmgr = SyncSourceManager()

	# Perform selected command
	if cmd == "list-sources":
		ssmgr.list_sources()
	elif cmd == "print-version":
		print_version()
	elif cmd == "print-usage":
		print_usage()
	elif cmd == "sync-sources":
		for x in args:
			repo = ssmgr.get_sync_source(x)
			if repo:
				print "Syncing '%s' into '%s'" % \
				      (repo.id, repo.overlay)
				repo.perform_sync()
	elif cmd == "all-sources":
		for repo in ssmgr.get_all_sync_sources():
			print "Syncing '%s' into '%s'" % \
			      (repo.id, repo.overlay)
			repo.perform_sync()
	else:
		print red("Unknown command '" + cmd + "'")

if __name__ == "__main__":
	try:
		main()
	except KeyboardInterrupt:
		print "Operation aborted by user keypress!"