websoft9/cli/model.py

118 lines
3 KiB
Python
Raw Normal View History

2021-09-26 07:22:38 +00:00
2021-09-28 10:34:18 +00:00
import os, io, sys, platform, psutil, json, secrets, string
2021-09-26 07:22:38 +00:00
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union
import urllib.request
2021-09-28 10:34:18 +00:00
2021-09-26 07:22:38 +00:00
class SmoothUrl:
2021-09-28 10:34:18 +00:00
''' Get the best smooth url for Git or Download'''
2021-09-26 07:22:38 +00:00
def __init__(self):
pass
def res(url_list: Tuple):
for item in url_list:
try:
urllib.request.urlopen(item,timeout=3).read()
2021-09-28 10:34:18 +00:00
print("Smooth URL is: " + item)
2021-09-26 07:22:38 +00:00
return item
2021-09-28 10:34:18 +00:00
except:
2021-09-26 07:22:38 +00:00
continue
2021-09-28 10:34:18 +00:00
print("Necessary resource URL can not reachable, system exit!")
sys.exit(0)
2021-09-26 07:22:38 +00:00
2021-09-28 10:34:18 +00:00
class GitOp:
'''Git operation'''
2021-09-26 07:22:38 +00:00
def __init__(self):
pass
2021-09-29 10:24:23 +00:00
def gitClone(self, cmd: str):
2021-09-28 10:34:18 +00:00
'''git clone'''
try:
print("Command is "+cmd)
os.system(cmd)
except:
print("Git clone failed, try again and check your URL can be accessed")
sys.exit(0)
2021-09-26 09:57:34 +00:00
class FileOp:
'''File operation'''
def __init__(self):
pass
2021-09-29 10:24:23 +00:00
def printJson(self, path: str):
'''output file content'''
2021-09-26 09:57:34 +00:00
with open(path,newline='') as file:
2021-09-28 10:34:18 +00:00
print(file.read())
2021-09-29 10:24:23 +00:00
def fileToJson(self, path: str, remark: Optional[str] = "#", separate: Optional[str] = "="):
''' convert file to Json '''
dict = {}
with open(path) as fh:
for line in fh:
print(line.find(remark))
if line.find(remark) != 0:
item, value = line.strip().split(separate, -1)
item = line.strip()
dict[item] = value
else:
continue
fh.close()
print (dict)
2021-09-28 10:34:18 +00:00
class NetOp:
'''Network and port manage'''
def __init__(self):
pass
def checkPort(self, port: int):
'''check the target port's status'''
search_key = "port="+str(port)
if str(psutil.net_connections()).find(search_key) != -1:
print(str(port)+" is used")
return False
else:
print(str(port)+" is free")
return True
def setPort(self, port: int):
'''set usable port'''
while self.checkPort(port) == False:
port=port+1
print(port)
return port
class SecurityOp:
'''Password and security operation'''
def __int__(self):
pass
def randomPass(self, length: Optional[int] = 16):
2021-09-29 10:24:23 +00:00
'''set strong password'''
2021-09-28 10:34:18 +00:00
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(secrets.choice(alphabet) for i in range(length))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
break
print(password)
2021-09-29 10:24:23 +00:00
test=FileOp()
2021-09-28 10:34:18 +00:00
#test.setPort(9001)
2021-09-29 10:24:23 +00:00
test.fileToJson('./joomla/.env_all')