如何在ruby钩子中加载python文件?
我试图添加钩子来忽略在gitlab中提交的二进制文件,所以我在/opt/gitlab/embedded/service/gitlab-shell/lib
添加了新的python(.py)钩子,我在/opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.rb
加载了该文件/opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.rb
但是当我尝试提交文件时,我在提交屏幕中得到了以下exception
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文件?
来自Kernel#require的这个基本片段应该可以为你总结一下:
如果文件名的扩展名为“.rb”,则将其作为源文件加载; 如果扩展名是“.so”,“。o”或“.dll”,或者是当前平台上的默认共享库扩展,Ruby会将共享库作为Ruby扩展加载。 否则,Ruby会尝试将“.rb”,“。so”等添加到名称中,直到找到。 如果找不到名为的文件,则会引发LoadError。
在旁注中你是如何期待这种行为的? ruby解释器应该如何处理python文件?