如何在ruby钩子中加载python文件? [英] How to load the python file in ruby hook?
问题描述
我试图添加钩子以忽略gitlab中提交的二进制文件,所以我在/opt/gitlab/embedded/service/gitlab-shell/lib
中添加了新的python(.py)钩子,并将该文件加载到/opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.rb
I trying to add the hook to ignore the binary file committing in gitlab, SO I added the new python(.py) hook in /opt/gitlab/embedded/service/gitlab-shell/lib
and i loaded that file in /opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.rb
但是当我尝试提交文件时,在提交屏幕中出现了以下异常
But When I tried to commit the file i got the below exception in commit screen
远程:钩子/预接收:17:在
require_relative': cannot load such file -- /opt/gitlab/embedded/service/gitlab-shell/lib/gitlab_ignore_binary.py (LoadError) remote: from hooks/pre-receive:17:in
'
remote: hooks/pre-receive:17:in
require_relative': cannot load such file -- /opt/gitlab/embedded/service/gitlab-shell/lib/gitlab_ignore_binary.py (LoadError) remote: from hooks/pre-receive:17:in
'
我的接收前挂钩文件是
#!/opt/gitlab/embedded/bin/ruby
# Fix the PATH so that gitlab-shell can find git-upload-pack and friends.
ENV['PATH'] = '/opt/gitlab/bin:/opt/gitlab/embedded/bin:' + ENV['PATH']
#!/usr/bin/env ruby
#!/usr/bin/env python
# This file was placed here by GitLab. It makes sure that your pushed commits
# will be processed properly.
refs = $stdin.read
key_id = ENV.delete('GL_ID')
protocol = ENV.delete('GL_PROTOCOL')
repo_path = Dir.pwd
gl_repository = ENV['GL_REPOSITORY']
require_relative '../lib/gitlab_ignore_binary.py'
require_relative '../lib/gitlab_custom_hook'
require_relative '../lib/gitlab_reference_counter'
require_relative '../lib/gitlab_access'
# It's important that on pre-receive `increase_reference_counter` gets executed
# last so that it only runs if everything else succeeded. On post-receive on the
# other hand, we run GitlabPostReceive first because the push is already done
# and we don't want to skip it if the custom hook fails.
if GitlabAccess.new(gl_repository, repo_path, key_id, refs, protocol).exec &&
GitlabCustomHook.new(repo_path, key_id).pre_receive(refs) &&
GitlabReferenceCounter.new(repo_path).increase
exit 0
else
exit 1
end
为什么我无法加载.py文件?
Why I can`t able to load the .py file?
推荐答案
This basic snippet from Kernel#require should probably sum it up for you:
如果文件名具有扩展名".rb",则将其作为源文件加载;如果扩展名是".so",.o"或".dll"或当前平台上的默认共享库扩展名,则Ruby将共享库作为Ruby扩展名加载.否则,Ruby会尝试在名称中添加".rb",.so"等,直到找到为止.如果找不到名为的文件,则会引发LoadError.
If the filename has the extension ".rb", it is loaded as a source file; if the extension is ".so", ".o", or ".dll", or the default shared library extension on the current platform, Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding ".rb", ".so", and so on to the name until found. If the file named cannot be found, a LoadError will be raised.
在旁注中,您如何期待这种行为?红宝石解释器应该如何处理python文件?
On a sidenote how did you expect this to behave? How is the ruby interpreter supposed to handle a python file?
这篇关于如何在ruby钩子中加载python文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!