Get the full path of the symlink target in Ansible - ansible

How do I get the full path of a symlink target in Ansible? I'm trying to cleanup a folder--delete files older than x number of days but I don't want to remove the target of a particular file symlink. My plan is to get the symlink target and then use the value as an exclude target when using the find module.
The symlink target changes from time to time. I tried using the stat module but it doesn't solve my requirement.

stat module returns lnk_source and lnk_target which should do the trick.
Pay attention to confusing names:
lnk_source => Target of the symlink normalized for the remote filesystem
lnk_target => Target of the symlink. Note that relative paths remain relative
For the actual SOURCE of the symlink, well, it's just path.

Related

Patch bitbake to use custom `wpa_supplicant.conf`

I have a wpa_supplicant.conf that I want to use at first boot. I tried to patch poky's recipe as follows. This is my wpa_supplicant_2.6.bbappend:
FILESEXTRAPATHS_append := ":${THISDIR}/${PN}"
SRC_URI_append = " file://wpa_supplicant.conf"
I have the conf file stored relative to the bbappend in wpa_supplicant/wpa_supplicant.conf. Still the original poky conf file is always added to the image.
How can I inject my config file?
(I'm on branch warrior)
NOTE/EDIT: For quick and efficient problem solving I recommend also discussing issues in the IRC channel (as also happened in this case). Super helpful people there!
First, the recipe is called wpa-supplicant and not wpa_supplicant so you need to name the bbappend wpa-supplicant_2.6.bbapend and not wpa_supplicant_2.6.bbappend. Remember, no underscore, no uppercase letter in recipe or package name.
One can check that a bbappend is parsed by using bitbake-layers show-appends wpa-supplicant. You'd have seen that your bbappend was not taken into account.
Then, one should usually use FILESEXTRAPATHS_prepend := because you want your path to be traversed before all the other ones.
FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
Finally, since the recipe is named wpa-supplicant and not wpa_supplicant, ${PN} will be wpa-supplicant, thus you need to put your file in wpa-supplicant/ and not wpa_supplicant.
To check in which order paths are traversed for files, one can run bitbake -e wpa-supplicant | grep -e "^FILESPATH=", the paths are traversed from leftmost to rightmost. The first file which matches the full path will be taken.
After discussing on IRC with you, I can add that we also figured out that wpa_supplicant.conf is installed in the documentation directory but wpa_supplicant.conf-sane is the one that will be used as wpa_supplicant.conf in the final image for the target. So one would need to name the wpa_supplicant.conf file as wpa_supplicant.conf-sane in order for it to replace the wpa_supplicant.conf file in the final image.
c.f. https://git.yoctoproject.org/cgit.cgi/poky/tree/meta/recipes-connectivity/wpa-supplicant/wpa-supplicant_2.6.bb?h=thud#n88 and https://git.yoctoproject.org/cgit.cgi/poky/tree/meta/recipes-connectivity/wpa-supplicant/wpa-supplicant_2.6.bb?h=thud#n91

Failing to open a file which should be in the base path

I have a Go project (bazel-remote) that tries to read a yaml file passed in the command line, when built with bazel. This yaml file sits in the same location from where I run the bazel run command.
But it fails to run because os.Open fails with no such file or directory.
I printed the basePath using os.Getwd, because someone suggested that my basePath might be set wrong. But my basePath is set to a location in my /private/var/tmp/ where the bazel objects are created and stored:
/private/var/tmp/bazel/312feba8ddcde6737ae7dd7ef9bc2a5a/execroot/main/bazel-out/darwin-fastbuild/bin/darwin_amd64_static_pure_stripped/bazel-remote.runfiles/main'
How do I set my basePath correctly? Why is my basePath set to where it is?
Binaries started with bazel run are executed in an internal Bazel directory. They'll have access to "runfiles", which are files mentioned in the data attribute of the binary rule or its dependencies. For example, if you have a rule like the one below, you'll be able to read foo.txt, but not bar.txt or other files:
load("#io_bazel_rules_go//go:def.bzl", "go_binary")
go_binary(
name = "hello",
srcs = ["hello.go"],
data = ["foo.txt"],
)
Note that the working directory of the binary corresponds to the repository root directory, not the directory where the binary is defined. You can debug with os.Getwd and filepath.Walk.
You mentioned you wanted to access a yaml file passed in on the command line though. Presumably, you want to be able to access any file the user passes in, not just files mentioned in the data attribute. For this case, take a look at the BUILD_WORKING_DIRECTORY environment variable (bazel run sets this). That gives the path to the directory where bazel run was invoked. Also, BUILD_WORKSPACE_DIRECTORY is the path to the workspace root directory.

How can I determine the original directory that an alias points to?

I'm working with a directory that's an alias on Mac OS, with Ruby. This is a folder that points to another folder. How can I determine the original directory that this alias points to?
In one of my Jenkins jobs, there is an alias called lastStable, which points to the latest stable build folder:
path = /Users/steve/.Jenkins/jobs/MyApp/lastStable
lastStable actually points to a folder called 2013-08-06_10_50_49.
How can I get this info dynamically in Ruby?
File.realpath resolves symlinks.
You could do:
File.realpath '/usr/bin/ruby'
#=> "/usr/bin/ruby1.9.3"
You can use the readlink method:
File.readlink(path)

Open file from same directory

Ok so with siriproxy it my lib folder along with the rb file for the plugin I have created a myconfig.yml file so I can change certain settings by writing to that file.
I have been able to write to the file but only if I include the full path all the way from the home directory down.
But is there not a way to open the file from the same directory i am in? I have tried every path combination I can think of.
There has to be one i am missing
If you use the following in your ruby file, you should get the absolute path where it is
File.expand_path(__FILE__)
From doc __FILE__
The name of the file currently being executed, including path relative to the directory where the application was started up (or the current directory, if it has been changed)
From doc File.expand_path
Converts a pathname to an absolute pathname.
As you probably want the directory, you should use File.dirname(__FILE__), so the path of your file myconfig.yml should be obtained with
File.join(File.expand_path(File.dirname(__FILE__)), 'myconfig.yml')
In more recent Ruby (>=2.0.0), you can use __dir__ (from Archonic's comment):
Returns the canonicalized absolute path of the directory of the file from which this method is called. It means symlinks in the path is resolved. If FILE is nil, it returns nil. The return value equals to File.dirname(File.realpath(FILE)).

RUBYLIB Environment Path

So currently I have included the following in my .bashrc file.
export RUBYLIB=/home/git/project/app/helpers
I am trying to run rspec with a spec that has
require 'output_helper'
This file is in the helpers directory. My question is that when I change the export line to:
export RUBYLIB=/home/git/project/
It no longer finds the helper file. I thought that ruby should search the entire path I supply, and not just the outermost directory supplied? Is this the correct way to think about it? And if not, how can I make it so RUBY will search through all subdirectories and their subdirectories, etc?
Thanks,
Robin
Similar to PATH, you need to explicitly name the directory under which to look for libraries. However, this will not include any child directories within, so you will need to list any child sub-directories as well, delimiting them with a colon.
For example:
export RUBYLIB=/home/git/project:/home/git/project/app/helpers
As buruzaemon mentions, Ruby does not search subdirectories, so you need to include all the directories you want in your search path. However, what you probably want to do is:
require 'app/helpers/output_helper'
This way you aren't depending on the RUBYLIB environment variable being set a certain way. When you're deploying code to production, or collaborating with others, these little dependencies can make for annoying debugging sessions.
Also as a side note, you can specify . as a search path, rather than using machine-specific absolute paths.

Resources