gendummydata.py 12.4 KB
Newer Older
eric's avatar
eric committed
1
#!/usr/bin/python
eric's avatar
eric committed
2
3
4
"""
usage: gendummydata.py outputfilename.sql
"""
eric's avatar
eric committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# This script seeds the AUR database with dummy data for
# use during development/testing.  It uses random entries
# from /usr/share/dict/words to create user accounts and
# package names.  It generates the SQL statements to
# insert these users/packages into the AUR database.
#

DBUG      = 1
SEED_FILE = "/usr/share/dict/words"
DB_HOST   = "localhost"
DB_NAME   = "AUR"
DB_USER   = "aur"
DB_PASS   = "aur"
eric's avatar
eric committed
19
USER_ID   = 5          # Users.ID of first bogus user
eric's avatar
eric committed
20
21
22
23
24
25
PKG_ID    = 1          # Packages.ID of first package
MAX_USERS = 500        # how many users to 'register'
MAX_DEVS  = .1         # what percentage of MAX_USERS are Developers
MAX_TUS   = .2         # what percentage of MAX_USERS are Trusted Users
MAX_PKGS  = 2500       # how many packages to load
PKG_FILES = (8, 30)    # min/max number of files in a package
26
27
PKG_DEPS  = (1, 5)     # min/max depends a package has
PKG_SRC   = (1, 3)     # min/max sources a package has
eric's avatar
eric committed
28
PKG_CMNTS = (1, 5)     # min/max number of comments a package has
eric's avatar
eric committed
29
VOTING    = (0, .30)   # percentage range for package voting
eric's avatar
eric committed
30
RANDOM_PATHS = (       # random path locations for package files
eric's avatar
eric committed
31
32
33
34
	"/usr/bin", "/usr/lib", "/etc", "/etc/rc.d", "/usr/share", "/lib",
	"/var/spool", "/var/log", "/usr/sbin", "/opt", "/usr/X11R6/bin",
	"/usr/X11R6/lib", "/usr/libexec", "/usr/man/man1", "/usr/man/man3",
	"/usr/man/man5", "/usr/X11R6/man/man1", "/etc/profile.d"
eric's avatar
eric committed
35
36
37
38
39
40
41
42
43
44
45
46
)
RANDOM_TLDS = ("edu", "com", "org", "net", "tw", "ru", "pl", "de", "es")
RANDOM_URL = ("http://www.", "ftp://ftp.", "http://", "ftp://")
RANDOM_LOCS = ("pub", "release", "files", "downloads", "src")
FORTUNE_FILES = ("computers", "fortunes", "literature", "science",
  "songs-poems", "startrek", "wisdom", "work", "buffy", "calvin",
  "futurama", "jargon", "matrix", "starwars", "tao", "chalkboard",
  "dune", "dune-messiah", "children-of-dune", "god-emperor", "definitions",
  "drugs", "education", "food", "humorists", "kids", "law", "news",
  "people", "pets", "politics", "platitudes", "zippy", "riddles"
)
FORTUNE_CMD = "/usr/bin/fortune -l " + " ".join(FORTUNE_FILES)
47

eric's avatar
eric committed
48
49
50
51
52
53

import random
import time
import os
import sys
import cStringIO
eric's avatar
eric committed
54
55
import commands

eric's avatar
eric committed
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

if len(sys.argv) != 2:
	sys.stderr.write("Missing output filename argument");
	raise SystemExit

# Just let python throw the errors if any happen
#
out = open(sys.argv[1], "w")

# make sure the seed file exists
#
if not os.path.exists(SEED_FILE):
	sys.stderr.write("Please install the 'words' Arch package\n");

# Make sure database access will be available
#
try:
	import MySQLdb
except:
	sys.stderr.write("Please install the 'mysql-python' Arch package\n");
	raise SystemExit

# try to connect to database
#
try:
	db = MySQLdb.connect(host = DB_HOST, user = DB_USER,
			db = DB_NAME, passwd = DB_PASS)
	dbc = db.cursor()
except:
	sys.stderr.write("Could not connect to database\n");
	raise SystemExit

eric's avatar
eric committed
88
89
esc = db.escape_string

eric's avatar
eric committed
90
91
92
93
94
95
96
97
98
99
100
101
102

# track what users/package names have been used
#
seen_users = {}
seen_pkgs = {}
locations = {}
categories = {}
location_keys = []
category_keys = []
user_keys = []

# some functions to generate random data
#
eric's avatar
eric committed
103
104
105
106
def genVersion(location_id=0):
	ver = []
	ver.append("%d" % random.randrange(0,10))
	ver.append("%d" % random.randrange(0,20))
eric's avatar
eric committed
107
	if random.randrange(0,2) == 0:
eric's avatar
eric committed
108
109
110
111
112
		ver.append("%d" % random.randrange(0,100))
	if location_id == 2: # the package is in the AUR
		return ".".join(ver) + "-u%d" % random.randrange(1,11)
	else:
		return ".".join(ver) + "%d" % random.randrange(1,11)
eric's avatar
eric committed
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
def genCategory():
	return categories[category_keys[random.randrange(0,len(category_keys))]]
def genLocation():
	return locations[location_keys[random.randrange(0,len(location_keys))]]
def genUID():
	return seen_users[user_keys[random.randrange(0,len(user_keys))]]


# load the words, and make sure there are enough words for users/pkgs
#
if DBUG: print "Grabbing words from seed file..."
fp = open(SEED_FILE, "r")
contents = fp.readlines()
fp.close()
if MAX_USERS > len(contents):
	MAX_USERS = len(contents)
if MAX_PKGS > len(contents):
	MAX_PKGS = len(contents)
if len(contents) - MAX_USERS > MAX_PKGS:
	need_dupes = 0
else:
	need_dupes = 1

# select random usernames
#
if DBUG: print "Generating random user names..."
user_id = USER_ID
while len(seen_users) < MAX_USERS:
	user = random.randrange(0, len(contents))
eric's avatar
eric committed
142
143
144
	word = contents[user].replace("'", "").replace(".","").replace(" ", "_")
	word = word.strip().lower()
	if not seen_users.has_key(word):
eric's avatar
eric committed
145
146
147
148
149
150
151
152
153
154
		seen_users[word] = user_id
		user_id += 1
user_keys = seen_users.keys()

# select random package names
#
if DBUG: print "Generating random package names..."
num_pkgs = PKG_ID
while len(seen_pkgs) < MAX_PKGS:
	pkg = random.randrange(0, len(contents))
eric's avatar
eric committed
155
156
	word = contents[pkg].replace("'", "").replace(".","").replace(" ", "_")
	word = word.strip().lower()
eric's avatar
eric committed
157
158
159
160
161
162
163
164
165
	if not need_dupes:
		if not seen_pkgs.has_key(word) and not seen_users.has_key(word):
			seen_pkgs[word] = num_pkgs
			num_pkgs += 1
	else:
		if not seen_pkgs.has_key(word):
			seen_pkgs[word] = num_pkgs
			num_pkgs += 1

eric's avatar
eric committed
166
167
168
169
# free up contents memory
#
contents = None

eric's avatar
eric committed
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
# Load package categories from database
#
if DBUG: print "Loading package categories/locations..."
q = "SELECT * FROM PackageCategories"
dbc.execute(q)
row = dbc.fetchone()
while row:
	categories[row[1]] = row[0]
	row = dbc.fetchone()
category_keys = categories.keys()

# Load package locations from database
#
q = "SELECT * FROM PackageLocations"
dbc.execute(q)
row = dbc.fetchone()
while row:
	locations[row[1]] = row[0]
	row = dbc.fetchone()
location_keys = locations.keys()

# done with the database
#
dbc.close()
db.close()

196
197
198
199
200
201
202
# developer/tu IDs
#
developers = []
trustedusers = []
has_devs = 0
has_tus = 0

eric's avatar
eric committed
203
204
205
206
207
# Begin by creating the User statements
#
if DBUG: print "Creating SQL statements for users.",
count = 0
for u in user_keys:
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	account_type = 1  # default to normal user
	if not has_devs or not has_tus:
		account_type = random.randrange(1, 4)
		if account_type == 3 and not has_devs:
			# this will be a dev account
			#
			developers.append(seen_users[u])
			if len(developers) >= MAX_DEVS * MAX_USERS:
				has_devs = 1
		elif account_type == 2 and not has_tus:
			# this will be a trusted user account
			#
			trustedusers.append(seen_users[u])
			if len(trustedusers) >= MAX_TUS * MAX_USERS:
				has_tus = 1
		else:
			# a normal user account
			#
			pass
	
228
	s = "INSERT INTO Users (ID, AccountTypeID, Username, Email, Passwd) VALUES (%d, %d, '%s', '%s@example.com', MD5('%s'));\n" % (seen_users[u], account_type, u, u, u)
eric's avatar
eric committed
229
230
231
232
233
	out.write(s)
	if count % 10 == 0:
		if DBUG: print ".",
	count += 1
if DBUG: print "."
234
235
236
237
238
if DBUG:
	print "Number of developers:", len(developers)
	print "Number of trusted users:", len(trustedusers)
	print "Number of users:", (MAX_USERS-len(developers)-len(trustedusers))
	print "Number of packages:", MAX_PKGS
eric's avatar
eric committed
239
240
241
242
243
244

# Create the package statements
#
if DBUG: print "Creating SQL statements for packages.",
count = 0
for p in seen_pkgs.keys():
eric's avatar
eric committed
245
	NOW = int(time.time())
246
247
248
249
250
251
252
253
254
255
256
	if count % 2 == 0:
		muid = developers[random.randrange(0,len(developers))]
	else:
		muid = trustedusers[random.randrange(0,len(trustedusers))]
	if count % 20 == 0: # every so often, there are orphans...
		muid = 0

	location_id = genLocation()
	if location_id == 1: # unsupported pkgs don't have a maintainer
		muid = 0

eric's avatar
eric committed
257
258
259
	uuid = genUID() # the submitter/user

	s = "INSERT INTO Packages (ID, Name, Version, CategoryID, LocationID, SubmittedTS, SubmitterUID, MaintainerUID, AURMaintainerUID) VALUES (%d, '%s', '%s', %d, %d, %d, %d, %d, %d);\n" % (seen_pkgs[p], p, genVersion(location_id),
eric's avatar
eric committed
260
			genCategory(), location_id, NOW, uuid, uuid, muid)
eric's avatar
eric committed
261
262
263
264
265
	out.write(s)
	if count % 100 == 0:
		if DBUG: print ".",
	count += 1

eric's avatar
eric committed
266
267
268
269
	# create random comments for this package
	#
	num_comments = random.randrange(PKG_CMNTS[0], PKG_CMNTS[1])
	for i in range(0, num_comments):
eric's avatar
eric committed
270
		fortune = esc(commands.getoutput(FORTUNE_CMD).replace("'",""))
eric's avatar
eric committed
271
		now = NOW + random.randrange(400, 86400*3)
eric's avatar
eric committed
272
		s = "INSERT INTO PackageComments (PackageID, UsersID, Comments, CommentTS) VALUES (%d, %d, '%s', %d);\n" % (seen_pkgs[p], genUID(), fortune, now)
eric's avatar
eric committed
273
274
		out.write(s)

275
276
	if location_id == 1: # Unsupported - just a PKGBUILD and maybe other stuff
		others = random.randrange(0,3)
277
		s = "INSERT INTO PackageContents (PackageID, URLPath, FSPath, FileSize) VALUES (%d, '%s', '%s', %d);\n" % (seen_pkgs[p], "PKGBUILD", "/home/aur/incoming/%s/PKGBUILD" % p,
278
279
280
				random.randrange(0,999))
		out.write(s)
		if others == 0:
281
			s = "INSERT INTO PackageContents (PackageID, URLPath, FSPath, FileSize) VALUES (%d, '%s', '%s', %d);\n" % (seen_pkgs[p], "%s.patch" % p,
282
283
284
285
286
					"/home/aur/incoming/%s/%s.patch" % (p,p),
					random.randrange(0,999))
			out.write(s)

		elif others == 1:
287
			s = "INSERT INTO PackageContents (PackageID, URLPath, FSPath, FileSize) VALUES (%d, '%s', '%s', %d);\n" % (seen_pkgs[p], "%s.patch" % p,
288
289
290
					"/home/aur/incoming/%s/%s.patch" % (p,p),
					random.randrange(0,999))
			out.write(s)
291
			s = "INSERT INTO PackageContents (PackageID, URLPath, FSPath, FileSize) VALUES (%d, '%s', '%s', %d);\n" % (seen_pkgs[p], "arch.patch",
292
293
294
295
296
					"/home/aur/incoming/%s/arch.patch" % p,
					random.randrange(0,999))
			out.write(s)

		elif others == 2:
297
			s = "INSERT INTO PackageContents (PackageID, URLPath, FSPath, FileSize) VALUES (%d, '%s', '%s', %d);\n" % (seen_pkgs[p], "%s.patch" % p,
298
299
300
					"/home/aur/incoming/%s/%s.patch" % (p,p),
					random.randrange(0,999))
			out.write(s)
301
			s = "INSERT INTO PackageContents (PackageID, URLPath, FSPath, FileSize) VALUES (%d, '%s', '%s', %d);\n" % (seen_pkgs[p], "arch.patch",
302
303
304
					"/home/aur/incoming/%s/arch.patch" % p,
					random.randrange(0,999))
			out.write(s)
305
			s = "INSERT INTO PackageContents (PackageID, URLPath, FSPath, FileSize) VALUES (%d, '%s', '%s', %d);\n" % (seen_pkgs[p], "%s.install" % p,
306
307
					"/home/aur/incoming/%s/%s.install" % (p,p),
					random.randrange(0,999))
eric's avatar
eric committed
308
			out.write(s)
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334

	else:
		# Create package contents
		#
		num_files = random.randrange(PKG_FILES[0], PKG_FILES[1])
		files = {}
		for f in range(num_files):
			loc = RANDOM_PATHS[random.randrange(len(RANDOM_PATHS))]
			if "lib" in loc:
				path = loc + "/lib" + p + ".so"
			elif "man" in loc:
				path = loc + "/" + p + "." + loc[-1] + ".gz"
			elif "share" in loc:
				path = loc + "/" + p + "/sounds/" + p + ".wav"
			elif "profile" in loc:
				path = loc + "/" + p + ".sh"
			elif "rc.d" in loc:
				path = loc + "/" + p
			elif "etc" in loc:
				path = loc + "/" + p + ".conf"
			elif "opt" in loc:
				path = loc + "/" + p + "/bin/" + p
			else:
				path = loc + "/" + p
			if not files.has_key(path):
				files[path] = 1
335
				s = "INSERT INTO PackageContents (PackageID, URLPath, FSPath, FileSize) VALUES (%d, '%s', '%s', %d);\n" % (seen_pkgs[p], os.path.basename(path), path,
336
337
						random.randrange(0,99999999))
				out.write(s)
eric's avatar
eric committed
338
339
340
341
if DBUG: print "."

# Cast votes
#
342
track_votes = {}
eric's avatar
eric committed
343
344
345
if DBUG: print "Casting votes for packages.",
count = 0
for u in user_keys:
346
347
	num_votes = random.randrange(int(len(seen_pkgs)*VOTING[0]),
			int(len(seen_pkgs)*VOTING[1]))
eric's avatar
eric committed
348
349
350
351
	pkgvote = {}
	for v in range(num_votes):
		pkg = random.randrange(0, len(seen_pkgs))
		if not pkgvote.has_key(pkg):
eric's avatar
eric committed
352
			s = "INSERT INTO PackageVotes (UsersID, PackageID) VALUES (%d, %d);\n" % (seen_users[u], pkg)
eric's avatar
eric committed
353
			pkgvote[pkg] = 1
354
355
356
			if not track_votes.has_key(pkg):
				track_votes[pkg] = 0
			track_votes[pkg] += 1
eric's avatar
eric committed
357
358
359
360
361
			out.write(s)
			if count % 100 == 0:
				if DBUG: print ".",
			count += 1

362
363
364
365
366
367
# Update statements for package votes
#
for p in track_votes.keys():
	s = "UPDATE Packages SET NumVotes = %d WHERE ID = %d;\n" % (track_votes[p], p)
	out.write(s)

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Create package dependencies and sources
#
if DBUG: print "."; print "Creating statements for package depends/sources.",
count = 0
for p in seen_pkgs.keys():
	num_deps = random.randrange(PKG_DEPS[0], PKG_DEPS[1])
	this_deps = {}
	i = 0
	while i != num_deps:
		dep = random.randrange(0, len(seen_pkgs))
		if not this_deps.has_key(dep):
			s = "INSERT INTO PackageDepends VALUES (%d, %d);\n" % (seen_pkgs[p], dep)
			out.write(s)
			i += 1

	num_sources = random.randrange(PKG_SRC[0], PKG_SRC[1])
	for i in range(num_sources):
		src_file = user_keys[random.randrange(0, len(user_keys))]
		src = "%s%s.%s/%s/%s-%s.tar.gz" % (
				RANDOM_URL[random.randrange(0,len(RANDOM_URL))],
				p, RANDOM_TLDS[random.randrange(0,len(RANDOM_TLDS))],
				RANDOM_LOCS[random.randrange(0,len(RANDOM_LOCS))],
				src_file, genVersion())
		s = "INSERT INTO PackageSources VALUES (%d, '%s');\n" % (
				seen_pkgs[p], src)
		out.write(s)

	if count % 100 == 0:
		if DBUG: print ".",
	count += 1


eric's avatar
eric committed
400
401
402
403
404
405
406
407
# close output file
#
out.write("\n")
out.close()

if DBUG: print "."
if DBUG: print "Done."