def achieveProdClf(prodClfPath, achieveName): achievePath = param.clf_ACHI + achieveName zipDir(achievePath, prodClfPath) emptyDir(prodClfPath) if not isEmpty(prodClfPath): print 'remove error' raise Exception('remove production clf failed, deploy abort')
def setupClfUpload(clfPath, clfUUID): uploadPath = getClfUploadPath(clfUUID) if os.path.exists(uploadPath): os.remove(uploadPath) zipDir(zipname=uploadPath, dir_to_zip=clfPath) return uploadPath
def packWGT(build_json=None, app_src=None, app_dest=None, app_name=None): BUILD_PARAMETERS = varshop.getValue("BUILD_PARAMETERS") BUILD_ROOT = varshop.getValue("BUILD_ROOT") DEFAULT_CMD_TIMEOUT= varshop.getValue("DEFAULT_CMD_TIMEOUT") if not utils.zipDir(app_src, os.path.join(app_dest, "%s.wgt" % app_name)): return False if BUILD_PARAMETERS.signature == True: if utils.safelyGetValue(build_json, "sign-flag") == "true": if not os.path.exists(os.path.join(BUILD_ROOT, "signing")): if not utils.doCopy( os.path.join(BUILD_PARAMETERS.pkgpacktools, "signing"), os.path.join(BUILD_ROOT, "signing")): return False signing_cmd = "%s --dist platform %s" % ( os.path.join(BUILD_ROOT, "signing", "sign-widget.sh"), os.path.join(app_dest, "%s.wgt" % app_name)) if not utils.doCMD(signing_cmd, DEFAULT_CMD_TIMEOUT): return False return True
def main(): global LOG LOG = utils.getLogger("pack-tool") try: usage = "Usage: ./pack.py -t apk -m shared -a x86" opts_parser = OptionParser(usage=usage) opts_parser.add_option("-c", "--cfg", dest="pkgcfg", help="specify the path of config json file") opts_parser.add_option( "-t", "--type", dest="pkgtype", help="specify the pkg type, e.g. apk, cordova ...") opts_parser.add_option( "-m", "--mode", dest="pkgmode", help= "specify the apk mode, not for embeddingapi, e.g. shared, embedded" ) opts_parser.add_option( "-a", "--arch", dest="pkgarch", help= "specify the apk arch, not for embeddingapi, cordova version 3.6, e.g. x86, arm" ) opts_parser.add_option( "-d", "--dest", dest="destdir", help="specify the installation folder for packed package") opts_parser.add_option( "-s", "--src", dest="srcdir", help="specify the path of pkg resource for packing") opts_parser.add_option("--tools", dest="pkgpacktools", help="specify the parent folder of pack tools") opts_parser.add_option( "--notclean", dest="bnotclean", action="store_true", help="disable the build root clean after the packing") opts_parser.add_option("-v", "--version", dest="bversion", action="store_true", help="show this tool's version") opts_parser.add_option("-u", "--user", dest="user", help="specify the user in inst.py") opts_parser.add_option( "--sub-version", dest="subversion", help= "specify the embeddingapi, cordova sub version, e.g. v1, v2, v3 ..." ) opts_parser.add_option("--pkg-version", dest="pkgversion", help="specify the pkg version, e.g. 0.0.0.1") opts_parser.add_option( "--pack-type", dest="packtype", help="specify the pack type, e.g. gradle, maven") opts_parser.add_option("--notdebug", dest="bnotdebug", action="store_true", help="specify the packed pkg is not debug mode") if len(sys.argv) == 1: sys.argv.append("-h") global BUILD_PARAMETERS (BUILD_PARAMETERS, args) = opts_parser.parse_args() except Exception as e: LOG.error("Got wrong options: %s, exit ..." % e) sys.exit(1) if BUILD_PARAMETERS.bversion: print "Version: %s" % TOOL_VERSION sys.exit(0) if not BUILD_PARAMETERS.srcdir: BUILD_PARAMETERS.srcdir = os.getcwd() BUILD_PARAMETERS.srcdir = os.path.expanduser(BUILD_PARAMETERS.srcdir) if not BUILD_PARAMETERS.user: BUILD_PARAMETERS.user = "******" if not os.path.exists( os.path.join(BUILD_PARAMETERS.srcdir, "..", "..", VERSION_FILE)): if not os.path.exists( os.path.join(BUILD_PARAMETERS.srcdir, "..", VERSION_FILE)): if not os.path.exists( os.path.join(BUILD_PARAMETERS.srcdir, VERSION_FILE)): LOG.info( "Not found pkg version file, try to use option --pkg-version" ) pkg_version_file_path = None else: pkg_version_file_path = os.path.join(BUILD_PARAMETERS.srcdir, VERSION_FILE) else: pkg_version_file_path = os.path.join(BUILD_PARAMETERS.srcdir, "..", VERSION_FILE) else: pkg_version_file_path = os.path.join(BUILD_PARAMETERS.srcdir, "..", "..", VERSION_FILE) try: pkg_main_version = 0 pkg_release_version = 1 if BUILD_PARAMETERS.pkgversion: LOG.info("Using %s as pkg version " % BUILD_PARAMETERS.pkgversion) pkg_main_version = BUILD_PARAMETERS.pkgversion CROSSWALK_BRANCH = "master" else: if pkg_version_file_path is not None: LOG.info("Using pkg version file: %s" % pkg_version_file_path) with open(pkg_version_file_path, "rt") as pkg_version_file: pkg_version_raw = pkg_version_file.read() pkg_version_file.close() pkg_version_json = json.loads(pkg_version_raw) pkg_main_version = pkg_version_json["main-version"] pkg_release_version = pkg_version_json["release-version"] CROSSWALK_BRANCH = pkg_version_json["crosswalk-branch"] except Exception as e: LOG.error("Fail to read pkg version file: %s, exit ..." % e) sys.exit(1) CROSSWALK_VERSION = pkg_main_version if not BUILD_PARAMETERS.pkgtype: LOG.error("No pkg type provided, exit ...") sys.exit(1) elif not BUILD_PARAMETERS.pkgtype in PKG_TYPES: LOG.error("Wrong pkg type, only support: %s, exit ..." % PKG_TYPES) sys.exit(1) if BUILD_PARAMETERS.pkgtype == "apk" or \ BUILD_PARAMETERS.pkgtype == "apk-aio": if not BUILD_PARAMETERS.pkgmode: LOG.error("No pkg mode option provided, exit ...") sys.exit(1) elif not BUILD_PARAMETERS.pkgmode in PKG_MODES: LOG.error( "Wrong pkg mode option provided, only support:%s, exit ..." % PKG_MODES) sys.exit(1) if not BUILD_PARAMETERS.pkgarch: LOG.error("No pkg arch option provided, exit ...") sys.exit(1) elif not BUILD_PARAMETERS.pkgarch in PKG_ARCHS: LOG.error( "Wrong pkg arch option provided, only support:%s, exit ..." % PKG_ARCHS) sys.exit(1) if BUILD_PARAMETERS.pkgtype == "apk-aio" or \ BUILD_PARAMETERS.pkgtype == "cordova-aio": if not BUILD_PARAMETERS.destdir or not os.path.exists( BUILD_PARAMETERS.destdir): LOG.error("No all-in-one installation dest dir found, exit ...") sys.exit(1) elif not BUILD_PARAMETERS.destdir: BUILD_PARAMETERS.destdir = BUILD_PARAMETERS.srcdir BUILD_PARAMETERS.destdir = os.path.expanduser(BUILD_PARAMETERS.destdir) if not BUILD_PARAMETERS.pkgpacktools: BUILD_PARAMETERS.pkgpacktools = os.path.join(BUILD_PARAMETERS.srcdir, "..", "..", "tools") BUILD_PARAMETERS.pkgpacktools = os.path.expanduser( BUILD_PARAMETERS.pkgpacktools) config_json = None if BUILD_PARAMETERS.pkgcfg: config_json_file_path = BUILD_PARAMETERS.pkgcfg else: config_json_file_path = os.path.join(BUILD_PARAMETERS.srcdir, "suite.json") try: LOG.info("Using config json file: %s" % config_json_file_path) with open(config_json_file_path, "rt") as config_json_file: config_raw = config_json_file.read() config_json_file.close() config_json = json.loads(config_raw) except Exception as e: LOG.error("Fail to read config json file: %s, exit ..." % e) sys.exit(1) global PKG_NAME PKG_NAME = utils.safelyGetValue(config_json, "pkg-name") if not PKG_NAME: PKG_NAME = os.path.basename(BUILD_PARAMETERS.srcdir) LOG.warning("Fail to read pkg name from json, " "using src dir name as pkg name ...") LOG.info("================= %s (%s-%s) ================" % (PKG_NAME, pkg_main_version, pkg_release_version)) if not utils.safelyGetValue(config_json, "pkg-list"): LOG.error("Fail to read pkg-list, exit ...") sys.exit(1) pkg_json = None global parameters_type parameters_type = None cordova_subv_list = ['4.x', '3.6'] if BUILD_PARAMETERS.pkgtype == "cordova" or BUILD_PARAMETERS.pkgtype == "cordova-aio": if BUILD_PARAMETERS.pkgarch and not BUILD_PARAMETERS.pkgarch in PKG_ARCHS: LOG.error("Wrong pkg-arch, only support: %s, exit ..." % PKG_ARCHS) sys.exit(1) if BUILD_PARAMETERS.pkgmode and not BUILD_PARAMETERS.pkgmode in PKG_MODES: LOG.error("Wrong pkg-mode, only support: %s, exit ..." % PKG_MODES) sys.exit(1) if BUILD_PARAMETERS.subversion: if not str(BUILD_PARAMETERS.subversion) in cordova_subv_list: LOG.error( "The argument of cordova --sub-version can only be '3.6' or '4.x' , exit ..." ) sys.exit(1) parameters_type = BUILD_PARAMETERS.pkgtype + \ BUILD_PARAMETERS.subversion if (BUILD_PARAMETERS.subversion == '4.x' and BUILD_PARAMETERS.packtype ) and not BUILD_PARAMETERS.packtype in CORDOVA_PACK_TYPES: LOG.error("cordova packtype can only be npm, local") sys.exit(1) if (BUILD_PARAMETERS.subversion == '3.6' or not BUILD_PARAMETERS.subversion) and BUILD_PARAMETERS.packtype: LOG.error("cordova packtype is only for cordova version 4.x") sys.exit(1) if (BUILD_PARAMETERS.subversion == '3.6' or not BUILD_PARAMETERS.subversion) and BUILD_PARAMETERS.pkgarch: LOG.error("Command -a is not for cordova version 3.6") sys.exit(1) if BUILD_PARAMETERS.pkgtype == "embeddingapi": if BUILD_PARAMETERS.packtype and not BUILD_PARAMETERS.packtype in PACK_TYPES: LOG.error("embeddingapi packtype can only be gradle, maven or ant") sys.exit(1) if BUILD_PARAMETERS.subversion: BUILD_PARAMETERS.pkgtype = BUILD_PARAMETERS.pkgtype + \ BUILD_PARAMETERS.subversion all_pkg_string = "".join(config_json["pkg-list"].keys()) if parameters_type and parameters_type in all_pkg_string: for i_pkg in config_json["pkg-list"].keys(): i_pkg_list = i_pkg.replace(" ", "").split(",") if parameters_type in i_pkg_list: pkg_json = config_json["pkg-list"][i_pkg] break elif BUILD_PARAMETERS.pkgtype in all_pkg_string: for i_pkg in config_json["pkg-list"].keys(): i_pkg_list = i_pkg.replace(" ", "").split(",") if BUILD_PARAMETERS.pkgtype in i_pkg_list: pkg_json = config_json["pkg-list"][i_pkg] break if pkg_json == config_json['pkg-list'].get( 'apk') and BUILD_PARAMETERS.subversion is not None: pkg_json = config_json["pkg-list"][BUILD_PARAMETERS.subversion] if not pkg_json: LOG.error("Fail to read pkg json, exit ...") sys.exit(1) if not prepareBuildRoot(): exitHandler(1) if "pkg-blacklist" in config_json: PKG_BLACK_LIST.extend(config_json["pkg-blacklist"]) try: varshop.setValue("BUILD_PARAMETERS", BUILD_PARAMETERS) varshop.setValue("BUILD_ROOT", BUILD_ROOT) varshop.setValue("BUILD_ROOT_SRC", BUILD_ROOT_SRC) varshop.setValue("BUILD_TIME", BUILD_TIME) varshop.setValue("CROSSWALK_BRANCH", CROSSWALK_BRANCH) varshop.setValue("CROSSWALK_VERSION", CROSSWALK_VERSION) varshop.setValue("DEFAULT_CMD_TIMEOUT", DEFAULT_CMD_TIMEOUT) varshop.setValue("PKG_MODES", PKG_MODES) varshop.setValue("PKG_ARCHS", PKG_ARCHS) except Exception as e: LOG.error("Fail to set global vars: %s, exit ..." % e) sys.exit(1) if not buildPKG(pkg_json): exitHandler(1) LOG.info("+Building package ...") if BUILD_PARAMETERS.pkgtype == "apk-aio" or \ BUILD_PARAMETERS.pkgtype == "cordova-aio": pkg_file_list = os.listdir(os.path.join(BUILD_ROOT, "pkg")) for i_file in pkg_file_list: if not utils.doCopy(os.path.join(BUILD_ROOT, "pkg", i_file), os.path.join(BUILD_PARAMETERS.destdir, i_file)): exitHandler(1) elif BUILD_PARAMETERS.pkgtype == "embeddingapi" and BUILD_PARAMETERS.subversion: pkg_file = os.path.join( BUILD_PARAMETERS.destdir, "%s-%s-%s-%s.%s.zip" % (PKG_NAME, pkg_main_version, pkg_release_version, BUILD_PARAMETERS.subversion, BUILD_PARAMETERS.pkgtype)) LOG.info("pkg_file: %s" % pkg_file) if not utils.zipDir(os.path.join(BUILD_ROOT, "pkg"), pkg_file): exitHandler(1) elif BUILD_PARAMETERS.pkgtype.startswith( "embeddingapi") and BUILD_PARAMETERS.packtype: pkg_file = os.path.join( BUILD_PARAMETERS.destdir, "%s-%s-%s.%s-%s.zip" % (PKG_NAME, pkg_main_version, pkg_release_version, BUILD_PARAMETERS.pkgtype, BUILD_PARAMETERS.packtype)) if not utils.zipDir(os.path.join(BUILD_ROOT, "pkg"), pkg_file): exitHandler(1) else: pkg_file = os.path.join( BUILD_PARAMETERS.destdir, "%s-%s-%s.%s.zip" % (PKG_NAME, pkg_main_version, pkg_release_version, BUILD_PARAMETERS.pkgtype)) if not utils.zipDir(os.path.join(BUILD_ROOT, "pkg"), pkg_file): exitHandler(1)
def main(): global LOG LOG = utils.getLogger("pack-tool") try: usage = "Usage: ./pack.py -t apk -m shared -a x86" opts_parser = OptionParser(usage=usage) opts_parser.add_option("-c", "--cfg", dest="pkgcfg", help="specify the path of config json file") opts_parser.add_option("-t", "--type", dest="pkgtype", help="specify the pkg type, e.g. apk, xpk, wgt ...") opts_parser.add_option( "-m", "--mode", dest="pkgmode", help="specify the apk mode, not for embeddingapi, e.g. shared, embedded" ) opts_parser.add_option( "-a", "--arch", dest="pkgarch", help="specify the apk arch, not for embeddingapi, cordova version 3.6, e.g. x86, arm", ) opts_parser.add_option( "-d", "--dest", dest="destdir", help="specify the installation folder for packed package" ) opts_parser.add_option("-s", "--src", dest="srcdir", help="specify the path of pkg resource for packing") opts_parser.add_option("--tools", dest="pkgpacktools", help="specify the parent folder of pack tools") opts_parser.add_option( "--notclean", dest="bnotclean", action="store_true", help="disable the build root clean after the packing" ) opts_parser.add_option( "--sign", dest="signature", action="store_true", help="signature operation will be done when packing wgt" ) opts_parser.add_option("-v", "--version", dest="bversion", action="store_true", help="show this tool's version") opts_parser.add_option("-u", "--user", dest="user", help="specify the user in inst.py") opts_parser.add_option( "--sub-version", dest="subversion", help="specify the embeddingapi, cordova sub version, e.g. v1, v2, v3 ...", ) opts_parser.add_option("--pkg-version", dest="pkgversion", help="specify the pkg version, e.g. 0.0.0.1") opts_parser.add_option("--pack-type", dest="packtype", help="specify the pack type, e.g. gradle, maven") opts_parser.add_option( "--notdebug", dest="bnotdebug", action="store_true", help="specify the packed pkg is not debug mode" ) if len(sys.argv) == 1: sys.argv.append("-h") global BUILD_PARAMETERS (BUILD_PARAMETERS, args) = opts_parser.parse_args() except Exception as e: LOG.error("Got wrong options: %s, exit ..." % e) sys.exit(1) if BUILD_PARAMETERS.bversion: print "Version: %s" % TOOL_VERSION sys.exit(0) if not BUILD_PARAMETERS.srcdir: BUILD_PARAMETERS.srcdir = os.getcwd() BUILD_PARAMETERS.srcdir = os.path.expanduser(BUILD_PARAMETERS.srcdir) if not BUILD_PARAMETERS.user: BUILD_PARAMETERS.user = "******" if not os.path.exists(os.path.join(BUILD_PARAMETERS.srcdir, "..", "..", VERSION_FILE)): if not os.path.exists(os.path.join(BUILD_PARAMETERS.srcdir, "..", VERSION_FILE)): if not os.path.exists(os.path.join(BUILD_PARAMETERS.srcdir, VERSION_FILE)): LOG.info("Not found pkg version file, try to use option --pkg-version") pkg_version_file_path = None else: pkg_version_file_path = os.path.join(BUILD_PARAMETERS.srcdir, VERSION_FILE) else: pkg_version_file_path = os.path.join(BUILD_PARAMETERS.srcdir, "..", VERSION_FILE) else: pkg_version_file_path = os.path.join(BUILD_PARAMETERS.srcdir, "..", "..", VERSION_FILE) try: pkg_main_version = 0 pkg_release_version = 1 if BUILD_PARAMETERS.pkgversion: LOG.info("Using %s as pkg version " % BUILD_PARAMETERS.pkgversion) pkg_main_version = BUILD_PARAMETERS.pkgversion else: if pkg_version_file_path is not None: LOG.info("Using pkg version file: %s" % pkg_version_file_path) with open(pkg_version_file_path, "rt") as pkg_version_file: pkg_version_raw = pkg_version_file.read() pkg_version_file.close() pkg_version_json = json.loads(pkg_version_raw) pkg_main_version = pkg_version_json["main-version"] pkg_release_version = pkg_version_json["release-version"] CROSSWALK_BRANCH = pkg_version_json["crosswalk-branch"] except Exception as e: LOG.error("Fail to read pkg version file: %s, exit ..." % e) sys.exit(1) CROSSWALK_VERSION = pkg_main_version if not BUILD_PARAMETERS.pkgtype: LOG.error("No pkg type provided, exit ...") sys.exit(1) elif not BUILD_PARAMETERS.pkgtype in PKG_TYPES: LOG.error("Wrong pkg type, only support: %s, exit ..." % PKG_TYPES) sys.exit(1) if BUILD_PARAMETERS.pkgtype == "apk" or BUILD_PARAMETERS.pkgtype == "apk-aio": if not BUILD_PARAMETERS.pkgmode: LOG.error("No pkg mode option provided, exit ...") sys.exit(1) elif not BUILD_PARAMETERS.pkgmode in PKG_MODES: LOG.error("Wrong pkg mode option provided, only support:%s, exit ..." % PKG_MODES) sys.exit(1) if not BUILD_PARAMETERS.pkgarch: LOG.error("No pkg arch option provided, exit ...") sys.exit(1) elif not BUILD_PARAMETERS.pkgarch in PKG_ARCHS: LOG.error("Wrong pkg arch option provided, only support:%s, exit ..." % PKG_ARCHS) sys.exit(1) if BUILD_PARAMETERS.pkgtype == "apk-aio" or BUILD_PARAMETERS.pkgtype == "cordova-aio": if not BUILD_PARAMETERS.destdir or not os.path.exists(BUILD_PARAMETERS.destdir): LOG.error("No all-in-one installation dest dir found, exit ...") sys.exit(1) elif not BUILD_PARAMETERS.destdir: BUILD_PARAMETERS.destdir = BUILD_PARAMETERS.srcdir BUILD_PARAMETERS.destdir = os.path.expanduser(BUILD_PARAMETERS.destdir) if not BUILD_PARAMETERS.pkgpacktools: BUILD_PARAMETERS.pkgpacktools = os.path.join(BUILD_PARAMETERS.srcdir, "..", "..", "tools") BUILD_PARAMETERS.pkgpacktools = os.path.expanduser(BUILD_PARAMETERS.pkgpacktools) config_json = None if BUILD_PARAMETERS.pkgcfg: config_json_file_path = BUILD_PARAMETERS.pkgcfg else: config_json_file_path = os.path.join(BUILD_PARAMETERS.srcdir, "suite.json") try: LOG.info("Using config json file: %s" % config_json_file_path) with open(config_json_file_path, "rt") as config_json_file: config_raw = config_json_file.read() config_json_file.close() config_json = json.loads(config_raw) except Exception as e: LOG.error("Fail to read config json file: %s, exit ..." % e) sys.exit(1) global PKG_NAME PKG_NAME = utils.safelyGetValue(config_json, "pkg-name") if not PKG_NAME: PKG_NAME = os.path.basename(BUILD_PARAMETERS.srcdir) LOG.warning("Fail to read pkg name from json, " "using src dir name as pkg name ...") LOG.info("================= %s (%s-%s) ================" % (PKG_NAME, pkg_main_version, pkg_release_version)) if not utils.safelyGetValue(config_json, "pkg-list"): LOG.error("Fail to read pkg-list, exit ...") sys.exit(1) pkg_json = None global parameters_type parameters_type = None cordova_subv_list = ["4.x", "3.6"] if BUILD_PARAMETERS.pkgtype == "cordova" or BUILD_PARAMETERS.pkgtype == "cordova-aio": if BUILD_PARAMETERS.pkgarch and not BUILD_PARAMETERS.pkgarch in PKG_ARCHS: LOG.error("Wrong pkg-arch, only support: %s, exit ..." % PKG_ARCHS) sys.exit(1) if BUILD_PARAMETERS.pkgmode and not BUILD_PARAMETERS.pkgmode in PKG_MODES: LOG.error("Wrong pkg-mode, only support: %s, exit ..." % PKG_MODES) sys.exit(1) if BUILD_PARAMETERS.subversion: if not str(BUILD_PARAMETERS.subversion) in cordova_subv_list: LOG.error("The argument of cordova --sub-version can only be '3.6' or '4.x' , exit ...") sys.exit(1) parameters_type = BUILD_PARAMETERS.pkgtype + BUILD_PARAMETERS.subversion if ( BUILD_PARAMETERS.subversion == "4.x" and BUILD_PARAMETERS.packtype ) and not BUILD_PARAMETERS.packtype in CORDOVA_PACK_TYPES: LOG.error("cordova packtype can only be npm, local") sys.exit(1) if (BUILD_PARAMETERS.subversion == "3.6" or not BUILD_PARAMETERS.subversion) and BUILD_PARAMETERS.packtype: LOG.error("cordova packtype is only for cordova version 4.x") sys.exit(1) if (BUILD_PARAMETERS.subversion == "3.6" or not BUILD_PARAMETERS.subversion) and BUILD_PARAMETERS.pkgarch: LOG.error("Command -a is not for cordova version 3.6") sys.exit(1) if BUILD_PARAMETERS.pkgtype == "embeddingapi": if BUILD_PARAMETERS.packtype and not BUILD_PARAMETERS.packtype in PACK_TYPES: LOG.error("embeddingapi packtype can only be gradle, maven or ant") sys.exit(1) if BUILD_PARAMETERS.subversion: BUILD_PARAMETERS.pkgtype = BUILD_PARAMETERS.pkgtype + BUILD_PARAMETERS.subversion all_pkg_string = "".join(config_json["pkg-list"].keys()) if parameters_type and parameters_type in all_pkg_string: for i_pkg in config_json["pkg-list"].keys(): i_pkg_list = i_pkg.replace(" ", "").split(",") if parameters_type in i_pkg_list: pkg_json = config_json["pkg-list"][i_pkg] break elif BUILD_PARAMETERS.pkgtype in all_pkg_string: for i_pkg in config_json["pkg-list"].keys(): i_pkg_list = i_pkg.replace(" ", "").split(",") if BUILD_PARAMETERS.pkgtype in i_pkg_list: pkg_json = config_json["pkg-list"][i_pkg] break if pkg_json == config_json["pkg-list"].get("apk") and BUILD_PARAMETERS.subversion is not None: pkg_json = config_json["pkg-list"][BUILD_PARAMETERS.subversion] if not pkg_json: LOG.error("Fail to read pkg json, exit ...") sys.exit(1) if not prepareBuildRoot(): exitHandler(1) if "pkg-blacklist" in config_json: PKG_BLACK_LIST.extend(config_json["pkg-blacklist"]) try: varshop.setValue("BUILD_PARAMETERS", BUILD_PARAMETERS) varshop.setValue("BUILD_ROOT", BUILD_ROOT) varshop.setValue("BUILD_ROOT_SRC", BUILD_ROOT_SRC) varshop.setValue("BUILD_TIME", BUILD_TIME) varshop.setValue("CROSSWALK_BRANCH", CROSSWALK_BRANCH) varshop.setValue("CROSSWALK_VERSION", CROSSWALK_VERSION) varshop.setValue("DEFAULT_CMD_TIMEOUT", DEFAULT_CMD_TIMEOUT) varshop.setValue("PKG_MODES", PKG_MODES) varshop.setValue("PKG_ARCHS", PKG_ARCHS) except Exception as e: LOG.error("Fail to set global vars: %s, exit ..." % e) sys.exit(1) if not buildPKG(pkg_json): exitHandler(1) LOG.info("+Building package ...") if BUILD_PARAMETERS.pkgtype == "apk-aio" or BUILD_PARAMETERS.pkgtype == "cordova-aio": pkg_file_list = os.listdir(os.path.join(BUILD_ROOT, "pkg")) for i_file in pkg_file_list: if not utils.doCopy( os.path.join(BUILD_ROOT, "pkg", i_file), os.path.join(BUILD_PARAMETERS.destdir, i_file) ): exitHandler(1) elif BUILD_PARAMETERS.pkgtype == "embeddingapi" and BUILD_PARAMETERS.subversion: pkg_file = os.path.join( BUILD_PARAMETERS.destdir, "%s-%s-%s-%s.%s.zip" % (PKG_NAME, pkg_main_version, pkg_release_version, BUILD_PARAMETERS.subversion, BUILD_PARAMETERS.pkgtype), ) LOG.info("pkg_file: %s" % pkg_file) if not utils.zipDir(os.path.join(BUILD_ROOT, "pkg"), pkg_file): exitHandler(1) elif BUILD_PARAMETERS.pkgtype.startswith("embeddingapi") and BUILD_PARAMETERS.packtype: pkg_file = os.path.join( BUILD_PARAMETERS.destdir, "%s-%s-%s.%s-%s.zip" % (PKG_NAME, pkg_main_version, pkg_release_version, BUILD_PARAMETERS.pkgtype, BUILD_PARAMETERS.packtype), ) if not utils.zipDir(os.path.join(BUILD_ROOT, "pkg"), pkg_file): exitHandler(1) else: pkg_file = os.path.join( BUILD_PARAMETERS.destdir, "%s-%s-%s.%s.zip" % (PKG_NAME, pkg_main_version, pkg_release_version, BUILD_PARAMETERS.pkgtype), ) if not utils.zipDir(os.path.join(BUILD_ROOT, "pkg"), pkg_file): exitHandler(1)
def convert_file_for_pptx(out_filename, template_file, ds_dict): '''按模板转换xlsx文件 按字典转换模板文件,输出为out_filename ''' unzip_path = os.path.join(out_filename + 't\\pptx_tmp') if (os.path.exists(unzip_path)): shutil.rmtree(unzip_path) unzip_single(template_file, unzip_path) embeddings_path = os.path.join(unzip_path, "ppt\\embeddings") tmp_pd_dict = {} tmp_excel_active_sheet_dict = {} if (os.path.exists(embeddings_path)): for x in os.listdir(embeddings_path): if x.endswith('.xlsx'): active_name = convert_file_for_xlsx( os.path.join(embeddings_path, x), os.path.join(embeddings_path, x), ds_dict, outImage=False) tmp_excel_active_sheet_dict[x] = active_name tmp_pd_dict[x] = pd.read_excel(os.path.join( embeddings_path, x)) xlsx_emf_arr = [] root_path = os.path.join(unzip_path, "ppt") for slide in os.listdir(f"{root_path}\slides"): if slide.endswith(".xml") == False: continue doc = lxml.etree.XML( open(f"{root_path}\\slides\\{slide}", 'rb').read()) id_embed_dict = {} for one_oleObj in doc.xpath("//p:oleObj", namespaces=doc.nsmap): for one_blip in one_oleObj.xpath(".//a:blip", namespaces=doc.nsmap): id = one_oleObj.attrib.get('{' + doc.nsmap['r'] + '}id') embed = one_blip.attrib.get('{' + doc.nsmap['r'] + '}embed') id_embed_dict[id] = embed if len(id_embed_dict) > 0: rels = lxml.etree.XML( open(f"{root_path}\\slides\_rels\\{slide}.rels", 'rb').read()) for id, embed in id_embed_dict.items(): xlsx = rels.xpath( f"//*[local-name() = 'Relationship'][@Id='{id}'] " )[0].attrib['Target'] emf = rels.xpath( f"//*[local-name() = 'Relationship'][@Id='{embed}'] " )[0].attrib['Target'] xlsx_emf_arr.append({"xlsx": xlsx, "emf": emf, "slide": slide}) for one in xlsx_emf_arr: png_file = os.path.realpath(root_path + "/slides/" + one['xlsx'] + "1.png") emf_file = os.path.realpath(root_path + "/slides/" + one['emf']) excel2img.export_img( root_path + "/slides/" + one['xlsx'], png_file, tmp_excel_active_sheet_dict[one['xlsx'].split("/")[-1]]) my_cmd = f'convert "{png_file}" "{emf_file}"' cmd_output = os.popen(my_cmd).readlines() os.remove(png_file) zipDir(unzip_path, out_filename) shutil.rmtree(out_filename + "t") env = get_jinja2_Environment() ppt_file = Presentation(out_filename) ''' #expr title_lines=1 loop_var=index,row dataset=a.sort_values(zhibiao,ascending=False)[:size] ''' def calc_frame_txt(obj, calc_dict, calc_kind=None): if calc_kind is None: calc_kind = 1 if len(obj.text_frame.paragraphs) < 1 else 3 if calc_kind == 3: #text_frame 中有多个不同格式的文本,需要查runs,通常不应该是这样的 for paragraph in obj.text_frame.paragraphs: exp_list = [] if paragraph.text.find('{{') > -1: start, end, s_num, e_num = -1, -1, 0, 0 for idx, run in enumerate(paragraph.runs): if run.text.find('{{') > -1: s_num += 1 if s_num == 1: start = idx if run.text.find('}}') > -1: end = idx e_num += 1 if start >= 0 and end >= 0 and s_num == e_num: exp_list.append((start, end)) start, end, s_num, e_num = -1, -1, 0, 0 for start, end in exp_list: if start >= 0 and end >= 0 and start <= end: text = ''.join([ x.text for x in paragraph.runs[start:end + 1] ]) try: result = exec_template(env, text, calc_dict) except Exception as e: raise RuntimeError(text) paragraph.runs[start].text = result for x in paragraph.runs[start + 1:end + 1]: x.text = '' elif calc_kind == 2: for paragraph in obj.text_frame.paragraphs: if paragraph.text.find('{{') > -1: try: result = exec_template(env, paragraph.text, calc_dict) except: raise RuntimeError(paragraph.text) for run in paragraph.runs: run.text = '' #直接copy font 报错,我们通过将其他runs中的文字清空,计算出的新文字赋值给第一个run。这样就保留了格式 paragraph.runs[0].text = result else: expr = obj.text_frame.text if expr.find('{{') > -1: try: result = exec_template(env, expr, calc_dict) # env.from_string(expr) except: raise RuntimeError(paragraph.text) for paragraph in obj.text_frame.paragraphs: for run in paragraph.runs: run.text = '' #直接copy font 报错,我们通过将其他runs中的文字清空,计算出的新文字赋值给第一个run。这样就保留了格式 obj.text_frame.paragraphs[0].runs[0].text = result def handle_all_shapes(shapes, real_dict, tmp_pd_dict): # real_dict 我们使用这个参数来层层传递外面定义变量 #tmp_pd_dict 是专为内嵌excel准备的,貌似递归取不到外层定义的变量 for shape in shapes: #shape.part.related_parts['rId4'].blob if hasattr(shape, "shapes"): handle_all_shapes(shape.shapes, real_dict, tmp_pd_dict) continue if shape.has_text_frame or shape.has_table: pass if shape.shape_type == MSO_SHAPE_TYPE.EMBEDDED_OLE_OBJECT: pass if shape.has_text_frame: calc_frame_txt(shape, real_dict) elif shape.has_chart: key = shape.chart._workbook.xlsx_part.partname.split("/")[-1] # 定义图表数据 --------------------- chart_data = ChartData() columns = list(tmp_pd_dict[key].columns.values) chart_data.categories = tmp_pd_dict[key][columns[0]] for one in columns[1:]: chart_data.add_series(one, tuple(tmp_pd_dict[key][one])) shape.chart.replace_data(chart_data) elif shape.has_table: current_row = 0 for row in shape.table.rows: current_col = 0 for cell in row.cells: if cell.text_frame.text.find('{{') < 0: current_col = current_col + 1 continue try: result = exec_template(env, cell.text_frame.text, real_dict) except: raise RuntimeError(cell.text_frame.text) for paragraph in cell.text_frame.paragraphs: for run in paragraph.runs: run.text = '' #直接copy font 报错,我们通过将其他runs中的文字清空,计算出的新文字赋值给第一个run。这样就保留了格式 copy_row = current_row result_lines = result.split('\n') for one_line in result_lines: #展开模板计算结果 copy_col = current_col #从当前位置开始,复制结果到ppt的table中 for one in one_line.split(): cur_row_cells = shape.table.rows[ copy_row].cells if copy_col >= len( cur_row_cells ): #如果ppt table 中的列不够用,当前行的复制就结束 break p_cell = cur_row_cells[copy_col] if len(p_cell.text_frame.paragraphs[0].runs ) == 0: p_cell.text_frame.paragraphs[0].add_run() p_cell.text_frame.paragraphs[0].runs[ 0].text = one copy_col = copy_col + 1 copy_row = copy_row + 1 if copy_row >= len(shape.table.rows): #行不够就结束复制 break current_col = current_col + 1 current_row = current_row + 1 if current_row >= len(shape.table.rows): break try: real_dict = ds_dict.copy() for slide in ppt_file.slides: if slide.has_notes_slide: #抽取备注栏里面的变量定义,后页会覆盖前页 notes_text = slide.notes_slide.notes_text_frame.text for one_line in notes_text.split("\n"): var_expr = one_line.split("=") if len(var_expr) < 2: continue try: if var_expr[1].strip().startswith("{{"): result_lines = exec_template( env, var_expr[1], real_dict) else: result_lines = exec_template( env, "{{" + var_expr[1] + "}}", real_dict) real_dict = real_dict.copy() real_dict[var_expr[0].strip()] = result_lines except Exception as e: raise RuntimeError("\n备注说明中的公式不正确:" + one_line) handle_all_shapes(slide.shapes, real_dict, tmp_pd_dict) ppt_file.save(out_filename) finally: if ppt_file is not None: ppt_file.save(out_filename) del ppt_file ppt2png(out_filename, ds_dict.get("_idx_", ''))