-
-
Save hiroki-o/4746997 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from pyPgSQL import PgSQL | |
import os | |
import argparse | |
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 get_latest_changeset_id(self): | |
qstr = 'select serial from changeset order by serial desc limit 1'; | |
cursor = self.conn.cursor() | |
cursor.execute(qstr) | |
row = cursor.fetchone() | |
serial = row[0] | |
return serial | |
def latest_version(self): | |
print self.get_latest_changeset_id() | |
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'} | |
#options = {'host': 'localhost', 'port': 10733, 'dbname': 'fgj2011', 'user': 'omae', 'password': 'omae'} | |
def main(): | |
parser = argparse.ArgumentParser(description='UnityAssetServer command line client.') | |
parser.add_argument('--host', required=True, help='AssetServer hostname') | |
parser.add_argument('--port', default='10733', help='AssetServer port (default is 10733)') | |
parser.add_argument('--projectname', required=True, help='Project to connect.(note: type in by all lower capital)') | |
parser.add_argument('--user', required=True, help='Username to connect') | |
parser.add_argument('--password', required=True, help='Password of the user') | |
parser.add_argument('--action', required=True, help='<clone | print_version>') | |
parser.add_argument('--clonedir', default='unity', help='directory output for cloning') | |
args = parser.parse_args() | |
options['host'] = args.host; | |
options['port'] = args.port; | |
options['dbname'] = args.projectname; | |
options['user'] = args.user; | |
options['pass'] = args.password; | |
client = AssetClient(options) | |
if args.action == 'clone': | |
client.clone(args.clonedir) | |
elif args.action == 'print_version': | |
client.latest_version() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment