Skip to content

Commit 5d9d3a5

Browse files
committed
【完善】:功能代码流程,优化代码
1 parent 6be14fe commit 5d9d3a5

File tree

1 file changed

+90
-112
lines changed

1 file changed

+90
-112
lines changed

cmds/cmd_package.py

Lines changed: 90 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@
4141
from cmd_menuconfig import find_macro_in_config
4242

4343

44+
class Logger:
45+
def __init__(self, log_name, clevel=logging.DEBUG):
46+
self.logger = logging.getLogger(log_name)
47+
self.logger.setLevel(logging.DEBUG)
48+
fmt = logging.Formatter(
49+
'[%(levelname)s] %(message)s')
50+
51+
# set cmd log
52+
sh = logging.StreamHandler()
53+
sh.setFormatter(fmt)
54+
sh.setLevel(clevel)
55+
self.logger.addHandler(sh)
56+
57+
def debug(self, message):
58+
self.logger.debug(message)
59+
60+
def info(self, message):
61+
self.logger.info(message)
62+
63+
def war(self, message):
64+
self.logger.warn(message)
65+
66+
def error(self, message):
67+
self.logger.error(message)
68+
69+
def cri(self, message):
70+
self.logger.critical(message)
71+
72+
4473
"""package command"""
4574

4675

@@ -405,7 +434,7 @@ def git_cmd_exec(cmd, cwd):
405434
(cwd, " path doesn't exist", e.message))
406435

407436

408-
def update_latest_packages(read_back_pkgs_json, bsp_packages_path):
437+
def update_latest_packages(pkgs_fn, bsp_packages_path):
409438
""" update the packages that are latest version.
410439
411440
If the selected package is the latest version,
@@ -420,6 +449,9 @@ def update_latest_packages(read_back_pkgs_json, bsp_packages_path):
420449
env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds')
421450
env_config_file = os.path.join(env_kconfig_path, '.config')
422451

452+
with open(pkgs_fn, 'r') as f:
453+
read_back_pkgs_json = json.load(f)
454+
423455
for pkg in read_back_pkgs_json:
424456
package = Package()
425457
pkg_path = pkg['path']
@@ -552,6 +584,13 @@ def pre_package_update():
552584
with open(pkgs_error_list_fn, 'r') as f:
553585
pkgs_delete_error_list = json.load(f)
554586

587+
# create SConscript file
588+
if not os.path.isfile(os.path.join(bsp_packages_path, 'SConscript')):
589+
bridge_script = file(os.path.join(
590+
bsp_packages_path, 'SConscript'), 'w')
591+
bridge_script.write(Bridge_SConscript)
592+
bridge_script.close()
593+
555594
return [oldpkgs, newpkgs, pkgs_delete_error_list, pkgs_fn, pkgs_error_list_fn, bsp_packages_path, dbsqlite_pathname]
556595

557596

@@ -564,7 +603,7 @@ def error_packages_handle(error_packages_list, read_back_pkgs_json, pkgs_fn):
564603
error_packages_redownload_error_list = []
565604

566605
if len(error_packages_list):
567-
print("\n==============================> Error packages list : \n")
606+
print("\n==============================> Packages list to download : \n")
568607
for pkg in error_packages_list:
569608
print pkg['name'], pkg['ver']
570609
print("\nThe package in the list above is accidentally deleted.")
@@ -619,35 +658,6 @@ def rm_package(dir):
619658
return True
620659

621660

622-
class Logger:
623-
def __init__(self, log_name, clevel=logging.DEBUG):
624-
self.logger = logging.getLogger(log_name)
625-
self.logger.setLevel(logging.DEBUG)
626-
fmt = logging.Formatter(
627-
'%(asctime)s [%(levelname)s] [%(filename)s] %(message)s')
628-
629-
# set cmd log
630-
sh = logging.StreamHandler()
631-
sh.setFormatter(fmt)
632-
sh.setLevel(clevel)
633-
self.logger.addHandler(sh)
634-
635-
def debug(self, message):
636-
self.logger.debug(message)
637-
638-
def info(self, message):
639-
self.logger.info(message)
640-
641-
def war(self, message):
642-
self.logger.warn(message)
643-
644-
def error(self, message):
645-
self.logger.error(message)
646-
647-
def cri(self, message):
648-
self.logger.critical(message)
649-
650-
651661
def get_package_remove_path(pkg, bsp_packages_path):
652662
dirpath = pkg['path']
653663
ver = pkg['ver']
@@ -662,6 +672,47 @@ def get_package_remove_path(pkg, bsp_packages_path):
662672
return removepath_ver
663673

664674

675+
def handle_download_error_packages(pkgs_fn, bsp_packages_path):
676+
""" handle download error packages.
677+
678+
Check to see if the packages stored in the Json file list actually exist,
679+
and then download the packages if they don't exist.
680+
"""
681+
682+
with open(pkgs_fn, 'r') as f:
683+
read_back_pkgs_json = json.load(f)
684+
685+
error_packages_list = []
686+
687+
for pkg in read_back_pkgs_json:
688+
removepath = get_package_remove_path(pkg, bsp_packages_path)
689+
690+
if os.path.exists(removepath):
691+
continue
692+
else:
693+
pkgs_update_log.war(
694+
'[Line: %d][Message : Path add to error list : %s ]' % (sys._getframe().f_lineno, removepath))
695+
error_packages_list.append(pkg)
696+
697+
# Handle the failed download packages
698+
get_flag = error_packages_handle(
699+
error_packages_list, read_back_pkgs_json, pkgs_fn)
700+
701+
return get_flag
702+
703+
704+
def write_storage_file(pkgs_fn, newpkgs):
705+
"""Writes the updated configuration to pkgs.json file.
706+
707+
Packages that are not downloaded correctly will be redownloaded at the
708+
next update.
709+
"""
710+
711+
pkgs_file = file(pkgs_fn, 'w')
712+
pkgs_file.write(json.dumps(newpkgs, indent=1))
713+
pkgs_file.close()
714+
715+
665716
def package_update(isDeleteOld=False):
666717
"""Update env's packages.
667718
@@ -671,16 +722,10 @@ def package_update(isDeleteOld=False):
671722
remind the user saved the modified file.
672723
"""
673724

674-
pkgs_update_log = Logger('pkgs_update', logging.ERROR)
675-
676-
pkgs_update_log.info(
677-
'[Line: %d][Message : Begin to update packages]' % sys._getframe().f_lineno)
678-
725+
pkgs_update_log = Logger('pkgs_update', logging.WARNING)
679726
bsp_root = Import('bsp_root')
680727
env_root = Import('env_root')
681-
682728
flag = True
683-
684729
sys_value = pre_package_update()
685730
oldpkgs = sys_value[0]
686731
newpkgs = sys_value[1]
@@ -690,18 +735,12 @@ def package_update(isDeleteOld=False):
690735
bsp_packages_path = sys_value[5]
691736
dbsqlite_pathname = sys_value[6]
692737

693-
# print "newpkgs:",newpkgs
694-
# print "oldpkgs:",oldpkgs
695-
696738
pkgs_update_log.info(
697739
'[Line: %d][Message : Begin to remove packages]' % sys._getframe().f_lineno)
698-
699740
pkgs_update_log.info(
700741
'[Line: %d][Message : oldpkgs: %s ]' % (sys._getframe().f_lineno, oldpkgs))
701-
702742
pkgs_update_log.info(
703743
'[Line: %d][Message : newpkgs: %s ]' % (sys._getframe().f_lineno, newpkgs))
704-
705744
pkgs_update_log.info(
706745
'[Line: %d][Message : pkgs_delete_error_list: %s ]' % (sys._getframe().f_lineno, pkgs_delete_error_list))
707746

@@ -731,7 +770,6 @@ def package_update(isDeleteOld=False):
731770
removepath_git = os.path.join(removepath_ver, '.git')
732771

733772
# print "removepath_git to delete",removepath_git
734-
735773
# Delete. Git directory.
736774

737775
if os.path.isdir(removepath_ver) and os.path.isdir(removepath_git):
@@ -770,7 +808,6 @@ def package_update(isDeleteOld=False):
770808
pkgs_delete_fail_list)
771809

772810
# write error messages
773-
774811
pkgs_file = file(pkgs_error_list_fn, 'w')
775812
pkgs_file.write(json.dumps(pkgs_delete_fail_list, indent=1))
776813
pkgs_file.close()
@@ -779,15 +816,11 @@ def package_update(isDeleteOld=False):
779816
else:
780817

781818
# write error messages
782-
783819
pkgs_file = file(pkgs_error_list_fn, 'w')
784820
pkgs_file.write(json.dumps(pkgs_delete_fail_list, indent=1))
785821
pkgs_file.close()
786822

787-
# 2.in old and in new
788-
#caseinoperation = and_list(newpkgs,oldpkgs)
789-
790-
# 3.in new not in old : Software packages to be installed.
823+
# 2.in new not in old : Software packages to be installed.
791824
# If the package download fails, record it, and then download again when
792825
# the update command is executed.
793826

@@ -824,67 +857,12 @@ def package_update(isDeleteOld=False):
824857
pkgs_download_fail_list)
825858
print("You need to reuse the <pkgs -update> command to download again.\n")
826859

827-
# Writes the updated configuration to pkgs.json file.
828-
# Packages that are not downloaded correctly will be redownloaded at the
829-
# next update.
830-
831-
# print("write to %s" % pkgs_fn)
832-
# print("newpkgs %s" % newpkgs)
833-
834-
pkgs_update_log.info(
835-
'[Line: %d][Message : Write to pkgs.json file]' % sys._getframe().f_lineno)
836-
837-
pkgs_file = file(pkgs_fn, 'w')
838-
pkgs_file.write(json.dumps(newpkgs, indent=1))
839-
pkgs_file.close()
840-
841-
pkgs_update_log.info(
842-
'[Line: %d][Message : Write to SConscript file]' % sys._getframe().f_lineno)
860+
# update pkgs.json and SConscript
861+
write_storage_file(pkgs_fn, newpkgs)
843862

844-
# update SConscript file
845-
if not os.path.isfile(os.path.join(bsp_packages_path, 'SConscript')):
846-
bridge_script = file(os.path.join(
847-
bsp_packages_path, 'SConscript'), 'w')
848-
bridge_script.write(Bridge_SConscript)
849-
bridge_script.close()
850-
851-
# Check to see if the packages stored in the Json file list actually exist,
852-
# and then download the packages if they don't exist.
853-
854-
with open(pkgs_fn, 'r') as f:
855-
read_back_pkgs_json = json.load(f)
856-
857-
# print("read_back_pkgs_json : %s" % read_back_pkgs_json)
858-
859-
error_packages_list = []
860-
861-
for pkg in read_back_pkgs_json:
862-
dirpath = pkg['path']
863-
ver = pkg['ver']
864-
# print 'ver is :',ver[1:]
865-
if dirpath[0] == '/' or dirpath[0] == '\\':
866-
dirpath = dirpath[1:]
867-
868-
dirpath = os.path.basename(dirpath)
869-
removepath = os.path.join(bsp_packages_path, dirpath)
870-
#print("if floder exist : %s"%removepath)
871-
git_removepath = get_pkg_folder_by_orign_path(removepath, ver)
872-
#print("if floder exist : %s"%git_removepath)
873-
removepath_ver = get_pkg_folder_by_orign_path(removepath, ver[1:])
874-
#print("if floder exist : %s"%removepath_ver)
875-
876-
if os.path.exists(removepath):
877-
continue
878-
elif os.path.exists(removepath_ver):
879-
continue
880-
elif os.path.exists(git_removepath):
881-
continue
882-
else:
883-
error_packages_list.append(pkg)
884-
885-
# Handle the failed download packages
886-
get_flag = error_packages_handle(
887-
error_packages_list, read_back_pkgs_json, pkgs_fn)
863+
# handle download error packages.
864+
get_flag = handle_download_error_packages(
865+
pkgs_fn, bsp_packages_path)
888866

889867
if get_flag != None:
890868
flag = get_flag
@@ -894,7 +872,7 @@ def package_update(isDeleteOld=False):
894872

895873
# Update the software packages, which the version is 'latest'
896874
try:
897-
update_latest_packages(read_back_pkgs_json, bsp_packages_path)
875+
update_latest_packages(pkgs_fn, bsp_packages_path)
898876
except KeyboardInterrupt:
899877
flag = False
900878

0 commit comments

Comments
 (0)