How to load the python file in ruby hook?

耗尽温柔 提交于 2019-12-25 11:57:20

问题


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

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'

My pre-receive hook file is

#!/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

Why I can`t able to load the .py file?


回答1:


This basic snippet from Kernel#require should probably sum it up for you:

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.

On a sidenote how did you expect this to behave? How is the ruby interpreter supposed to handle a python file?



来源:https://stackoverflow.com/questions/46069393/how-to-load-the-python-file-in-ruby-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!