Beispiel #1
0
def installer(request, pk):
    
    buf = []
    
    try:
        pki = int(pk)
        prj = models.SChAppSet.objects.get(id=pki)
        name = prj.name
    except:
        name = pk
    
    base_path = os.path.join(settings.PRJ_PATH, name)
    zip_path = os.path.join(settings.DATA_PATH, 'temp')
    
    
    buf.append("COMPILE TEMPLETE FILES:")
    
    (code, output, err) = py_run([os.path.join(base_path, 'manage.py'), 'compile_templates'])
    
    if output:
        for pos in output:
            buf.append(pos)
    
    if err:
        buf.append("ERRORS:")
        for pos in err:
            buf.append(pos)
    
    exclude=[".*\.pyc", ".*__pycache__.*"]
    zip = ZipWriter(os.path.join(zip_path, name+".ptig"), base_path, exclude=exclude)
    zip.toZip(base_path)
    
    buf.append("PACK PROGRAM FILES TO: " + zip_path + name + ".ptig")
    
    db_name = os.path.join(os.path.join(settings.DATA_PATH, name), name+".db")
    
    buf.append("ADDING DATABASE FILES")
    
    (code, output, err) = py_run([os.path.join(base_path, 'manage.py'), 'export_to_local_db'])
    
    buf.append("Export to local db:")
    if output:
        for pos in output:
            buf.append(pos)
    
    if err:
        buf.append("ERRORS:")
        for pos in err:
            buf.append(pos)
            
    zip.write(db_name, name_in_zip=name+".db")
    zip.close()
    
    buf.append("END")
    
    url = reverse('start')+'schbuilder/download_installer/'+name+'/'
    
    return { 'object_list': buf, 'name': name, 'url': url, 'tp': "SChAppSet" }
Beispiel #2
0
 def process(self, request, queryset=None):
 
     install_file = request.FILES['install_file']
     name = install_file.name.split('.')[0]
     zip_file = zipfile.ZipFile(install_file.file)
     ret = extract_ptig(zip_file, name)
     
     extract_to = os.path.join(settings.PRJ_PATH, name)
     (ret_code, output, err) = py_run([os.path.join(extract_to, 'manage.py'), 'post_installation'])
     
     
     return { "object_list": ret }
Beispiel #3
0
def make(data_path, files_path):
    if platform_name() == "Emscripten":
        return None, None, None
    ret_output = []
    ret_errors = []
    ret = 0

    p = Path(files_path)
    fl = p.glob("**/*.pyx")
    for pos in fl:
        pyx_filename = p.joinpath(pos).as_posix()
        c_filename = pyx_filename.replace(".pyx", ".c")
        (ret_code, output, err) = py_run(["-m", "cython", "-3", pyx_filename])
        if ret_code:
            ret = ret_code
        if output:
            for pos2 in output:
                ret_output.append(pos2)
        if err:
            for pos2 in err:
                ret_errors.append(pos2)
        if os.path.exists(c_filename):
            (ret_code, output, err) = compile(data_path, c_filename, pyd=True)
            if ret_code:
                ret = ret_code
            os.unlink(c_filename)
            if output:
                for pos2 in output:
                    ret_output.append(pos2)
            if err:
                for pos2 in err:
                    ret_errors.append(pos2)
    fl = p.glob("**/*.c")
    for pos in fl:
        c_filename = p.joinpath(pos).as_posix()
        if os.path.exists(c_filename):
            (ret_code, output, err) = compile(data_path, c_filename, pyd=False)
            if ret_code:
                ret = ret_code
            if output:
                for pos2 in output:
                    ret_output.append(pos2)
            if err:
                for pos2 in err:
                    ret_errors.append(pos2)

    return ret, ret_output, ret_errors
Beispiel #4
0
    def extract_ptig(self):
        import pytigon.schserw.settings
        from django.conf import settings

        if hasattr(pytigon.schserw.settings, "_PRJ_PATH_ALT"):
            base_path = os.path.join(pytigon.schserw.settings._PRJ_PATH_ALT,
                                     self.prj_name)
        else:
            base_path = os.path.join(settings.PRJ_PATH_ALT, self.prj_name)

        ret = []
        ret.append("Install file: " + self.prj_name)
        test_update = True

        extract_to = os.path.join(base_path, self.prj_name)
        ret.append("install to: " + extract_to)

        if not os.path.exists(base_path):
            os.mkdir(settings.PRJ_PATH)
        if not os.path.exists(extract_to):
            os.mkdir(extract_to)
            test_update = False

        self.extract_to = extract_to

        zipname = (datetime.datetime.now().isoformat("_")[:19].replace(
            ":", "").replace("-", ""))
        zipname2 = os.path.join(extract_to, zipname + ".zip")
        if test_update:
            backup_zip = zipfile.ZipFile(zipname2, "a")
            exclude = [
                ".*settings_local.py.*",
            ]
        else:
            backup_zip = None
            exclude = None

        extractall(
            self.archive,
            base_path,
            backup_zip=backup_zip,
            exclude=exclude,
            only_path=self.prj_name + "/",
            backup_exts=[
                "py",
                "txt",
                "wsgi",
                "asgi",
                "ihtml",
                "htlm",
                "css",
                "js",
                "prj",
            ],
        )

        if backup_zip:
            backup_zip.close()

        src_db = self.get_db()
        if src_db:
            ret.append("Synchronize database:")
            dest_path_db = os.path.join(settings.DATA_PATH, self.prj_name)
            dest_db = os.path.join(dest_path_db, self.prj_name + ".db")

            if not os.path.exists(settings.DATA_PATH):
                os.mkdir(settings.DATA_PATH)
            if not os.path.exists(dest_path_db):
                os.mkdir(dest_path_db)
            if not os.path.exists(dest_db):
                with open(dest_path_db, "wb") as f:
                    f.write(src_db)

            (ret_code, output, err) = py_run(
                [os.path.join(extract_to, "manage.py"), "postinstallation"])

            if output:
                for pos in output:
                    ret.append(pos)
            if err:
                ret.append("ERRORS:")
                for pos in err:
                    ret.append(pos)
        return ret
Beispiel #5
0
def gen(request, pk):
    
    prj = models.SChAppSet.objects.get(id=pk)
    root_path = settings.ROOT_PATH
    base_path = os.path.join(settings.PRJ_PATH, prj.name)
    src_path = os.path.join(settings.PRJ_PATH, "schdevtools")
    object_list = []
    gmt = time.gmtime()
    gmt_str = "%04d.%02d.%02d %02d:%02d:%02d" % (gmt[0], gmt[1], gmt[2], gmt[3], gmt[4], gmt[5])
    
    if not os.path.exists(base_path):
        object_list.append((datetime.datetime.now().time().isoformat(), 'mkdir:', base_path))
        os.makedirs(base_path, exist_ok=True)
        os.makedirs(base_path+"/templates", exist_ok=True)
        os.makedirs(base_path+"/templates/template", exist_ok=True)
        os.makedirs(base_path+"/templates_src", exist_ok=True)
        os.makedirs(base_path+"/templates_src/template", exist_ok=True)
        os.makedirs(base_path+"/apache", exist_ok=True)
    
    apps = prj.schapp_set.all()
    
    #template_to_file(base_path, "license", "LICENSE.txt", {'prj': prj})
    #template_to_file(base_path, "readme", "README.txt",  {'prj': prj})
    with open(os.path.join(base_path, "README.txt"), "wt") as f:
        if prj.readme_file:
            f.write(prj.readme_file)
    with open(os.path.join(base_path, "LICENSE.txt"), "wt") as f:
        if prj.license_file:
            f.write(prj.license_file)
    with open(os.path.join(base_path, "install.ini"), "wt") as f:
        f.write("[DEFAULT]\nPRJ_NAME=%s\n" % prj.name)
        f.write("GEN_TIME='%s'\n\n" % gmt_str)
        if prj.install_file:
            f.write(prj.install_file)
    if prj.app_main:
        with open(os.path.join(base_path, "prj_main.py"), "wt") as f:
            f.write(prj.app_main)
    
    template_to_file(base_path, "manage", "manage.py",  {'prj': prj})
    template_to_file(base_path, "init", "__init__.py",  {'prj': prj})
    template_to_file(base_path, "wsgi", "wsgi.py",  {'prj': prj, 'base_path': base_path.replace('\\','/')})
    template_to_file(base_path, "asgi", "asgi.py",  {'prj': prj, 'base_path': base_path.replace('\\','/')})
    
    app_names = []
    for app in apps:
        object_list.append((datetime.datetime.now().time().isoformat(), 'create app:', app.name))
        if not os.path.exists(base_path+"/"+app.name):
            os.makedirs(base_path+"/"+app.name, exist_ok=True)
        if not os.path.exists(base_path+"/templates_src/"+app.name):
            os.makedirs(base_path+"/templates_src/"+app.name, exist_ok=True)
        if not os.path.exists(base_path+"/templates/"+app.name):
            os.makedirs(base_path+"/templates/"+app.name, exist_ok=True)
    
        app_names.append(app.name)
    
        tables = app.schtable_set.all()
        choices = app.schchoice_set.all()
        templates = app.schtemplate_set.all()
    
        is_tree_table = False
        gfields = []
        for table in tables:
            object_list.append((datetime.datetime.now().time().isoformat(), 'create tab:', table.name))
            table.tree_table = 0
            for field in table.schfield_set.filter(type__in=['GForeignKey','GManyToManyField', 'GHiddenForeignKey', 'GTreeForeignKey', 'GHiddenTreeForeignKey']):
                if field.type in ('GTreeForeignKey', 'GHiddenTreeForeignKey'):
                    is_tree_table = True
                    if table.base_table in (None, "", "models.Model") and not table.proxy_model:
                        table.base_table = 'TreeModel'
                        table.tree_tab = 1
                    else: 
                        table.tree_tab = 2
                gfields.append(field)
    
        template_to_file(base_path, "models", app.name+"/models.py",  {'tables': tables, 'app': app, 'prj': prj, 'choices': choices, 'is_tree_table': is_tree_table })
    
        views = app.schview_set.all()
        forms = app.schform_set.all()
        tasks = app.schtask_set.all()
        consumers = app.schchannelconsumer_set.all()
        template_to_file(base_path, "views", app.name+"/views.py",  {'views': views, 'forms': forms, 'app': app})
        template_to_file(base_path, "urls",  app.name+"/urls.py",  {'views': views, 'templates': templates, 'tables': tables, 'forms': forms, 'app': app, 'gfields': gfields})
        template_to_file(base_path, "tasks", app.name+"/tasks.py",  {'tasks': tasks, 'app': app})
        template_to_file(base_path, "consumers", app.name+"/consumers.py",  {'consumers': consumers, 'app': app})
    
        for template in templates:
            str_to_file(base_path, template.template_code, "templates_src/"+app.name+"/"+template.name.lower().replace(' ','_')+".ihtml")
    
        appmenus = app.schappmenu_set.all()
        
        if app.user_param:
            user_param = str(dict([ pos.split('=') for pos in app.user_param.split('\n') if pos and '=' in pos ]))
        else:
            user_param = '{}'
        
        template_to_file(base_path, "app_init", app.name+"/__init__.py",  {'appmenus': appmenus, 'app': app, 'user_param': user_param})
    
        for file_obj in app.schfiles_set.all():
            if file_obj.file_type=='f':
                file_name = base_path + "/" + app.name+"/templatetags/"+ file_obj.name + ".py"
            elif file_obj.file_type=='t':
                file_name = base_path + "/" + app.name+"/templatetags/"+ file_obj.name + ".py"
            elif file_obj.file_type=='c':
                file_name = base_path + "/" + app.name+"/"+ file_obj.name
            elif file_obj.file_type=='m':
                f = open_and_create_dir(base_path + "/" + app.name+"/management/__init__.py", "wb")
                f.close()
                f = open_and_create_dir(base_path + "/" + app.name+"/management/commands/__init__.py","wb")
                f.close()
                file_name = base_path + "/" + app.name+"/management/commands/"+ file_obj.name
            elif file_obj.file_type=='p':
                if '/' in file_obj.name:
                    x = file_obj.name.split('/')
                    plugin_name = x[0]
                    file_name = x[1]
                else:
                    plugin_name = file_obj.name
                    file_name = '__init__'
                file_name = base_path + "/plugins/" + app.name + "/" + plugin_name + "/" + file_name + ".py"
            elif file_obj.file_type=='i':
                if '/' in file_obj.name:
                    x = file_obj.name.split('/')
                    plugin_name = x[0]
                    file_name = x[1]
                else:
                    plugin_name = file_obj.name
                    file_name = 'index'
                file_name = base_path + "/plugins/" + app.name + "/" + plugin_name + "/" + file_name + ".html"
                content =  ihtml_to_html(None, file_obj.content)
                f = open_and_create_dir(file_name,"wb")
                f.write(content.encode('utf-8'))
                f.close()
                file_name = None
            elif file_obj.file_type in ('l', 'x', 'C'):
                f = open_and_create_dir(base_path + "/applib/__init__.py", "wb")
                f.close()
                f = open_and_create_dir(base_path + "/applib/" + app.name + "lib/__init__.py", "wb")
                f.close()
                file_name = base_path + "/applib/" + app.name + "lib/"+ file_obj.name 
                if file_obj.file_type == 'l':
                    file_name += ".py"
                elif file_obj.file_type == 'x':
                    file_name += ".pyx"
                else:
                    file_name += '.c'
            else: 
                file_name = None
                
            if file_name:
                f = open_and_create_dir(file_name,"wb")
                if type(file_obj.content)==str:
                    f.write(file_obj.content.encode('utf-8'))
                else:
                    f.write(file_obj.content)
                f.close()
            
    
    template_to_file(base_path, "apps", "apps.py",  {'prj': prj, 'app_names': app_names })
    
    static_files = prj.schstatic_set.all()
    
    if settings.STATIC_APP_ROOT:
        static_root = os.path.join(settings.STATIC_APP_ROOT, prj.name)
    else:
        if settings.STATIC_ROOT:
            static_root = os.path.join(os.path.join(settings.STATIC_ROOT, 'app'),prj.name)
        else:
            static_root = os.path.join(os.path.join(settings.STATICFILES_DIRS[0], 'app'),prj.name)
    
    static_scripts = os.path.join(static_root,'js')
    static_style = os.path.join(static_root,'css')
    static_components = os.path.join(static_root,'components')
    
    offline_support = False
    
    for static_file in static_files:
        txt = static_file.code
        typ = static_file.type
        dest_path = None
        if static_file.name=='sw.js':
            offline_support = True
            
        if static_file.type=='U':
            dest_path = os.path.join(static_root,static_file.name)
            if '.pyj' in static_file.name:
                dest_path = os.path.join(static_root,static_file.name.replace('.pyj', '.js'))
                typ = 'P'
            elif '.sass' in static_file.name:
                dest_path = os.path.join(static_root,static_file.name.replace('.sass', '.css'))
                typ = 'I'
            elif '.vue' in static_file.name:
                dest_path = os.path.join(static_root,static_file.name.replace('.vue', '.js'))
                typ = 'R'
                        
        if typ=='C':
            t = Template(txt)
            txt2 = t.render(Context({'prj': prj} ))
            f = open_and_create_dir(os.path.join(static_style, static_file.name+".css"),"wb")
            f.write(txt2.encode('utf-8'))
            f.close()        
        if typ=='J':
            t = Template(txt)
            txt2 = t.render(Context({'prj': prj} ))
            f = open_and_create_dir(os.path.join(static_scripts,static_file.name+".js"),"wb")
            f.write(txt2.encode('utf-8'))
            f.close()        
        if typ=='P':
            t = Template(txt)
            txt2 = t.render(Context({'prj': prj} ))
            try:
                codejs = pytigon_lib.schindent.indent_style.py_to_js(txt2, None)
                codejs = codejs.split("__pragma__ ('<all>')",1)[0]
            except:
                codejs = ""
            f = open_and_create_dir(dest_path if dest_path else os.path.join(static_scripts,static_file.name+".js"),"wb")
            f.write(codejs.encode('utf-8'))
            f.close()        
        if typ=='R':
            try:
                codejs = pytigon_lib.schindent.indent_style.py_to_js(txt, None)
                codejs = codejs.split("__pragma__ ('<all>')",1)[0]
            except:
                codejs = ""
            f = open_and_create_dir(dest_path if dest_path else os.path.join(static_components, static_file.name+'.js'),"wb")
            f.write(codejs.encode('utf-8'))
            f.close()        
        if typ=='I':
            if sass:
                buf = sass.compile(string=txt, indented=True,)
                t = Template(buf)
                txt2 = t.render(Context({'prj': prj} ))
                f = open_and_create_dir(dest_path if dest_path else os.path.join(static_style,static_file.name+".css"),"wb")
                f.write(txt2.encode('utf-8'))
                f.close()        
        if typ=='U':
            t = Template(txt)
            txt2 = t.render(Context({'prj': prj} ))
            f = open_and_create_dir(dest_path,"wb")
            f.write(txt2.encode('utf-8'))
            f.close()        
    
    component_elements = []
    
    if prj.custom_tags:
        component_elements += [ 'app/'+pos.split('/')[0] + '/components/' + pos.split('/')[1] for pos in prj.custom_tags.replace('\n',';').replace('\r','').split(';') if pos and '/' in pos ]    
    component_elements += [ 'app/' + prj.name + "/components/" + pos.name for pos in static_files if pos.type in ('R',) ]
    
    js_static_files = [ pos for pos in static_files if pos.type in ('J', 'P') ]
    css_static_files = [ pos for pos in static_files if pos.type in ('C', 'I') ]
    
    
    static_for_ext_apps = []
    
    if prj.ext_apps:
        prj_tab = []
        tab = prj.get_ext_apps()
        for pos in tab:
            if pos:
                x = pos.split('.')[0]
                if not x in prj_tab:
                    prj_tab.append(x)
        
        #for pos in prj.ext_apps.split(','):
        #    if pos:
        #        x = pos.split('.')[0]
        #        if not x in prj_tab:
        #            prj_tab.append(x)
        for pos in prj_tab:
            try:
                prj2 = models.SChAppSet.objects.get(name=pos)
            except:
                prj2 = None
            if prj2:
                static_files2 = prj2.schstatic_set.all()
                js_static_files2 = [ pos2 for pos2 in static_files2 if pos2.type in ('J', 'P') ]
                css_static_files2 = [ pos2 for pos2 in static_files2 if pos2.type in ('C', 'I') ]
                static_for_ext_apps.append((pos, js_static_files2, css_static_files2))
    
                if prj2.custom_tags:
                    component_elements += [ 'app/'+pos.split('/')[0] + '/components/' + pos.split('/')[1] for pos in prj2.custom_tags.replace('\n',';').replace('\r','').split(';') if pos and '/' in pos ]    
                component_elements += [ 'app/' + prj2.name + "/components/" + pos.name for pos in static_files2 if pos.type in ('R',) ]
    
    template_to_file(base_path, "desktop", "templates_src/template/desktop.ihtml",  {'prj': prj, 'js_static_files': set(js_static_files), 'css_static_files': set(css_static_files), 'static_for_ext_apps': static_for_ext_apps, 'component_elements': set(component_elements) })
    
    
    #print(component_elements)
    #template_to_i_file(base_path, src_path+"templates_src/schbuilder/wzr/schweb.ihtml","templates_src/template/schweb.ihtml",  {'prj': prj, 'component_elements': component_elements })
    
    template_to_file(base_path, "schweb", "templates_src/template/schweb.ihtml",  {'prj': prj, 'component_elements': component_elements })
    
    consumers_tab = []
    for _app in apps:
        consumers = _app.schchannelconsumer_set.all()
        for consumer in consumers:
            consumers_tab.append((app.name+"/"+consumer.url+"/socket.io/", _app.name+".consumers."+consumer.name))
    
    for pos in prj.get_ext_apps():
        if pos:
            x = pos.split('.')
            tab1 = models.SChAppSet.objects.filter(name=x[0])
            for _prj in tab1:
                tab2 = _prj.schapp_set.filter(name=x[1])
                for _app in tab2:
                    consumers = _app.schchannelconsumer_set.all()
                    for consumer in consumers:
                        consumers_tab.append((_app.name+"/"+consumer.url+"/socket.io/", _app.name+".consumers."+consumer.name))
                
    template_to_file(base_path, "settings_app", "settings_app.py",  {'prj': prj, 'gmtime': gmt_str, 'offline_support': offline_support, 'consumers': consumers_tab })
    
    base_path_src = base_path + "/src"
    
    if os.path.exists(base_path_src):
        copy_files_and_dirs(base_path_src, base_path)
    
    file_name = None
    file_content = []
    file_append = -1
    
    def output(file_name, file_append, file_content):
        os.makedirs(os.path.dirname(os.path.join(base_path, file_name)), exist_ok=True)
        if file_append==-1:        
            f = open(os.path.join(base_path, file_name), "wb")
            f.write(("\n".join(file_content)).encode('utf-8'))
            f.close()
        elif file_append==-2:
            f = open(os.path.join(base_path, file_name), "ab")
            f.write(("\n".join(file_content)).encode('utf-8'))
            f.close()
        else:
            f = open(os.path.join(base_path, file_name), "rb")
            txt = f.read().decode('utf-8').split('\n')
            f.close()
            if file_append < len(txt):
                txt = txt[:file_append] + file_content+txt[file_append:]
            else:
                txt = txt+file_content
            f = open(os.path.join(base_path, file_name), "wb")
            f.write(("\n".join(txt)).encode('utf-8'))
            f.close()
            
    if prj.user_app_template and len(prj.user_app_template)>0:
        txt = prj.user_app_template
        tab = txt.split('\n')
        for row in tab:
            if row.startswith('###'):
                if file_name and len(file_content)>0:
                    output(file_name, file_append, file_content)
                file_content = []
                file_name = row[3:]
                if file_name.startswith('>'):
                    file_name = file_name[1:].strip()
                    file_append = -2
                elif file_name.startswith('<') and '>' in file_name:
                    f = file_name[1:].split('>')
                    file_name = f[1].strip()
                    file_append = int(f[0])                
                else:
                    file_name = file_name.strip()
                    file_append = -1
            else:
                file_content.append(row)
                    
        if file_name and len(file_content)>0:
            output(file_name, file_append, file_content)
    
    if prj.encoded_zip:
        bcontent = base64.decodebytes(prj.encoded_zip.encode('utf-8'))
        bstream = io.BytesIO(bcontent)
        with zipfile.ZipFile(bstream, "r") as izip:
            izip.extractall(base_path)
    
    success = True
    
    if platform_name()!='Android' and prj.install_file:
        init_str = "[DEFAULT]\n"+prj.install_file
        config = configparser.ConfigParser(allow_no_value=True)
        config.read_string(init_str)
        pip_str = config['DEFAULT']['pip']
        if pip_str:
            applib_path  = os.path.join(base_path, "applib")
            if not os.path.exists(applib_path):
                os.mkdir(applib_path)
            packages = [ x.strip() for x in pip_str.split(' ') if x ]
            exit_code, output_tab, err_tab = py_run(['-m', 'pip', 'install', f'--target={applib_path}', '--upgrade', ] + packages)
            if output_tab:
                for pos in output_tab:
                    if pos:
                        object_list.append((datetime.datetime.now().time().isoformat(), "pip info", pos))
            if err_tab:
                for pos in err_tab:
                    if pos:
                        object_list.append((datetime.datetime.now().time().isoformat(), "pip error", pos))
                        success = False
    if success:    
        object_list.append((datetime.datetime.now().time().isoformat(), 'SUCCESS:', ""))    
    else:
        object_list.append((datetime.datetime.now().time().isoformat(), 'ERRORS:', ""))    
    
    (exit_code, output_tab, err_tab) = post_install(root_path, base_path)
    if output_tab:
        for pos in output_tab:
            if pos:
                object_list.append((datetime.datetime.now().time().isoformat(), "compile info", pos))
    if err_tab:
        for pos in err_tab:
            if pos:
                object_list.append((datetime.datetime.now().time().isoformat(), "compile error", pos))
                success = False
        
    return { 'object_list': reversed(object_list) }
Beispiel #6
0
def translate_sync(request, pk):
    
    locale_obj = models.SChLocale.objects.get(id=pk)
    prj = locale_obj.parent
    
    base_path = settings.PRJ_PATH
    
    app_path = os.path.join(base_path, prj.name)
    locale_path = os.path.join(app_path, 'locale')
    lang_path = os.path.join(locale_path, locale_obj.name)
    msg_path = os.path.join(lang_path, 'LC_MESSAGES')
    po_path = os.path.join(msg_path, 'django.po')
    
    if not os.path.exists(po_path):
        if not os.path.isdir(locale_path):
            os.mkdir(locale_path)
        if not os.path.isdir(lang_path):
            os.mkdir(lang_path)
        if not os.path.isdir(msg_path):
            os.mkdir(msg_path)
        
        po_init = """#\nmsgid ""\nmsgstr ""\n"Project-Id-Version: pytigon\\n"\n"Language: %s\\n"\n"MIME-Version: 1.0\\n"\n"Content-Type: text/plain; charset=UTF-8\\n"\n"Content-Transfer-Encoding: 8bit\\n"\n"""
        if locale_obj.name in locale.locale_alias:
            locale_str = locale.locale_alias[locale_obj.name].split('.')[0]
        else:
            locale_str = locale_obj.name
        
        po_init2 = po_init % locale_str
        with open(po_path, "wt") as f:
            f.write(po_init2)
        
    
    (code, output, err) = py_run([os.path.join(app_path, 'manage.py'), 'compile_templates'])
    locale_gen_internal(prj.id)
        
    po = polib.pofile(po_path)
    
    locale_obj.schtranslate_set.update(status='#')
    
    inserted = 0
    updated = 0
    save = False
    
    for entry in po:
        print(entry.msgid, entry.msgstr, entry.msgctxt)
        t = locale_obj.schtranslate_set.filter(description=entry.msgid)
        if len(t)>0:
            obj = t[0]
            updated += 1
            if obj.translation:
                entry.msgstr =  obj.translation
                save = True
        else:
            obj = models.SChTranslate()
            obj.description = entry.msgid
            obj.parent = locale_obj
            obj.translation = entry.msgstr
            inserted += 1    
        if obj.translation:
            obj.status = 'OK'
        else:
            obj.status = ''
        obj.save()
    
    if save:
        po.save(po_path)
    
    locale_gen_internal(prj.id)
    
    return { 'object_list': [[ updated, inserted ],] }