<s> import sys , os <EOL> import socket <EOL> import re <EOL> import string <EOL> from optparse import OptionParser <EOL> import traceback <EOL> try : <EOL> from distutils . sysconfig import get_python_lib <EOL> sys . real_prefix = '<STR_LIT>' ; <EOL> sys . path . append ( get_python_lib ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> except : <EOL> pass <EOL> try : <EOL> import sqlalchemy <EOL> import karesansui <EOL> from karesansui import __version__ <EOL> from karesansui . lib . utils import is_uuid , is_int <EOL> from karesansui . lib . utils import generate_phrase , generate_uuid , string_from_uuid <EOL> from karesansui . lib . file . k2v import K2V <EOL> from karesansui . lib . crypt import sha1encrypt <EOL> from karesansui . lib . const import MACHINE_ATTRIBUTE , MACHINE_HYPERVISOR <EOL> from karesansui . db import get_engine , get_metadata , get_session <EOL> from karesansui . db . model . user import User <EOL> from karesansui . db . model . notebook import Notebook <EOL> from karesansui . db . model . tag import Tag <EOL> from karesansui . db . model . machine import Machine <EOL> except ImportError : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> usage = '<STR_LIT>' <EOL> def getopts ( ) : <EOL> optp = OptionParser ( usage = usage , version = __version__ ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:email>' , help = _ ( "<STR_LIT>" ) , default = "<STR_LIT>" ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:password>' , help = _ ( "<STR_LIT>" ) , default = "<STR_LIT>" ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , help = _ ( "<STR_LIT>" ) , default = "<STR_LIT>" ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , help = _ ( "<STR_LIT>" ) , default = "<STR_LIT>" ) <EOL> optp . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , help = _ ( "<STR_LIT>" ) , default = "<STR_LIT>" ) <EOL> return optp . parse_args ( ) <EOL> def chkopts ( opts ) : <EOL> from karesansui . lib . utils import generate_phrase , generate_uuid , string_from_uuid , is_uuid <EOL> from karesansui . lib . const import DEFAULT_LANGS <EOL> reg_email = re . compile ( "<STR_LIT>" ) <EOL> if opts . email : <EOL> if reg_email . search ( opts . email ) is None : <EOL> raise Exception ( '<STR_LIT>' % ( '<STR_LIT>' , opts . email ) ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % '<STR_LIT>' ) <EOL> reg_passwd = re . compile ( "<STR_LIT>" ) <EOL> if opts . password : <EOL> if reg_passwd . search ( opts . password ) is None : <EOL> raise Exception ( '<STR_LIT>' % ( '<STR_LIT>' , opts . password ) ) <EOL> else : <EOL> pass <EOL> if opts . password == "<STR_LIT>" : <EOL> opts . password = generate_phrase ( <NUM_LIT:8> ) <EOL> if opts . uuid : <EOL> if is_uuid ( opts . uuid ) is False : <EOL> raise Exception ( '<STR_LIT>' % ( '<STR_LIT>' , opts . uuid ) ) <EOL> else : <EOL> pass <EOL> if opts . uuid == "<STR_LIT>" : <EOL> opts . uuid = string_from_uuid ( generate_uuid ( ) ) <EOL> reg_fqdn = re . compile ( "<STR_LIT>" ) <EOL> if opts . fqdn : <EOL> if reg_fqdn . search ( opts . fqdn ) is None : <EOL> raise Exception ( '<STR_LIT>' % ( '<STR_LIT>' , opts . fqdn ) ) <EOL> else : <EOL> pass <EOL> if opts . fqdn == "<STR_LIT>" : <EOL> opts . fqdn = socket . gethostname ( ) <EOL> reg_lang = re . compile ( "<STR_LIT>" ) <EOL> if opts . lang : <EOL> if reg_lang . search ( opts . lang ) is None : <EOL> raise Exception ( '<STR_LIT>' % ( '<STR_LIT>' , opts . lang ) ) <EOL> else : <EOL> pass <EOL> if opts . lang == "<STR_LIT>" : <EOL> try : <EOL> DEFAULT_LANGS [ os . environ [ "<STR_LIT>" ] [ <NUM_LIT:0> : <NUM_LIT:5> ] ] <EOL> opts . lang = os . environ [ "<STR_LIT>" ] [ <NUM_LIT:0> : <NUM_LIT:5> ] <EOL> except : <EOL> opts . lang = "<STR_LIT>" <EOL> karesansui . config = K2V ( "<STR_LIT>" ) . read ( ) <EOL> ( opts , args ) = getopts ( ) <EOL> chkopts ( opts ) <EOL> for k in dir ( opts ) : <EOL> v = getattr ( opts , k ) <EOL> if type ( v ) == str and k [ <NUM_LIT:0> : <NUM_LIT:2> ] != "<STR_LIT>" : <EOL> exec ( "<STR_LIT>" % ( k , v , ) ) <EOL> engine = get_engine ( ) <EOL> metadata = get_metadata ( ) <EOL> try : <EOL> metadata . drop_all ( ) <EOL> metadata . tables [ '<STR_LIT>' ] . create ( ) <EOL> metadata . create_all ( ) <EOL> except Exception , e : <EOL> traceback . format_exc ( ) <EOL> raise Exception ( '<STR_LIT>' % '<STR_LIT>' . join ( e . args ) ) <EOL> session = get_session ( ) <EOL> try : <EOL> ( password , salt ) = sha1encrypt ( u"<STR_LIT:%s>" % password ) <EOL> user = session . query ( User ) . filter ( User . email == email ) . first ( ) <EOL> if user is None : <EOL> new_user = User ( u"<STR_LIT:%s>" % email , <EOL> unicode ( password ) , <EOL> unicode ( salt ) , <EOL> u"<STR_LIT>" , <EOL> u"<STR_LIT:%s>" % lang , <EOL> ) <EOL> if string . atof ( sqlalchemy . __version__ [ <NUM_LIT:0> : <NUM_LIT:3> ] ) >= <NUM_LIT> : <EOL> session . add ( new_user ) <EOL> else : <EOL> session . save ( new_user ) <EOL> session . commit ( ) <EOL> else : <EOL> user . password = password <EOL> user . salt = salt <EOL> user . languages = lang <EOL> if string . atof ( sqlalchemy . __version__ [ <NUM_LIT:0> : <NUM_LIT:3> ] ) >= <NUM_LIT> : <EOL> session . add ( user ) <EOL> else : <EOL> session . update ( user ) <EOL> session . commit ( ) <EOL> tag = Tag ( u"<STR_LIT:default>" ) <EOL> if string . atof ( sqlalchemy . __version__ [ <NUM_LIT:0> : <NUM_LIT:3> ] ) >= <NUM_LIT> : <EOL> session . add ( tag ) <EOL> else : <EOL> session . save ( tag ) <EOL> session . commit ( ) <EOL> user = session . query ( User ) . filter ( User . email == email ) . first ( ) <EOL> notebook = Notebook ( u"<STR_LIT>" , u"<STR_LIT>" ) <EOL> machine = Machine ( user , <EOL> user , <EOL> u"<STR_LIT:%s>" % uuid , <EOL> u"<STR_LIT:%s>" % fqdn , <EOL> MACHINE_ATTRIBUTE [ '<STR_LIT>' ] , <EOL> MACHINE_HYPERVISOR [ '<STR_LIT>' ] , <EOL> notebook , <EOL> [ tag ] , <EOL> u"<STR_LIT:%s>" % fqdn , <EOL> u'<STR_LIT>' , <EOL> False , <EOL> None , <EOL> ) <EOL> if string . atof ( sqlalchemy . __version__ [ <NUM_LIT:0> : <NUM_LIT:3> ] ) >= <NUM_LIT> : <EOL> session . add ( machine ) <EOL> else : <EOL> session . save ( machine ) <EOL> session . commit ( ) <EOL> session . close ( ) <EOL> except Exception , e : <EOL> traceback . format_exc ( ) <EOL> raise Exception ( '<STR_LIT>' % '<STR_LIT>' . join ( e . args ) ) </s>
