document.write("");

将.gradle下的 带hash名称文件夹中的依赖 转换为 .m2上的依赖

背景:  android studio 在无法下载依赖的情况下 , 仅 使用 mavenLocal()

本地 .gradle 下有对应依赖 , .m2下没有

故将.gradle下的 带hash名称文件夹中的依赖 转换为 .m2上的依赖

 

-- 2024 更新

之前的脚本运行过,近期打算再次使用,发现脚本运行后数据有误,百思不得其解,毕竟之前是正常使用过的,只能说是发布博客时,手动脱敏过程中不小心误改了什么内容,

新调整了一版代码

import os
import io
import sys
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
pathsimple = r'D:\XXX\XXXX\.gradle\caches\modules-2\files-2.1'
pathtwoin = 'D:/xx/xx/repository'
patho = ''

def backup_file(path, path_two, paths):
    path = get_gradle_path(paths)
    path_two = path_two.replace('/', '\\')
    if not os.path.exists(path_two):
        os.makedirs(path_two)
    """
    :param path:   路径1
    :param path_two: 路径2
    :return:
    """
    if os.path.isdir(path) and os.path.isdir(path_two):  # 判断传入的值为文件夹
        a = os.listdir(path)  # 读取该路径下的文件为列表
        for i in a:
            if os.path.isdir(path + '\\' + i):  # 判断传入的值为文件夹
                patho = path + '\\' + i
                af = os.listdir(patho)  # 读取该路径下的文件为列表
                print (af)
                for m in af:
                    po = os.path.join(patho, m)  # 路径1拼接
                    po_two = os.path.join(path_two, m)  # 路径2拼接
                    with open(po, "rb") as f:
                        res_one = f.read()
                        with open(po_two, "wb") as a:
                            a.write(res_one)
                            print("{}复制成功".format(i))
            else:               
                po = os.path.join(path, i)  # 路径1拼接
                po_two = os.path.join(path_two, i)  # 路径2拼接
                with open(po, "rb") as f:
                    res_one = f.read()
                    with open(po_two, "wb") as a:
                        a.write(res_one)
                        print("{}复制成功".format(i))
    else:
        print("不是文件夹")

path_two = ''

def get_gradle_path(ones):
    global pathsimple
    initPath = pathsimple
    for m in range(len(ones)):
        initPath += '\\' + ones[m]
    if os.path.exists(initPath):
        print ('转换后的 gradle地址 存在', initPath)
    else:
        print ('不存在,请手动传递地址')
    return initPath

def getbypath(path):
    global pathtwoin
    initPath = pathtwoin
    paths = path.split(':')
    pathsone = paths[0].split('.')
    index = 0
    for p in  paths:
        if index == 0:
            for pa in pathsone:
                initPath += '/' + pa
        else :
            initPath += '/' + p
        index += 1
    backup_file('', initPath, paths)

def listpath (need_path):
    for pat in need_path:
        getbypath(pat)

def get_dependencies():
    global pathsimple
    dependencies_data = []
    if os.path.isdir(pathsimple):  # 判断传入的值为文件夹
        dirstpath = os.listdir(pathsimple)  # 读取该路径下的文件为列表
        for i in dirstpath:
            if os.path.isdir(pathsimple + '\\' + i):  # 判断传入的值为文件夹
                path1 = path1 = pathsimple + '\\' + i
                dirstpath1 = os.listdir(path1)
                for i1 in dirstpath1:
                    path2 = path1 + '\\' + i1
                    if os.path.isdir(path2):  # 判断传入的值为文件夹
                        dirstpath2 = os.listdir(path2)
                        for i2 in dirstpath2:
                            str = i + ':' + i1 + ':' + i2
                            dependencies_data.append(str)
    listpath(dependencies_data)
    
get_dependencies()
# getbypath('com.squareup.okhttp3:okhttp:3.8.0')

 

---旧版

 

import os
import io
import sys
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

patho = ''

def backup_file(path, path_two):
    path = get_gradle_path(path_two)
    path_two = path_two.replace('/', '\\')
    print (path_two)
    if not os.path.exists(path_two):
        os.makedirs(path_two)
    """
    :param path:   路径1
    :param path_two: 路径2
    :return:
    """
    if os.path.isdir(path) and os.path.isdir(path_two):  # 判断传入的值为文件夹
        a = os.listdir(path)  # 读取该路径下的文件为列表
        print (a[0])
        print (len(a))
        # if os.path.isdir(path_two):
        for i in a:
            if os.path.isdir(path + '\\' + i):  # 判断传入的值为文件夹
                patho = path + '\\' + i
                af = os.listdir(patho)  # 读取该路径下的文件为列表
                print (af)
                for m in af:
                    po = os.path.join(patho, m)  # 路径1拼接
                    po_two = os.path.join(path_two, m)  # 路径2拼接
                    with open(po, "rb") as f:
                        res_one = f.read()
                        with open(po_two, "wb") as a:
                            a.write(res_one)
                            print("{}复制成功".format(i))
            else:               
                po = os.path.join(path, i)  # 路径1拼接
                po_two = os.path.join(path_two, i)  # 路径2拼接
                with open(po, "rb") as f:
                    res_one = f.read()
                    with open(po_two, "wb") as a:
                        a.write(res_one)
                        print("{}复制成功".format(i))
    else:
        print("不是文件夹")

path_two = ''
pathsimple = r'D:\xxxxx\workspace\.gradle\caches\modules-2\files-2.1'
def get_gradle_path(pathtwo):
    ones = pathtwo.split('/')
    print (ones)
    pathsimple = r'D:\xxxxx\workspace\.gradle\caches\modules-2\files-2.1'
    for m in range(len(ones) - 5):
        print (ones[m + 5])
        pathsimple += '\\' + ones[m + 5]
    res = ''
    if len(ones) == 8:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            res += m + '\\'
            index += 1
    if len(ones) == 9:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            if index == 7 :
                res += m + '.'
            else:
                res += m + '\\'
            index += 1
    if len(ones) == 10:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            if index == 7 or index == 8:
                res += m + '.'
            else:
                res += m + '\\'
            index += 1
    if len(ones) == 11:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            if index == 7 or index == 8 or index == 9:
                res += m + '.'
            else:
                res += m + '\\'
            index += 1
    if len(ones) == 12:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            if index == 7 or index == 8 or index == 9 or index == 10:
                res += m + '.'
            else:
                res += m + '\\'
            index += 1
    print (res)
    if os.path.exists(res):
        print ('转换后的 gradle地址 存在', res)
    else:
        print ('不存在,请手动传递地址')
    return res
# get_gradle_path('C:/Users/xxxxx/.m2/repository/com/googlecode/json-simple/json-simple/1.1')
# backup_file('', 'C:/Users/xxxxx/.m2/repository/com/zhy/autolayout/1.4.5')

def getbypath (path):
    pathtwoin = 'C:/Users/xxxxx/.m2/repository'
    paths = path.split(':')
    pathsone = paths[0].split('.')
    print (pathsone)
    print (paths)
    index = 0
    for p in  paths:
        if index == 0:
            for pa in pathsone:
                pathtwoin += '/' + pa
        else :
            pathtwoin += '/' + p
        index += 1
    print (pathtwoin)
    backup_file('', pathtwoin)

getbypath('com.squareup.okhttp3:okhttp:3.8.0')


def listpath (need_path):
    # need_path = ['com.android.support:support-compat:26.1.0', 
    # 'com.android.support:support-media-compat:26.1.0', 
    # 'com.android.support:support-core-utils:26.1.0', 
    # 'com.android.support:support-core-ui:26.1.0']
    print (need_path[0])
    print (len(need_path))
    for pat in need_path:
        getbypath(pat)
    
# listpath()

def get_file_list():
    data = []
    for line in open(r'C:\\Users\\xxxxx\\Desktop\\test.txt',"r"):     #设置文件对象并读取每一行文件
        if 'resolve' in line:
            data.append(line[26:-1])    #将每一行文件加入到list中
    print (data)
    listpath(data)

# get_file_list()

# get_file_list()  根据指定的文件地址,查询文件中包含 resolve的行,并截取对应的依赖名称
# listpath()  根据指定的插件list,获取本地的 .m2 的路径,若没有则创建对应路径
# getbypath() 根据指定的插件名称,获取本地的 .m2 的路径,若没有则创建对应路径
# backup_file() 根据指定路径 把 对应的 .gradle\caches\modules-2\files-2.1 下的依赖 copy到.m2 对应的路径下

 ---------------------------------------------------------------------------------------------

上半部分是可以单独copy指定的依赖

下面是全部转换,使用的时候,全局替换一下gradle缓存和.m2地址对应的字符串即可

----------------------------------------------------------------------------------------------

 

import os
import io
import sys
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
# 将 caches\modules-2\files-2.1 下的 依赖格式 都修改为 .m2格式的依赖
# 1. 获取files-2.1下的路径 
# 2. for 循环 获取文件夹名称, 再获取下一层,下下一层的文件名称 ,然后组装为依赖名
patho = ''

def backup_file(path, path_two):
    path = get_gradle_path(path_two)
    path_two = path_two.replace('/', '\\')
    print (path_two)
    if not os.path.exists(path_two):
        os.makedirs(path_two)
    """
    :param path:   路径1
    :param path_two: 路径2
    :return:
    """
    if os.path.isdir(path) and os.path.isdir(path_two):  # 判断传入的值为文件夹
        a = os.listdir(path)  # 读取该路径下的文件为列表
        print (a[0])
        print (len(a))
        # if os.path.isdir(path_two):
        for i in a:
            if os.path.isdir(path + '\\' + i):  # 判断传入的值为文件夹
                patho = path + '\\' + i
                af = os.listdir(patho)  # 读取该路径下的文件为列表
                print (af)
                for m in af:
                    po = os.path.join(patho, m)  # 路径1拼接
                    po_two = os.path.join(path_two, m)  # 路径2拼接
                    with open(po, "rb") as f:
                        res_one = f.read()
                        with open(po_two, "wb") as a:
                            a.write(res_one)
                            print("{}复制成功".format(i))
            else:               
                po = os.path.join(path, i)  # 路径1拼接
                po_two = os.path.join(path_two, i)  # 路径2拼接
                with open(po, "rb") as f:
                    res_one = f.read()
                    with open(po_two, "wb") as a:
                        a.write(res_one)
                        print("{}复制成功".format(i))
    else:
        print("不是文件夹")

path_two = ''
pathsimple = r'D:\xxxxxxx\workspace\.gradle\caches\modules-2\files-2.1'
def get_gradle_path(pathtwo):
    ones = pathtwo.split('/')
    print (ones)
    pathsimple = r'D:\xxxxxxx\workspace\.gradle\caches\modules-2\files-2.1'
    for m in range(len(ones) - 5):
        print (ones[m + 5])
        pathsimple += '\\' + ones[m + 5]
    res = ''
    if len(ones) == 8:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            res += m + '\\'
            index += 1
    if len(ones) == 9:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            if index == 7 :
                res += m + '.'
            else:
                res += m + '\\'
            index += 1
    if len(ones) == 10:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            if index == 7 or index == 8:
                res += m + '.'
            else:
                res += m + '\\'
            index += 1
    if len(ones) == 11:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            if index == 7 or index == 8 or index == 9:
                res += m + '.'
            else:
                res += m + '\\'
            index += 1
    if len(ones) == 12:
        sims = pathsimple.split('\\')
        index = 0
        for m in sims:
            if index == 7 or index == 8 or index == 9 or index == 10:
                res += m + '.'
            else:
                res += m + '\\'
            index += 1
    print (res)
    if os.path.exists(res):
        print ('转换后的 gradle地址 存在', res)
    else:
        print ('不存在,请手动传递地址')
    return res

def getbypath (path):
    pathtwoin = 'D:/xxxxxxx/workspace/.m2/repository'
    paths = path.split(':')
    pathsone = paths[0].split('.')
    print (pathsone)
    print (paths)
    index = 0
    for p in  paths:
        if index == 0:
            for pa in pathsone:
                pathtwoin += '/' + pa
        else :
            pathtwoin += '/' + p
        index += 1
    print (pathtwoin)
    backup_file('', pathtwoin)

def listpath (need_path):
    print (need_path[0])
    print (len(need_path))
    for pat in need_path:
        getbypath(pat)

def get_dependencies():
    files_path = r'D:\xxxxxxx\workspace\.gradle\caches\modules-2\files-2.1';
    dependencies_data = []
    if os.path.isdir(files_path):  # 判断传入的值为文件夹
        dirstpath = os.listdir(files_path)  # 读取该路径下的文件为列表
        for i in dirstpath:
            if os.path.isdir(files_path + '\\' + i):  # 判断传入的值为文件夹
                path1 = path1 = files_path + '\\' + i
                dirstpath1 = os.listdir(path1)
                for i1 in dirstpath1:
                    path2 = path1 + '\\' + i1
                    if os.path.isdir(path2):  # 判断传入的值为文件夹
                        dirstpath2 = os.listdir(path2)
                        for i2 in dirstpath2:
                            str = i + ':' + i1 + ':' + i2
                            dependencies_data.append(str)
    print (dependencies_data)
    listpath(dependencies_data)

get_dependencies()

  

posted @ 2021-05-19 16:37  人间春风意  阅读(345)  评论(0编辑  收藏  举报