replace print statements with logging module in gendummydata

use the logging module instead of writing directly to stderr
this makes the code cleaner as it removes the numerous tests for the value
of DBUG, yet allows devs to control the level of output verbosity.

Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
This commit is contained in:
elij 2011-04-06 19:23:43 -07:00 committed by Lukas Fleischer
parent 2e444a79d5
commit e08731d24c

View file

@ -15,9 +15,9 @@ import os
import sys import sys
import cStringIO import cStringIO
import commands import commands
import logging
LOG_LEVEL = logging.DEBUG # logging level. set to logging.INFO to reduce output
DBUG = 1
SEED_FILE = "/usr/share/dict/words" SEED_FILE = "/usr/share/dict/words"
DB_HOST = os.getenv("DB_HOST", "localhost") DB_HOST = os.getenv("DB_HOST", "localhost")
DB_NAME = os.getenv("DB_NAME", "AUR") DB_NAME = os.getenv("DB_NAME", "AUR")
@ -46,15 +46,19 @@ RANDOM_URL = ("http://www.", "ftp://ftp.", "http://", "ftp://")
RANDOM_LOCS = ("pub", "release", "files", "downloads", "src") RANDOM_LOCS = ("pub", "release", "files", "downloads", "src")
FORTUNE_CMD = "/usr/bin/fortune -l" FORTUNE_CMD = "/usr/bin/fortune -l"
# setup logging
logformat = "%(levelname)s: %(message)s"
logging.basicConfig(format=logformat, level=LOG_LEVEL)
log = logging.getLogger()
if len(sys.argv) != 2: if len(sys.argv) != 2:
sys.stderr.write("Missing output filename argument"); log.error("Missing output filename argument")
raise SystemExit raise SystemExit
# make sure the seed file exists # make sure the seed file exists
# #
if not os.path.exists(SEED_FILE): if not os.path.exists(SEED_FILE):
sys.stderr.write("Please install the 'words' Arch package\n"); log.error("Please install the 'words' Arch package")
raise SystemExit raise SystemExit
# track what users/package names have been used # track what users/package names have been used
@ -80,7 +84,7 @@ def genUID():
# load the words, and make sure there are enough words for users/pkgs # load the words, and make sure there are enough words for users/pkgs
# #
if DBUG: print "Grabbing words from seed file..." log.debug("Grabbing words from seed file...")
fp = open(SEED_FILE, "r") fp = open(SEED_FILE, "r")
contents = fp.readlines() contents = fp.readlines()
fp.close() fp.close()
@ -95,7 +99,7 @@ else:
# select random usernames # select random usernames
# #
if DBUG: print "Generating random user names..." log.debug("Generating random user names...")
user_id = USER_ID user_id = USER_ID
while len(seen_users) < MAX_USERS: while len(seen_users) < MAX_USERS:
user = random.randrange(0, len(contents)) user = random.randrange(0, len(contents))
@ -108,7 +112,7 @@ user_keys = seen_users.keys()
# select random package names # select random package names
# #
if DBUG: print "Generating random package names..." log.debug("Generating random package names...")
num_pkgs = PKG_ID num_pkgs = PKG_ID
while len(seen_pkgs) < MAX_PKGS: while len(seen_pkgs) < MAX_PKGS:
pkg = random.randrange(0, len(contents)) pkg = random.randrange(0, len(contents))
@ -141,8 +145,7 @@ out.write("BEGIN;\n")
# Begin by creating the User statements # Begin by creating the User statements
# #
if DBUG: print "Creating SQL statements for users.", log.debug("Creating SQL statements for users.")
count = 0
for u in user_keys: for u in user_keys:
account_type = 1 # default to normal user account_type = 1 # default to normal user
if not has_devs or not has_tus: if not has_devs or not has_tus:
@ -166,19 +169,15 @@ for u in user_keys:
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) 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)
out.write(s) out.write(s)
if count % 10 == 0:
if DBUG: print ".", log.debug("Number of developers: %d" % len(developers))
count += 1 log.debug("Number of trusted users: %d" % len(trustedusers))
if DBUG: print "." log.debug("Number of users: %d" % (MAX_USERS-len(developers)-len(trustedusers)))
if DBUG: log.debug("Number of packages: %d" % MAX_PKGS)
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
# Create the package statements # Create the package statements
# #
if DBUG: print "Creating SQL statements for packages.", log.debug("Creating SQL statements for packages.")
count = 0 count = 0
for p in seen_pkgs.keys(): for p in seen_pkgs.keys():
NOW = int(time.time()) NOW = int(time.time())
@ -199,8 +198,6 @@ for p in seen_pkgs.keys():
genCategory(), NOW, uuid, muid) genCategory(), NOW, uuid, muid)
out.write(s) out.write(s)
if count % 100 == 0:
if DBUG: print ".",
count += 1 count += 1
# create random comments for this package # create random comments for this package
@ -212,13 +209,10 @@ for p in seen_pkgs.keys():
s = "INSERT INTO PackageComments (PackageID, UsersID, Comments, CommentTS) VALUES (%d, %d, '%s', %d);\n" % (seen_pkgs[p], genUID(), fortune, now) s = "INSERT INTO PackageComments (PackageID, UsersID, Comments, CommentTS) VALUES (%d, %d, '%s', %d);\n" % (seen_pkgs[p], genUID(), fortune, now)
out.write(s) out.write(s)
if DBUG: print "."
# Cast votes # Cast votes
# #
track_votes = {} track_votes = {}
if DBUG: print "Casting votes for packages.", log.debug("Casting votes for packages.")
count = 0
for u in user_keys: for u in user_keys:
num_votes = random.randrange(int(len(seen_pkgs)*VOTING[0]), num_votes = random.randrange(int(len(seen_pkgs)*VOTING[0]),
int(len(seen_pkgs)*VOTING[1])) int(len(seen_pkgs)*VOTING[1]))
@ -232,9 +226,6 @@ for u in user_keys:
track_votes[pkg] = 0 track_votes[pkg] = 0
track_votes[pkg] += 1 track_votes[pkg] += 1
out.write(s) out.write(s)
if count % 100 == 0:
if DBUG: print ".",
count += 1
# Update statements for package votes # Update statements for package votes
# #
@ -244,8 +235,7 @@ for p in track_votes.keys():
# Create package dependencies and sources # Create package dependencies and sources
# #
if DBUG: print "."; print "Creating statements for package depends/sources.", log.debug("Creating statements for package depends/sources.")
count = 0
for p in seen_pkgs.keys(): for p in seen_pkgs.keys():
num_deps = random.randrange(PKG_DEPS[0], PKG_DEPS[1]) num_deps = random.randrange(PKG_DEPS[0], PKG_DEPS[1])
this_deps = {} this_deps = {}
@ -269,17 +259,9 @@ for p in seen_pkgs.keys():
seen_pkgs[p], src) seen_pkgs[p], src)
out.write(s) out.write(s)
if count % 100 == 0:
if DBUG: print ".",
count += 1
# close output file # close output file
# #
out.write("COMMIT;\n") out.write("COMMIT;\n")
out.write("\n") out.write("\n")
out.close() out.close()
log.debug("Done.")
if DBUG: print "."
if DBUG: print "Done."