remove mysql dependency from gendummydata

- remove need to use mysql for escaping the sql -- removing single quote
  should be enough
- instead of using sql to fetch categories from a live database, simply
  consider categories an integer range, specified to the size of that in the
  aur-schema.

Lukas: Add "CATEGORIES_COUNT" initialization. Fix random number range
used in genCategory() (AUTO_INCREMENT columns are 1-based by default,
not 0-based).

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

View file

@ -33,6 +33,7 @@ PKG_FILES = (8, 30) # min/max number of files in a package
PKG_DEPS = (1, 5) # min/max depends a package has PKG_DEPS = (1, 5) # min/max depends a package has
PKG_SRC = (1, 3) # min/max sources a package has PKG_SRC = (1, 3) # min/max sources a package has
PKG_CMNTS = (1, 5) # min/max number of comments a package has PKG_CMNTS = (1, 5) # min/max number of comments a package has
CATEGORIES_COUNT = 17 # the number of categories from aur-schema
VOTING = (0, .30) # percentage range for package voting VOTING = (0, .30) # percentage range for package voting
RANDOM_PATHS = ( # random path locations for package files RANDOM_PATHS = ( # random path locations for package files
"/usr/bin", "/usr/lib", "/etc", "/etc/rc.d", "/usr/share", "/lib", "/usr/bin", "/usr/lib", "/etc", "/etc/rc.d", "/usr/share", "/lib",
@ -56,33 +57,10 @@ if not os.path.exists(SEED_FILE):
sys.stderr.write("Please install the 'words' Arch package\n"); sys.stderr.write("Please install the 'words' Arch package\n");
raise SystemExit raise SystemExit
# 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
esc = db.escape_string
# track what users/package names have been used # track what users/package names have been used
# #
seen_users = {} seen_users = {}
seen_pkgs = {} seen_pkgs = {}
categories = {}
category_keys = []
user_keys = [] user_keys = []
# some functions to generate random data # some functions to generate random data
@ -95,7 +73,7 @@ def genVersion():
ver.append("%d" % random.randrange(0,100)) ver.append("%d" % random.randrange(0,100))
return ".".join(ver) + "-u%d" % random.randrange(1,11) return ".".join(ver) + "-u%d" % random.randrange(1,11)
def genCategory(): def genCategory():
return categories[category_keys[random.randrange(0,len(category_keys))]] return random.randrange(1,CATEGORIES_COUNT)
def genUID(): def genUID():
return seen_users[user_keys[random.randrange(0,len(user_keys))]] return seen_users[user_keys[random.randrange(0,len(user_keys))]]
@ -149,22 +127,6 @@ while len(seen_pkgs) < MAX_PKGS:
# #
contents = None contents = None
# Load package categories from database
#
if DBUG: print "Loading package categories..."
q = "SELECT * FROM PackageCategories"
dbc.execute(q)
row = dbc.fetchone()
while row:
categories[row[1]] = row[0]
row = dbc.fetchone()
category_keys = categories.keys()
# done with the database
#
dbc.close()
db.close()
# developer/tu IDs # developer/tu IDs
# #
developers = [] developers = []
@ -245,7 +207,7 @@ for p in seen_pkgs.keys():
# #
num_comments = random.randrange(PKG_CMNTS[0], PKG_CMNTS[1]) num_comments = random.randrange(PKG_CMNTS[0], PKG_CMNTS[1])
for i in range(0, num_comments): for i in range(0, num_comments):
fortune = esc(commands.getoutput(FORTUNE_CMD).replace("'","")) fortune = commands.getoutput(FORTUNE_CMD).replace("'","")
now = NOW + random.randrange(400, 86400*3) now = NOW + random.randrange(400, 86400*3)
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)