mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-03 03:40:49 +08:00
init project
This commit is contained in:
0
lib/tasks/.keep
Normal file
0
lib/tasks/.keep
Normal file
53
lib/tasks/git_check.rake
Normal file
53
lib/tasks/git_check.rake
Normal file
@@ -0,0 +1,53 @@
|
||||
# 需要做的几件事:
|
||||
# 1、检测两边(TPM\TPI)同步后有更新的仓库
|
||||
#
|
||||
|
||||
namespace :git do
|
||||
desc "检测是否TPM是否需要更新"
|
||||
task :shixun_check_update => :environment do
|
||||
g = Gitlab.client
|
||||
file_txt = File.new("lib/gitcheck/shixun_update.txt", "r+")
|
||||
file_error_txt = File.new("lib/gitcheck/shixun_update_error.txt", "r+")
|
||||
host = EduSetting.find_by_name("git_address_domain").try(:value)
|
||||
Shixun.find_each do |shixun|
|
||||
begin
|
||||
gitlab_commit = g.commits(shixun.gpid, :ref_name => 'master').first.try(:id)
|
||||
repo_name = shixun.repo_name
|
||||
git_commit = GitService.commits(repo_path: "#{repo_name}.git").first["id"]
|
||||
git_url = host + "/" + repo_name + ".git\n"
|
||||
if git_commit != gitlab_commit
|
||||
file_txt.syswrite(git_url)
|
||||
end
|
||||
rescue
|
||||
file_error_txt.syswrite("#{shixun.identifier}\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "检测版本库是否有更新"
|
||||
task :myshixuns_check_update => :environment do
|
||||
g = Gitlab.client
|
||||
file_txt = File.new("lib/gitcheck/myshixun_update.txt", "r+")
|
||||
file_error_txt = File.new("lib/gitcheck/myshixun_update_error.txt", "r+")
|
||||
host = EduSetting.find_by_name("git_address_domain").try(:value)
|
||||
Myshixun.find_each do |myshixun|
|
||||
begin
|
||||
gitlab_commit = g.commits(myshixun.gpid, :ref_name => 'master').first.try(:id)
|
||||
repo_name = myshixun.repo_name
|
||||
git_commit = GitService.commits(repo_path: "#{repo_name}.git").first["id"]
|
||||
git_url = host + "/" + repo_name + ".git\n"
|
||||
if git_commit != gitlab_commit
|
||||
file_txt.syswrite(git_url)
|
||||
end
|
||||
rescue
|
||||
file_error_txt.syswrite("#{myshixun.identifier}\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# 检测TPI没有迁移过来的
|
||||
task :check => :environment do
|
||||
|
||||
end
|
||||
end
|
||||
53
lib/tasks/gitlab.rake
Normal file
53
lib/tasks/gitlab.rake
Normal file
@@ -0,0 +1,53 @@
|
||||
namespace :sync do
|
||||
desc "sync repository for myshixun"
|
||||
task :myshixun => :environment do
|
||||
g = Gitlab.client
|
||||
myshixuns = Myshixun.where("repo_name is null")
|
||||
myshixuns.find_each do |myshixun|
|
||||
begin
|
||||
puts myshixun.identifier
|
||||
repo_name = g.project(myshixun.gpid).path_with_namespace
|
||||
puts repo_name
|
||||
myshixun.update_column(:repo_name, repo_name)
|
||||
rescue Exception => e
|
||||
Rails.logger.error("#{e.message}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
task :shixun => :environment do
|
||||
g = Gitlab.client
|
||||
shixuns = Shixun.where("repo_name is null")
|
||||
shixuns.find_each do |shixun|
|
||||
begin
|
||||
puts shixun.identifier
|
||||
repo_name = g.project(shixun.gpid).path_with_namespace
|
||||
puts repo_name
|
||||
shixun.update_column(:repo_name, repo_name)
|
||||
rescue Exception => e
|
||||
Rails.logger.error("#{e.message}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
task :check => :environment do
|
||||
g = Gitlab.client
|
||||
shixuns = Shixun.where("repo_name is null and fork_from is not null")
|
||||
shixuns.find_each do |shixun|
|
||||
begin
|
||||
puts shixun.identifier
|
||||
original_shixun = Shixun.find(fork_from)
|
||||
|
||||
gshixun = g.fork(original_shixun.gpid, shixun.owner.try(:gid))
|
||||
|
||||
repo_name = g.project(gshixun.id).path_with_namespace
|
||||
puts repo_name
|
||||
shixun.update_attributes(:repo_name => repo_name, :gpid => gshixun.id)
|
||||
rescue Exception => e
|
||||
Rails.logger.error("#{e.message}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
lib/tasks/sync_table_structure.rake
Normal file
11
lib/tasks/sync_table_structure.rake
Normal file
@@ -0,0 +1,11 @@
|
||||
desc "Initialize the data table structure"
|
||||
|
||||
namespace :sync_table_structure do
|
||||
task import_csv: :environment do
|
||||
puts "init table structure......."
|
||||
|
||||
system "mysql -uroot -p123456 -h127.0.0.1 forge_development < #{Rails.root}/db/structure.sql"
|
||||
|
||||
puts "init success"
|
||||
end
|
||||
end
|
||||
70
lib/tasks/sync_trustie_program_question.rake
Normal file
70
lib/tasks/sync_trustie_program_question.rake
Normal file
@@ -0,0 +1,70 @@
|
||||
desc "同步trustie的编程作业"
|
||||
|
||||
namespace :sync_program do
|
||||
DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z)
|
||||
task data: :environment do
|
||||
ProgramBank.where(homework_type: 2).each do |program|
|
||||
unless Hack.where(name: program.name).exists?
|
||||
strip_des = strip_html(program.description, 5000)
|
||||
description = strip_des.present? ? strip_des : program.name
|
||||
hack_params = {name: program.name[0..59], description: description, difficult: 1, open_or_not: 1, score: 200, status:0, time_limit: 3, sub_discipline_id: program.oj_sub_discipline_id}
|
||||
puts "language::::#{program.language}"
|
||||
puts "program_bank::::#{program.id}"
|
||||
hack = Hack.new(hack_params)
|
||||
hack.user_id = 1
|
||||
hack.identifier = generate_identifier Hack, 8
|
||||
ActiveRecord::Base.transaction do
|
||||
hack.save!
|
||||
# 创建测试集与代码
|
||||
position = 1
|
||||
ProgramBankTest.where(homework_bank_id: program.id).each do |test_set|
|
||||
if !test_set.input.blank? && !test_set.output.blank? && !hack.hack_sets.where(input: test_set.input).exists? && test_set.input.length <= 1000 && test_set.output.length <= 1000
|
||||
hack.hack_sets.create!(input: test_set.input, output: test_set.output, position: position)
|
||||
position += 1
|
||||
end
|
||||
end
|
||||
# 新建知识点
|
||||
hack_code_params = {code: program.standard_code.blank? ? nil : Base64.encode64(program.standard_code), language: program.oj_language}
|
||||
hack_codes = hack.hack_codes.new(hack_code_params)
|
||||
hack_codes.modify_time = Time.now
|
||||
hack_codes.save!
|
||||
new_item_params = {name: program.name, sub_discipline_id: program.oj_sub_discipline_id, container: hack, item_type: 'PROGRAM', public: 0, difficulty: 1, user_id: 1}
|
||||
ItemBank.create!(new_item_params)
|
||||
end
|
||||
puts hack.id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 随机生成字符
|
||||
def generate_identifier(container, num)
|
||||
code = DCODES.sample(num).join
|
||||
if container == User
|
||||
while container.exists?(login: code) do
|
||||
code = DCODES.sample(num).join
|
||||
end
|
||||
else
|
||||
while container.exists?(identifier: code) do
|
||||
code = DCODES.sample(num).join
|
||||
end
|
||||
end
|
||||
code
|
||||
end
|
||||
|
||||
def strip_html(text, len=0, endss="...")
|
||||
ss = ""
|
||||
if !text.nil? && text.length>0
|
||||
ss=text.gsub(/<\/?.*?>/, '').strip
|
||||
ss = ss.gsub(/ */, '')
|
||||
ss = ss.gsub(/\r\n/,'') #新增
|
||||
ss = ss.gsub(/\n/,'') #新增
|
||||
if len > 0 && ss.length > len
|
||||
ss = ss[0, len-4] + endss
|
||||
elsif len > 0 && ss.length <= len
|
||||
ss = ss
|
||||
#ss = truncate(ss, :length => len)
|
||||
end
|
||||
end
|
||||
ss
|
||||
end
|
||||
end
|
||||
73
lib/tasks/user.rake
Normal file
73
lib/tasks/user.rake
Normal file
@@ -0,0 +1,73 @@
|
||||
# add test user
|
||||
namespace :sync do
|
||||
task :add_user => :environment do
|
||||
DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z)
|
||||
user_count = ENV['args'].split(",")[0].to_i # 用户数
|
||||
rand_chart = ENV['args'].split(",")[1] # 手机号或者邮箱等前面的随机数
|
||||
|
||||
puts rand_chart
|
||||
puts user_count
|
||||
|
||||
(1..user_count).each do |i|
|
||||
puts i
|
||||
|
||||
no = sprintf("%04d", i)
|
||||
phone = "#{rand_chart}160731#{no}"
|
||||
mail = "#{rand_chart}#{no}@educoder.net"
|
||||
code = generate_identifier User, 8
|
||||
login = "m" + code
|
||||
|
||||
|
||||
l = "赵,钱,孙,李,周,吴,郑,王,冯,陈,褚,卫,蒋,沈,韩,杨,朱,秦,尤,许,何,吕,施,张,孔,曹,严,华,金,魏,陶,姜,黄".split(",")
|
||||
f = "爱童,安妮,若婷,安煜,博,雷,梅,静,士红,大龙,冰波,慧娟,梅,婧婧,军,淋,真,维,涛,程程,
|
||||
谷南,慕儿,夏岚,友儿,小萱,紫青,妙菱,冬寒,曼柔,语蝶,青筠,夜安,觅海,问安,晓槐,雅山,访云,翠容,寒凡,晓绿,以菱,
|
||||
冬云,含玉,访枫,含卉,夜白,冷安,灵竹,醉薇,元珊,幻波,盼夏,元瑶,迎曼,水云,访琴,谷波,乐之,笑白,之山,妙海,紫霜,
|
||||
平夏,凌旋,孤丝,怜寒,向萍,凡松,青丝,翠安,如天,凌雪,绮菱,代云,南莲,寻南,春文,香薇,冬灵,凌珍,采绿,天春,沛文,
|
||||
紫槐,幻柏,采文,春梅,雪旋,盼海,映梦,安雁,映容,凝阳,访风,天亦,平绿,盼香,觅风,小霜,雪萍,半雪,山柳,谷雪,靖易,
|
||||
白薇,梦菡,飞绿,如波,又晴,友易,香菱,冬亦,问,妙春,海冬,半安,平春,幼柏,秋灵,凝芙,念烟,白山,从灵,尔芙,迎蓉,
|
||||
念寒,翠绿,翠芙,靖儿,妙柏,千凝,小珍,天巧,妙旋,雪枫,夏菡,元绿,痴灵,绮琴,雨双,听枫,觅荷,凡之,晓凡,雅彤,香薇,
|
||||
孤风,从安,绮彤,之玉,雨珍,幻丝,代梅,香波,青亦,元菱,海瑶,飞槐,听露,梦岚,幻竹,新冬,盼翠,谷云".split(",")
|
||||
|
||||
lastname = l[rand(l.length)].strip + f[rand(f.length)].strip
|
||||
|
||||
edit_params = {
|
||||
login: login,
|
||||
admin: false,
|
||||
type: User,
|
||||
phone: phone,
|
||||
lastname: lastname,
|
||||
mail: mail,
|
||||
authentication: 1,
|
||||
professional_certification: 1,
|
||||
profile_completed: true,
|
||||
is_test: true
|
||||
}
|
||||
user = User.create!(edit_params)
|
||||
user.password = "edu12345678"
|
||||
user.save!
|
||||
UserExtension.create!(user_id: user.id, school_id: 117)
|
||||
puts i
|
||||
end
|
||||
end
|
||||
|
||||
task :password => :environment do
|
||||
|
||||
end
|
||||
|
||||
|
||||
# 随机生成字符
|
||||
def generate_identifier(container, num)
|
||||
code = DCODES.sample(num).join
|
||||
if container == User
|
||||
while container.exists?(login: code) do
|
||||
code = DCODES.sample(num).join
|
||||
end
|
||||
else
|
||||
while container.exists?(identifier: code) do
|
||||
code = DCODES.sample(num).join
|
||||
end
|
||||
end
|
||||
code
|
||||
end
|
||||
|
||||
end
|
||||
28
lib/tasks/user_login.rake
Normal file
28
lib/tasks/user_login.rake
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace :user do
|
||||
task :update_login => :environment do
|
||||
begin
|
||||
user_count = ENV['args'].split(",")[0].to_i # 更新的用户数
|
||||
status = ENV['args'].split(",")[1] # 测试用户类型
|
||||
base_login = ENV['args'].split(",")[2] # 基本的用户参数
|
||||
|
||||
users = User.where(:is_test => status).limit(user_count)
|
||||
users.each_with_index do |user, i|
|
||||
puts i
|
||||
|
||||
no = sprintf("%04d", i)
|
||||
login = "#{base_login}#{no}"
|
||||
puts no
|
||||
puts login
|
||||
|
||||
sql1 = "update users set login='#{login}' where id=#{user.id}"
|
||||
sql2 = "update users set lastname='#{login}' where id=#{user.id}"
|
||||
sql3 = "update users set nickname='#{login}' where id=#{user.id}"
|
||||
ActiveRecord::Base.connection.execute(sql1)
|
||||
ActiveRecord::Base.connection.execute(sql2)
|
||||
ActiveRecord::Base.connection.execute(sql3)
|
||||
end
|
||||
rescue Exception => e
|
||||
Rails.logger.error(e.message)
|
||||
end
|
||||
end
|
||||
end
|
||||
22
lib/tasks/user_profile_completed_transfer.rake
Normal file
22
lib/tasks/user_profile_completed_transfer.rake
Normal file
@@ -0,0 +1,22 @@
|
||||
desc 'transfer user profile completed data'
|
||||
task transfer_user_profile_completed: :environment do
|
||||
buffer_size = 1000
|
||||
|
||||
ids = []
|
||||
User.includes(:user_extension).find_each do |user|
|
||||
extension = user.user_extension
|
||||
next if extension.blank? || user.lastname.blank? || user.mail.blank? || extension.school_id.blank? || extension.identity.blank?
|
||||
|
||||
ids << user.id
|
||||
if ids.size == buffer_size
|
||||
batch_update_user_profile_completed(ids)
|
||||
ids.clear
|
||||
end
|
||||
end
|
||||
|
||||
batch_update_user_profile_completed(ids) if ids.present?
|
||||
end
|
||||
|
||||
def batch_update_user_profile_completed(ids)
|
||||
User.connection.execute("UPDATE users SET profile_completed = true WHERE id IN (#{ids.join(',')})")
|
||||
end
|
||||
98
lib/tasks/zip_pack.rake
Normal file
98
lib/tasks/zip_pack.rake
Normal file
@@ -0,0 +1,98 @@
|
||||
# 执行示例 bundle exec rake zip_pack:shixun_pack class=Course ids=123,2323 parallel_size=4
|
||||
# 执行示例 bundle exec rake zip_pack:shixun_pack class=HomeworkCommon ids=123,2323
|
||||
namespace :zip_pack do
|
||||
desc "手工打包作品"
|
||||
OUTPUT_FOLDER = "#{Rails.root}/files/archiveZip"
|
||||
|
||||
task :shixun_pack => :environment do
|
||||
|
||||
if ENV['class'] && ENV['ids']
|
||||
parallel_size = ENV['parallel_size'] || 2
|
||||
parallel_size = parallel_size.to_i
|
||||
env_ids = ENV['ids'].split(",").map(&:to_i)
|
||||
folders = []
|
||||
if ENV['class'] == "Course"
|
||||
courses = Course.where(id: env_ids)
|
||||
courses.each do |course|
|
||||
homeworks = course.practice_homeworks.homework_published
|
||||
new_dir_name = "#{course.name.to_s.strip}_#{Time.now.strftime("%Y%m%d%H%M%S").to_s}"
|
||||
new_dir_name.gsub!(" ", "-")
|
||||
new_dir_name.gsub!("/", "_")
|
||||
new_folder = "#{OUTPUT_FOLDER}/#{new_dir_name}"
|
||||
zip_homework_pdf homeworks, new_folder, parallel_size
|
||||
folders << new_folder
|
||||
end
|
||||
else
|
||||
homeworks = HomeworkCommon.where(id: env_ids)
|
||||
new_dir_name = "#{homeworks.first&.course&.name.to_s.strip}_#{Time.now.strftime("%Y%m%d%H%M%S").to_s}"
|
||||
new_dir_name.gsub!(" ", "-")
|
||||
new_dir_name.gsub!("/", "_")
|
||||
new_folder = "#{OUTPUT_FOLDER}/#{new_dir_name}"
|
||||
zip_homework_pdf homeworks, new_folder, parallel_size
|
||||
folders << new_folder
|
||||
end
|
||||
|
||||
puts "下载路径: #{folders.join(",")}"
|
||||
end
|
||||
end
|
||||
|
||||
def zip_homework_pdf homeworks, folder, parallel_size
|
||||
Dir.mkdir(folder) unless File.directory?(folder)
|
||||
|
||||
homeworks.includes(:score_student_works).each do |homework|
|
||||
out_file_name = "#{Time.now.strftime("%Y%m%d%H%M%S").to_s}-#{homework.course_id}-#{homework.name}.zip"
|
||||
out_file_name.gsub!(" ", "-")
|
||||
out_file_name.gsub!("/", "_")
|
||||
|
||||
zipfile_name = "#{folder}/#{out_file_name}"
|
||||
|
||||
student_works = homework.score_student_works
|
||||
|
||||
if student_works.size > 0
|
||||
pdfs = []
|
||||
file_paths = []
|
||||
student_works.find_in_batches(batch_size: 500) do |sw|
|
||||
Parallel.each(sw, in_threads: parallel_size) do |student_work|
|
||||
export = ExportShixunReportService.new(homework, student_work)
|
||||
pdf = export.to_pdf
|
||||
pdfs << pdf
|
||||
file_paths << {filename: export.filename, path: pdf.path}
|
||||
puts "out: #{export.filename}_#{pdf.path}"
|
||||
end
|
||||
end
|
||||
|
||||
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zip|
|
||||
file_paths.each do |pdf|
|
||||
begin
|
||||
zip.add(pdf[:filename], pdf[:path])
|
||||
rescue => ex
|
||||
Rails.logger.error(ex.message)
|
||||
|
||||
zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") }
|
||||
next
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
zipfile = {:message => "no file"}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 执行示例 bundle exec rake zip_pack:homework_attach_pack args=123
|
||||
task :homework_attach_pack => :environment do
|
||||
include ExportHelper
|
||||
if ENV['args']
|
||||
homework_id = ENV['args']
|
||||
homework = HomeworkCommon.find homework_id
|
||||
zip_works = homework.student_works.where("work_status > 0")
|
||||
if zip_works.size > 0
|
||||
zipfile = zip_homework_common homework, zip_works
|
||||
else
|
||||
zipfile = {:message => "no file"}
|
||||
end
|
||||
puts "out: #{zipfile}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user