예제 #1
0
def install_pkg(env_root, bsp_root, pkg):
    """Install the required packages."""

    # default true
    ret = True
    local_pkgs_path = os.path.join(env_root, 'local_pkgs')
    bsp_pkgs_path = os.path.join(bsp_root, 'packages')

    # get the .config file from env
    env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds')
    env_config_file = os.path.join(env_kconfig_path, '.config')

    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)

    url_from_json = package.get_url(pkg['ver'])
    package_url = package.get_url(pkg['ver'])
    pkgs_name_in_json = package.get_name()

    if package_url[-4:] == '.git':
        ver_sha = package.get_versha(pkg['ver'])

    # print("==================================================>")
    # print("packages name :"%pkgs_name_in_json.encode("utf-8"))
    # print("ver :"%pkg['ver'])
    # print("url :"%package_url.encode("utf-8"))
    # print("url_from_json : "%url_from_json.encode("utf-8"))
    # print("==================================================>")

    get_package_url = None
    get_ver_sha = None
    upstream_change_flag = False

    try:
        if (not os.path.isfile(env_config_file)) or (
                os.path.isfile(env_config_file) and find_macro_in_config(
                    env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE')):
            get_package_url, get_ver_sha = get_url_from_mirror_server(
                pkgs_name_in_json, pkg['ver'])

            #  determine whether the package package url is valid
            if get_package_url != None and determine_url_valid(
                    get_package_url):
                package_url = get_package_url

                if get_ver_sha != None:
                    ver_sha = get_ver_sha

                upstream_change_flag = True
    except Exception, e:
        # print('e.message:%s\t' % e.message)
        print(
            "Failed to connect to the mirror server, package will be downloaded from non-mirror server.\n"
        )
예제 #2
0
def install_pkg(env_root, bsp_root, pkg):
    ret = True
    local_pkgs_path = os.path.join(env_root, 'local_pkgs')
    bsp_pkgs_path = os.path.join(bsp_root, 'packages')

    package = Package()
    pkg_path = pkg['path']
    if pkg_path[0] == '/' or pkg_path[0] == '\\': pkg_path = pkg_path[1:]

    #pkg_path = pkg_path.replace('/', '\\')
    pkg_path = os.path.join(env_root, 'packages', pkg_path, 'package.json')

    package.parse(pkg_path)

    package_url = package.get_url(pkg['ver'])
    package_name = pkg['name']
    pkgs_name_in_json = package.get_name()

    #print "get name here:",pkgs_name_in_json
    #print "ver:",pkg['ver']
    #print "url:",package_url
    #print "name:",package_name

    beforepath = os.getcwd()

    if package_url[-4:] == '.git':
        ver_sha = package.get_versha(pkg['ver'])
        repo_path = os.path.join(bsp_pkgs_path, pkgs_name_in_json)
        cmd = 'git clone ' + package_url + ' ' + repo_path
        os.system(cmd)
        os.chdir(repo_path)
        cmd = 'git checkout ' + ver_sha
        os.system(cmd)
        cmd = 'git submodule init '
        os.system(cmd)
        cmd = 'git submodule update '
        if not os.system(cmd):
            print "Submodule update success"
        os.chdir(beforepath)
    else:
        # download package
        if not package.download(pkg['ver'], local_pkgs_path):
            ret = False
            return ret

        pkg_dir = package.get_filename(pkg['ver'])
        pkg_dir = os.path.splitext(pkg_dir)[0]

        pkg_fullpath = os.path.join(local_pkgs_path,
                                    package.get_filename(pkg['ver']))

        # unpack package
        if not os.path.exists(pkg_dir):
            package.unpack(pkg_fullpath, bsp_pkgs_path)
            ret = True

    return ret
예제 #3
0
def install_pkg(env_root, bsp_root, pkg):
    """Install the required packages."""

    # default true
    ret = True
    local_pkgs_path = os.path.join(env_root, 'local_pkgs')
    bsp_pkgs_path = os.path.join(bsp_root, 'packages')

    env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds')
    # get the .config file from env
    env_config_file = os.path.join(env_kconfig_path, '.config')

    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)

    url_from_json = package.get_url(pkg['ver'])
    package_url = package.get_url(pkg['ver'])
    #package_name = pkg['name']
    pkgs_name_in_json = package.get_name()

    if package_url[-4:] == '.git':
        ver_sha = package.get_versha(pkg['ver'])

    # print("==================================================>")
    # print "packages name:",pkgs_name_in_json
    # print "ver:",pkg['ver']
    # print "url:",package_url
    # print "url_from_json: ",url_from_json
    # print("==================================================>")

    get_package_url = None
    get_ver_sha = None
    upstream_change_flag = False

    if os.path.isfile(env_config_file) and find_macro_in_config(
            env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
        get_package_url, get_ver_sha = get_url_from_mirror_server(
            pkgs_name_in_json, pkg['ver'])

        #  determine whether the package package url is valid
        if get_package_url != None and determine_url_valid(get_package_url):
            package_url = get_package_url

            if get_ver_sha != None:
                ver_sha = get_ver_sha

            upstream_change_flag = True

    if package_url[-4:] == '.git':

        repo_path = os.path.join(bsp_pkgs_path, pkgs_name_in_json)
        repo_path = repo_path + '-' + pkg['ver']

        cmd = 'git clone ' + package_url + ' ' + repo_path
        execute_command(cmd, cwd=bsp_pkgs_path)

        cmd = 'git checkout -q ' + ver_sha
        execute_command(cmd, cwd=repo_path)

        if upstream_change_flag:
            cmd = 'git remote set-url origin ' + url_from_json
            execute_command(cmd, cwd=repo_path)

        # If there is a .gitmodules file in the package, prepare to update the
        # submodule.
        submod_path = os.path.join(repo_path, '.gitmodules')
        if os.path.isfile(submod_path):
            print("Start to update submodule")

            if os.path.isfile(env_config_file) and find_macro_in_config(
                    env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
                replace_list = modify_submod_file_to_mirror(
                    submod_path)  # Modify .gitmodules file

            cmd = 'git submodule update --init --recursive'
            execute_command(cmd, cwd=repo_path)

            if os.path.isfile(env_config_file) and find_macro_in_config(
                    env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
                if len(replace_list):
                    for item in replace_list:
                        submod_dir_path = os.path.join(repo_path, item[2])
                        if os.path.isdir(submod_dir_path):
                            cmd = 'git remote set-url origin ' + item[0]
                            execute_command(cmd, cwd=submod_dir_path)

        if os.path.isfile(env_config_file) and find_macro_in_config(
                env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE'):
            if os.path.isfile(submod_path):
                cmd = 'git checkout .gitmodules'
                execute_command(cmd, cwd=repo_path)

    else:
        # Download a package of compressed package type.
        if not package.download(pkg['ver'], local_pkgs_path, package_url):
            return False

        pkg_dir = package.get_filename(pkg['ver'])
        pkg_dir = os.path.splitext(pkg_dir)[0]
        pkg_fullpath = os.path.join(local_pkgs_path,
                                    package.get_filename(pkg['ver']))

        if not archive.packtest(pkg_fullpath):
            return False

        # unpack package
        if not os.path.exists(pkg_dir):
            try:
                if not package.unpack(pkg_fullpath, bsp_pkgs_path, pkg,
                                      pkgs_name_in_json):
                    ret = False
            except Exception, e:
                os.remove(pkg_fullpath)
                ret = False
                print('e.message: %s\t' % e.message)
예제 #4
0
def install_pkg(env_root, pkgs_root, bsp_root, pkg):
    """Install the required packages."""

    # default true
    ret = True
    local_pkgs_path = os.path.join(env_root, 'local_pkgs')
    bsp_pkgs_path = os.path.join(bsp_root, 'packages')

    # get the .config file from env
    env_kconfig_path = os.path.join(env_root, 'tools\scripts\cmds')
    env_config_file = os.path.join(env_kconfig_path, '.config')

    package = Package()
    pkg_path = pkg['path']
    if pkg_path[0] == '/' or pkg_path[0] == '\\':
        pkg_path = pkg_path[1:]
    pkg_path = os.path.join(pkgs_root, pkg_path, 'package.json')
    package.parse(pkg_path)

    url_from_json = package.get_url(pkg['ver'])
    package_url = package.get_url(pkg['ver'])
    pkgs_name_in_json = package.get_name()

    if package_url[-4:] == '.git':
        ver_sha = package.get_versha(pkg['ver'])

    # print("==================================================>")
    # print("packages name :"%pkgs_name_in_json.encode("utf-8"))
    # print("ver :"%pkg['ver'])
    # print("url :"%package_url.encode("utf-8"))
    # print("url_from_json : "%url_from_json.encode("utf-8"))
    # print("==================================================>")

    get_package_url = None
    get_ver_sha = None
    upstream_change_flag = False

    try:
        if (not os.path.isfile(env_config_file)) or (
                os.path.isfile(env_config_file) and find_macro_in_config(
                    env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE')):
            get_package_url, get_ver_sha = get_url_from_mirror_server(
                pkgs_name_in_json, pkg['ver'])

            #  determine whether the package package url is valid
            if get_package_url != None and determine_url_valid(
                    get_package_url):
                package_url = get_package_url

                if get_ver_sha != None:
                    ver_sha = get_ver_sha

                upstream_change_flag = True
    except Exception as e:
        print('error message:%s\t' % e)
        print(
            "Failed to connect to the mirror server, package will be downloaded from non-mirror server.\n"
        )

    if package_url[-4:] == '.git':
        try:
            repo_path = os.path.join(bsp_pkgs_path, pkgs_name_in_json)
            repo_path = repo_path + '-' + pkg['ver']
            repo_path_full = '"' + repo_path + '"'

            clone_cmd = 'git clone ' + package_url + ' ' + repo_path_full
            execute_command(clone_cmd, cwd=bsp_pkgs_path)

            git_check_cmd = 'git checkout -q ' + ver_sha
            execute_command(git_check_cmd, cwd=repo_path)

        except Exception as e:
            print(
                "\nFailed to download software package with git. Please check the network connection."
            )
            os.chdir(before)
            return False

        if upstream_change_flag:
            cmd = 'git remote set-url origin ' + url_from_json
            execute_command(cmd, cwd=repo_path)

        # If there is a .gitmodules file in the package, prepare to update submodule.
        submod_path = os.path.join(repo_path, '.gitmodules')
        if os.path.isfile(submod_path):
            print("Start to update submodule")
            # print("开始更新软件包子模块")

            if (not os.path.isfile(env_config_file)) or (
                    os.path.isfile(env_config_file) and find_macro_in_config(
                        env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE')):
                # print("开启了镜像加速,开始修改 .gitmodules 文件")
                replace_list = modify_submod_file_to_mirror(
                    submod_path)  # Modify .gitmodules file

            # print("开始执行更新动作")
            cmd = 'git submodule update --init --recursive'
            execute_command(cmd, cwd=repo_path)

            if (not os.path.isfile(env_config_file)) or (
                    os.path.isfile(env_config_file) and find_macro_in_config(
                        env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE')):
                if len(replace_list):
                    for item in replace_list:
                        submod_dir_path = os.path.join(repo_path, item[2])
                        if os.path.isdir(submod_dir_path):
                            cmd = 'git remote set-url origin ' + item[0]
                            execute_command(cmd, cwd=submod_dir_path)

        if (not os.path.isfile(env_config_file)) or (
                os.path.isfile(env_config_file) and find_macro_in_config(
                    env_config_file, 'SYS_PKGS_DOWNLOAD_ACCELERATE')):
            if os.path.isfile(submod_path):
                cmd = 'git checkout .gitmodules'
                execute_command(cmd, cwd=repo_path)

    else:
        # Download a package of compressed package type.
        if not package.download(pkg['ver'], local_pkgs_path, package_url):
            return False

        pkg_dir = package.get_filename(pkg['ver'])
        pkg_dir = os.path.splitext(pkg_dir)[0]
        pkg_fullpath = os.path.join(local_pkgs_path,
                                    package.get_filename(pkg['ver']))

        if not archive.packtest(pkg_fullpath):
            print("package : %s is invalid" % pkg_fullpath.encode("utf-8"))
            return False

        # unpack package
        if not os.path.exists(pkg_dir):

            try:
                if not package.unpack(pkg_fullpath, bsp_pkgs_path, pkg,
                                      pkgs_name_in_json):
                    ret = False
            except Exception as e:
                os.remove(pkg_fullpath)
                ret = False
                print('error message: %s\t' % e)
        else:
            print("The file does not exist.")
    return ret
예제 #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.")