def pre_package_update(): """ Make preparations before updating the software package. """ bsp_root = Import('bsp_root') env_root = Import('env_root') if not os.path.exists('.config'): if platform.system() == "Windows": os.system('chcp 65001 > nul') print("\n\033[1;31;40m当前路径下没有发现 .config 文件,请确保当前目录为 BSP 根目录。\033[0m") print( "\033[1;31;40m如果确定当前目录为 BSP 根目录,请先使用 <menuconfig> 命令来生成 .config 文件。\033[0m\n" ) print('No system configuration file : .config.') print('You should use < menuconfig > command to config bsp first.') if platform.system() == "Windows": os.system('chcp 437 > nul') return False bsp_packages_path = os.path.join(bsp_root, 'packages') if not os.path.exists(bsp_packages_path): os.mkdir("packages") os.chdir(bsp_packages_path) fp = open("pkgs.json", 'w') fp.write("[]") fp.close() fp = open("pkgs_error.json", 'w') fp.write("[]") fp.close() os.chdir(bsp_root) # prepare target packages file dbsqlite_pathname = os.path.join(bsp_packages_path, 'packages.dbsqlite') Export('dbsqlite_pathname') dbsqlite_pathname = dbsqlite_pathname.decode('gbk') # Avoid creating tables more than one time if not os.path.isfile(dbsqlite_pathname): conn = pkgsdb.get_conn(dbsqlite_pathname) sql = '''CREATE TABLE packagefile (pathname TEXT ,package TEXT ,md5 TEXT );''' pkgsdb.create_table(conn, sql) fn = '.config' pkgs = kconfig.parse(fn) newpkgs = pkgs if not os.path.exists(bsp_packages_path): os.mkdir(bsp_packages_path) pkgs_fn = os.path.join(bsp_packages_path, 'pkgs.json') # regenerate file : packages/pkgs.json if not os.path.exists(pkgs_fn): os.chdir(bsp_packages_path) fp = open("pkgs.json", 'w') fp.write("[]") fp.close() os.chdir(bsp_root) # Reading data back from pkgs.json with open(pkgs_fn, 'r') as f: oldpkgs = json.load(f) # regenerate file : packages/pkgs_error.json pkgs_error_list_fn = os.path.join(bsp_packages_path, 'pkgs_error.json') if not os.path.exists(pkgs_error_list_fn): os.chdir(bsp_packages_path) fp = open("pkgs_error.json", 'w') fp.write("[]") fp.close() os.chdir(bsp_root) # Reading data back from pkgs_error.json with open(pkgs_error_list_fn, 'r') as f: pkgs_error = json.load(f) # create SConscript file if not os.path.isfile(os.path.join(bsp_packages_path, 'SConscript')): bridge_script = file(os.path.join(bsp_packages_path, 'SConscript'), 'w') bridge_script.write(Bridge_SConscript) bridge_script.close() return [ oldpkgs, newpkgs, pkgs_error, pkgs_fn, pkgs_error_list_fn, bsp_packages_path, dbsqlite_pathname ]
def pre_package_update(): bsp_root = Import('bsp_root') if not os.path.exists('.config'): print( "Can't find file .config.Maybe your working directory isn't in bsp root now." ) print( "if your working directory isn't in bsp root now,please change your working directory to bsp root." ) print( "if your working directory is in bsp root now, please use menuconfig command to create .config file first." ) return bsp_packages_path = os.path.join(bsp_root, 'packages') if not os.path.exists(bsp_packages_path): os.mkdir("packages") os.chdir(bsp_packages_path) fp = open("pkgs.json", 'w') fp.write("[]") fp.close() fp = open("pkgs_error.json", 'w') fp.write("[]") fp.close() os.chdir(bsp_root) # prepare target packages file dbsqlite_pathname = os.path.join(bsp_packages_path, 'packages.dbsqlite') Export('dbsqlite_pathname') # Avoid creating tables more than one time if not os.path.isfile(dbsqlite_pathname): conn = pkgsdb.get_conn(dbsqlite_pathname) sql = '''CREATE TABLE packagefile (pathname TEXT ,package TEXT ,md5 TEXT );''' pkgsdb.create_table(conn, sql) fn = '.config' pkgs = kconfig.parse(fn) newpkgs = pkgs if not os.path.exists(bsp_packages_path): os.mkdir(bsp_packages_path) pkgs_fn = os.path.join(bsp_packages_path, 'pkgs.json') if not os.path.exists(pkgs_fn): print("Maybe you delete the file pkgs.json by mistake.") print("Do you want to create a new pkgs.json ?") rc = raw_input('Press the Y Key to create a new pkgs.json.') if rc == 'y' or rc == 'Y': os.chdir(bsp_packages_path) fp = open("pkgs.json", 'w') fp.write("[]") fp.close() os.chdir(bsp_root) print("Create a new file pkgs.json done.") # Reading data back from pkgs.json with open(pkgs_fn, 'r') as f: oldpkgs = json.load(f) pkgs_error_list_fn = os.path.join(bsp_packages_path, 'pkgs_error.json') if not os.path.exists(pkgs_error_list_fn): os.chdir(bsp_packages_path) fp = open("pkgs_error.json", 'w') fp.write("[]") fp.close() os.chdir(bsp_root) # print ("Create a new error file : pkgs_error.json.") # Reading data back from pkgs_error.json with open(pkgs_error_list_fn, 'r') as f: pkgs_error = json.load(f) # create SConscript file if not os.path.isfile(os.path.join(bsp_packages_path, 'SConscript')): bridge_script = file(os.path.join(bsp_packages_path, 'SConscript'), 'w') bridge_script.write(Bridge_SConscript) bridge_script.close() return [ oldpkgs, newpkgs, pkgs_error, pkgs_fn, pkgs_error_list_fn, bsp_packages_path, dbsqlite_pathname ]
def package_update(): """Update env's packages. Compare the old and new software package list and update the package. Remove unwanted packages and download the newly selected package. Check if the files in the deleted packages have been changed, and if so, remind the user saved the modified file. Args: none Returns: none Raises: none """ bsp_root = Import('bsp_root') env_root = Import('env_root') flag = True #print bsp_root #print env_root if not os.path.exists('.config'): print( "Can't find file .config.Maybe your working directory isn't in bsp root now." ) print( "if your working directory isn't in bsp root now,please change your working directory to bsp root." ) print( "if your working directory is in bsp root now, please use menuconfig command to create .config file first." ) return bsp_packages_path = os.path.join(bsp_root, 'packages') if not os.path.exists(bsp_packages_path): os.mkdir("packages") os.chdir(bsp_packages_path) fp = open("pkgs.json", 'w') fp.write("[]") fp.close() os.chdir(bsp_root) # prepare target packages file dbsqlite_pathname = os.path.join(bsp_packages_path, 'packages.dbsqlite') Export('dbsqlite_pathname') # Avoid creating tables more than one time if not os.path.isfile(dbsqlite_pathname): conn = pkgsdb.get_conn(dbsqlite_pathname) sql = '''CREATE TABLE packagefile (pathname TEXT ,package TEXT ,md5 TEXT );''' pkgsdb.create_table(conn, sql) fn = '.config' pkgs = kconfig.parse(fn) newpkgs = pkgs if not os.path.exists(bsp_packages_path): os.mkdir(bsp_packages_path) pkgs_fn = os.path.join(bsp_packages_path, 'pkgs.json') if not os.path.exists(pkgs_fn): print("Maybe you delete the file pkgs.json by mistake.") print("Do you want to create a new pkgs.json ?") rc = raw_input('Press the Y Key to create a new pkgs.json.') if rc == 'y' or rc == 'Y': os.chdir(bsp_packages_path) fp = open("pkgs.json", 'w') fp.write("[]") fp.close() os.chdir(bsp_root) print("Create a new file pkgs.json done.") # Reading data back from pkgs.json with open(pkgs_fn, 'r') as f: oldpkgs = json.load(f) #print "newpkgs:",newpkgs #print "oldpkgs:",oldpkgs # 1.in old ,not in new casedelete = SubList(oldpkgs, newpkgs) for pkg in casedelete: dirpath = pkg['path'] ver = pkg['ver'] #print 'ver is :',ver[1:] if dirpath[0] == '/' or dirpath[0] == '\\': dirpath = dirpath[1:] #dirpath = dirpath.replace('/', '\\') dirpath = os.path.basename(dirpath) #print "basename:",os.path.basename(dirpath) removepath = os.path.join(bsp_packages_path, dirpath) #print "floder to delere",removepath # Delete. Git directory. if os.path.isdir(removepath): #uppername = str.upper(str(os.path.basename(removepath))) #dirname = os.path.dirname(removepath) #gitdir = os.path.join(dirname,uppername) gitdir = removepath print( "\nOperation : Delete a git package or change the version of a package." ) print( "If you want to change the version of a package,you should aslo delete the old package before update.\nOtherwise,you may fail to update.\n" ) print("Folder to delete: %s" % (gitdir)) print( "The folder is managed by git,are you sure you want to delete this folder?\n" ) rc = raw_input( 'Press the Y Key to delete the folder or just press Enter to keep the file:' ) if rc == 'y' or rc == 'Y': if platform.system() != "Windows": shutil.rmtree(gitdir) else: cmd = 'rd /s /q ' + gitdir os.system(cmd) if os.path.isdir(gitdir): if platform.system() != "Windows": shutil.rmtree(gitdir) else: cmd = 'rd /s /q ' + gitdir os.system(cmd) print("Delete not entirely,try again.") else: print("Folder has been removed.") else: removepath = removepath + '-' + ver[1:] #print removepath pkgsdb.deletepackdir(removepath, dbsqlite_pathname) # 2.in old and in new caseinoperation = AndList(newpkgs, oldpkgs) # 3.in new not in old # If the package download fails, record it, and then download again when the update command is executed. casedownload = SubList(newpkgs, oldpkgs) #print 'in new not in old:',casedownload list = [] for pkg in casedownload: if install_pkg(env_root, bsp_root, pkg): print( "==============================> %s %s is downloaded successfully. \n" % (pkg['name'], pkg['ver'])) else: list.append( pkg) # If the PKG download fails, record it in the list. print pkg, 'download failed.' flag = False newpkgs = SubList(newpkgs, list) # Get the currently updated configuration. # Give hints based on the success of the download. if len(list): print("Package download failed list: %s \n" % list) print( "You need to reuse the 'pkgs -update' command to download again.\n" ) # Writes the updated configuration to pkgs.json file. # Packages that are not downloaded correctly will be redownloaded at the next update. pkgs_file = file(pkgs_fn, 'w') pkgs_file.write(json.dumps(newpkgs, indent=1)) pkgs_file.close() # update SConscript file if not os.path.isfile(os.path.join(bsp_packages_path, 'SConscript')): bridge_script = file(os.path.join(bsp_packages_path, 'SConscript'), 'w') bridge_script.write(Bridge_SConscript) bridge_script.close() # Check to see if the packages stored in the Json file list actually exist, # and then download the packages if they don't exist. with open(pkgs_fn, 'r') as f: read_back_pkgs_json = json.load(f) #print(read_back_pkgs_json) error_packages_list = [] error_packages_redownload_error_list = [] for pkg in read_back_pkgs_json: dirpath = pkg['path'] ver = pkg['ver'] #print 'ver is :',ver[1:] if dirpath[0] == '/' or dirpath[0] == '\\': dirpath = dirpath[1:] dirpath = os.path.basename(dirpath) removepath = os.path.join(bsp_packages_path, dirpath) #print "if floder exist",removepath removepath_ver = removepath + '-' + ver[1:] #print "if floder exist",removepath if os.path.exists(removepath): continue elif os.path.exists(removepath_ver): continue else: error_packages_list.append(pkg) if len(error_packages_list): print("\n==============================> Error packages list : \n") for pkg in error_packages_list: print pkg['name'], pkg['ver'] print("\nThe package in the list above is accidentally deleted.") print( "Env will redownload packages that have been accidentally deleted." ) print( "If you really want to remove these packages, do that in the menuconfig command.\n" ) for pkg in error_packages_list: # Redownloaded the packages in error_packages_list if install_pkg(env_root, bsp_root, pkg): print( "==============================> %s %s is redownloaded successfully. \n" % (pkg['name'], pkg['ver'])) else: error_packages_redownload_error_list.append(pkg) print pkg, 'download failed.' flag = False if len(error_packages_redownload_error_list): print("%s" % error_packages_redownload_error_list) print( "Packages:%s,%s redownloed error,you need to use 'pkgs --update' command again to redownload them." % pkg['name'], pkg['ver']) write_back_pkgs_json = SubList( read_back_pkgs_json, error_packages_redownload_error_list) read_back_pkgs_json = write_back_pkgs_json #print("write_back_pkgs_json:%s"%write_back_pkgs_json) pkgs_file = file(pkgs_fn, 'w') pkgs_file.write(json.dumps(write_back_pkgs_json, indent=1)) pkgs_file.close() else: print( "\nAll the selected packages have been downloaded successfully.\n") # If the selected package is the latest version, # check to see if it is the latest version after the update command, # if not, then update the latest version from the remote repository. # If the download has a conflict, you are currently using the prompt message provided by git. beforepath = os.getcwd() for pkg in read_back_pkgs_json: package = Package() pkg_path = pkg['path'] if pkg_path[0] == '/' or pkg_path[0] == '\\': pkg_path = pkg_path[1:] pkg_path = os.path.join(env_root, 'packages', pkg_path, 'package.json') package.parse(pkg_path) pkgs_name_in_json = package.get_name() if pkg['ver'] == "latest_version" or pkg['ver'] == "latest": repo_path = os.path.join(bsp_packages_path, pkgs_name_in_json) ver_sha = package.get_versha(pkg['ver']) #print repo_path, ver_sha os.chdir(repo_path) cmd = 'git pull' # Only one trace relationship can be used directly with git pull. os.system(cmd) os.chdir(beforepath) print("==============================> %s update done \n" % (pkgs_name_in_json)) if flag: print("Operation completed successfully.") else: print("Operation failed.")