python license函数_Python 自动获取License文件

python license函数_Python 自动获取License文件1importos2importsys3importjson4importtime567#returndependenciesstring8#forexample:9#'”axios”:”^0.19.2″,”myjs-common”:”^1.0.6″,”oneport”:”^1.0.2″’10defreadPackageJson(filename):11de…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1 importos2 importsys3 importjson4 importtime5

6

7 #return dependencies string

8 #for example:

9 #'”axios”: “^0.19.2″,”myjs-common”: “^1.0.6″,”oneport”: “^1.0.2” ‘

10 defreadPackageJson(filename):11 dependenceStr = “”

12 try:13 ifos.path.exists(filename):14 with open(filename,”r”,encoding=’utf8′) as dependencies:15 packageJson =json.load(dependencies)16 if “dependencies” in packageJson and packageJson[“dependencies”] !={}:17 dependenceStr = str(packageJson[“dependencies”]).replace(“{“,””).replace(“}”,””)18 returndependenceStr19 exceptException as e:20 print(e)21

22

23 #for example:

24 #'”license”: “MIT”‘ in package.json file

25 #if keyName == license, valueStr==MIT;

26 #if keyName = homepage, valueStr=https://github.com/indutny/node-spdy

27 defGetPackageJsonInfo(filePath,keyName):28 valueStr = “”

29 try:30 filePath = os.path.join(“%s%s” % (filePath,”\\package.json”))31 ifos.path.exists(filePath):32 with open(filePath,”r”,encoding=’utf8′) as pkgJson:33 packageJson =json.load(pkgJson)34 if keyName inpackageJson:35 valueStr =str(packageJson[keyName])36 returnvalueStr37 exceptException as e:38 print(e)39

40 #Whether the readme.md file contains MIT license content,

41 #it returns True and file dir, but does not return False.

42 defreadReadmefile(filePath):43 fileNames = [“\\readme.md”,”\\README.md”,”\\Readme.md”,”\\readme.txt”]44 for fileName infileNames:45 filePath = os.path.join(“%s%s”%(filePath,fileName))46 ifos.path.exists(filePath):47 with open(filePath,”r”,encoding=”utf8″) as readMe:48 if “copies or substantial portions of the Software.” inreadMe.read():49 returnTrue50 else:51 returnFalse52

53

54

55 #depStr is dependencies from package.json file,like ‘”axios”: “^0.19.2″,”myjs-common”: “^1.0.6″,”oneport”: “^1.0.2” ‘

56 #return a dependencies list, for example:[“axios”,”myjs-common”,”oneport”]

57 defParsingPackageJson(depStr):58 depList =[]59 for dep in depStr.split(“,”):60 if len(dep.split(“:”)) == 2:61 depList.append(dep.split(“:”)[0].strip().replace(“‘”,””))62 returnsorted(list(set(depList)))63

64 defgetLicenseFilePath(filepath):65 licensefilename = [“LICENSE”,”LICENCE”,”LICENSE-MIT”,”LICENCE-MIT”]66 licensepath =None67 stopCircle =False68 count =069 for dirpath,dirnames,filenames inos.walk(filepath):70 for filename infilenames:71 count = count + 1

72 fileNameTemp = filename.upper().split(“.”)73 if (len(fileNameTemp) >= 2 and (fileNameTemp[-1] != “JS” or fileNameTemp[-1] != “HTML”)) or len(fileNameTemp) == 1:74 if fileNameTemp[0] inlicensefilename :75 licensepath = os.path.join(‘%s%s’ % (filepath, “\\” +filename))76 stopCircle =True77 break

78 #print(filename)

79 if stopCircle or count ==len(filenames):80 break

81 returnlicensepath82

83 #Get the dependencies in the package.json file in the root directory

84 #return dependencies info str

85 defgetRootPackageJson(rootPath):86 depStr = “”

87 findPackageJson =False88 for fileNames inos.walk(rootPath):89 if “node_modules” in fileNames[1]:90 globalnodeModulesDir91 nodeModulesDir = os.path.join(‘%s%s’ % (fileNames[0], “\\node_modules”))92 for pfile in fileNames[-1]:93 if pfile == “package.json”:94 packageJsonPath = os.path.join(‘%s%s’ % (fileNames[0], “\\package.json”))95 depStr =readPackageJson(packageJsonPath)96 findPackageJson =True97 break

98 break

99 iffindPackageJson:100 print(“get pacakageJson”)101 else:102 print(“No pacakageJson”)103 returndepStr104

105 #DepDir,pkg file dir

106 defgetDependencyName(DepDir):107 if “@” inDepDir:108 dependenceName =”@” + (DepDir.split(“@”)[-1]).replace(“\\”,”/”)109 else:110 dependenceName = DepDir.split(“\\”)[-1]111 returndependenceName112

113 #Find the dir where all package.json files are located,

114 #Return a list containing all package.json dir

115 defgetAllPackageJsonDir(Dir):116 packageJsonDir =[]117 depName =[]118 for dirs,fileNames,files inos.walk(Dir):119 for file infiles:120 if file==”package.json”:121 dependenceName =getDependencyName(dirs)122 if dependenceName not indepName:123 depName.append(dependenceName)124 packageJsonDir.append(dirs)125 else:126 #The dependencies in package.json in the dependency folders of the same name under different folders may be different

127 #for example:

128 #G:…\..\node_modules\body-parser

129 #G:…\..\node_modules\@types\body-parser

130 for pjsonDir inpackageJsonDir:131 if pjsonDir.split(“\\”)[-1]==dirs.split(“\\”)[-1]:132 pjsonPath = os.path.join(“%s%s” % (pjsonDir,”\\package.json”))133 pjsonPathNew = os.path.join(“%s%s” % (dirs,”\\package.json”))134 pjsonStr =readPackageJson(pjsonPath)135 pjsonStrNew =readPackageJson(pjsonPathNew)136 pName =ParsingPackageJson(pjsonStr)137 pNameNew =ParsingPackageJson(pjsonStrNew)138 if pName!=pNameNew:139 packageJsonDir.append(dirs)140 #saveInfoToTxt(ResultInfoPath,dirs)

141 print(“all package.json file dir count:”,len(packageJsonDir))142 returnpackageJsonDir143

144 #for example: dictCountDir[“axios”]=1,dictNameDir[“axios”] =r”C:…\meetingServer_1.0.8\node_modules\axios”

145 #return dictCountDir,dictNameDir

146 defgetPkgDirDict(pacakageDir,rootDir):147 #dictCountDir key: dependence name, value: dir count of same dependencies

148 dictCountDir={}149 #dictNameDir key: dependence name,value: dependence dir

150 dictNameDir={}151 for pkgDir inpacakageDir:152 if pkgDir ==rootDir:153 continue

154 pkgDirName =getDependencyName(pkgDir)155 countDir=0156 if pkgDirName not indictNameDir.keys():157 dictNameDir[pkgDirName] =pkgDir158 dictCountDir[pkgDirName] = countDir+1

159 else:160 dictCountDir[pkgDirName] = dictCountDir[pkgDirName] +1

161 returndictNameDir,dictCountDir162

163 #Read the package.json file under pkgPath, if dependencies is not empty,

164 #analyze whether the dependence is in the deplist, if not, add it to the deplist, and return to the deplist.

165 #pkgName:dependence name, depList:dependence list, pkgPath: package.json file path

166 #return dependency list

167 defgetSubDepList(pkgName,deplist,pkgPath):168 pkgJson =readPackageJson(pkgPath)169 ifpkgJson:170 pkgDep =ParsingPackageJson(pkgJson)171 for subDep inpkgDep:172 if subDep not indeplist:173 deplist.append(subDep)174 info = “pkgName:”+ pkgName + “pkgPath:”+ pkgPath +”\r pkgJson:”+ str(pkgJson)+”\r\n”

175 saveInfoToTxt(ResultInfoPath,info)176 returndeplist177

178 #Query all dependencies according to the dependencies value in package.json

179 #Return a list of all dependencies

180 defgetDepList(rootDepStr,pacakageDir,dirDict):181 deplist=[]182 dictNameDirs=dirDict[0]183 dictCountDir = dirDict[1]184 ifrootDepStr:185 #根据packageDir 分析依赖库

186 deplist =ParsingPackageJson(rootDepStr)187 for dep indeplist:188 for pkgDir inpacakageDir:189 pkgDirName =getDependencyName(pkgDir)190 if dep==pkgDirName:191 pkgPath = os.path.join(‘%s%s’ % (pkgDir, “\\package.json”))192 getSubDepList(dep,deplist,pkgPath)193 if dictCountDir[pkgDirName] ==1:194 break

195 saveInfoToTxt(ResultInfoPath,”deplist count:”+ str(len(deplist))+ “\r” +str(deplist))196 dcount =len(deplist)197 print(“dependence count:”,str(dcount))198

199 returndeplist200

201 #test function, find all depencies

202 defgetAllDepList(pacakageDir):203 depStr = “”

204 for dir inpacakageDir:205 pkgPath = os.path.join(‘%s%s’ % (dir, “\\package.json”))206 pJson =readPackageJson(pkgPath)207 ifpJson:208 depStr = depStr +pJson209 depStr = depStr +”,”

210 allDep =ParsingPackageJson(depStr)211 returnallDep212

213 defgetLicenses(rootdir):214

215 pkgDirList =getAllPackageJsonDir(rootdir)216 dirDict =getPkgDirDict(pkgDirList,rootdir)217 rootDepStr =getRootPackageJson(rootdir)218 deplist =getDepList(rootDepStr,pkgDirList,dirDict)219

220 licenseTypeDict= {“FindInReadme”:0, “AddMITLicenseTemp”:0,”NotMITLicense”:0,”Others”:0}221 #According to find all deplist, find the license file under the corresponding file.

222 licenseNo = 0 #license number in AllLicenses.txt file

223 lNoFindCount = 0 #Total number of license files not found

224 LicenseFileNotFind=[] #225 depFileNotFind=[] #The file directory where the dependency file is not found

226 for depName indeplist:227 licenseNo = licenseNo + 1

228 dictNameDir =dirDict[0]229 if depName indictNameDir.keys():230 licensepath =getLicenseFilePath(dictNameDir[depName])231 if licensepath isNone:232 licensepath =None233 LicenseFileNotFind.append(depName)234 lNoFindCount = lNoFindCount+1

235 res =AddLicenseTemp(licenseNo,dictNameDir[depName],depName)236 licenseTypeDict[res] = licenseTypeDict[res]+1

237 else:238 readLicense(licenseNo,licensepath,depName)239 else:240 lNoFindCount = lNoFindCount+1

241 depFileNotFind.append(depName)242 res =AddLicenseTemp(licenseNo,None,depName)243 licenseTypeDict[res] = licenseTypeDict[res]+1

244

245 notFindDepCount =str(len(depFileNotFind))246 notFindLinceseCount =str(len(LicenseFileNotFind))247

248 print(“Not find Dependence file :”,notFindDepCount)249 print(“Not find license file:”, notFindLinceseCount)250 print(“License file Not find count:”,str(lNoFindCount))251 for key inlicenseTypeDict:252 print(key,licenseTypeDict[key])253 saveInfoToTxt(ResultInfoPath,”\r\n Info of license file is found”+str(licenseTypeDict))254 saveInfoToTxt(ResultInfoPath,”\r\n Not find Dependence dir count:”+ notFindDepCount +”\r”+str(depFileNotFind))255 saveInfoToTxt(ResultInfoPath,”\r\n Not find license file count:”+ notFindLinceseCount + “\r”+str(LicenseFileNotFind))256 saveInfoToTxt(ResultInfoPath,”\r\n total license file Not find count:”+str(lNoFindCount))257

258 defsaveInfoToTxt(savePath,line):259 with open(savePath,”a+”) as f:260 f.write(str(line))261 f.write(‘\r’)262

263 #no:license number

264 #filepath: license file path

265 #depName: dependence name

266 #There is a license file under dir, read the license content

267 defreadLicense(no,filepath,depName):268 with open(ResultLincensePath,”a+”,encoding=”utf8″) as f:269 f.write(“\r==================================\r”)270 f.write(str(no) + “)” +depName)271 f.writelines(“\r==================================\r”)272 with open(filepath,”r”, encoding=”utf8″) as licensefile:273 f.write(licensefile.read())274 f.write(‘\r’)275

276 #There is no license file under dir, add license template

277 #

278 defAddLicenseTemp(no,filepath,depName):279 if filepath !=None:280 isMITlicense =readReadmefile(filepath)281 with open(ResultLincensePath,”a+”,encoding=”utf8″) as f:282 f.write(“\r==================================\r”)283 f.write(str(no) + “)” +depName)284 f.writelines(“\r==================================\r”)285 ifisMITlicense:286 saveInfoToTxt(ResultInfoPath,”** %s MIT License in readme.md file,Please paste manually. %s \r” %(depName,filepath))287 with open(ResultLincensePath,”a+”,encoding=”utf8″) as f:288 f.write(“MIT License in readme.md file,Please paste manually!”)289 f.write(‘\r’)290 return “FindInReadme”

291 else:292 license = GetPackageJsonInfo(filepath,”license”)293 homepage = GetPackageJsonInfo(filepath,”homepage”)294 ifhomepage:295 with open(ResultLincensePath,”a+”,encoding=”utf8″) as f:296 f.write(“Homepage: %s%s” % (homepage,”\r”))297 if license==”MIT”:298 with open(ResultLincensePath,”a+”,encoding=”utf8″) as f:299 with open(lincenseTempPath,”r”, encoding=”utf8″) as licenseTemp:300 f.write(licenseTemp.read())301 f.write(‘\r’)302 saveInfoToTxt(ResultInfoPath,”>> %s Add MIT License Template.” %depName)303 return “AddMITLicenseTemp”

304 else:305 #with open(ResultLincensePath,”a+”,encoding=”utf8″) as f:

306 #f.write(“License:%s” % license )

307 saveInfoToTxt(ResultInfoPath, “@@ %s is not an MIT License, its license is: %s” %(depName,license))308 return “NotMITLicense”

309 else:310 with open(ResultLincensePath,”a+”,encoding=”utf8″) as f:311 f.write(“\r==================================\r”)312 f.write(str(no) + “)” +depName)313 f.writelines(“\r==================================\r”)314 f.write(“Nothing find !”)315 saveInfoToTxt(ResultInfoPath,”/(ㄒoㄒ)/~~ Nothing find ! %s” %depName)316 return “Others”

317

318 #Initialization Result path

319 definite():320 globalResultDefaultPath,ResultLincensePath,ResultInfoPath,lincenseTempPath321 ResultDefaultPath = os.path.join(os.getcwd(),”Results”)322 if notos.path.exists(ResultDefaultPath):323 os.mkdir(ResultDefaultPath)324 ResultLincensePath = os.path.join(‘%s%s’ % (ResultDefaultPath, “\\AllLicenses.txt”))325 ResultInfoPath = os.path.join(‘%s%s’ % (ResultDefaultPath, “\\AllInfo.txt”))326 lincenseTempPath = os.path.join(‘%s%s’ % (ResultDefaultPath, “\\MITLicenseTemp.txt”))327 if notos.path.exists(lincenseTempPath):328 print(“license template file in Result dir is not exists.”)329 time.sleep(20)330 exit(0)331 ifos.path.exists(ResultLincensePath):332 os.remove(ResultLincensePath)333 ifos.path.exists(ResultInfoPath):334 os.remove(ResultInfoPath)335

336 if __name__ == ‘__main__’:337

338 inite()339 rootDir =os.getcwd()340 #os.getcwd()

341 print(“***Root File Path:”,rootDir)342 print(“Begin Time:”,time.strftime(‘%Y/%m/%d %H:%M:%S’))343 saveInfoToTxt(ResultInfoPath,”Begin Time:”+time.strftime(‘%Y/%m/%d %H:%M:%S’))344 getLicenses(rootDir)345 print(“All finished:”,time.strftime(‘%Y/%m/%d %H:%M:%S’))346 saveInfoToTxt(ResultInfoPath,”All finished:”+ time.strftime(‘%Y/%m/%d %H:%M:%S’))347 print(“\r\n ***Complete! Please refer Results folder.***”)348 input(“Press any key to close.”)349

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/164392.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • cpus Vs cpu period and cpu quota[通俗易懂]

    1.https://docs.oracle.com/cd/E37670_01/E75728/html/section-zb1_wqw_gt.htmlTocontrolacontainer’sCPUusage,youcanusethe–cpu-periodand–cpu-quotaoptionswiththedockercreateanddocker…

  • git 修改用户名以及邮箱_163怎么更改账号

    git 修改用户名以及邮箱_163怎么更改账号最近在提交代码时发现用户名和邮箱很长,感觉很奇怪,于是通过Git命令修改了一下用户名,用户名截图如下:修改步骤如下:1.进入Git的安装目录,找到git\git-cmd.exe,例如我的目录是D:\softwore\git\Git,目录截图如下:或者配置环境变量进行修改,环境变量配置为在PATH后面加上git的bin目录D:\softwore\git\Git\bin,截图如下…

  • log4j 配置详解_指定log4j2配置文件位置

    log4j 配置详解_指定log4j2配置文件位置先来个配置文件—-log4j.rootLogger=debug,stdout,logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.SimpleLayoutlog4j.appender.logfile=org.apache.log4j.FileAppender

  • [日常] Go语言圣经-匿名函数习题

    [日常] Go语言圣经-匿名函数习题

  • python ip池(python 连接池)

    ,都说标题是文章的灵魂,想了半天没想到什么比较有创意的标题,只好拿一个去年的爆款标题套一下。啊哈哈哈哈哈哈,朕真是太机智了这是一篇介绍如何使用python搭建IP池的文章,如果爱卿对此不感兴趣,那很抱歉,标题耽误了你宝贵的时间。事情的起因是这样,前段时间我写了一篇介绍如何爬取小说的blog【python那些事.No2】,在爬取的过程中,发现同一个IP连续只能获取前几页小说内容,原本是想搭建…

  • FPS游戏:实现GDI方框透视「建议收藏」

    FPS游戏:实现GDI方框透视「建议收藏」FPS游戏可以说一直都比较热门,典型的代表有反恐精英,穿越火线,绝地求生等,基本上只要是FPS游戏都会有透视挂的存在,而透视挂还分为很多种类型,常见的有D3D透视,方框透视,还有一些比较高端的显卡透视

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号