How check is my file create/modyfy after other file? - ruby

I need a function similar 'make' program.
If my file not exist or if file need update (modyfy time is before my other file) tell me true.
I have one file dependencies to other file. How update it only if neccesary.

You might want to use FileUtils.uptodate?.
uptodate?(new, old_list)`
Returns true if new is newer than all old_list. Non-existent files are older than any file.
In your example you can use it like this:
unless FileUtils.uptodate?('file_a', ['file_b'])
# file_a needs to be updated
end

require 'time_difference'
TimeDifference.between(File.ctime("FilenameA"), File.ctime("FilenameB.txt")).humanize
Using gem 'time_difference'
trouble with other lang than english (withouth .humanize You get number)

Related

Fetch variable from yaml in puppet manifest

I'm doing one project for puppet, however currently stuck in one logic.
Thus, want to know can we fetch variable from .yaml, .json or plain text file in puppet manifest file.
For example,
My puppet manifest want to create user but the variable exist in the .yaml or any configuration file, hence need to fetch the varibale from the outside file. The puppet manifest also can do looping if it exist multiple users in .yaml file.
I read about hiera but let say we are not using hiera is there any possible way.
There are a number of ways you can do this using a combination of built-in and stdlib functions, at least for YAML and JSON.
Using the built-in file function and the parseyaml or parsejson stdlib functions:
Create a file at mymodule/files/myfile.yaml:
▶ cat files/myfile.yaml
---
foo: bar
Then in your manifests read it into a string and parse it:
$myhash = parseyaml(file('mymodule/myfile.yaml'))
notice($myhash)
That will output:
Notice: Scope(Class[mymodule]): {foo => bar}
Or, using the loadyaml or loadjson stdlib functions:
$myhash = loadyaml('/etc/puppet/data/myfile.yaml')
notice($myhash)
The problem with that approach is that you need to know the path to file on the Puppet master. Or, you could use a Puppet 6 deferred function and read the data from a file on the agent node.
(Whether or not you should do this is another matter entirely - hint: answer is you almost certainly should be using Hiera - but that isn't the question you asked.)

chef recipe FileEdit insert_line_after_match and insert_line_if_no_match

I want to edit file using chef cookbook recipe.
The file appears now as,
[attribute1]
foo=bar
[attribute2]
....
I want to change it like:
[attribute1]
foo=bar
newfoo=newbar
[attribute2]
....
So basically, I want to add a line if it does not exist in the file and I want to add it after a particular line in that file.
I found 2 options under Class: Chef::Util::FileEdit which could be useful here insert_line_after_match and insert_line_if_no_match. But I want an option which can perform both of the actions. If I use insert_line_after_match, it works for first run but for next run it just keep adding lines even if line is already in the file. And insert_line_if_no_match adds line at the end of file if line does not exist in file but I want to add line after particular line in that file.
I am bit new to chef recipes. Is there any solution to solve above problem?
I would suggest not editing files, but rather overwriting them. You should create a template or a file inside the cookbook and then using template or cookbook_file resource overwrite the file on the machine with the one from cookbook.
Your config file looks similar to toml, so you can also use toml-rb gem to generate this file from json (data bag) or attributes like that:
chef_gem 'toml-rb' do
compile_time false
end
file '/path/to/file.conf' do
content( lazy do
require 'toml'
"# This file is managed by Chef\n" +
TOML.dump( my_json )
end )
end
Pretty please don't use FileEdit. It is an internal API and not intended for public use. What you want is the line cookbook, specifically the replace_or_add custom resource. Make sure you craft your regexp very carefully.
In general we do not recommend this kind of management style as it is very brittle and easily broken by unrelated changed. A better option is to use a template resource or similar to manage the whole file in a convergent manner.

How to set processing_root in rp5rc

I'm attempting to install ruby processing. I followed this tutorial:
https://github.com/jashkenas/ruby-processing/wiki/Getting-Started
After I rake ( before I install jruby ), all of the tests fail. I get the following result before every print out and not sure how to fix it.
WARNING: you need to set PROCESSING_ROOT in ~/.rp5rc
Following the tipp by Oscar here is an easy Copy & Paste solution for Mac Users:
echo PROCESSING_ROOT: "/Applications/Processing.app/Contents/Java" > ~/.rp5rc
The instructions on the wiki have been updated since you asked this question.
As is now suggested, you can use this gist to create your .rp5rc file. Create a new sketch in Processing using the contents of SetProcessingRoot.pde in the gist, and it will suggest the correct PROCESSING_ROOT value for your system and create the file. Note that you'll have to delete the default text ("enter your processing root here") and enter the suggested (or another) path.
Or, if you know the correct PROCESSING_ROOT path for your system, do the following:
echo PROCESSING_ROOT: \"correct_path\" > ~/.rp5rc

Ruby require path

I have a Ruby code with different classes in a few files. In one file, I start the execution. This file requires my other files.
Is this a good way to start a ruby code?
When I run the code from a symbolic link, for example DIR2/MyRubyCode is a link to the main file DIR1/MyRubyCode.rb, then my requires will fail. I solved the problem by adding the path DIR1 to $LOAD_PATH before the require, but I think there would be much better ways to do it. Do you have any suggestions about that?
If you're using Ruby 1.9 or greater, user require_relative for your dependencies.
require_relative 'foo_class'
require_relative 'bar_module'
If you want to check if a Ruby file is being 'require'ed or executed with 'ruby MyRubyCode.rb', check the __FILE__ constant.
# If the first argument to `ruby` is this file.
if $0 == __FILE__
# Execute some stuff.
end
As far as the require/$LOAD_PATH issue, you could always use the relative path in the require statement. For example:
# MyRubyCode.rb
require "#{File.dirname(__FILE__)}/foo_class"
require "#{File.dirname(__FILE__)}/bar_module"
Which would include the foo_class.rb and bar_module.rb files in the same directory as MyRubyCode.rb.
I know this is an old question, but there is an updated answer to it, and I wanted to post it:
Starting in a more recent version of Ruby (I'm not sure when), you can require files in the same directory by using the following:
require './foo_class'
require './bar_module'
and it'll load files called foo_class.rb and bar_module.rb in the same directory.
For checking if your file is being required or ran normally, check the other answer.

How can I get the path for the last created file in a directory using Ruby?

How can I get the path for the last created file in a directory using Ruby?
I think this is fairly brief:
Dir.glob(File.join(path, '*.*')).max { |a,b| File.ctime(a) <=> File.ctime(b) }
Dir.entries("testdir").reject{|f| f== '.' || f=='..'}.sort_by{|f| File.ctime(f)}.last
you can use the dir class to list all files and check the ctime or atime of the file object (ctime is the time the file was changed the last time, atime is the time the file was accessed the last time)
Dir.foreach("testdir") {|f| puts File.ctime(x) }
Dir.glob(root_path + ".").map{ |file| [file,File.ctime(file)]}.max.first
I added this method to my supermanpatches.rb file inside of railsapp/config/initializers to open my latest generated migration (in TextMate) without having to copy and paste the filename or anything like that:
def latestmigration
`mate db/migrate/#{Dir.glob(File.join(Rails.root, 'db', 'migrate', '*.rb')).max { |a,b| File.ctime(a) <=> File.ctime(b)} }`
end
‡: (FYI for ruby/rails beginners, initializer code is omnipresent and requires no class-to-filename scoping to be accessible from anywhere within rails)
NOTE: With windows (or mac) you could use the vim command instead of the mate command, and sublimetext can be configured to do this too, I think its called the subl command. mate & subl don't work by default though I think, so you have to set that up first

Resources