Skip to content

Instantly share code, notes, and snippets.

@dpogue
Created January 25, 2012 21:43
Show Gist options
  • Save dpogue/1678981 to your computer and use it in GitHub Desktop.
Save dpogue/1678981 to your computer and use it in GitHub Desktop.
Terrible and crude, but effective, Unity Asset Server checkout script
#!/usr/bin/env python
from pyPgSQL import PgSQL
import os
def DebugPrint(string):
print '[DEBUG] %s' % string
class Changeset:
def __init__(self, dbcol):
self.id = dbcol[0]
self.description = dbcol[1]
self.time = dbcol[2]
class AssetFile:
def __init__(self, dbcol):
self.id = dbcol[0]
self.type = dbcol[1]
self.lobj = dbcol[2]
def read(self):
self.lobj.open('r')
data = self.lobj.read()
self.lobj.close()
return data
class AssetClient:
def __init__(self, opts={}):
connstr = '%s:%s:%s:%s:%s' % (opts['host'], opts['port'], opts['dbname'], opts['user'], opts['pass'])
self.conn = PgSQL.connect(connstr)
def get_changeset(self, cs_id=None):
qstr = 'SELECT * FROM changeset';
if cs_id:
qstr += ' WHERE serial = %d' % cs_id
cursor = self.conn.cursor()
DebugPrint(qstr)
cursor.execute(qstr)
sets = []
allsets = cursor.fetchall()
for s in allsets:
sets.append(Changeset(s))
return sets
def get_changeset_assets(self, cs_id):
qstr = 'SELECT assetversion.* FROM assetversion \
INNER JOIN changesetcontents \
ON assetversion.serial = changesetcontents.assetversion \
WHERE changesetcontents.changeset = %d' % cs_id
cursor = self.conn.cursor()
DebugPrint(qstr)
cursor.execute(qstr)
assets = cursor.fetchall()
return assets
def get_asset_file(self, asset_version):
qstr = 'SELECT * FROM assetcontents WHERE assetversion = %d' % asset_version
cursor = self.conn.cursor()
DebugPrint(qstr)
cursor.execute(qstr)
afile = AssetFile(cursor.fetchone())
return afile
def get_asset_file_path(self, asset_id):
qstr = 'SELECT name,parent FROM assetversion WHERE asset = %d ORDER BY revision DESC LIMIT 1' % asset_id
cursor = self.conn.cursor()
DebugPrint(qstr)
cursor.execute(qstr)
row = cursor.fetchone()
name = row[0]
parent = row[1]
if parent:
# This probably won't work on Windows
name = self.get_asset_file_path(parent) + '/' + name
return name
def clone(self, output_dir):
changesets = self.get_changeset()
for cset in changesets:
assets = self.get_changeset_assets(cset.id)
for a in assets:
if a[7] == 7000:
continue
path = output_dir + '/' + self.get_asset_file_path(a[1])
d = os.path.dirname(path)
if not os.path.exists(d):
os.makedirs(d)
afile = self.get_asset_file(a[0])
f = file(path, 'wb')
f.write(afile.read())
f.close()
options = {'host': 'MYSERVER', 'port': 10733, 'dbname': 'PROJECTNAME', 'user': 'USERNAME', 'pass': 'PASSWD'}
client = AssetClient(options)
client.clone('unity')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment