Compiling haml from a different directory - haml

I use this to launch my haml compile:
/install-location/haml /myproject/index.haml /myproject/index.html
It runs fine when I'm in the directory with the haml file but when I change to a different directory I get:
Exception on line 3: No such file or directory # rb_sysopen - assets/page/structure/_head.haml
Use --trace for backtrace.
What can I do to fix this?

The path you are trying to read — assets/page/structure/_head.haml is relative to the working directory, not to the source file directory. When you’re in the same directory it works because these two directories are the same.
To be able to run the code from a different directory you need to use absolute paths. You can convert the relative path to an absolute one with File::expand_path, File::dirname and __FILE__:
= Haml::Engine.new(File.read(File.expand_path 'assets/page/structure/_head.haml', File.dirname(__FILE__))).render

Related

How can I target a nested directory in .prettierignore?

I would like to write a .prettierignore file to target the files in a single directory, which is nested one level down from the root of the project. I figure the most elegant way to accomplish this is to use a negated pattern to ignore everything but the target directory.
So, I currently have this:
# Ignore all
/*
# but for /views
!src/views/
Running the npx prettier --write . command with that pattern (or similar variations) appears to match no files, and no files change. However, if I run the command without/views in the pattern, i.e. just !src, then prettier formats everything in the src directory, including all the files in /views.
What am I missing that I can't successfully target only the nested /views directory?
After some more searching, I found this solution, which accomplishes the outcome I was seeking, though, I don't quite understand how it works.
#ignore all
/*
#but don't ignore files or directories at root called src
!/src
#ignore everything in src dir?
/src/*
#but don't ignore views dir in src?
!/src/views
Why would that be necessary compared to just src/views?

Singularity definition file with paths relative to it

Question
When building Singularity images using definition files, is there a way to specify the path to a file on the host system relative to the definition file (i.e. independent of where the build command is called)?
Example to Illustrate the Problem
I have the following files in the same directory (e.g. a git repository):
foobar.def
some_file.txt
foobar.def looks as follows:
Bootstrap: library
From: ubuntu:20.04
Stage: build
%files
# Add some_file.txt at the root of the image
some_file.txt /some_file.txt
This works fine when I build with the following command in the directory which contains the files:
singularity build --fakeroot foobar.sif foobar.def
However, it fails if I call the build command from anywhere else (e.g. from a dedicated "build" directory) because it searches some_file.txt relative to the current working directory of the build command, not relative to the definition file.
Is there a way to implement the definition file such that the build works independently of where the command is called? I know that I could use absolute paths but this is not a viable solution in my case.
To make it even more complicated: My actual definition file is bootstrapping from another local image, which is located in the build directory. So ideally I would need a solution where some files are found relative the working directory while others are found relative to the location of the definition file.
Short answer: Not really
Longer answer: Not really, but there's a reason why and it shouldn't really matter for most use cases. While Docker went the route of letting you specify what your directory context is, Singularity decided to base all of its commands off the current directory where it is being executed. This also follows with $PWD being auto-mounted into the container, so it makes sense for it to be consistent.
That said, is there a reason you can't run singularity build --fakeroot $build_dir/foobar.sif foobar.def from the repo directory? There isn't any other output written besides the final image and it makes more sense for the directory with the data being used to be the context to work from.

Apache server cannot find local file [duplicate]

I'm working on a Flask extension from which I want to create a directory in the project's root path on the file system.
Suppose we have this directory structure
/project
/app
/tests
/my_folder
manage.py
my_folder should be created dynamically by the extension, which is a test utility and wraps the application under test in the /tests directory. However, I'm struggling to determine the project's root path within my extension.
For now, I am trying to guess the path from the run file:
def root_path(self):
# Infer the root path from the run file in the project root (e.g. manage.py)
fn = getattr(sys.modules['__main__'], '__file__')
root_path = os.path.abspath(os.path.dirname(fn))
return root_path
This obviously breaks as soon as the tests are run from within the IDE instead of the manage.py. I could simply infer the project's root relative to the app or tests directory, but I don't want to make any assumptions regarding the name or structure of these directories (since multiple apps might be hosted as subpackages in a single package).
I was wondering if there is a best practice for this type of problem or an undocumented method which the Flask object provides (such as get_root_path).
app.root_path contains the root path for the application. This is determined based on the name passed to Flask. Typically, you should use the instance path (app.instance_path) not the root path, as the instance path will not be within the package code.
filename = os.path.join(app.instance_path, 'my_folder', 'my_file.txt')
app.root_path is the absolute path to the root directory containing your app code.
app.instance_path is the absolute path to the instance folder. os.path.dirname(app.instance_path) is the directory above the instance folder. During development, this is next to or the same as the root path, depending on your project layout.

I cannot install PhpWord Yii

I have tried to install PhpWord to yii. I have downloaded zip file and extracted it into extentions folder:
extenstions
--PHPWord
--PHPWord.php
However, I cannot make it to run. I got following error:
include(PHPWord.php): failed to open stream: No such file or directory
How can i solve it?
After extracting the file in extension folder, you have to import that file in controller.
Yii::import('ext.phpword.PHPWord');
First of all, you didn't say if it's Yii 1 or 2. They have different autoloading methods.
Second, you have extracted it into extension folder, and I assume your file where you want to include it is in a completely different folder.
You should do it like this
include('/full/path/to/PHPWord.php');
You need either absolute or a relative path to the file (I suggest using abosulte path (the one I used as an example).
Relative path means the path to the file you want to include compared to where your file, in which you are including it, is.

where is bin directory in rails3?

I am trying to place the wkhtmltopdf executable in the "bin directory" of my rails project so that it works with heroku, as mentioned here.
dumb question....where is the "bin" folder in the rails project? I placed the executable in root of my executable, however, heroku gives an error saying:
No wkhtmltopdf executable found at /app/bin/wkhtmltopdf-i386
And the following code is used to provide path for wkhtmltopdf
config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-i386');
You need to create the bin folder under the app/ folder and add it in there.
mkdir app/bin