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地址对应的字符串即可

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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 @   人间春风意  阅读(359)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示

距今时间:
1025天8.00 小时 52.75 分钟

当前新增阅读数:140327