4343_BUILDPKG_VERSION = "v0.2.0"
4444_BUILDPKG_AUTHOR = "liu2guang"
4545_BUILDPKG_LICENSE = "GPLv3"
46- _BUILDPKG_RELEASE = True
46+ _BUILDPKG_RELEASE = False
4747
4848_BUILDPKG_CONTRIBUTORS = {
4949 "liu2guang" : "https://github.com/liu2guang" ,
@@ -185,7 +185,41 @@ def _load_config(filename):
185185 _save_config ("config.json" )
186186
187187 _run_log .info ("Update %s to config.json." % (_config ["username" ]))
188-
188+
189+ # --------------------------------------------------------------------------------
190+ # Save package confg
191+ # --------------------------------------------------------------------------------
192+ def _save_package_config (filename , config ):
193+ if sys .version_info < (3 , 0 ):
194+ with open (filename , "w+" ) as _file :
195+ _file .write (json .dumps (config , indent = 4 ))
196+ else :
197+ with open (filename , "w+" , encoding = 'utf-8' ) as _file :
198+ _file .write (json .dumps (config , indent = 4 ))
199+
200+ # --------------------------------------------------------------------------------
201+ # Load package confg
202+ # --------------------------------------------------------------------------------
203+ def _load_package_config (filename ):
204+ # 1. check if "filename" file exists
205+ if os .path .exists (filename ) == False :
206+ return False
207+
208+ # 2. check that "filename" is illegal, Waiting for user processing
209+ try :
210+ with open (filename , 'r' ) as file :
211+ json .load (file )
212+ except ValueError :
213+ return False
214+
215+ # 3. load package config
216+ with open (filename , 'r' ) as file :
217+ _package_config = json .load (file )
218+
219+ _run_log .debug ("Buildpkg load package config: \n " + json .dumps (_package_config , indent = 4 ))
220+
221+ return _package_config
222+
189223# --------------------------------------------------------------------------------
190224# Check self(It will load the configuration)
191225# --------------------------------------------------------------------------------
@@ -291,14 +325,14 @@ def _generate_file(template_name, target_name, replace_list):
291325# Commit package git
292326# --------------------------------------------------------------------------------
293327def _commit_git (pkgname , commit_content ):
294- _run_log .info ("Start commit the first commit." )
328+ _run_log .info ("Start commit the commit." )
295329 os .chdir (_buildpkg_packages_xxx_path )
296330
297331 os .system ('git add -A' )
298332 os .system ("git commit -m \" " + commit_content .replace ("${pkgname}" , pkgname ) + "\" " )
299333
300334 os .chdir (_buildpkg_path )
301- _run_log .info ("Commit the first commit success." )
335+ _run_log .info ("Commit the commit success." )
302336
303337# --------------------------------------------------------------------------------
304338# Generate package
@@ -375,14 +409,10 @@ def _make_package(pkgname, version = None, license = None):
375409 __buildpkg_config_json_path = os .path .join (_buildpkg_path , "config.json" )
376410 __buildpkg_packages_xxx_config_json_path = os .path .join (_buildpkg_packages_xxx_path , "config.json" )
377411
378- if sys .version_info < (3 , 0 ):
379- with open (__buildpkg_config_json_path , 'r' ) as _file_in , open (__buildpkg_packages_xxx_config_json_path , 'w+' ) as _file_out :
380- _contents = _file_in .read ()
381- _file_out .write (_contents )
382- else :
383- with open (__buildpkg_config_json_path , 'r' , encoding = 'utf-8' ) as _file_in , open (__buildpkg_packages_xxx_config_json_path , 'w+' , encoding = 'utf-8' ) as _file_out :
384- _contents = _file_in .read ()
385- _file_out .write (_contents )
412+ _config_save = {}
413+ _config_save .update (_config )
414+ _config_save .update (_replace_list )
415+ _save_package_config (__buildpkg_packages_xxx_config_json_path , _config_save )
386416
387417 _run_log .info ("Save the configuration success." )
388418
@@ -411,7 +441,7 @@ def _transplant_package(pkgname, pkgrepo, version = None, license = None, submod
411441 os .chdir (_buildpkg_packages_xxx_path )
412442 os .system ("git submodule add " + pkgrepo )
413443 os .chdir (_buildpkg_path )
414- _run_log .debug ("Add %s to the %s repository as a submodule" % (pkgrepo , pkgname ))
444+ _run_log .debug ("Add %s to the %s repository as a submodule. " % (pkgrepo , pkgname ))
415445
416446 # 1.2 Add to the repository as source code when Transplant package
417447 else :
@@ -423,16 +453,72 @@ def _transplant_package(pkgname, pkgrepo, version = None, license = None, submod
423453 os .system ('attrib -r ' + _git_remove_path + '\\ *.* /s' )
424454 shutil .rmtree (_git_remove_path )
425455 os .chdir (_buildpkg_path )
426- _run_log .info ("Add %s to the %s repository as source code" % (pkgrepo , pkgname ))
456+ _run_log .info ("Add %s to the %s repository as source code. " % (pkgrepo , pkgname ))
427457
428458# --------------------------------------------------------------------------------
429459# Update package
430460# --------------------------------------------------------------------------------
431461def _update_package (pkgname , version = None , license = None ):
432- # 1. 读取之前生成的配置
433- # 2. 更新readme/scons/scons_demo/license/ci
434- # 3. 保持配置文件
435- pass
462+ global _config
463+ global _buildpkg_path
464+ global _buildpkg_template_path
465+ global _buildpkg_packages_path
466+ global _buildpkg_packages_xxx_path
467+ global _buildpkg_packages_xxx_example_path
468+ global _buildpkg_packages_xxx_scripts_path
469+
470+ # 1. Read the previously generated configuration
471+ _package_config_path = os .path .join (_buildpkg_packages_path , pkgname , "config.json" )
472+ _package_config = _load_package_config (_package_config_path )
473+
474+ _replace_list = {
475+ "username" : _config ["username" ],
476+ "pkgname" : _package_config ["pkgname" ],
477+ "version" : _package_config ["version" ] if version == None else version ,
478+ "pkgname_letter" : _package_config ["pkgname_letter" ],
479+ "list_ignore_inc" : str (_config ["list_ignore_inc" ]),
480+ "list_ignore_src" : str (_config ["list_ignore_src" ])
481+ }
482+
483+ # 2. Update the /readme.md file
484+ _generate_file (_templates ["readme_md" ], "readme.md" , _replace_list )
485+
486+ # 3. Update the /SConscript file
487+ _generate_file (_templates ["sconscript" ], "SConscript" , _replace_list )
488+
489+ # 4. Update the /example/SConscript file
490+ _example_sconscript_path = os .path .join ("example" , "SConscript" )
491+ _generate_file (_templates ["sconscript_example" ], _example_sconscript_path , _replace_list )
492+
493+ # 5. Update the /.travis.yml file
494+ _templates_ci_script_dir_path = os .path .join (_buildpkg_template_path , "ci_script_github" )
495+ shutil .rmtree (_buildpkg_packages_xxx_scripts_path )
496+ shutil .copytree (_templates_ci_script_dir_path , _buildpkg_packages_xxx_scripts_path )
497+ _generate_file (_templates ["ci_github" ], ".travis.yml" , _replace_list )
498+
499+ # 6. Update the /LICENSE file
500+ if license != None :
501+ _license_file_path = os .path .join (_buildpkg_packages_xxx_path , "LICENSE" )
502+ _run_log .info ("Generate [%s LICENSE] file by \" lice module\" ." % (license ))
503+ cmd = "lice " + license .lower () + " -f " + _license_file_path + " -o " + _config ["username" ]
504+ os .system (cmd )
505+
506+ if os .path .exists (_license_file_path ) == True :
507+ _run_log .info ("Generate [%s LICENSE] file success." % (license ))
508+ else :
509+ _run_log .error ("Generate [%s LICENSE] file failed." % (license ))
510+
511+ # 7. Save the configuration file when generating the project
512+ _run_log .info ("Save the configuration file to [%s]." % (pkgname ))
513+ __buildpkg_config_json_path = os .path .join (_buildpkg_path , "config.json" )
514+ __buildpkg_packages_xxx_config_json_path = os .path .join (_buildpkg_packages_xxx_path , "config.json" )
515+
516+ _config_save = {}
517+ _config_save .update (_config )
518+ _config_save .update (_replace_list )
519+ _save_package_config (__buildpkg_packages_xxx_config_json_path , _config_save )
520+
521+ _run_log .info ("Save the configuration success." )
436522
437523# --------------------------------------------------------------------------------
438524# Main
@@ -462,11 +548,11 @@ def main():
462548 # 4.1. make package
463549 if _args .pkgrepo == None :
464550 _make_package (_args .pkgname , _args .version , _args .license )
465- _commit_git (_args .pkgname , _config ["commit_content" ] )
551+ _commit_git (_args .pkgname , _config ["commit_content" ])
466552 # 4.2. transplant package
467553 else :
468554 _transplant_package (_args .pkgname , _args .pkgrepo , _args .version , _args .license , _args .submodule )
469- _commit_git (_args .pkgname , _config ["commit_content" ] )
555+ _commit_git (_args .pkgname , _config ["commit_content" ])
470556
471557 _run_log .info ("To complete to make package [%s]!" % (_args .pkgname ))
472558 _pkg_log .info ("To complete to make package [%s]!" % (_args .pkgname ))
0 commit comments