2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-03 08:39:21 +08:00

DEV: Fix methods removed in Ruby 3.2 (#15459)

* File.exists? is deprecated and removed in Ruby 3.2 in favor of
File.exist?
* Dir.exists? is deprecated and removed in Ruby 3.2 in favor of
Dir.exist?
This commit is contained in:
Peter Zhu 2022-01-05 12:45:08 -05:00 committed by GitHub
parent 692ba188bf
commit c5fd8c42db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
75 changed files with 140 additions and 140 deletions

View file

@ -112,7 +112,7 @@ unless $? == 0
abort "Apache Bench is not installed. Try: apt-get install apache2-utils or brew install ab"
end
unless File.exists?("config/database.yml")
unless File.exist?("config/database.yml")
puts "Copying database.yml.development.sample to database.yml"
`cp config/database.yml.development-sample config/database.yml`
end

View file

@ -359,7 +359,7 @@ class BulkImport::DiscourseMerger < BulkImport::Base
next if row['user_id'].nil?
end
row['url'] = "/uploads/default/#{rel_filename}" if File.exists?(absolute_filename)
row['url'] = "/uploads/default/#{rel_filename}" if File.exist?(absolute_filename)
@raw_connection.put_copy_data(row.values)
end

View file

@ -203,7 +203,7 @@ class BulkImport::Vanilla < BulkImport::Base
end
def import_avatars
if ATTACHMENTS_BASE_DIR && File.exists?(ATTACHMENTS_BASE_DIR)
if ATTACHMENTS_BASE_DIR && File.exist?(ATTACHMENTS_BASE_DIR)
puts "", "importing user avatars"
start = Time.now
@ -237,7 +237,7 @@ class BulkImport::Vanilla < BulkImport::Base
next
end
if !File.exists?(photo_path)
if !File.exist?(photo_path)
puts "Path to avatar file not found! Skipping. #{photo_path}"
next
end
@ -265,7 +265,7 @@ class BulkImport::Vanilla < BulkImport::Base
end
def import_attachments
if ATTACHMENTS_BASE_DIR && File.exists?(ATTACHMENTS_BASE_DIR)
if ATTACHMENTS_BASE_DIR && File.exist?(ATTACHMENTS_BASE_DIR)
puts "", "importing attachments"
start = Time.now
@ -297,7 +297,7 @@ class BulkImport::Vanilla < BulkImport::Base
path.gsub!("s3://uploads/", "")
file_path = "#{ATTACHMENTS_BASE_DIR}/#{path}"
if File.exists?(file_path)
if File.exist?(file_path)
upload = create_upload(post.user.id, file_path, File.basename(file_path))
if upload && upload.errors.empty?
# upload.url
@ -318,7 +318,7 @@ class BulkImport::Vanilla < BulkImport::Base
file_path = "#{ATTACHMENTS_BASE_DIR}/#{attachment_id}"
if File.exists?(file_path)
if File.exist?(file_path)
upload = create_upload(post.user.id, file_path, File.basename(file_path))
if upload && upload.errors.empty?
upload.url
@ -348,13 +348,13 @@ class BulkImport::Vanilla < BulkImport::Base
base_guess = base_filename.dup
full_guess = File.join(path, base_guess) # often an exact match exists
return full_guess if File.exists?(full_guess)
return full_guess if File.exist?(full_guess)
# Otherwise, the file exists but with a prefix:
# The p prefix seems to be the full file, so try to find that one first.
['p', 't', 'n'].each do |prefix|
full_guess = File.join(path, "#{prefix}#{base_guess}")
return full_guess if File.exists?(full_guess)
return full_guess if File.exist?(full_guess)
end
# Didn't find it.

View file

@ -531,7 +531,7 @@ class BulkImport::VBulletin < BulkImport::Base
real_filename = db_filename
real_filename.prepend SecureRandom.hex if real_filename[0] == '.'
unless File.exists?(filename)
unless File.exist?(filename)
puts "Attachment file #{row.inspect} doesn't exist"
return nil
end
@ -601,7 +601,7 @@ class BulkImport::VBulletin < BulkImport::Base
end
def import_avatars
if AVATAR_DIR && File.exists?(AVATAR_DIR)
if AVATAR_DIR && File.exist?(AVATAR_DIR)
puts "", "importing user avatars"
RateLimiter.disable
@ -620,7 +620,7 @@ class BulkImport::VBulletin < BulkImport::Base
# raise "User not found for id #{user_id}" if user.blank?
photo_real_filename = File.join(AVATAR_DIR, item)
puts "#{photo_real_filename} not found" unless File.exists?(photo_real_filename)
puts "#{photo_real_filename} not found" unless File.exist?(photo_real_filename)
upload = create_upload(u.id, photo_real_filename, File.basename(photo_real_filename))
count += 1

View file

@ -616,7 +616,7 @@ class BulkImport::VBulletin5 < BulkImport::Base
real_filename = db_filename
real_filename.prepend SecureRandom.hex if real_filename[0] == '.'
unless File.exists?(filename)
unless File.exist?(filename)
filename = check_database_for_attachment(row) if filename.blank?
return nil if filename.nil?
end

View file

@ -367,7 +367,7 @@ class ImportScripts::AnswerHub < ImportScripts::Base
if user
filename = "avatar-#{user_id}.png"
path = File.join(AVATAR_DIR, filename)
next if !File.exists?(path)
next if !File.exist?(path)
# Scrape Avatars - Avatars are saved in the db, but it might be easier to just scrape them
if SCRAPE_AVATARS == 1
@ -403,7 +403,7 @@ class ImportScripts::AnswerHub < ImportScripts::Base
filepath = File.basename(image).split('"')[0]
filepath = File.join(ATTACHMENT_DIR, filepath)
if File.exists?(filepath)
if File.exist?(filepath)
filename = File.basename(filepath)
upload = create_upload(user_id, filepath, filename)
image_html = html_for_upload(upload, filename)
@ -421,7 +421,7 @@ class ImportScripts::AnswerHub < ImportScripts::Base
filepath = File.basename(file).split('"')[0]
filepath = File.join(ATTACHMENT_DIR, filepath)
if File.exists?(filepath)
if File.exist?(filepath)
filename = File.basename(filepath)
upload = create_upload(user_id, filepath, filename)
file_html = html_for_upload(upload, filename)

View file

@ -6,7 +6,7 @@ module ImportScripts
class GenericDatabase
def initialize(directory, batch_size:, recreate: false, numeric_keys: false)
filename = "#{directory}/index.db"
File.delete(filename) if recreate && File.exists?(filename)
File.delete(filename) if recreate && File.exist?(filename)
@db = SQLite3::Database.new(filename, results_as_hash: true)
@batch_size = batch_size

View file

@ -316,7 +316,7 @@ class ImportScripts::Bbpress < ImportScripts::Base
attachments.each do |a|
print_status(count += 1, total_attachments, get_start_time("attachments_from_postmeta"))
path = File.join(BB_PRESS_ATTACHMENTS_DIR, a["meta_value"])
if File.exists?(path)
if File.exist?(path)
if post = Post.find_by(id: post_id_from_imported_post_id(a["post_id"]))
filename = File.basename(a["meta_value"])
upload = create_upload(post.user.id, path, filename)

View file

@ -927,7 +927,7 @@ class ImportScripts::DiscuzX < ImportScripts::Base
end
filename = File.join(DISCUZX_BASE_DIR, ATTACHMENT_DIR, row['attachment_path'])
unless File.exists?(filename)
unless File.exist?(filename)
puts "Attachment file doesn't exist: #{filename}"
return nil
end

View file

@ -428,7 +428,7 @@ class ImportScripts::Drupal < ImportScripts::Base
real_filename = CGI.unescapeHTML(uri)
file = File.join(ATTACHMENT_DIR, real_filename)
unless File.exists?(file)
unless File.exist?(file)
puts "Attachment file #{attachment['filename']} doesn't exist"
tmpfile = "attachments_failed.txt"

View file

@ -23,7 +23,7 @@ class ImportScripts::DrupalJson < ImportScripts::Base
def load_json(arg)
filename = File.join(JSON_FILES_DIR, arg)
raise RuntimeError.new("File #{filename} not found!") if !File.exists?(filename)
raise RuntimeError.new("File #{filename} not found!") if !File.exist?(filename)
JSON.parse(File.read(filename)).reverse
end

View file

@ -178,7 +178,7 @@ class ImportScripts::FMGP < ImportScripts::Base
end
def load_fmgp_json(filename)
raise RuntimeError.new("File #{filename} not found") if !File.exists?(filename)
raise RuntimeError.new("File #{filename} not found") if !File.exist?(filename)
JSON.parse(File.read(filename))
end

View file

@ -69,7 +69,7 @@ class ImportScripts::GetSatisfaction < ImportScripts::Base
def csv_filename(table_name, use_fixed: true)
if use_fixed
filename = File.join(@path, "#{table_name}_fixed.csv")
return filename if File.exists?(filename)
return filename if File.exist?(filename)
end
File.join(@path, "#{table_name}.csv")

View file

@ -223,7 +223,7 @@ def crawl
start_time = Time.now
status_filename = File.join(@path, "status.yml")
if File.exists?(status_filename)
if File.exist?(status_filename)
yaml = YAML.load_file(status_filename)
@finished = yaml[:finished]
@scraped_topic_urls = yaml[:urls]

View file

@ -195,7 +195,7 @@ class ImportScripts::HigherLogic < ImportScripts::Base
original_filename = "#{a['VersionName']}.#{a['FileExtension']}"
path = File.join(ATTACHMENT_DIR, original_filename)
if File.exists?(path)
if File.exist?(path)
if post = Post.find(post_id_from_imported_post_id(a['MessageKey']))
filename = File.basename(original_filename)
upload = create_upload(post.user.id, path, filename)

View file

@ -213,7 +213,7 @@ EOM
post_create_action: proc do |newuser|
if user['avatar_url'] && user['avatar_url'].length > 0
photo_path = AVATARS_DIR + user['avatar_url']
if File.exists?(photo_path)
if File.exist?(photo_path)
begin
upload = create_upload(newuser.id, photo_path, File.basename(photo_path))
if upload && upload.persisted?

View file

@ -87,7 +87,7 @@ class ImportScripts::IPBoard3 < ImportScripts::Base
new_user.update(suspended_at: DateTime.now, suspended_till: 100.years.from_now)
elsif u["pp_main_photo"].present?
path = File.join(UPLOADS_DIR, u["pp_main_photo"])
if File.exists?(path)
if File.exist?(path)
begin
upload = create_upload(new_user.id, path, File.basename(path))
if upload.persisted?
@ -390,7 +390,7 @@ class ImportScripts::IPBoard3 < ImportScripts::Base
markdown.gsub!(/\[attachment=(\d+):.+\]/) do
if a = mysql_query("SELECT attach_file, attach_location FROM attachments WHERE attach_id = #{$1}").first
path = File.join(UPLOADS_DIR, a["attach_location"])
if File.exists?(path)
if File.exist?(path)
begin
upload = create_upload(user_id, path, a["attach_file"])
return html_for_upload(upload, a["attach_file"]) if upload.persisted?

View file

@ -842,7 +842,7 @@ SQL
filename = attachment_id.to_s.rjust(4, "0")
filename = File.join(ATTACHMENT_DIR, "000#{filename[0]}/#{filename}.dat")
unless File.exists?(filename)
unless File.exist?(filename)
puts "Attachment file doesn't exist: #{filename}"
return nil
end
@ -953,9 +953,9 @@ SQL
# check to see if we have it
if File.exist?(png)
image = png
elsif File.exists?(jpg)
elsif File.exist?(jpg)
image = jpg
elsif File.exists?(gif)
elsif File.exist?(gif)
image = gif
end

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
if ARGV.length != 1 || !File.exists?(ARGV[0])
if ARGV.length != 1 || !File.exist?(ARGV[0])
STDERR.puts '', 'Usage of mbox importer:', 'bundle exec ruby mbox.rb <path/to/settings.yml>'
STDERR.puts '', "Use the settings file from #{File.expand_path('mbox/settings.yml', File.dirname(__FILE__))} as an example."
exit 1

View file

@ -261,7 +261,7 @@ FROM #{TABLE_PREFIX}discuss_users
end
filename = File.join(ATTACHMENT_DIR, row['user_id'].to_s.split('').join('/'), "#{row['file_id']}.attach")
unless File.exists?(filename)
unless File.exist?(filename)
puts "Attachment file doesn't exist: #{filename}"
return
end

View file

@ -55,7 +55,7 @@ class ImportScripts::Ning < ImportScripts::Base
def load_ning_json(arg)
filename = File.join(JSON_FILES_DIR, arg)
raise RuntimeError.new("File #{filename} not found!") if !File.exists?(filename)
raise RuntimeError.new("File #{filename} not found!") if !File.exist?(filename)
JSON.parse(repair_json(File.read(filename))).reverse
end
@ -112,7 +112,7 @@ class ImportScripts::Ning < ImportScripts::Base
if u["profilePhoto"] && newuser.user_avatar.try(:custom_upload_id).nil?
photo_path = file_full_path(u["profilePhoto"])
if File.exists?(photo_path)
if File.exist?(photo_path)
begin
upload = create_upload(newuser.id, photo_path, File.basename(photo_path))
if upload.persisted?
@ -315,7 +315,7 @@ class ImportScripts::Ning < ImportScripts::Base
ning_filename = matches[1]
filename = File.join(JSON_FILES_DIR, ning_filename.split("?").first)
if !File.exists?(filename)
if !File.exist?(filename)
puts "Attachment file doesn't exist: #{filename}"
next s
end
@ -339,7 +339,7 @@ class ImportScripts::Ning < ImportScripts::Base
file_names.each do |f|
filename = File.join(JSON_FILES_DIR, f.split("?").first)
if !File.exists?(filename)
if !File.exist?(filename)
puts "Attachment file doesn't exist: #{filename}"
next
end
@ -363,7 +363,7 @@ class ImportScripts::Ning < ImportScripts::Base
# filename = File.join(JSON_FILES_DIR, file_name)
filename = file_full_path(file_name)
if File.exists?(filename)
if File.exist?(filename)
upload = create_upload(@system_user.id, filename, File.basename(filename))
if upload.nil? || !upload.valid?

View file

@ -216,7 +216,7 @@ class ImportScripts::NodeBB < ImportScripts::Base
filepath = File.join(ATTACHMENT_DIR, picture)
filename = File.basename(picture)
unless File.exists?(filepath)
unless File.exist?(filepath)
puts "Avatar file doesn't exist: #{filepath}"
return nil
end
@ -276,7 +276,7 @@ class ImportScripts::NodeBB < ImportScripts::Base
filepath = File.join(ATTACHMENT_DIR, picture)
filename = File.basename(picture)
unless File.exists?(filepath)
unless File.exist?(filepath)
puts "Background file doesn't exist: #{filepath}"
return nil
end
@ -481,7 +481,7 @@ class ImportScripts::NodeBB < ImportScripts::Base
# if file exists
# upload attachment and return html for it
if File.exists?(filepath)
if File.exist?(filepath)
filename = File.basename(filepath)
upload = create_upload(post.user_id, filepath, filename)

View file

@ -3,7 +3,7 @@
# Importer for phpBB 3.0 and 3.1
# Documentation: https://meta.discourse.org/t/importing-from-phpbb3/30810
if ARGV.length != 1 || !File.exists?(ARGV[0])
if ARGV.length != 1 || !File.exist?(ARGV[0])
STDERR.puts '', 'Usage of phpBB3 importer:', 'bundle exec ruby phpbb3.rb <path/to/settings.yml>'
STDERR.puts '', "Use the settings file from #{File.expand_path('phpbb3/settings.yml', File.dirname(__FILE__))} as an example."
STDERR.puts '', 'Still having problems? Take a look at https://meta.discourse.org/t/importing-from-phpbb3/30810'

View file

@ -411,7 +411,7 @@ class ImportScripts::Smf1 < ImportScripts::Base
next unless post = PostCustomField.joins(:post).find_by(name: "import_id", value: u["id_msg"].to_s)&.post
path = File.join(UPLOADS_DIR, "#{u["id_attach"]}_#{u["file_hash"]}")
next unless File.exists?(path) && File.size(path) > 0
next unless File.exist?(path) && File.size(path) > 0
if upload = create_upload(post.user_id, path, u["filename"])
html = html_for_upload(upload, u["filename"])

View file

@ -303,7 +303,7 @@ class ImportScripts::Smf2 < ImportScripts::Base
[ filename, "#{attachment_id}_#{file_hash}", legacy_name ]
.map { |name| File.join(options.smfroot, 'attachments', name) }
.detect { |file| File.exists?(file) }
.detect { |file| File.exist?(file) }
end
def decode_entities(*args)

View file

@ -156,7 +156,7 @@ class ImportScripts::VanillaSQL < ImportScripts::Base
end
def import_avatars
if ATTACHMENTS_BASE_DIR && File.exists?(ATTACHMENTS_BASE_DIR)
if ATTACHMENTS_BASE_DIR && File.exist?(ATTACHMENTS_BASE_DIR)
puts "", "importing user avatars"
User.find_each do |u|
@ -183,7 +183,7 @@ class ImportScripts::VanillaSQL < ImportScripts::Base
next
end
if !File.exists?(photo_path)
if !File.exist?(photo_path)
puts "Path to avatar file not found! Skipping. #{photo_path}"
next
end
@ -214,13 +214,13 @@ class ImportScripts::VanillaSQL < ImportScripts::Base
base_guess = base_filename.dup
full_guess = File.join(path, base_guess) # often an exact match exists
return full_guess if File.exists?(full_guess)
return full_guess if File.exist?(full_guess)
# Otherwise, the file exists but with a prefix:
# The p prefix seems to be the full file, so try to find that one first.
['p', 't', 'n'].each do |prefix|
full_guess = File.join(path, "#{prefix}#{base_guess}")
return full_guess if File.exists?(full_guess)
return full_guess if File.exist?(full_guess)
end
# Didn't find it.
@ -554,7 +554,7 @@ class ImportScripts::VanillaSQL < ImportScripts::Base
end
def import_attachments
if ATTACHMENTS_BASE_DIR && File.exists?(ATTACHMENTS_BASE_DIR)
if ATTACHMENTS_BASE_DIR && File.exist?(ATTACHMENTS_BASE_DIR)
puts "", "importing attachments"
start = Time.now
@ -586,7 +586,7 @@ class ImportScripts::VanillaSQL < ImportScripts::Base
path.gsub!("s3://uploads/", "")
file_path = "#{ATTACHMENTS_BASE_DIR}/#{path}"
if File.exists?(file_path)
if File.exist?(file_path)
upload = create_upload(post.user.id, file_path, File.basename(file_path))
if upload && upload.errors.empty?
# upload.url
@ -607,7 +607,7 @@ class ImportScripts::VanillaSQL < ImportScripts::Base
file_path = "#{ATTACHMENTS_BASE_DIR}/#{attachment_id}"
if File.exists?(file_path)
if File.exist?(file_path)
upload = create_upload(post.user.id, file_path, File.basename(file_path))
if upload && upload.errors.empty?
upload.url

View file

@ -426,7 +426,7 @@ EOM
real_filename = row['filename']
real_filename.prepend SecureRandom.hex if real_filename[0] == '.'
unless File.exists?(filename)
unless File.exist?(filename)
if row['dbsize'].to_i == 0
puts "Attachment file #{row['filedataid']} doesn't exist"
return nil

View file

@ -138,7 +138,7 @@ class ImportScripts::VBulletin < ImportScripts::Base
upload = UploadCreator.new(file, picture["filename"]).create_for(imported_user.id)
else
filename = File.join(AVATAR_DIR, picture['filename'])
unless File.exists?(filename)
unless File.exist?(filename)
puts "Avatar file doesn't exist: #{filename}"
return nil
end
@ -366,7 +366,7 @@ class ImportScripts::VBulletin < ImportScripts::Base
real_filename = upload['filename']
real_filename.prepend SecureRandom.hex if real_filename[0] == '.'
unless File.exists?(filename)
unless File.exist?(filename)
# attachments can be on filesystem or in database
# try to retrieve from database if the file did not exist on filesystem
if upload['dbsize'].to_i == 0