From 531b3d88c2346bd80b56a658c52afc39def0c501 Mon Sep 17 00:00:00 2001 From: Loong Date: Thu, 14 Dec 2023 18:28:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=BB=BA=E7=AB=8B=E4=BB=93=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PyclibCommon.py | 11 +++++++++++ PyclibDependencyPackage.py | 29 +++++++++++++++++++++++++++++ PyclibPackage.py | 23 +++++++++++++++++++++++ package.json | 17 +++++++++++++++++ pyclib.py | 21 +++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 PyclibCommon.py create mode 100644 PyclibDependencyPackage.py create mode 100644 PyclibPackage.py create mode 100644 package.json create mode 100644 pyclib.py diff --git a/PyclibCommon.py b/PyclibCommon.py new file mode 100644 index 0000000..07866fc --- /dev/null +++ b/PyclibCommon.py @@ -0,0 +1,11 @@ +enableDebugPrint = 1 + +def debug_print(*args, **kwargs): + if enableDebugPrint: + print(*args, **kwargs) + +def info_print(*args, **kwargs): + print(*args, **kwargs) + +def error_print(*args, **kwargs): + print(*args, **kwargs) diff --git a/PyclibDependencyPackage.py b/PyclibDependencyPackage.py new file mode 100644 index 0000000..ffeab2e --- /dev/null +++ b/PyclibDependencyPackage.py @@ -0,0 +1,29 @@ +import PyclibCommon +import string +import requests + +giteeRawUrlTemplate = "https://gitee.com/${author}/${repo}/raw/${version}/${filePath}" +class PyclibDependencyPackage: + def __init__(self, packageDic): + self.location = packageDic.get('location') + self.repo = packageDic.get('repo') + self.version = packageDic.get('version') + self.token = packageDic.get('token') + self.url = packageDic.get('url') + self.path = packageDic.get('path') + + def getDependPackageUrl(self): + rawUrlTemplate = string.Template(giteeRawUrlTemplate) + rawUrl = rawUrlTemplate.substitute(author = "iamzhanglong", repo = "clib", version = self.version, filePath = "clib.json") + PyclibCommon.debug_print(rawUrl) + return rawUrl + + def downloadPackageJsonFile(self): + url = self.getDependPackageUrl() + response = requests.get(url) + + if response.status_code == 200: + return response.text + else: + PyclibCommon.error_print("download" + "url" + "fail") + return None diff --git a/PyclibPackage.py b/PyclibPackage.py new file mode 100644 index 0000000..7ce11bb --- /dev/null +++ b/PyclibPackage.py @@ -0,0 +1,23 @@ +# { +# "name": "clib_test", +# "version": "1.0.0", +# "dependencies": [ +# { +# "location":"github", +# "repo":"", +# "version":"", +# "token":"", +# "url":"", +# "path":"" +# } +# ], +# "src":[ + +# ] +# } +class PyclibPackage: + def __init__(self, packageDic): + self.name = packageDic.get('name') + self.version = packageDic.get('version') + self.dependencies = packageDic.get('dependencies') + self.src = packageDic.get('src') diff --git a/package.json b/package.json new file mode 100644 index 0000000..09e002c --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "clib_test", + "version": "1.0.0", + "dependencies": [ + { + "location": "github", + "repo": "repo", + "version": "master", + "token": "token", + "url": "url", + "path": "path" + } + ], + "src": [ + "package.json" + ] +} diff --git a/pyclib.py b/pyclib.py new file mode 100644 index 0000000..fa91f23 --- /dev/null +++ b/pyclib.py @@ -0,0 +1,21 @@ +import os +import json +import PyclibCommon +from PyclibPackage import PyclibPackage +from PyclibDependencyPackage import PyclibDependencyPackage + +def main(): + currentPath = os.getcwd() + packageJsonFilePath = os.path.join(currentPath, 'package.json') + PyclibCommon.info_print("package.json path is \"" + packageJsonFilePath + "\"") + with open(packageJsonFilePath,'r',encoding='utf8') as packageJsonFile: + packageDic = json.load(packageJsonFile) + + pyclibPackage = PyclibPackage(packageDic) + + for dependPackageDic in pyclibPackage.dependencies: + dependPackage = PyclibDependencyPackage(dependPackageDic) + responseText = dependPackage.downloadPackageJsonFile() + PyclibCommon.info_print(responseText) +if __name__=="__main__": + main() -- Gitee From 58ce6d5a7caeaebb8c576b2528e8e9dee0efe664 Mon Sep 17 00:00:00 2001 From: Loong Date: Thu, 14 Dec 2023 23:29:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E5=8C=85=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PyclibCommon.py | 32 ++++++++++++++++++ PyclibDependencyPackage.py | 32 +++++++++--------- PyclibPackage.py | 66 +++++++++++++++++++++++++++++--------- package.json | 6 ++-- pyclib.py | 10 +++--- 5 files changed, 107 insertions(+), 39 deletions(-) diff --git a/PyclibCommon.py b/PyclibCommon.py index 07866fc..d770750 100644 --- a/PyclibCommon.py +++ b/PyclibCommon.py @@ -1,3 +1,6 @@ +import requests +import string + enableDebugPrint = 1 def debug_print(*args, **kwargs): @@ -5,7 +8,36 @@ def debug_print(*args, **kwargs): print(*args, **kwargs) def info_print(*args, **kwargs): + #print("\033[1;33m error : ", end="") + print(*args, **kwargs) + #print("\033[0m", end="") + +def warn_print(*args, **kwargs): + print("\033[1;33mwarn : ", end="") print(*args, **kwargs) + print("\033[0m", end="") def error_print(*args, **kwargs): + print("\033[1;31merror :", end="") print(*args, **kwargs) + print("\033[0m", end="") + +giteeRawUrlTemplate = "https://gitee.com/${author}/${repo}/raw/${version}/${filePath}" + +def getRawFileUrl(author, repo, version, filePath): + rawUrlTemplate = string.Template(giteeRawUrlTemplate) + rawUrl = rawUrlTemplate.substitute(author = author, repo = repo, version = version, filePath = filePath) + return rawUrl + +def getRawFileFromUrl(url): + response = requests.get(url) + if response.status_code == 200: + return response.text + else: + PyclibCommon.error_print("download" + "url" + "fail") + return None + +def downloadFileFromUrlAndSave(url, filePath): + response = requests.get(url) + with open(filePath, "wb") as f: + f.write(response.content) \ No newline at end of file diff --git a/PyclibDependencyPackage.py b/PyclibDependencyPackage.py index ffeab2e..56cf294 100644 --- a/PyclibDependencyPackage.py +++ b/PyclibDependencyPackage.py @@ -1,29 +1,29 @@ import PyclibCommon import string import requests +import json +import os -giteeRawUrlTemplate = "https://gitee.com/${author}/${repo}/raw/${version}/${filePath}" class PyclibDependencyPackage: def __init__(self, packageDic): self.location = packageDic.get('location') self.repo = packageDic.get('repo') + self.author = packageDic.get('author') self.version = packageDic.get('version') self.token = packageDic.get('token') self.url = packageDic.get('url') - self.path = packageDic.get('path') + self.localpath = packageDic.get('localpath') - def getDependPackageUrl(self): - rawUrlTemplate = string.Template(giteeRawUrlTemplate) - rawUrl = rawUrlTemplate.substitute(author = "iamzhanglong", repo = "clib", version = self.version, filePath = "clib.json") - PyclibCommon.debug_print(rawUrl) - return rawUrl - - def downloadPackageJsonFile(self): - url = self.getDependPackageUrl() - response = requests.get(url) - - if response.status_code == 200: - return response.text + def getPackageJsonDic(self): + if self.localpath: + packageJsonFilePath = os.path.join(self.localpath, 'package.json') + with open(packageJsonFilePath,'r',encoding='utf8') as packageJsonFile: + packageDic = json.load(packageJsonFile) + packageDic['localpath'] = self.localpath + return packageDic else: - PyclibCommon.error_print("download" + "url" + "fail") - return None + url = PyclibCommon.getRawFileUrl(self.author, self.repo, self.version, "package.json") + PyclibCommon.info_print("fetch : " + url) + fileText =PyclibCommon. getRawFileFromUrl(url) + packageDic = json.loads(fileText) + return packageDic diff --git a/PyclibPackage.py b/PyclibPackage.py index 7ce11bb..87aac86 100644 --- a/PyclibPackage.py +++ b/PyclibPackage.py @@ -1,23 +1,57 @@ -# { -# "name": "clib_test", -# "version": "1.0.0", -# "dependencies": [ -# { -# "location":"github", -# "repo":"", -# "version":"", -# "token":"", -# "url":"", -# "path":"" -# } -# ], -# "src":[ +from PyclibDependencyPackage import PyclibDependencyPackage +import os +import PyclibCommon +import shutil -# ] -# } class PyclibPackage: def __init__(self, packageDic): self.name = packageDic.get('name') + self.author = packageDic.get('author') self.version = packageDic.get('version') self.dependencies = packageDic.get('dependencies') self.src = packageDic.get('src') + self.localpath = packageDic.get('localpath') + + def install(self, dirPath): + pkgPath = os.path.join(dirPath, self.name) + if not os.path.exists(pkgPath): + os.makedirs(pkgPath) + + if self.localpath: + self.copySrcFile(pkgPath) + else: + self.downloadSrcFile(pkgPath) + + self.installDependencyPackages(dirPath) + PyclibCommon.info_print(self.name + " is installed successfully!") + PyclibCommon.info_print("----------------------------------------------") + + def copySrcFile(self, pkgPath): + if not self.src: + return 0 + for srcFilePath in self.src: + srcFilePath = os.path.join(self.localpath, srcFilePath) + fileName = os.path.basename(srcFilePath) + desFilePath = os.path.join(pkgPath, fileName) + shutil.copyfile(srcFilePath, desFilePath) + + def downloadSrcFile(self, pkgPath): + if not self.src: + return 0 + for srcFilePath in self.src: + fileName = os.path.basename(srcFilePath) + localFilePath = os.path.join(pkgPath, fileName) + if not os.path.exists(localFilePath): + rawFileUrl = PyclibCommon.getRawFileUrl(self.author, self.name, self.version, srcFilePath) + PyclibCommon.downloadFileFromUrlAndSave(rawFileUrl, localFilePath) + else: + PyclibCommon.debug_print(localFilePath + " is existe!") + + def installDependencyPackages(self, dirPath): + if not self.dependencies: + return 0 + for dependDic in self.dependencies: + dependPkg = PyclibDependencyPackage(dependDic) + packageDic = dependPkg.getPackageJsonDic() + pyclibPackage = PyclibPackage(packageDic) + pyclibPackage.install(dirPath) diff --git a/package.json b/package.json index 09e002c..f618b08 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,11 @@ "dependencies": [ { "location": "github", - "repo": "repo", + "author": "iamzhanglong", + "repo": "clib", "version": "master", "token": "token", - "url": "url", - "path": "path" + "url": "url" } ], "src": [ diff --git a/pyclib.py b/pyclib.py index fa91f23..84ace54 100644 --- a/pyclib.py +++ b/pyclib.py @@ -13,9 +13,11 @@ def main(): pyclibPackage = PyclibPackage(packageDic) - for dependPackageDic in pyclibPackage.dependencies: - dependPackage = PyclibDependencyPackage(dependPackageDic) - responseText = dependPackage.downloadPackageJsonFile() - PyclibCommon.info_print(responseText) + depsPath = os.path.join(currentPath, 'deps') + if not os.path.exists(depsPath): + os.makedirs(depsPath) + + pyclibPackage.installDependencyPackages(depsPath) + if __name__=="__main__": main() -- Gitee