unable to append string in the directory path - ruby

Objective : Rename the folder in directory with today's
date
I am using ruby file where I am using linux command to modify directory(ruby version is 2.2) and my code looks like this.
require 'date'
class BSDK
TDATE = Date.today.to_s
DEFAULT_PATH = "/home/cyborg/bsdk/"
VERSION = "bsdk-tk-4.2.71"
def bsdk_processing
bsdk_version = "#{DEFAULT_PATH}#{VERSION}"
bsdk_latest = "#{bsdk_version}(#{TDATE})"
system "mv #{bsdk_version} #{bsdk_latest}"
end
end
bsdk = BSDK.new
bsdk.bsdk_processing
Error:
mv: missing destination file operand after '/home/cyborg/bsdk/bsdk-tk-4.2.71'
When I tried printing bsdk_latest, it is giving me as
/home/cyborg/bsdk/bsdk-tk-4.2.71
2019-08-22
and not as /home/cyborg/bsdk/bsdk-tk-4.2.71(2019-08-22)
Note: we have directory named bsdk-tk-4.2.71 in the path /home/cyborg/bsdk/

The issue got resolved as pointed by #user1934428 , there was new line embedded in the VERSION
require 'date'
require 'fileutils'
class BSDK
TDATE = Date.today.to_s
DEFAULT_PATH = "/home/cyborg/bsdk/"
VERSION = "bsdk-tk-4.2.71"
VERSION.strip!
def bsdk_processing
bsdk_version = "#{DEFAULT_PATH}#{VERSION}"
bsdk_latest = "#{bsdk_version}""(#{TDATE})"
Fileutils.mv("#{bsdk_version}", "#{bsdk_latest}")
end
end
bsdk = BSDK.new
bsdk.bsdk_processing

Related

How do I run a Ruby script in the Terminal?

I'm attempting to run a Ruby script (linked below) that was shared by DHH to convert a number of .dcp Leica Q camera profiles to Leica M10 camera profiles.
I'm just not sure how to run it. I understand it needs to be run in Terminal but that's about it.
I have all Leica Q camera profiles in a single folder on the desktop... Now what?
I've downloaded the DCP tool that's mentioned in the comments of the script.
Here's a link to the GitHub repo: https://gist.github.com/dhh/d3c8cf9309b662047257b7e583c3f595#file-dcp-converter-rb-L8
I know this might be pretty basic but any help would be greatly appreciated!
Here's the actual script:
#!/usr/bin/env ruby
# Requires that you have ./bin/dcpTool from https://sourceforge.net/projects/dcptool/
require 'rubygems'
require 'bundler/setup'
require 'nokogiri'
input_camera_model = ARGV[0] || "LEICA Q (Typ 116)"
output_camera_model = ARGV[1] || "LEICA M10"
input_dir = ARGV[2] || "./input"
output_dir = ARGV[3] || "./output"
def convert_profile_name(profile_name, input_camera_model, output_camera_model)
File.basename(profile_name.gsub(/#{input_camera_model.gsub(/\(/, "\\(").gsub(/\)/, "\\)")}/, output_camera_model), ".dcp")
end
def replace_camera_model(xml_profile_filename, output_camera_model)
profile_doc = Nokogiri::XML(File.read(xml_profile_filename))
profile_doc.xpath('//UniqueCameraModelRestriction').first.content = output_camera_model
File.open(xml_profile_filename, "w+") { |file| file.write(profile_doc.to_xml) }
end
Dir.entries(input_dir).reject { |file| file =~ /^(\.|\.\.)$/ }.each do |existing_profile|
converted_profile = convert_profile_name(existing_profile, input_camera_model, output_camera_model)
existing_dcp_filename = File.join(input_dir, existing_profile)
xml_filename = "#{File.join(output_dir, converted_profile)}.xml"
decompile_command = "./bin/dcpTool -d '#{existing_dcp_filename}' '#{xml_filename}'"
puts "Decompiling #{existing_dcp_filename} into XML"
`#{decompile_command}`
puts "Replacing camera model: #{input_camera_model} -> #{output_camera_model}"
replace_camera_model(xml_filename, output_camera_model)
converted_dcp_filename = "#{File.join(output_dir, converted_profile)}.dcp"
recompile_command = "./bin/dcpTool -c '#{xml_filename}' '#{converted_dcp_filename}'"
puts "Recompiling XML into #{converted_dcp_filename}"
`#{recompile_command}`
File.delete(xml_filename)
puts
end```
Easiest way:
Create an "input" and an "output" directory in the same location as this script.
Place all of your files in "input"
In the terminal navigate to this location
type ruby dcp-converter.rb.
Note: You may have to run gem install bundler nokogiri first.
If you have a different model than the one shown you may have to pass additional arguments e.g. ruby dcp-converter.rb "LEICA Q (Typ 202)"
The argument order would be ruby dcp-converter.rb [input_model] [output_model] [input_directory] [output_directory]
The defaults are
[input_model] = "LEICA Q (Typ 116)"
[output_model]="LEICA M10"
[input_directory]="./input"
[output_directory]="./output"

On windows download file at custom path using Selenium WebDriver is not working using ruby

I am using following code to download CSV file and its working properly on MAC but on Windows its not working.
On window it download file but it save it in download folder i.e. default path
def self.launch(browser=:firefox, profile=nil)
profile = Selenium::WebDriver::Firefox::Profile.new
path = File.join(File.join(Dir.pwd), 'csv_files')
FileUtils.rm_rf(path) if Dir.exists? path
Dir.mkdir(path)
profile['browser.download.dir'] = path
profile['browser.download.folderList'] = 2
profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/csv'
profile['pdfjs.disabled'] = true
$watir_browser = UITest.new_browser_session browser, profile
$driver = $watir_browser.wd
return $watir_browser
end
Please suggest me if any changes are required.
Problem
When you do the line:
path = File.join(File.join(Dir.pwd), 'csv_files')
The path will be:
'some/path/csv_files'
In Windows, the "/" needs to be "\":
'some\path\csv_files'
Solution
What you can do is replace the slashes when the platform is Windows:
path = File.join(File.join(Dir.pwd), 'csv_files')
path.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
Please try this else let me know.
relative_path = File.expand_path File.dirname(__FILE__)
OR
require 'open-uri'
require 'selenium-webdriver'
path = File.join(File.join(Dir.pwd), 'csv_files')
Dir.mkdir(path) unless File.exists?(path)
modified_path = path.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
File.delete(File.join(modified_path, 'test.csv')) if File.exist?(File.join(modified_path, 'test.csv'))
open(File.join(modified_path, 'test.csv'), 'wb') do |file|
file << open('http://example.com').read
end

Accessing text file in ruby gem

I am using ruby version 2.0.0 , I made some custom logo in text file named logo.txt like this:
_____
| |
|_____|
|
|
|
Now i made a gem with name of "custom" and placed this file under lib/logo.txt . Now i wants to print this file in my script under ruby gem so i wrote in this way.
file = File.open("lib/logo.txt")
contents = file.read
puts "#{contents}"
But above code produce errors, like:
/usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/gems/2.0.0/gems/custom-0.0.1/lib/custom/custom.rb:1551:in `initialize': No such file or directory - lib/logo.txt (Errno::ENOENT)
I include this logo.txt file in gemspec as per below:
Gem::Specification.new do |s|
s.name = "custom"
s.version = VERSION
s.author = "Custom Wear"
s.email = "custom#custom.com"
s.homepage = "http://custom.com"
s.summary = "custom wera"
s.description = File.read(File.join(File.dirname(__FILE__), 'README'))
s.license = 'ALL RIGHTS RESERVED'
s.files = [""lib/custom.rb", "lib/custom/custom.rb", "lib/custom /version.rb","lib/logo.txt"]
s.test_files = Dir["spec/**/*"]
s.executables = [ 'custom' ]
s.require_paths << 'lib/'
The file is opened relative to the current working directory, unless you specify the full path.
In order to avoid hard-coding full paths, you can get the full path of the current file from Ruby using __FILE__. In fact you can see in the custom.gemspec file something very similar going on:
File.join( File.dirname(__FILE__), 'README')
I think you can get to your logo file like this:
logo_path = File.join( File.dirname(__FILE__), '../logo.txt' )
file = File.open( logo_path )
In Ruby 2.0, you also have __dir__ (which can replace File.dirname(__FILE__)) but that would not be compatible with Ruby 1.9. Generally you are safer using backward-compatible syntax in gems in case you are not sure what someone has when they run your library.

file extension dependend actions

I want to check if a directory has a ".ogg" or ".m4a" file. In every case the dir is empty before starting a download session. So it just can have one "ogg" or one "m4a" file.
I tried out this code to fetch the filename:
def self.get_filename
if File.exists?('*.ogg')
file = Dir.glob('*.ogg')
#testfile = file[0]
#filename = File.basename(#testfile,File.extname(#testfile))
end
if File.exists?('*.m4a')
file = Dir.glob('*.m4a')
#testfile = file[0]
#filename = File.basename(#testfile,File.extname(#testfile))
end
end
Sadly the filename is actual empty. Maybe anyone knows why?
I think that you need Dir.glob instead.
Dir.glob('/path/to/dir/*.ogg') do |ogg_file|
#testfile = ogg_file
#filename = File.basename(#testfile,File.extname(#testfile))
end
File#exists? does not support regular expressions.
You can do this instead:
if Dir["*.rb"].any?
#....

Ruby / Watir-Webdriver - Cannot access the file after downloading it

I am working on a tests scenario that downloads a file from a website and adds it to folder.
For the download part, I am using the code described on the browser-downloads page within the Watir documentation.
The main problem was encountered in my tests when I am waiting for the file to be downloaded:
def verify_csv_file_exists
path = Dir.getwd + "/downloads/"
until File.exist?("#{path}*.csv") == true
sleep 1
end
end
When running the tests, the procedure above never stops, because it cannot see the file in the directory, although the file is downloaded.
Does anyone know a way how I can handle this situation?
Thank you.
You simply check the directory contents before you download the file, then wait until there's a new file added to the directory (by comparing the current content with the previous content). This is how you get the new file name:
This should do the job:
require 'watir-webdriver'
file_name = nil
download_directory = "#{Dir.pwd}/downloads"
download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
downloads_before = Dir.entries download_directory
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 # custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"
b = Watir::Browser.new :firefox, :profile => profile
b.goto 'https://dl.dropbox.com/u/18859962/hello.csv'
30.times do
difference = Dir.entries(download_directory) - downloads_before
if difference.size == 1
file_name = difference.first
break
end
sleep 1
end
raise "Could not locate a new file in the directory '#{download_directory}' within 30 seconds" if not file_name
puts file_name
You can't use "glob" with File.exists? like File.exists?("*.csv"). It checks whether the file named *.csv exists, not any file with name ends with .csv. You should use exact file name to check if a file exists.
Try it like this instead:
Dir.glob('downloads/*.csv').any?
Also how is sleeping for 1 second supposed to change anything? Is this a multithreaded app?

Resources