Please find the code which I ran from the IRB terminal:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\rakshiar>irb
irb(main):001:0> src = 'E:\WIPData\Ruby\Scripts\TaxDocumentDownload'
=> "E:\\WIPData\\Ruby\\Scripts\\TaxDocumentDownload"
irb(main):002:0> dest = 'E:\WIPData\Ruby\Scripts'
=> "E:\\WIPData\\Ruby\\Scripts"
irb(main):003:0> dest<<'H00371101'
=> "E:\\WIPData\\Ruby\\ScriptsH00371101"
irb(main):004:0>
Why here such \\ is coming? How to fix that?
When i am running the same part from the script getting the below warnings:
CODE
src = 'E:\WIPData\Ruby\Scripts\TaxDocumentDownload'
dest = 'E:\WIPData\Ruby\Scripts'
dest<<'H00371101'
FileUtils.copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)
Warning:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\rakshiar>cd..
C:\Documents and Settings>cd..
C:\>e:
E:\>cd E:\WIPData\Ruby\Scripts
E:\WIPData\Ruby\Scripts>downloadv1.rb
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:93: warning: already initialized constant
OPT_TABLE
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1268: warning: already initialized consta
nt S_IF_DOOR
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1496: warning: already initialized consta
nt DIRECTORY_TERM
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1500: warning: already initialized consta
nt SYSCASE
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1619: warning: already initialized consta
nt LOW_METHODS
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1625: warning: already initialized consta
nt METHODS
Could you please say why such warnings are coming?
When try the below from the IRB again different output:
C:\Documents and Settings\rakshiar>irb
irb(main):001:0> src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload"
=> "E:WIPDataRubyScriptsTaxDocumentDownload"
irb(main):002:0> est = "E:\WIPData\Ruby\Scripts"
=> "E:WIPDataRubyScripts"
irb(main):003:0> est<<"H00371101"
=> "E:WIPDataRubyScriptsH00371101"
irb(main):004:0> est<<"H00371101"
EDIT:
ERROR
E:\WIPData\Ruby\Scripts>downloadv1.rb
E:/WIPData/Ruby/Scripts/downloadv1.rb:87: syntax error, unexpected tCONSTANT, ex
pecting $end
dest<<"H00371101"
^
From the script code part:
src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload"
dest = "E:\WIPData\Ruby\Scripts\"
dest<<"H00371101"
FileUtils.copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)
I want that src and dest directory as the real directory path. How to get that?
Thanks.
Ruby has, broadly speaking, two types of strings. In a double-quoted string, backslashes "escape" characters - a backslash followed by another letter produces a special character. For example, "\n" gives you a newline. Inside single-quoted strings, the backslash doesn't escape characters - '\n' is just a backslash followed by the letter n. (Actually this isn't 100% true, the exception is '\'' which is a single quote - otherwise there would be no way to embed a single quote in a single-quoted string.
That's why your single-quoted src = 'E:\WIPData\Ruby\Scripts\TaxDocumentDownload' will work, and double-quoted src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload" will not.
The double-backslashes are printed there because irb uses inspect on the resulting output, which returns the string in double-quoted form (with special characters escaped):
'"Hello," said Andy'.inspect # => "\"Hello,\" said Andy"
They're not really in the string, as you can see with puts:
puts '"Hello," said Andy' # => "Hello," said Andy
The error you have is because of using a double-quoted string, the backslashes are treated as escape characters, so your string is unterminated:
src = "E:\WIPData\Ruby\Scripts\"
dest<<"H00371101"
is parsed the same as
src = 'E:WIPDataRubyScripts"dest<<'H00371101
which is a syntax error.
You should go and read about the difference between single- and double-quoted strings. Here's one resource.
A quick google suggests that you might be doing require 'FileUtils' not require 'fileutils'? This post said that the same warnings disappeared once they changed to the latter. It's because Windows' file system is not case-sensitive - to Ruby, FileUtils.rb and fileutils.rb are two different files, but to Windows, they're the same.
The FileUtils warning it is because you have to change your required gem, like this:
require 'FileUtils' WRONG
require 'fileutils' OKAY
This will solve your warnings :)
Related
I am building an open source project using Ruby for testing HTTP services: https://github.com/Comcast/http-blackbox-test-tool
I want to be able to reference environment variables in my test-plan.yaml file. I could use ERB, however I don't want to support embedding any random Ruby code and ERB syntax is odd for non-rubyists, I just want to access environment variables using the commonly used Unix style ${ENV_VAR} syntax.
e.g.
order-lunch-app-health:
request:
url: ${ORDER_APP_URL}
headers:
content-type: 'application/text'
method: get
expectedResponse:
statusCode: 200
maxRetryCount: 5
All examples I have found for Ruby use ERB. Does anyone have a suggestion on the best way to deal with this? I an open to using another tool to preprocess the YAML and then send that to the Ruby application.
I believe something like this should work under most circumstances:
require 'yaml'
def load_yaml(file)
content = File.read file
content.gsub! /\${([^}]+)}/ do
ENV[$1]
end
YAML.load content
end
p load_yaml 'sample.yml'
As opposed to my original answer, this is both simpler and handles undefined ENV variables well.
Try with this YAML:
# sample.yml
path: ${PATH}
home: ${HOME}
error: ${NO_SUCH_VAR}
Original Answer (left here for reference)
There are several ways to do it. If you want to allow your users to use the ${VAR} syntax, then perhaps one way would be to first convert these variables to Ruby string substitution format %{VAR} and then evaluate all environment variables together.
Here is a rough proof of concept:
require 'yaml'
# Transform environments to a hash of { symbol: value }
env_hash = ENV.to_h.transform_keys(&:to_sym)
# Load the file and convert ${ANYTHING} to %{ANYTHING}
content = File.read 'sample.yml'
content.gsub! /\${([^}]+)}/, "%{\\1}"
# Use Ruby string substitution to replace %{VARS}
content %= env_hash
# Done
yaml = YAML.load content
p yaml
Use it with this sample.yml for instance:
# sample.yml
path: ${PATH}
home: ${HOME}
There are many ways this can be improved upon of course.
Preprocessing is easy, and I recommend you use a YAML loaderd/dumper
based solution, as the replacement might require quotes around the
replacement scalar. (E.g. you substitute the string true, if that
were not quoted, the resulting YAML would be read as a boolean).
Assuming your "source" is in input.yaml and your env. variable
ORDER_APP_URL set to https://some.site/and/url. And the following
script in expand.py:
import sys
import os
from pathlib import Path
import ruamel.yaml
def substenv(d, env):
if isinstance(d, dict):
for k, v in d.items():
if isinstance(v, str) and '${' in v:
d[k] = v.replace('${', '{').format(**env)
else:
substenv(v, env)
elif isinstance(d, list):
for idx, item in enumerate(d):
if isinstance(v, str) and '${' in v:
d[idx] = item.replace('${', '{').format(**env)
else:
substenv(item, env)
yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
data = yaml.load(Path(sys.argv[1]))
substenv(data, os.environ)
yaml.dump(data, Path(sys.argv[2]))
You can then do:
python expand.py input.yaml output.yaml
which writes output.yaml:
order-lunch-app-health:
request:
url: https://some.site/and/url
headers:
content-type: 'application/text'
method: get
expectedResponse:
statusCode: 200
maxRetryCount: 5
Please note that the spurious quotes around 'application/text' are preserved, as would be any comments
in the original file.
Quotes around the substituted URL are not necessary, but the would have been added if they were.
The substenv routine recursively traverses the loaded data, and substitutes even if the substitution is in mid-scalar, and if there are more than substitution in one scalar. You can "tighten" the test:
if isinstance(v, str) and '${' in v:
if that would match too many strings loaded from YAML.
I have dozens of devices I need to login to using an API script. One set of devices has a password ending in $. I've tried a bunch of things but I can't seem to escape that $ char. Here is the error I'm seeing.
critical/config: Error: Validation failed for object 'gelt-uk4-gp!HTTP/80: Status Check ' of type 'Service'; Attribute 'vars' -> 'gspass': Closing $ not found in macro format string 'n0t-real#$'.
Location: in /etc/icinga2/zones.d/global-templates/global-services.conf: 55:5-55:31
/etc/icinga2/zones.d/global-templates/global-services.conf(53): if ( host.vars.company == "gelt-emea" ) {
/etc/icinga2/zones.d/global-templates/global-services.conf(54): vars.gsuser = "admin"
/etc/icinga2/zones.d/global-templates/global-services.conf(55): vars.gspass = "n0t-real#$"
^^^^^^^^^^^^^^^^^^^^^^^^^^^
You add an extra $ right beside the literal dollar sign.
So if the password is word54s$ you type:
vars.geltpass = "word54s$$"
I am trying to read the contents of the file from a local disk as follows :
content = File.read("C:\abc.rb","r")
when I execute the rb file I get an exception as Error: No such file or directory .What am I missing in this?
In a double quoted string, "\a" is a non-printable bel character. Similar to how "\n" is a newline. (I think these originate from C)
You don't have a file with name "C:<BEL>bc.rb" which is why you get the error.
To fix, use single quotes, where these interpolations don't happen:
content = File.read('C:\abc.rb')
content = File.read("C:\/abc.rb","r")
First of all:
Try using:
Dir.glob(".")
To see what's in the directory (and therefore what directory it's looking at).
open("C:/abc.rb", "rb") { |io| a = a + io.read }
EDIT: Unless you're concatenating files together, you could write it as:
data = File.open("C:/abc.rb", "rb") { |io| io.read }
I have a .csv file, which contains the following data:
"Ա","Բ"
1,10
2,20
I cannot read it into R so that the column names are displayed like they are in the file.
d <- read.csv("./Data/1.csv", fileEncoding="UTF-8")
head(d)
Produces the following:
> d <- read.csv("./Data/1.csv", fileEncoding="UTF-8")
Warning messages:
1: In read.table(file = file, header = header, sep = sep, quote = quote, :
invalid input found on input connection './Data/1.csv'
2: In read.table(file = file, header = header, sep = sep, quote = quote, :
incomplete final line found by readTableHeader on './Data/1.csv'
> head(d)
[1] X.
<0 rows> (or 0-length row.names)
Meanwhile, doing the same without specifying the fileEncoding produces this:
> d <- read.csv("./Data/1.csv")
> head(d)
Ô. Ô²
1 1 10
2 2 20
When I run the "file" utility to find out the encoding of the file, it says it is UTF-8:
Data\1.csv: UTF-8 Unicode text, with CRLF line terminators
I am using RStudio, Windows 7, R version 2.15.2, 32-bit.
Thanks in advance.
I wrote a longer answer on the same issue here: R on Windows: character encoding hell .
Quick answer, using the parameter encoding instead of fileEncoding should fix your first issue. You will not be able to read it possibly in either console or table view in RStudio, but you will be able to use it in formulaes.
d <- read.csv("./Data/1.csv", encoding="UTF-8")
head(d)
Having saved your table into a UTF-8 file:
> test2 <- read.csv("test2.csv", header = FALSE, sep = ",", quote = "\"", dec = ".", fill = TRUE, comment.char = "", encoding = "UTF-8")
Warning message:
In read.table(file = file, header = header, sep = sep, quote = quote, :
incomplete final line found by readTableHeader on 'test2.csv'
This gives you how it looks like in the console and RStudio view
> test2
V1 V2
1 <U+0531> <U+0532>
2 1 10
3 2 20
However importantly you are able to manipulate this within R. Thus in my case it is possible to see that the script window input Ա has UTF-8 encoding, and a grep correctly finds this encoding in your table.
> Encoding("Ա")
[1] "UTF-8"
> grep("Ա", as.character(test2[1,1]))
[1] 1
You may need to find suitable encoding variants that work on your settings, or possibly change them. Unfortunately I am not sure where it is done.
You might not be able to make it pretty in all stages, but it is definitely possible to get it to work also in Windows 7 environment.
I tried two ways to replicate your problem.
I copied the characters above into RStudio, saved it to a csv with this code:
write.csv(c("Ա","Բ",
1,10,
2,20), "test.csv")
df <- read.csv("test.csv")
This worked fine.
Then I thought, well maybe R is cheating when I save it to CSV with R? So I just pasted the characters to a text file and save it as a CSV. This approach doesn't have problems either.
Here's my session info:
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C LC_TIME=en_CA.UTF-8
[4] LC_COLLATE=en_CA.UTF-8 LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_CA.UTF-8
[7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats4 grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] party_1.0-9 modeltools_0.2-21 strucchange_1.4-7 sandwich_2.2-10 zoo_1.7-10
[6] GGally_0.4.4 reshape_0.8.4 plyr_1.8 ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] coin_1.0-23 colorspace_1.2-2 dichromat_2.0-0 digest_0.6.3
[5] gtable_0.1.2 labeling_0.2 lattice_0.20-23 MASS_7.3-29
[9] munsell_0.4.2 mvtnorm_0.9-9995 proto_0.3-10 RColorBrewer_1.0-5
[13] reshape2_1.2.2 scales_0.2.3 splines_3.0.1 stringr_0.6.2
I had the same problem and found out that the file was corrupted.
I opened the file with OpenOffice and saved it back using "UTF8" character set (you need to click the edit filter settings box) and then imported it with the read.csv()(no encoding or filencoding option) and it worked fine.
I am using a custom Ruby function in Puppet to read a string of text from a file. I am than comparing whatever version is read against a list of known versions to determine which config file I should use for that particular server. The problem is that when I compare the read version to my list of known versions, none of them match.
I printed out the variable to the screen, and it looked fine. I then added a '-' to the beginning and the end and this time, the following was printed
-2.2#012-
Does anyone know what this is and how it could be removed?
Here is my process.
A script that handles the installation of an app
sudo echo "2.2" > /opt/version
My ruby function
if FileTest.exists?("/opt/version")
Facter.add("app_version") do
setcode do
version = File.open('/opt/version', &:readline)
version
end
end
end
My puppet manifest
if versioncmp( $app_version, '2.2') == 0 {
notice("===> Installing 2.2 Configs")
} elsif versioncmp ($app_version, '2.3') == 0 {
notice("===> Installing 2.3 Configs")
} else {
notice("===> No version match. Continuing on.")
}
}
File.readline includes the line termination (in your case, "\n"). chomp will get rid of the line termination:
version = File.open('/opt/version', &:readline).chomp
When debugging and you want to see what's really in a variable, use p instead of puts. p will escape unprintable characters so you can see them:
puts "2.2\n" # => 2.2
#
p "2.2\n" # => "2.2\n"