setup.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import os
  2. import sys
  3. import time
  4. from typing import Union, List
  5. print("开始检查依赖")
  6. try:
  7. __import__("pip")
  8. except ImportError:
  9. print("检查结束, 未找到pip")
  10. exit(1)
  11. else:
  12. print("依赖 pip 存在")
  13. if os.system(f"{sys.executable} -m pip install --upgrade pip") != 0:
  14. print(f"依赖 pip 更新失败")
  15. else:
  16. print(f"依赖 pip 更新成功")
  17. __setup = os.path.dirname(os.path.abspath(__file__))
  18. def check_import(packages: Union[str, List[str]], pips: Union[str, List[str]]):
  19. if type(pips) is str:
  20. pips = [pips]
  21. if type(packages) is str:
  22. packages = [packages]
  23. try:
  24. for package in packages:
  25. __import__(package)
  26. print(f"依赖 {package} 存在")
  27. except ImportError:
  28. for pip in pips:
  29. if os.system(f"{sys.executable} -m pip install {pip}") != 0:
  30. print(f"{pip} 依赖安装失败")
  31. exit(1)
  32. else:
  33. print(f"依赖 {packages}:{pip} 安装成功")
  34. check_import("cv2", "opencv-python") # 图像处理
  35. check_import("qrcode", "qrcode") # 二维码生成
  36. check_import("pymysql", "PyMySQL") # 连接 MySQL服务器
  37. check_import("cryptography", "cryptography") # 链接 MySQL 服务器时加密
  38. check_import("flask", "Flask") # 网页服务
  39. check_import("PIL", "Pillow") # 图片处理
  40. check_import("numpy", "numpy") # matplotlib依赖
  41. check_import("matplotlib", "matplotlib") # matplotlib依赖
  42. check_import(["oss2", "viapi", "aliyunsdkcore", "aliyunsdkimagerecog"],
  43. ["oss2", "aliyun-python-sdk-viapiutils", "viapi-utils", "aliyun-python-sdk-imagerecog"]) # 阿里云依赖
  44. print("是否执行数据库初始化程序?\n执行初始化程序会令你丢失所有数据.")
  45. res = input("[Y/n]")
  46. if res != 'Y':
  47. exit(0)
  48. import pymysql
  49. from conf import Config
  50. mysql_url = Config.mysql_url
  51. mysql_name = Config.mysql_name
  52. mysql_passwd = Config.mysql_passwd
  53. sql = pymysql.connect(user=mysql_name, password=mysql_passwd, host=mysql_url)
  54. cursor = sql.cursor()
  55. with open(os.path.join(__setup, "setup.sql"), "r", encoding='utf-8') as f:
  56. all_sql = f.read().split(';')
  57. for s in all_sql:
  58. if s.strip() == "":
  59. continue
  60. cursor.execute(f"{s};")
  61. sql.commit()
  62. admin_passwd = input("Enter Admin Passwd: ")
  63. admin_phone = ""
  64. while len(admin_phone) != 11:
  65. admin_phone = input("Enter Admin Phone[len = 11]: ")
  66. from tool.login import create_uid
  67. from tool.time_ import mysql_time
  68. # 生成基本 admin 用户
  69. uid = create_uid("admin", admin_passwd)
  70. cursor.execute(f"INSERT INTO user(UserID, Name, IsManager, Phone, Score, Reputation, CreateTime) "
  71. f"VALUES ('{uid}', 'admin', 1, '{admin_phone}', 10, 300, {mysql_time()});")
  72. sql.commit()
  73. print("是否伪造数据?")
  74. if input("[Y/n]") != "Y":
  75. cursor.close()
  76. sql.close()
  77. exit(0)
  78. # 伪造数据
  79. # 只有执行setup时可以伪造数据, 目的是不希望在生产环境上允许伪造数据
  80. # 不过这是个开源程序, 所以你完全可以绕开限制
  81. import random
  82. from tool.login import randomPassword
  83. random_manager = []
  84. random_normal = []
  85. loc = ["LOC-A", "LOC-B", "LOC-C"]
  86. def random_phone() -> str:
  87. r_phone = ""
  88. while len(r_phone) < 11:
  89. r_phone += f"{random.randint(0, 9)}"
  90. return r_phone
  91. def random_time() -> str:
  92. r_time = time.time()
  93. r_h = random.randint(0, 4 * 24)
  94. r_time -= r_h * 60 * 60
  95. return mysql_time(r_time)
  96. def random_time_double() -> tuple[str, str]:
  97. r_time2 = r_time1 = time.time()
  98. r_h1 = random.randint(0, 4 * 24)
  99. r_h2 = random.randint(0, 4 * 24)
  100. r_time1 -= min(r_h1, r_h2) * 60 * 60
  101. r_time2 -= max(r_h1, r_h2) * 60 * 60
  102. return mysql_time(r_time1), mysql_time(r_time2)
  103. def random_user(r_name, r_passwd, r_phone, r_time, is_manager: int, cur):
  104. r_score = random.randint(0, 50000) / 100
  105. r_reputation = random.randint(5, 995)
  106. r_uid = create_uid(r_name, r_passwd)
  107. cur.execute(f"INSERT INTO user(UserID, Name, IsManager, Phone, Score, Reputation, CreateTime) "
  108. f"VALUES ('{r_uid}', '{r_name}', {is_manager}, '{r_phone}', {r_score}, {r_reputation}, {r_time});")
  109. if is_manager:
  110. random_manager.append(r_uid)
  111. print(f"管理员: {r_name} {r_passwd} {r_phone} {r_reputation} {r_score} {r_uid}")
  112. else:
  113. random_normal.append(r_uid)
  114. print(f"普通用户: {r_name} {r_passwd} {r_phone} {r_reputation} {r_score} {r_uid}")
  115. def random_garbage_n(r_time, cur):
  116. cur.execute(f"INSERT INTO garbage(CreateTime, Flat) VALUES ({r_time}, 0);")
  117. def random_garbage_c(r_time, r_time2, cur):
  118. user = random.choice(random_normal)
  119. r_loc = random.choice(loc)
  120. cur.execute(f"INSERT INTO garbage(CreateTime, Flat, UserID, UseTime, GarbageType, Location) "
  121. f"VALUES ({r_time}, 1, '{user}', {r_time2}, {random.randint(1, 4)}, '{r_loc}');")
  122. def random_garbage_u(r_time, r_time2, cur):
  123. user = random.choice(random_normal)
  124. checker = random.choice(random_manager)
  125. r_loc = random.choice(loc)
  126. cur.execute(f"INSERT INTO garbage(CreateTime, Flat, UserID, UseTime, GarbageType, Location, "
  127. f"CheckerID, CheckResult) "
  128. f"VALUES ({r_time}, 1, '{user}', {r_time2}, {random.randint(1, 4)}, '{r_loc}', "
  129. f"'{checker}', {random.randint(0, 1)});")
  130. print("步骤1, 注册管理账户[输入q结束]:")
  131. while True:
  132. name = input("输入用户名:")
  133. passwd = input("输入密码:")
  134. phone = input("输入手机号码[输入x表示随机]:")
  135. creat_time = input("是否随机时间[n=不随机 y=随机]:")
  136. if name == 'q' or passwd == 'q' or phone == 'q' or creat_time == 'q':
  137. break
  138. if phone == 'x':
  139. phone = random_phone()
  140. if creat_time == 'n':
  141. c_time = mysql_time()
  142. else:
  143. c_time = random_time()
  144. random_user(name, passwd, phone, c_time, 1, cursor)
  145. print("步骤2, 注册普通账户[输入q结束]:")
  146. while True:
  147. name = input("输入用户名:")
  148. passwd = input("输入密码:")
  149. phone = input("输入手机号码[输入x表示随机]:")
  150. creat_time = input("是否随机时间[n=不随机 y=随机]:")
  151. if name == 'q' or passwd == 'q' or phone == 'q' or creat_time == 'q':
  152. break
  153. if creat_time == 'n':
  154. c_time = mysql_time()
  155. else:
  156. c_time = random_time()
  157. if phone == 'x':
  158. phone = random_phone()
  159. random_user(name, passwd, phone, c_time, 0, cursor)
  160. count = int(input("步骤3, 注册随机管理员账户[输入个数]:"))
  161. while count > 0:
  162. name = randomPassword()[:5]
  163. passwd = randomPassword()
  164. phone = random_phone()
  165. c_time = random_time()
  166. random_user(name, passwd, phone, c_time, 1, cursor)
  167. count -= 1
  168. count = int(input("步骤3, 注册随机普通账户[输入个数]:"))
  169. while count > 0:
  170. name = randomPassword()[:5]
  171. passwd = randomPassword()
  172. phone = random_phone()
  173. c_time = random_time()
  174. random_user(name, passwd, phone, c_time, 0, cursor)
  175. count -= 1
  176. count = int(input("步骤4, 注册随机已检查垃圾袋[输入个数]:"))
  177. while count > 0:
  178. count -= 1
  179. c_time2, c_time1 = random_time_double()
  180. random_garbage_u(c_time1, c_time2, cursor)
  181. count = int(input("步骤5, 注册随机待检查垃圾袋[输入个数]:"))
  182. while count > 0:
  183. count -= 1
  184. c_time2, c_time1 = random_time_double()
  185. random_garbage_c(c_time1, c_time2, cursor)
  186. count = int(input("步骤6, 注册随机未使用垃圾袋[输入个数]:"))
  187. while count > 0:
  188. count -= 1
  189. c_time = random_time()
  190. random_garbage_n(c_time, cursor)
  191. sql.commit()
  192. cursor.close()
  193. sql.close()