module Autoproj::Ops

Public Class Methods

atomic_write(file_name, temp_dir = Dir.tmpdir) { |temp_file| ... } click to toggle source

Shamelessly stolen from ActiveSupport

# File lib/autoproj/ops/atomic_write.rb, line 4
def self.atomic_write(file_name, temp_dir = Dir.tmpdir)
    require "tempfile" unless defined?(Tempfile)
    require "fileutils" unless defined?(FileUtils)

    temp_file = Tempfile.new(File.basename(file_name), temp_dir)
    yield temp_file
    temp_file.flush
    begin temp_file.fsync
    rescue NotImplementedError
    end
    temp_file.close

    begin
        # Get original file permissions
        old_stat = File.stat(file_name)
    rescue Errno::ENOENT
        # No old permissions, write a temp file to determine the defaults
        check_name = File.join(
            File.dirname(file_name), ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}"
        )
        File.open(check_name, "w") {}
        old_stat = File.stat(check_name)
        File.unlink(check_name)
    end

    # Overwrite original file with temp file
    FileUtils.mv(temp_file.path, file_name)

    # Set correct permissions on new file
    File.chown(old_stat.uid, old_stat.gid, file_name)
    File.chmod(old_stat.mode, file_name)
end
cached_env_path(root_dir) click to toggle source
# File lib/autoproj/ops/cached_env.rb, line 5
def self.cached_env_path(root_dir)
    File.join(root_dir, ".autoproj", "env.yml")
end
load_cached_env(root_dir) click to toggle source
# File lib/autoproj/ops/cached_env.rb, line 9
def self.load_cached_env(root_dir)
    path = cached_env_path(root_dir)
    if File.file?(path)
        env = YAML.safe_load(File.read(path))
        Autobuild::Environment::ExportedEnvironment.new(
            env["set"], env["unset"], env["update"]
        )
    end
end
loader() click to toggle source

@deprecated use Autoproj.workspace, or better make sure all ops classes

get their own workspace object as argument
# File lib/autoproj/ops/loader.rb, line 107
def self.loader
    Autoproj.workspace
end
save_cached_env(root_dir, env) click to toggle source
# File lib/autoproj/ops/cached_env.rb, line 19
def self.save_cached_env(root_dir, env)
    env = env.exported_environment
    path = cached_env_path(root_dir)
    existing =
        begin
            YAML.safe_load(File.read(path))
        rescue Exception
        end

    env = Hash["set" => env.set, "unset" => env.unset, "update" => env.update]
    if env != existing
        Ops.atomic_write(path) do |io|
            io.write YAML.dump(env)
        end
        true
    end
end
watch_cleanup_marker(io) click to toggle source
# File lib/autoproj/ops/watch.rb, line 32
def self.watch_cleanup_marker(io)
    FileUtils.rm_f io.path
    io.close
end
watch_create_marker(root_dir) click to toggle source
# File lib/autoproj/ops/watch.rb, line 18
def self.watch_create_marker(root_dir)
    io = File.open(watch_marker_path(root_dir), "a+")
    unless io.flock(File::LOCK_EX | File::LOCK_NB)
        raise WatchAlreadyRunning, "autoproj watch is already running as PID #{io.read.strip}"
    end

    io.truncate(0)
    io.puts Process.pid
    io.flush
rescue Exception
    io&.close
    raise
end
watch_marker_path(root_dir) click to toggle source
# File lib/autoproj/ops/watch.rb, line 3
def self.watch_marker_path(root_dir)
    File.join(root_dir, ".autoproj", "watch")
end
watch_running?(root_dir) click to toggle source
# File lib/autoproj/ops/watch.rb, line 7
def self.watch_running?(root_dir)
    io = File.open(watch_marker_path(root_dir))
    !io.flock(File::LOCK_EX | File::LOCK_NB)
rescue Errno::ENOENT
    false
ensure
    io&.close
end
which(cmd, path_entries: nil) click to toggle source

Find the given executable file in PATH

If ‘cmd` is an absolute path, it will either return it or raise if `cmd` is not executable. Otherwise, looks for an executable named `cmd` in PATH and returns it, or raises if it cannot be found. The exception contains a more detailed reason for failure

@param [String] cmd @return [String] the resolved program @raise [ExecutableNotFound] if an executable file named ‘cmd` cannot

be found
# File lib/autoproj/ops/which.rb, line 19
def self.which(cmd, path_entries: nil)
    path = Pathname.new(cmd)
    if path.absolute?
        if path.file? && path.executable?
            cmd
        elsif path.exist?
            raise ExecutableNotFound.new(cmd),
                  "given command `#{cmd}` exists but is not an executable file"
        else
            raise ExecutableNotFound.new(cmd),
                  "given command `#{cmd}` does not exist, "\
                  "an executable file was expected"
        end
    else
        path_entries = path_entries.call if path_entries.respond_to?(:call)
        absolute = Autobuild::Environment.find_executable_in_path(cmd, path_entries)

        if absolute
            absolute
        elsif (file = Autobuild::Environment.find_in_path(cmd, path_entries))
            raise ExecutableNotFound.new(cmd),
                  "`#{cmd}` resolves to #{file} which is not executable"
        else
            raise ExecutableNotFound.new(cmd),
                  "cannot resolve `#{cmd}` to an executable in the workspace"
        end
    end
end