Class: PacksSaver

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

Instance Method Summary collapse

Constructor Details

#initialize(repo_id, packs, lang, release_id) ⇒ PacksSaver

Returns a new instance of PacksSaver.



8
9
10
11
12
13
# File 'lib/recorder.rb', line 8

def initialize(repo_id, packs, lang, release_id)
  @release_id = release_id
  @repo_id = repo_id
  @packs = packs
  @lang = lang
end

Instance Method Details

#enqueue_result(pg_result) ⇒ Object

处理api_add_pack()返回的结果



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/recorder.rb', line 93

def enqueue_result(pg_result)
  begin
    if pg_result != nil
      pack_id = pg_result['pack_id'].to_i
      is_newbie = pg_result['is_newbie'] == 't'
      api_add_product_repo_pack(@repo_id, pack_id, @release_id)

      if is_newbie
        queue_name = 'license_auto.pack'
        $rmq.publish(queue_name, {:pack_id => pack_id}.to_json, check_exist=true)
      end
      $plog.debug("pack_id: #{pack_id}, is_newbie: #{is_newbie}")
    else
      $plog.error("#{pack_url} insert failed!")
    end
  rescue Exception => _
    $plog.error(_)
  end
end

#license_name_format(origin_license) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/recorder.rb', line 62

def license_name_format(origin_license)
  if origin_license.nil?
    return nil
  end
  empty_pattern = /---\s\[\]\n/
  license = origin_license.gsub(empty_pattern, '').gsub(/---\s\n[\.]+\n/, '').gsub(/---\n-\s/, '').gsub(/\n/, '').gsub(/['"\s]+$/, '').gsub(/^['"\s]+/, '')
  if license.empty?
    return nil
  else
    return license
  end
end

#saveObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/recorder.rb', line 15

def save()
  if @lang == 'Golang'
    save_golang
  elsif @lang == 'Ruby'
    save_ruby
  elsif @lang == 'manifest.yml' or @lang == 'NodeJs' or @lang == 'Erlang'
    save_manifest
  elsif @lang == 'Gradle'
    save_gradle
  end
end

#save_golangObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/recorder.rb', line 113

def save_golang()
  @packs.each { |pack_name, pack_url|
    begin
      $plog.debug("save_golange: #{pack_url}")

      pack_version = 'unknown'
      homepage = nil
      source_url = nil
      status = 10
      cmt = nil

      remote = API::RemoteSourceVCS.new(pack_url)

      if remote.vcs == nil
        status = 30
        cmt = 'Unknown repo site'
        pack_name = pack_name.split('/').last
      else
        pack_name = remote.vcs.repo
        source_url = remote.vcs.repo_url
        homepage = remote.get_homepage
        last_commit = remote.vcs.last_commits
        if last_commit
          pack_version = last_commit['sha']
        end
      end
      # $plog.debug("#{pack_version}, #{pack_url}")
      license = nil
      pg_result = api_add_pack(pack_name, pack_version, 'Golang', homepage, source_url, license, status, cmt)
      enqueue_result(pg_result)
    rescue Exception => _
      $plog.error(_)
    end
  }
end

#save_gradleObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/recorder.rb', line 27

def save_gradle
  @packs.each {|pack|
    begin
      $plog.debug(pack)
      pack_name = [pack[:group], pack[:name]].join(':')
      pack_version = pack[:version]
      status = 10
      homepage, source_url = nil, pack['uri']
      license, cmt = nil, nil

      pg_result = api_add_pack(pack_name, pack_version, @lang, homepage, source_url, license, status, cmt)
      enqueue_result(pg_result)
    rescue Exception => _
      $plog.error(_)
    end
  }
end

#save_manifestObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/recorder.rb', line 45

def save_manifest
  @packs.each {|pack|
    begin
      $plog.debug(pack)
      pack_name, pack_version, status = pack['name'], pack['version'], 10
      homepage, source_url = nil, pack['uri']
      license, cmt = nil, nil

      pg_result = api_add_pack(pack_name, pack_version, @lang, homepage, source_url, license, status, cmt)
      enqueue_result(pg_result)
    rescue Exception => _
      $plog.error(_)
    end
  }
end

#save_rubyObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/recorder.rb', line 61

def save_ruby
  def license_name_format(origin_license)
    if origin_license.nil?
      return nil
    end
    empty_pattern = /---\s\[\]\n/
    license = origin_license.gsub(empty_pattern, '').gsub(/---\s\n[\.]+\n/, '').gsub(/---\n-\s/, '').gsub(/\n/, '').gsub(/['"\s]+$/, '').gsub(/^['"\s]+/, '')
    if license.empty?
      return nil
    else
      return license
    end
  end

  @packs.each {|pack|
    begin
      pack[:license] = license_name_format(pack[:license])
      # $plog.debug(pack)
      pack_name, pack_version, status = pack[:pack_name], pack[:pack_version], pack[:status]
      homepage, source_url = pack[:homepage], pack[:source_url]
      # lang is a website in act
      license, cmt, lang = pack[:license], pack[:cmt], pack[:language]

      pg_result = api_add_pack(pack_name, pack_version, lang, homepage, source_url, license, status, cmt)
      enqueue_result(pg_result)
    rescue Exception => _
      $plog.error(_)
    end
  }
end