setup.py 6.6 KB

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