完成生成cine.sql的逻辑

main
RogerWork 10 months ago
parent db9471e2e0
commit 8b53aacc54
  1. 95
      update/utils/git_util.py

@ -27,6 +27,7 @@ class GitUtil:
'host': '172.16.3.112',
'user': 'test',
'password': 'cine123456',
'port': '3306',
'connect_timeout': 5,
}
@ -38,7 +39,7 @@ class GitUtil:
# 获取指定版本的最新代码
def checkout_release(self):
local_repo = Repo(self.local_code_path)
r = local_repo.git.checkout(self.short_release)
r = local_repo.git.checkout(self.get_release())
print(r)
r = local_repo.git.pull()
print(r)
@ -68,13 +69,27 @@ class GitUtil:
}
Release.objects.create(**data)
# 复制文cine.sql到本地路径
def copy_cine_sql(self):
# 复制cine.sql到本地路径cine_xxx_org.sql,然后生成新数据库名称的cine_xxx.sql
# 如果cine.sql和cine_xxx_org.sql文件md5一致则跳过
# 如果cine.sql和cine_xxx_org.sql文件md5不一致,则重新拷贝cine_xxx_org.sql并生成cine_xxx.sql
# 如果生成新的cine_xxx.sql则返回True,否则返回False
def create_cine_sql(self):
self.checkout_release()
org_path = os.path.join(self.local_code_path, 'install', 'cine.sql')
target_org_path = os.path.join(self.cine_sql_path, f'{self.db_name}_org.sql')
target_path = os.path.join(self.cine_sql_path, f'{self.db_name}.sql')
# if os.path.exists(target_path):
# os.remove(target_path)
if os.path.exists(target_org_path):
if self.get_md5(org_path) == self.get_md5(target_org_path):
if os.path.exists(target_path):
return False
else:
os.remove(target_org_path)
os.remove(target_path)
shutil.copyfile(org_path, target_org_path)
else:
shutil.copyfile(org_path, target_org_path)
with open(target_path, 'w', encoding='utf-8') as target_file:
with open(org_path, 'r', encoding='utf') as f:
lines = f.readlines()
@ -93,52 +108,13 @@ class GitUtil:
f.close()
target_file.writelines(target_lines)
target_file.close()
return True
# 检查cine.sql是否是最新的
def check_cine_sql(self):
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.db_name}.sql')):
saved_md5 = self.get_md5(sql_path)
new_md5 = self.get_md5(os.path.join(self.local_code_path, 'install', 'cine.sql'))
if saved_md5 == new_md5:
print('md5相同')
return True
else:
print('md5不同')
os.remove(sql_path)
return False
else:
print('文件不存在')
return False
def write_sql(self):
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.db_name}.sql')):
execute_sql_list = []
with open(sql_path, 'r', encoding='utf-8') as sql_file:
content = sql_file.read()
sql_list = content.split('\n\n')
for sql in sql_list:
sql = sql.strip()
if sql.startswith('CREATE'):
print('CREATE')
execute_sql_list.append(sql + ';')
if sql.startswith('DROP'):
print('DROP')
execute_sql_list.append(sql + ';')
if sql.startswith('USE'):
print('USE')
execute_sql_list.append(sql + ';')
db_conn = pymysql.Connect(**self.db_config)
db_cursor = db_conn.cursor()
# for ext_sql in execute_sql_list:
# result = db_cursor.execute(ext_sql)
# print(result, ext_sql)
# 将生成的cine.sql写入数据库
def write_cine_sql_by_mysql(self):
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.db_name}.sql')):
cmd = f'mysql -h172.16.3.112 -P3306 -utest -pcine123456 < {sql_path}'
cmd = f'mysql -h{self.db_config["host"]} -P{self.db_config["port"]} -u{self.db_config["user"]} -p{self.db_config["password"]} < {sql_path}'
r = os.system(cmd)
print(type(r))
print(r)
# 获取数据库名称
@ -154,15 +130,18 @@ class GitUtil:
# 写入cine.sql
def handle_create_cine(self):
if not self.check_cine_sql():
self.copy_cine_sql()
self.write_cine_sql_by_mysql()
# 如果生成了新的文件则写库,如果sql没有变化则跳过
if self.create_cine_sql():
self.write_cine_sql_by_mysql()
# 获取端短版本号
# @staticmethod
# def get_short_version_by_release(_release):
# data = Release.objects.filter(short_release=_release).first()
# return data.short_release
# 获取release信息
def get_release(self, _release=''):
if _release == '':
release = self.short_release
else:
release = _release
rel = Release.objects.filter(short_release=release).first()
return rel.release
@staticmethod
def get_short_version():
@ -181,6 +160,6 @@ class GitUtil:
return m.hexdigest() # 返回md5对象
if __name__ == '__main__':
git_util = GitUtil()
# git_util.clone()
# if __name__ == '__main__':
# git_util = GitUtil()
# # git_util.clone()

Loading…
Cancel
Save