Class: RIM::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/rim/processor.rb

Direct Known Subclasses

CommandHelper, ModuleHelper

Constant Summary collapse

MaxThreads =
10
GerritServer =
"ssh://gerrit/"

Instance Method Summary collapse

Constructor Details

#initialize(workspace_root, logger) ⇒ Processor

Returns a new instance of Processor.



17
18
19
20
# File 'lib/rim/processor.rb', line 17

def initialize(workspace_root, logger)
  @ws_root = workspace_root
  @logger = logger
end

Instance Method Details

#clone_or_fetch_repository(remote_url, local_path, clone_log = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rim/processor.rb', line 72

def clone_or_fetch_repository(remote_url, local_path, clone_log = nil)
  FileUtils.mkdir_p local_path
  RIM::git_session(local_path) do |s|
    if !File.exist?(File.join(local_path, ".git"))
      @logger.info(clone_log) if clone_log
      FileHelper.make_empty_dir(local_path)
      s.execute("git clone #{remote_url} .")
      s.execute("git config core.ignorecase false")
    else
      s.execute("git remote set-url origin #{remote_url}") if !s.has_valid_remote_repository?()
      s.execute("git fetch")
    end
  end
  local_path
end

#commit(session, message) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rim/processor.rb', line 88

def commit(session, message)
  msg_file = Tempfile.new('message')
  begin
    msg_file << message
    msg_file.close
    session.execute("git add --all")
    session.execute("git commit -F #{msg_file.path}")
  ensure
    msg_file.close(true)
  end
end

#each_module_parallel(task_desc, modules) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rim/processor.rb', line 100

def each_module_parallel(task_desc, modules)
  if !modules.empty?
    @logger.debug "starting \"#{task_desc}\" for #{modules.size} modules\r"
    threads = []
    messages = []
    i = 0
    done = 0
    while i == 0 || !threads.empty?
      while threads.size < MaxThreads && i < modules.size
        threads << Thread.new(i) do |i|
          begin
            yield(modules[i], i)
          rescue RimException => e
            messages += e.messages
          end
        end
        i += 1
      end
      sleep(0.1)
      threads = threads.select{|t|
        if t.alive?
          true
        else
          t.join
          done += 1
          @logger.debug "#{task_desc} #{done}/#{modules.size}\r"
          false
        end
      }
    end
    if !messages.empty?
      raise RimException.new(messages)
    end
  end
end

#get_absolute_remote_url(remote_url) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rim/processor.rb', line 48

def get_absolute_remote_url(remote_url)
  if remote_url.start_with?("file:")
    remote_url = remote_url.gsub(/^file:(\/\/)?/, "")
    match = remote_url.match(/^\/(\w)\|/)
    if match
      remote_url = "#{match[1]}:#{remote_url[match[0].size..-1]}"
    elsif !remote_url.start_with?(File::SEPARATOR)
      File.expand_path(remote_url, @ws_root)
    else
      remote_url
    end
  else
    URI.parse(GerritServer).merge(URI.parse(remote_url)).to_s
  end
end

#get_relative_path(path) ⇒ Object



44
45
46
# File 'lib/rim/processor.rb', line 44

def get_relative_path(path)
  FileHelper.get_relative_path(path, @ws_root)
end

#local_changes?(ws_dir, dir = ws_dir) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/rim/processor.rb', line 64

def local_changes?(ws_dir, dir=ws_dir)
  stat = nil
  RIM::git_session(ws_dir) do |s|
    stat = s.status(dir)
  end
  stat.lines.all?{|l| l.ignored?}
end

#module_git_path(remote_path) ⇒ Object



22
23
24
25
26
# File 'lib/rim/processor.rb', line 22

def module_git_path(remote_path)
  # remote url without protocol specifier
  # this way the local path should be unique
  File.join(@ws_root, ".rim", remote_path)
end

#module_tmp_git_path(remote_path) ⇒ Object



28
29
30
31
32
# File 'lib/rim/processor.rb', line 28

def module_tmp_git_path(remote_path)
  # remote url without protocol specifier
  # this way the local path should be unique
  File.join(@ws_root, ".rim", ".tmp", remote_path)
end

#remote_path(remote_url) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rim/processor.rb', line 34

def remote_path(remote_url)
  remote_url.
    # protocol specifier, e.g. ssh://
    sub(/^\w+:\/\//,'').
    # windows drive letter 
    sub(/^\w+:/,'\1').
    # make sure we don't .. up in a filesystem
    gsub(/\.\.[\\\/]/,'')
end