コード例 #1
0
def render_migration(name, model, comment, col_defs = "", PARTS_DIR = powlib.PARTS_DIR, prefix_dir = "./"):
    """
    Renders a database migration file.
    @param name:        A Name for the migration. By default the modelname is taken.
    @param model:       Modelname for this migration (typically defining the model's base table)
    @param comment:     a Comment for this migration
    @param col_defs:    pre defined column definitions of the form NAME TYPE OPTIONS, NAME1 TYPE1 OPTIONS1, ...
    @param PARTS_DIR:   A relative path to the stubs/partials dir from the executing script.
    @param prefix_dir:  A prefix path to be added to migrations making prefix_dir/migrations the target dir
    """
    
    # add the auto generated (but can be safely edited) warning to the outputfile
    infile = open (os.path.normpath(PARTS_DIR + "/db_migration_stub.py"), "r")
    ostr = infile.read()
    infile.close()

    # Replace the TAGGED Placeholders with the actual values
    ostr = ostr.replace( "#DATE", str(datetime.date.today() ))
    pluralname = powlib.plural(model)
    ostr = ostr.replace("#TABLENAME", pluralname)
    
    #
    # Add / Replace the column definitions with the given ones by -d (if there were any)
    # 
    if col_defs != "None":
        ostr = transform_col_defs( ostr, col_defs )

    # generate the new version
    version = get_new_version()
    verstring = powlib.version_to_string(version)

    print "generate_migration: " + name + " for model: " + model

    # really write the migration now
    write_migration(name, model, comment, prefix_dir, ostr)

    return
コード例 #2
0
def render_migration(name, model, comment, col_defs = "", PARTS_DIR = powlib.PARTS_DIR, prefix_dir = "./"):
    # 
    #print "generate_migration: " + name + "  for model: " + model
    #
    
    # add the auto generated (but can be safely edited) warning to the outputfile
    infile = open (os.path.normpath(PARTS_DIR + "/can_be_edited.txt"), "r")
    ostr = infile.read()
    infile.close()
    
    # add a creation date
    ostr = ostr + os.linesep
    ostr = ostr + "# date created: \t" + str(datetime.date.today())
    ostr = ostr + os.linesep
    
    # Add the model_stub part1 content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part1.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    
    pluralname = powlib.plural(model)
    ostr += powlib.tab +  "table_name=\"" + pluralname + "\""
    ostr += powlib.linesep
    #print "modelname was: " + model + "  pluralized table_name is:" + pluralname
    
    # Add the model_stub part2 content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part2.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    
    #
    # Add / Replace the column definitions with the given ones by -d (if there were any)
    # 
    if col_defs != "None":
        ostr = transform_col_defs( ostr, col_defs )
        
    app = powlib.load_class( "App", "App")
    app_versions = powlib.load_class( "Version", "Version")
    sess = app.pbo.getSession()
    app = sess.query(App.App).first()
    
    version = app.maxversion
    oldmaxversion = version
    version += 1
    
    verstring = powlib.version_to_string(version)
    print "generate_migration: " + name + " for model: " + model
    #print "version: " + str(version)
    #print "version2string: " + verstring
    filename = os.path.normpath ( "./migrations/" + verstring +"_" + name +".py" )
    
    #update the app table with the new version
    #appTable.update().values(maxversion= str(version) ).execute()
    app.maxversion = str(version)
    app.update()
    app_versions.filename = str(verstring +"_" + name )
    app_versions.version = str(version)
    app_versions.comment = str(comment)
    app_versions.update()
    print " -- maxversion (old,new): (" + str(oldmaxversion) + "," + str(app.maxversion) +")"
    ofile = open(  os.path.normpath(os.path.join(prefix_dir,filename)) , "w+") 
    print  " -- created file:" + str(os.path.normpath(os.path.join(prefix_dir,filename)))
    ofile.write( ostr )
    ofile.close()
    return
コード例 #3
0
ファイル: generate_model.py プロジェクト: cponeill/pow_devel
def render_model(modelname, force, comment, prefix_path="./", properties=None):
    
    print "generate_model: " + modelname
    #print "force: ", force
    infile = None
    infile = open (os.path.normpath( PARTS_DIR + "can_be_edited.txt"), "r")
    ostr = infile.read()
    infile.close()
    
    # add a creation date
    ostr = ostr + pow_newline
    ostr = ostr + "# date created: \t" + str(datetime.date.today())
    ostr = ostr + pow_newline
    
    # Add the model_stub content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR +  "model_stub_part1.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    classname = string.capitalize(modelname)  
    baseclassname = "Base" + classname
    
    ostr += "import " + baseclassname + powlib.newline
    ostr += powlib.newline
    #ostr += "import " + baseclassname + powlib.linesep + powlib.linesep
    ostr += render_class(classname, baseclassname + "." + baseclassname)
    ostr += powlib.tab + "pass" + powlib.newline
        
    # write the output file to disk
    filename = classname + ".py"
    filename = os.path.normpath( prefix_path+ "/models/" + filename)
    file_exists = False
    if os.path.isfile( os.path.normpath(  filename) ):
        file_exists = True
    else:
        file_exists = False
    if file_exists and force != True:
        print filename + " (exists)...(Use -f to force override)"
    else:
        ofile = open(  filename , "w+") 
        print " --", filename + " (created)"
        ofile.write( ostr )
        ofile.close()
    ### genrate BaseModel if neccessary
    filename = "Base" + classname + ".py"
    
    ### generate the BaseClass
    infile = None
    infile = open (os.path.normpath( PARTS_DIR +  "basemodel_stub_part0.py"), "r")
    ostr = infile.read()
    infile.close()
    
    ostr += "class " + baseclassname +"(Base):" + powlib.newline
    infile = open (os.path.normpath( PARTS_DIR +  "basemodel_stub_part1.py"), "r")
    ostr += infile.read()
    infile.close()
    ostr += powlib.tab + "__table__ = Base.metadata.tables['" + powlib.plural(string.lower(modelname)) +"']" 
    ostr += powlib.newline
    infile = open (os.path.normpath( PARTS_DIR +  "basemodel_stub_part2.py"), "r")
    ostr += infile.read()
    infile.close()
    ### adding the properties list
    if properties == None:
        ostr += powlib.tab + "properties_list = []" + powlib.newline
    else:
        ostr += powlib.tab + "properties_list = " + properties  + powlib.newline
    ostr += powlib.tab + "modelname = '" + string.capitalize(modelname) + "'" + powlib.newline
    infile = open (os.path.normpath( PARTS_DIR +  "basemodel_stub_part3.py"), "r")
    ostr += infile.read()
    infile.close()
        
        
    filename = os.path.normpath( prefix_path + "/models/basemodels/" + filename)
    if os.path.isfile( os.path.normpath(  filename) ) and force != True:
        print filename + " (exists)...(Use -f to force override)"
    else:
        ofile = open(  filename , "w+") 
        print  " --", filename + " (created)"
        ofile.write( ostr )
        ofile.close()
        
    # check if App / BaseApp exist and repair if necessary
    #if os.path.isfile(os.path.normpath( "./models/basemodels/BaseApp.py")):
    #    #BasApp exists, ok.
    #    pass
    #else:
    #    # copy the BaseClass
    #    powlib.check_copy_file(os.path.normpath(  PARTS_DIR +  "BaseApp.py"), os.path.normpath( "./models/basemodels/"))
        
    #if os.path.isfile(os.path.normpath( "./models/powmodels/App.py")):
    #    #App exists, ok.
    #    pass
    #else:
    #    # copy the BaseClass
    #    powlib.check_copy_file(os.path.normpath( PARTS_DIR +  "App.py"), os.path.normpath( "./models/powmodels/"))
        
    render_test_stub(modelname, classname, prefix_path)
    return 
コード例 #4
0
def render_model(modelname = "NO_MODELNAME_GIVEN", 
                 force = False, 
                 comment="", 
                 prefix_path="./", 
                 properties=None, 
                 parts_dir= powlib.PARTS_DIR ):
    """
    Renders the generated Model Class in prefix_path/models.
    Renders the according BaseModel in prefix_path/models/basemodels.
    Renders a basic test in tests dierctory.
    Uses the stubs from stubs/partials.
    """
    print "generate_model: " + modelname
    # new model filename
    classname = string.capitalize(modelname)  
    baseclassname = "Base" + classname
    filename = classname + ".py"
    filename = os.path.normpath( prefix_path+ "/models/" + filename)
    if os.path.isfile( os.path.normpath( filename ) ) and force != True:
        print filename + " (exists)...(Use -f to force override)"
    else:
        infile = None
        infile = open (os.path.normpath( parts_dir +  "model_stub.part"), "r")
        ostr = ""
        ostr = ostr + infile.read()
        infile.close()
        
        ostr = ostr.replace("#DATE", str(datetime.date.today()) )
        ostr = ostr.replace("#MODELCLASS", classname)
        
        ostr = ostr.replace("#BASECLASS", baseclassname)
        ostr = ostr.replace( "#MODELTABLE",  powlib.plural(string.lower(modelname))  ) 
        
        # write the output file to disk
        ofile = open( filename , "w+") 
        print " --", filename + " (created)"
        ofile.write( ostr )
        ofile.close()
    
    ### generate BaseModel if neccessary
    filename = "Base" + classname + ".py"
    if os.path.isfile( os.path.normpath( filename ) ) and force != True:
        print filename + " (exists)...(Use -f to force override)"
    else:
        infile = None
        ### generate the BaseClass
        infile = open (os.path.normpath( PARTS_DIR +  "basemodel_stub.part"), "r")
        ostr = infile.read()
        infile.close()
        # Add Class declaration and Table relation for sqlalchemy
        ostr = ostr.replace( "#BASECLASSNAME",  baseclassname )
        ostr = ostr.replace( "#MODELTABLE",  powlib.plural(string.lower(modelname))  ) 
         
        ### adding the properties list
        # TODO: Needs to be tested. 
        if properties == None:
            ostr = ostr.replace("#PROPERTIES_LIST",  "[]")
        else:
            ostr = ostr.replace("#PROPERTIES_LIST",  properties )
            
        ostr = ostr.replace("#MODELNAME" , string.capitalize(modelname) )        
            
        filename = os.path.normpath( prefix_path + "/models/basemodels/" + filename)
    
        ofile = open(  filename , "w+") 
        print  " --", filename + " (created)"
        ofile.write( ostr )
        ofile.close()
        
    # render a basic testcase 
    render_test_stub(modelname, classname, prefix_path, PARTS_DIR)
    return 
コード例 #5
0
def render_migration(name, model, comment):
    # 
    
    #print "generate_migration: " + name + "  for model: " + model
    
    # add the auto generated warning to the outputfile
    infile = open (os.path.normpath(PARTS_DIR + "/can_be_edited.txt"), "r")
    ostr = infile.read()
    infile.close()
    
    # add a creation date
    ostr = ostr + os.linesep
    ostr = ostr + "# date created: \t" + str(datetime.date.today())
    ostr = ostr + os.linesep
    
    # Add the model_stub part1 content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part1.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    
    pluralname = powlib.plural(model)
    ostr += powlib.tab +  "table_name=\"" + pluralname + "\""
    ostr += powlib.linesep
    #print "modelname was: " + model + "  pluralized table_name is:" + pluralname
    
    # Add the model_stub part2 content to the newly generated file. 
    infile = open (os.path.normpath( PARTS_DIR + "db_migration_stub2_part2.py"), "r")
    ostr = ostr + infile.read()
    infile.close()
    
    #ostr += powlib.tab + powlib.tab + powlib.tab +  "Column('id', Integer, Sequence('" + model +"_id_seq'), primary_key=True),"
    #ostr += powlib.newline
    
    app = powlib.load_class( "Appinfo", "Appinfo")
    app_versions = powlib.load_class( "Version", "Version")
    sess = app.pbo.getSession()
    app = sess.query(Appinfo.Appinfo).first()
    
    version = app.maxversion
    oldmaxversion = version
    version += 1
    
    verstring = powlib.version_to_string(version)
    print "generate_migration: " + name + " for model: " + model
    #print "version: " + str(version)
    #print "version2string: " + verstring
    filename = os.path.normpath ( "./migrations/" + verstring +"_" + name +".py" )
    
    #update the app table with the new version
    #appTable.update().values(maxversion= str(version) ).execute()
    app.maxversion = str(version)
    app.update()
    app_versions.filename = str(verstring +"_" + name )
    app_versions.version = str(version)
    app_versions.comment = str(comment)
    app_versions.update()
    print " -- maxversion (old,new): (" + str(oldmaxversion) + "," + str(app.maxversion) +")"
    ofile = open(  filename , "w+") 
    print  " -- created file:" + str(filename)
    ofile.write( ostr )
    ofile.close()
    return