diff --git a/PyclibCommon.py b/PyclibCommon.py new file mode 100644 index 0000000000000000000000000000000000000000..d7707504f57092008c2eedc37b48127e037ff909 --- /dev/null +++ b/PyclibCommon.py @@ -0,0 +1,43 @@ +import requests +import string + +enableDebugPrint = 1 + +def debug_print(*args, **kwargs): + if enableDebugPrint: + 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 new file mode 100644 index 0000000000000000000000000000000000000000..56cf2944657aa7cdd6886879487b156213fc9aba --- /dev/null +++ b/PyclibDependencyPackage.py @@ -0,0 +1,29 @@ +import PyclibCommon +import string +import requests +import json +import os + +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.localpath = packageDic.get('localpath') + + 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: + 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 new file mode 100644 index 0000000000000000000000000000000000000000..87aac867f81bf0b68c16e0c4c7ab69dfee25a44e --- /dev/null +++ b/PyclibPackage.py @@ -0,0 +1,57 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..f618b08fa6bae73ef0a0365335737e78c1cf3eb3 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "clib_test", + "version": "1.0.0", + "dependencies": [ + { + "location": "github", + "author": "iamzhanglong", + "repo": "clib", + "version": "master", + "token": "token", + "url": "url" + } + ], + "src": [ + "package.json" + ] +} diff --git a/pyclib.py b/pyclib.py new file mode 100644 index 0000000000000000000000000000000000000000..84ace5468724ba0b144d9ef4ca1305f03d282b96 --- /dev/null +++ b/pyclib.py @@ -0,0 +1,23 @@ +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) + + depsPath = os.path.join(currentPath, 'deps') + if not os.path.exists(depsPath): + os.makedirs(depsPath) + + pyclibPackage.installDependencyPackages(depsPath) + +if __name__=="__main__": + main()