Alvis Zhao
5 years ago
1 changed files with 53 additions and 0 deletions
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
''' |
||||
Author: Alvis Zhao |
||||
Date: 2020-08-04 12:15:30 |
||||
LastEditTime: 2020-08-14 11:43:39 |
||||
Description: 解析package-lock.json下载npm缓存包 |
||||
''' |
||||
|
||||
import os |
||||
import json |
||||
import urllib.request |
||||
|
||||
dependencyDict = {} |
||||
|
||||
def analyzeDependency(dependencies): |
||||
for key in dependencies: |
||||
dependenciesInfo = dependencies[key] |
||||
|
||||
if "resolved" in dependenciesInfo: |
||||
url = dependenciesInfo["resolved"] |
||||
version = dependenciesInfo["version"] |
||||
# 存入字典去重 |
||||
dependencyDict[key + "-" + version] = url |
||||
|
||||
if "dependencies" in dependenciesInfo: |
||||
childDependencies = dependenciesInfo["dependencies"] |
||||
analyzeDependency(childDependencies) |
||||
|
||||
output = "offline-packages" |
||||
|
||||
if not os.path.exists(output): |
||||
os.mkdir(output) |
||||
|
||||
f = open('package-lock.json', 'r') |
||||
content = f.read() |
||||
contentDict = json.loads(content) |
||||
dependencies = contentDict["dependencies"] |
||||
# 分析并保存依赖 |
||||
analyzeDependency(dependencies) |
||||
|
||||
# 按照key排序下载 |
||||
sortedKeys = sorted(dependencyDict) |
||||
for key in sortedKeys: |
||||
url = dependencyDict[key] |
||||
params = url.split("?")[0].split("/") |
||||
filename = params[len(params) -1] |
||||
outputFile = output + os.sep + filename |
||||
print("downloading " + " " + key + " " + url) |
||||
if not os.path.isfile(outputFile): |
||||
urllib.request.urlretrieve(url, outputFile) |
||||
|
||||
f.close() |
Loading…
Reference in new issue