示例#1
0
def delete_zip_package(pkg, remove_path_with_version, package_delete_fail_list,
                       sqlite_pathname):
    if os.path.isdir(remove_path_with_version):
        print("Start to remove %s \nplease wait..." %
              remove_path_with_version.encode("utf-8"))
        try:
            pkgsdb.deletepackdir(remove_path_with_version, sqlite_pathname)
        except Exception as e:
            package_delete_fail_list.append(pkg)
            logging.warning(
                'Error message:\n%s %s. %s \n\t' %
                ("Delete folder failed, please delete the folder manually",
                 remove_path_with_version.encode("utf-8"), e))
示例#2
0
def package_update(isDeleteOld=False):
    """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.
    """

    pkgs_update_log = Logger('pkgs_update', logging.WARNING)
    bsp_root = Import('bsp_root')
    env_root = Import('env_root')
    flag = True
    sys_value = pre_package_update()
    oldpkgs = sys_value[0]
    newpkgs = sys_value[1]
    pkgs_delete_error_list = sys_value[2]
    pkgs_fn = sys_value[3]
    pkgs_error_list_fn = sys_value[4]
    bsp_packages_path = sys_value[5]
    dbsqlite_pathname = sys_value[6]

    pkgs_update_log.info('[Line: %d][Message : Begin to remove packages]' %
                         sys._getframe().f_lineno)
    pkgs_update_log.info('[Line: %d][Message : oldpkgs: %s ]' %
                         (sys._getframe().f_lineno, oldpkgs))
    pkgs_update_log.info('[Line: %d][Message : newpkgs: %s ]' %
                         (sys._getframe().f_lineno, newpkgs))
    pkgs_update_log.info('[Line: %d][Message : pkgs_delete_error_list: %s ]' %
                         (sys._getframe().f_lineno, pkgs_delete_error_list))

    if len(pkgs_delete_error_list):
        for error_package in pkgs_delete_error_list:
            removepath_ver = get_package_remove_path(error_package,
                                                     bsp_packages_path)

            if os.path.isdir(removepath_ver):
                print(
                    "\nError: %s package delete failed, begin to remove it." %
                    error_package['name'])

                if rm_package(removepath_ver) == False:
                    print(
                        "Error: Delete package %s failed! Please delete the folder manually.\n"
                        % error_package['name'])
                    return

    # 1.in old ,not in new : Software packages that need to be removed.
    casedelete = sub_list(oldpkgs, newpkgs)
    pkgs_delete_fail_list = []

    for pkg in casedelete:

        removepath_ver = get_package_remove_path(pkg, bsp_packages_path)
        removepath_git = os.path.join(removepath_ver, '.git')

        # print "removepath_git to delete",removepath_git
        # Delete. Git directory.

        if os.path.isdir(removepath_ver) and os.path.isdir(removepath_git):
            gitdir = removepath_ver

            print("\nStart to remove %s, please wait...\n" % gitdir)
            if isDeleteOld:
                if rm_package(gitdir) == False:
                    print("Floder delete fail: %s" % gitdir)
                    print("Please delete this folder manually.")
            else:
                print(
                    "The folder is managed by git. Do you want to delete this folder?\n"
                )
                rc = raw_input(
                    'Press the Y Key to delete the folder or just press Enter to keep it : '
                )
                if rc == 'y' or rc == 'Y':
                    try:
                        if rm_package(gitdir) == False:
                            pkgs_delete_fail_list.append(pkg)
                            print("Error: Please delete the folder manually.")
                    except Exception, e:
                        print('Error message:%s%s. error.message: %s\n\t' %
                              ("Delete folder failed: ", gitdir, e.message))
        else:
            if os.path.isdir(removepath_ver):
                print("Start to remove %s, please wait...\n" % removepath_ver)
                try:
                    pkgsdb.deletepackdir(removepath_ver, dbsqlite_pathname)
                except Exception, e:
                    pkgs_delete_fail_list.append(pkg)
                    print('Error message:\n%s %s. %s \n\t' % (
                        "Delete folder failed, please delete the folder manually",
                        removepath_ver, e.message))
示例#3
0
def package_update(isDeleteOld=False):
    """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.
    """

    sys_value = pre_package_update()

    if not sys_value:
        return

    bsp_root = Import('bsp_root')
    env_root = Import('env_root')
    flag = True

    # According to the env version, whether Chinese output is supported or not
    if determine_support_chinese(env_root):
        if platform.system() == "Windows":
            os.system('chcp 65001 > nul')

    oldpkgs = sys_value[0]
    newpkgs = sys_value[1]
    pkgs_delete_error_list = sys_value[2]
    pkgs_fn = sys_value[3]
    pkgs_error_list_fn = sys_value[4]
    bsp_packages_path = sys_value[5]
    dbsqlite_pathname = sys_value[6]

    if len(pkgs_delete_error_list):
        for error_package in pkgs_delete_error_list:
            removepath_ver = get_package_remove_path(error_package,
                                                     bsp_packages_path)

            if os.path.isdir(removepath_ver):
                print(
                    "\nError: %s package delete failed, begin to remove it." %
                    error_package['name'].encode("utf-8"))

                if rm_package(removepath_ver) == False:
                    print(
                        "Error: Delete package %s failed! Please delete the folder manually.\n"
                        % error_package['name'].encode("utf-8"))
                    return

    # 1.in old ,not in new : Software packages that need to be removed.
    casedelete = sub_list(oldpkgs, newpkgs)
    pkgs_delete_fail_list = []

    for pkg in casedelete:

        removepath_ver = get_package_remove_path(pkg, bsp_packages_path)
        removepath_git = os.path.join(removepath_ver, '.git')

        # Delete. Git directory.
        if os.path.isdir(removepath_ver) and os.path.isdir(removepath_git):
            gitdir = removepath_ver

            print("\nStart to remove %s \nplease wait..." %
                  gitdir.encode("utf-8"))
            if isDeleteOld:
                if rm_package(gitdir) == False:
                    print("Floder delete fail: %s" % gitdir.encode("utf-8"))
                    print("Please delete this folder manually.")
            else:
                print(
                    "The folder is managed by git. Do you want to delete this folder?\n"
                )
                rc = raw_input(
                    'Press the Y Key to delete the folder or just press Enter to keep it : '
                )
                if rc == 'y' or rc == 'Y':
                    try:
                        if rm_package(gitdir) == False:
                            pkgs_delete_fail_list.append(pkg)
                            print("Error: Please delete the folder manually.")
                    except Exception, e:
                        print('Error message:%s%s. error.message: %s\n\t' %
                              ("Delete folder failed: ",
                               gitdir.encode("utf-8"), e.message))
        else:
            if os.path.isdir(removepath_ver):
                print("Start to remove %s \nplease wait..." %
                      removepath_ver.encode("utf-8"))
                try:
                    pkgsdb.deletepackdir(removepath_ver, dbsqlite_pathname)
                except Exception, e:
                    pkgs_delete_fail_list.append(pkg)
                    print('Error message:\n%s %s. %s \n\t' % (
                        "Delete folder failed, please delete the folder manually",
                        removepath_ver.encode("utf-8"), e.message))
示例#4
0
def package_update(isDeleteOld=False):
    """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.
    """

    sys_value = pre_package_update()

    if not sys_value:
        return

    bsp_root = Import('bsp_root')
    env_root = Import('env_root')
    pkgs_root = Import('pkgs_root')
    flag = True

    # According to the env version, whether Chinese output is supported or not
    if determine_support_chinese(env_root):
        if platform.system() == "Windows":
            os.system('chcp 65001 > nul')

    oldpkgs = sys_value[0]
    newpkgs = sys_value[1]
    pkgs_delete_error_list = sys_value[2]
    pkgs_fn = sys_value[3]
    pkgs_error_list_fn = sys_value[4]
    bsp_packages_path = sys_value[5]
    dbsqlite_pathname = sys_value[6]

    # print(oldpkgs)
    # print(newpkgs)

    if len(pkgs_delete_error_list):
        for error_package in pkgs_delete_error_list:
            removepath_ver = get_package_remove_path(error_package,
                                                     bsp_packages_path)

            if os.path.isdir(removepath_ver):
                print(
                    "\nError: %s package delete failed, begin to remove it." %
                    error_package['name'].encode("utf-8"))

                if rm_package(removepath_ver) == False:
                    print(
                        "Error: Delete package %s failed! Please delete the folder manually.\n"
                        % error_package['name'].encode("utf-8"))
                    return

    # 1.in old ,not in new : Software packages that need to be removed.
    casedelete = sub_list(oldpkgs, newpkgs)
    pkgs_delete_fail_list = []

    for pkg in casedelete:

        removepath_ver = get_package_remove_path(pkg, bsp_packages_path)
        removepath_git = os.path.join(removepath_ver, '.git')

        # Delete. Git directory.
        if os.path.isdir(removepath_ver) and os.path.isdir(removepath_git):
            gitdir = removepath_ver

            print("\nStart to remove %s \nplease wait..." %
                  gitdir.encode("utf-8"))
            if isDeleteOld:
                if rm_package(gitdir) == False:
                    print("Floder delete fail: %s" % gitdir.encode("utf-8"))
                    print("Please delete this folder manually.")
            else:
                print(
                    "The folder is managed by git. Do you want to delete this folder?\n"
                )
                if sys.version_info < (3, 0):
                    rc = raw_input(
                        'Press the Y Key to delete the folder or just press Enter to keep it : '
                    )
                else:
                    rc = input(
                        'Press the Y Key to delete the folder or just press Enter to keep it : '
                    )

                if rc == 'y' or rc == 'Y':
                    try:
                        if rm_package(gitdir) == False:
                            pkgs_delete_fail_list.append(pkg)
                            print("Error: Please delete the folder manually.")
                    except Exception as e:
                        print('Error message:%s%s. error.message: %s\n\t' %
                              ("Delete folder failed: ",
                               gitdir.encode("utf-8"), e))
        else:
            if os.path.isdir(removepath_ver):
                print("Start to remove %s \nplease wait..." %
                      removepath_ver.encode("utf-8"))
                try:
                    pkgsdb.deletepackdir(removepath_ver, dbsqlite_pathname)
                except Exception as e:
                    pkgs_delete_fail_list.append(pkg)
                    print('Error message:\n%s %s. %s \n\t' % (
                        "Delete folder failed, please delete the folder manually",
                        removepath_ver.encode("utf-8"), e))

    if len(pkgs_delete_fail_list):
        # write error messages
        pkgs_file = file(pkgs_error_list_fn, 'w')
        pkgs_file.write(json.dumps(pkgs_delete_fail_list, indent=1))
        pkgs_file.close()
        return
    else:
        # write error messages
        with open(pkgs_error_list_fn, 'w') as f:
            f.write(str(json.dumps(pkgs_delete_fail_list, indent=1)))

    # 2.in new not in old : Software packages to be installed.
    # If the package download fails, record it, and then download again when
    # the update command is executed.

    casedownload = sub_list(newpkgs, oldpkgs)
    # print 'in new not in old:', casedownload
    pkgs_download_fail_list = []

    for pkg in casedownload:
        if install_pkg(env_root, pkgs_root, bsp_root, pkg):
            print(
                "==============================>  %s %s is downloaded successfully. \n"
                % (pkg['name'], pkg['ver']))
        else:
            # If the PKG download fails, record it in the
            # pkgs_download_fail_list.
            pkgs_download_fail_list.append(pkg)
            print(pkg, 'download failed.')
            flag = False

    # Get the currently updated configuration.
    newpkgs = sub_list(newpkgs, pkgs_download_fail_list)

    # Give hints based on the success of the download.
    if len(pkgs_download_fail_list):
        print("\nPackage download failed list:")
        for item in pkgs_download_fail_list:
            print(item)

        print(
            "You need to reuse the <pkgs -update> command to download again.")

    # update pkgs.json and SConscript
    write_storage_file(pkgs_fn, newpkgs)

    # handle download error packages.
    get_flag = handle_download_error_packages(pkgs_fn, bsp_packages_path)

    if get_flag != None:
        flag = get_flag

    # Update the software packages, which the version is 'latest'
    try:
        update_latest_packages(pkgs_fn, bsp_packages_path)
    except KeyboardInterrupt:
        flag = False

    if flag:
        print("Operation completed successfully.")
    else:
        print("Operation failed.")
示例#5
0
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.")