-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Asgore-Dreemur/master
增加win和linux下的java搜索
- Loading branch information
Showing
5 changed files
with
292 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class Account: | ||
def __init__(self) -> None: | ||
self.name = None | ||
self.uuid = None | ||
self.accessToken = None | ||
self.refresh_token = None | ||
self.error = None | ||
|
||
class OfflineAccount(Account): | ||
pass | ||
|
||
# Microsoft类 --Begin-- | ||
class MicrosoftAccount: | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.name = None | ||
self.id = None | ||
self.access_token = None | ||
self.refresh_token = None | ||
self.skins = [MicrosoftPlayerSkin()] | ||
self.error = None | ||
|
||
class MicrosoftPlayerSkin: | ||
def __init__(self) -> None: | ||
self.id = None | ||
self.state = None | ||
self.url = None | ||
self.variant = None | ||
self.alias = None | ||
|
||
# Microsoft类 --End-- | ||
|
||
# Yggdrasil类 --Begin-- | ||
class YggdrasilAccount: | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.name = None | ||
self.uuid = None | ||
self.accessToken = None | ||
self.availableProfiles = [YggdrasilProfile()] | ||
self.selectedProfile = YggdrasilProfile() | ||
self.error = None | ||
|
||
class YggdrasilProfile(): | ||
def __init__(self) -> None: | ||
self.id = None | ||
self.name = None | ||
|
||
# Yggdrasil类 --End-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import os | ||
import re | ||
import subprocess | ||
|
||
# JavaFind:java寻找类 | ||
class JavaFinder: | ||
# Find_By_Classpath:通过环境变量寻找java,通用win和linux | ||
# return value:元组,(java路径,java版本) or None | ||
@staticmethod | ||
def Find_By_Classpath() -> tuple: | ||
javapath = "" | ||
javaver = "" | ||
paths = dict(os.environ) | ||
if("JAVA_HOME" in paths.keys()): # 通过环境变量JAVA_HOME查找 | ||
javahome = paths["JAVA_HOME"] | ||
if(os.path.exists(javahome)): | ||
unixjavapath = os.path.join(javahome, "bin", "java") | ||
winjavapath = os.path.join(javahome, "bin", "java.exe") | ||
if(os.path.exists(unixjavapath)): | ||
javapath = unixjavapath | ||
elif(os.path.exists(winjavapath)): | ||
javapath = winjavapath | ||
else: | ||
return None | ||
javaver = JavaFinder.GetJavaVersion(javahome) | ||
else: | ||
return None | ||
return (javapath, javaver) | ||
|
||
# GetJavaVersion:通过查看release文件确定java版本 | ||
# return value:str,java版本 or "Unkown" | ||
@staticmethod | ||
def GetJavaVersion(javapath:str) -> str: | ||
releasefile = os.path.join(javapath, "release") # java目录下的release文件存在java信息 | ||
if(os.path.exists(releasefile)): | ||
f = open(releasefile, "r") | ||
filecon = f.read() | ||
result = re.findall("JAVA_VERSION=\"([\\s\\S]+)\"", filecon) # 通过正则匹配文件内容 | ||
if(len(result) != 0): | ||
return result[0] | ||
else: | ||
return "Unkown" | ||
else: | ||
return "Unkown" | ||
|
||
# Find_By_Command_Linux:通过命令查找java,仅限linux | ||
# return value:元组,(java路径,java版本) or None | ||
@staticmethod | ||
def Find_By_Command_Linux() -> tuple: | ||
javaver = "" | ||
result = subprocess.run(["which", "java"], stdout=subprocess.PIPE) # 通过which命令查找java | ||
output = result.stdout.decode("utf-8") | ||
if(output is None or output == ""):return None | ||
if(os.path.exists(output.strip())): | ||
output = os.path.realpath(output.strip()) | ||
javasp = output.strip().split(os.path.sep) | ||
javasp = javasp[0:len(javasp)] | ||
javahome = os.path.sep.join(javasp) | ||
javahome = os.path.realpath(javahome) | ||
javaver = JavaFinder.GetJavaVersion(javahome) | ||
return (javahome, javaver) | ||
else:return None | ||
|
||
# Find_By_ProgramFile_Win:通过查找Program Files目录寻找java | ||
# return value:list,[(java路径,java版本), ...] or [] | ||
@staticmethod | ||
def Find_By_ProgramFile_Win(): | ||
javas = [] | ||
SysDrive = os.getenv("SystemDrive") | ||
JavaPath = os.path.join(SysDrive, "Program Files", "Java") | ||
if(os.path.exists(JavaPath) and os.path.isdir(JavaPath)): | ||
javadirs = os.listdir(JavaPath) | ||
if(len(javadirs) != 0): | ||
for i in javadirs: | ||
javaabspath = os.path.join(JavaPath, i) | ||
javabin = os.path.join(javaabspath, "bin", "java.exe") | ||
if(os.path.exists(javabin)): # 通过判断是否存在java.exe判断是不是合法的java目录 | ||
javaver = JavaFinder.GetJavaVersion(javaabspath) | ||
javas.append((javaabspath, javaver)) | ||
return javas | ||
|
||
# Find_Linux:在linux下寻找java的总方法 | ||
# return value:元组,(java路径,java版本) or None | ||
@staticmethod | ||
def Find_Linux() -> tuple: | ||
result = JavaFinder.Find_By_Classpath() | ||
result2 = JavaFinder.Find_By_Command_Linux() | ||
if(any([result, result2])): | ||
if(result is None):return result2 | ||
else:return result | ||
|
||
# Find_Linux:在Windows下寻找java的总方法 | ||
# return value:list,[(java路径,java版本), ...] or [] | ||
@staticmethod | ||
def Find_Windows(): | ||
result = JavaFinder.Find_By_Classpath() | ||
result2 = JavaFinder.Find_By_ProgramFile_Win() | ||
if(result is not None or len(result2) > 0): | ||
if(len(result2) > 0): | ||
if(result is not None and result in result2): | ||
return result2 | ||
elif(result is not None): | ||
result2.append(result) | ||
return result2 | ||
else:return result2 | ||
else:return [result] | ||
else:return [] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,4 +207,3 @@ def StartRefreshToken(self) -> list: | |
arg_again = arg1.StartRefreshToken() | ||
print(arg_again) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import json | ||
class PyJsonEntity: | ||
@staticmethod | ||
def JsonToEntity(jsonstr: str, classobj): | ||
def handlejsonobj(classobj, jdict: dict): | ||
for i in jdict.keys(): | ||
if (i in classobj.__dict__.keys()): | ||
value = jdict[i] | ||
if (type(value) == dict): | ||
if ("__dict__" not in dir(classobj.__dict__[i])): | ||
setattr(classobj, i, value) | ||
else: | ||
objvalue = handlejsonobj(classobj.__dict__[i], value) | ||
setattr(classobj, i, objvalue) | ||
elif (type(value) == list): | ||
clist = classobj.__dict__[i] | ||
if (type(clist) != list or len(clist) == 0): | ||
setattr(classobj, i, value) | ||
else: | ||
arrvalue = handlejsonarray(clist[0], value) | ||
setattr(classobj, i, arrvalue) | ||
else: | ||
setattr(classobj, i, value) | ||
return classobj | ||
def handlejsonarray(classobj, listobj): | ||
array = [] | ||
for i in listobj: | ||
if(type(i) == list and type(classobj) == list): | ||
array2 = handlejsonarray(classobj[0], i) | ||
array.append(array2) | ||
else: | ||
array.append(handlejsonobj(classobj.__class__(), i)) | ||
return array | ||
jdict = json.loads(jsonstr) | ||
return handlejsonobj(classobj, jdict) | ||
|
||
@staticmethod | ||
def EntityToJson(classobj): | ||
def handlejsonobj(classobj): | ||
jdict:dict = {} | ||
for i in classobj.__dict__.items(): | ||
if(type(i[1]) == list): | ||
value = handlejsonarray(i[1]) | ||
jdict[i[0]] = value | ||
elif("__dict__" in dir(i[1])): | ||
value = handlejsonobj(i[1]) | ||
jdict[i[0]] = value | ||
else: | ||
jdict[i[0]] =i[1] | ||
return jdict | ||
|
||
def handlejsonarray(listobj:list): | ||
array = [] | ||
for i in listobj: | ||
value = None | ||
if(type(i) == list): | ||
value = handlejsonarray(i) | ||
elif("__dict__" in dir(i)): | ||
value = handlejsonobj(i) | ||
else: | ||
value = i | ||
array.append(value) | ||
return array | ||
|
||
return handlejsonobj(classobj) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import requests | ||
import json | ||
import Accounts | ||
from PyJsonEntity import PyJsonEntity | ||
|
||
# YggdrasilAuthenticator 外置登录验证类 | ||
class YggdrasilAuthenticator: | ||
# authserver:外置登录提供商提供的api认证地址 | ||
# mail:用户名(邮件) | ||
# password:密码 | ||
def __init__(self, authserver:str, mail:str, password:str) -> None: | ||
self.mail = mail | ||
self.password = password | ||
self.authserver = authserver.strip("/") | ||
|
||
# 登录 | ||
# return value:YggdrasilAccount类 | ||
def Login(self): | ||
account = Accounts.YggdrasilAccount() | ||
RequestParams:dict = { | ||
"username": self.mail, | ||
"password": self.password, | ||
"agent":{ | ||
"name": "Minecraft", | ||
"version": 1 | ||
} | ||
} | ||
requesturl = self.authserver + "/authserver/authenticate" | ||
result = requests.post(requesturl, data=json.dumps(RequestParams, ensure_ascii=False), | ||
headers={"Content-Type": "application/json"}) | ||
if(result.status_code != 200): | ||
account.error = result.text | ||
return account | ||
else: | ||
return PyJsonEntity.JsonToEntity(result.text, account) | ||
|
||
# 刷新令牌 | ||
# return value:YggdrasilAccount类 | ||
def Refresh(self, account:Accounts.YggdrasilAccount): | ||
raccount = Accounts.YggdrasilAccount() | ||
RequestParams:dict = { | ||
"accessToken": account.accessToken | ||
} | ||
requesturl = self.authserver + "/authserver/refresh" | ||
result = requests.post(requesturl, data=json.dumps(RequestParams, ensure_ascii=False), | ||
headers={"Content-Type": "application/json"}) | ||
if(result.status_code != 200): | ||
raccount.error = result.text | ||
return raccount | ||
else: | ||
return PyJsonEntity.JsonToEntity(result.text, raccount) | ||
|
||
# 登出 | ||
# return value:bool,是否成功 | ||
def Signout(self) -> bool: | ||
RequestParams:dict = { | ||
"username": self.mail, | ||
"password": self.password | ||
} | ||
requesturl = self.authserver + "/authserver/signout" | ||
result = requests.post(requesturl, data=json.dumps(RequestParams, ensure_ascii=False), | ||
headers={"Content-Type": "application/json"}) | ||
if(result.status_code != 204): | ||
return False | ||
else: | ||
return True |