Class: Bibliothecary::Parsers::Conan

Inherits:
Object
  • Object
show all
Includes:
Analyser
Defined in:
lib/bibliothecary/parsers/conan.rb

Class Method Summary collapse

Methods included from Analyser

create_analysis, create_error_analysis, included

Class Method Details

.mappingObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bibliothecary/parsers/conan.rb', line 11

def self.mapping
  {
    match_filename("conanfile.py") => {
      kind: "manifest",
      parser: :parse_conanfile_py,
    },
    match_filename("conanfile.txt") => {
      kind: "manifest",
      parser: :parse_conanfile_txt,
    },
    match_filename("conan.lock") => {
      kind: "lockfile",
      parser: :parse_lockfile,
    },
  }
end

.parse_conan_reference(ref) ⇒ Object

Parse Conan reference Handles the full Conan reference format: name/version[@username][#recipe_revision][:package_id][%timestamp]

Based on OSV Scalibr’s parseConanReference implementation: github.com/google/osv-scalibr/blob/f37275e81582aee924103d49d9a27c8e353477e7/extractor/filesystem/language/cpp/conanlock/conanlock.go

Returns a hash with keys: name, version, username, channel, recipe_revision, package_id, package_revision, timestamp



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/bibliothecary/parsers/conan.rb', line 180

def self.parse_conan_reference(ref)
  reference = {
    name: nil,
    version: nil,
    username: nil,
    channel: nil,
    recipe_revision: nil,
    package_id: nil,
    package_revision: nil,
    timestamp: nil,
  }

  return reference if ref.nil? || ref.empty?

  # Validate that ref contains "/" (name/version format)
  # This filters out invalid entries like "1.2.3" (version without name)
  return reference unless ref.include?("/")

  # Strip timestamp: name/version%1234 -> name/version
  parts = ref.split("%", 2)
  if parts.length == 2
    ref = parts[0]
    reference[:timestamp] = parts[1]
  end

  # Strip package revision: name/version:pkgid#prev -> name/version
  parts = ref.split(":", 2)
  if parts.length == 2
    ref = parts[0]
    pkg_parts = parts[1].split("#", 2)
    reference[:package_id] = pkg_parts[0]
    reference[:package_revision] = pkg_parts[1] if pkg_parts.length == 2
  end

  # Strip recipe revision: name/version#rrev -> name/version
  parts = ref.split("#", 2)
  if parts.length == 2
    ref = parts[0]
    reference[:recipe_revision] = parts[1]
  end

  # Strip username/channel: name/version@user/channel -> name/version
  parts = ref.split("@", 2)
  if parts.length == 2
    ref = parts[0]
    user_channel = parts[1].split("/", 2)
    reference[:username] = user_channel[0]
    reference[:channel] = user_channel[1] if user_channel.length == 2
  end

  # Split name/version: name/version -> [name, version]
  parts = ref.split("/", 2)
  reference[:name] = parts[0]
  reference[:version] = parts.length == 2 ? parts[1] : nil

  reference
end

.parse_conan_requires(dependencies, requires, type, options) ⇒ Object

Helper method to parse an array of Conan package references Similar to OSV Scalibr’s parseConanRequires function



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bibliothecary/parsers/conan.rb', line 152

def self.parse_conan_requires(dependencies, requires, type, options)
  return unless requires && !requires.empty?

  requires.each do |ref|
    reference = parse_conan_reference(ref)

    # Skip entries with no name, they are most likely consumer's conanfiles
    # and not dependencies to be searched in a database anyway
    next if reference[:name].nil? || reference[:name].empty?

    dependencies << Dependency.new(
      name: reference[:name],
      requirement: reference[:version] || "*",
      type: type,
      source: options.fetch(:filename, nil),
      platform: platform_name
    )
  end
end

.parse_conanfile_py(file_contents, options: {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bibliothecary/parsers/conan.rb', line 28

def self.parse_conanfile_py(file_contents, options: {})
  dependencies = []

  # Parse self.requires() calls in conanfile.py
  # Pattern matches: self.requires("package/version") or self.requires("package/version", force=True, options={...})
  # Captures only the package spec string; additional keyword arguments are ignored
  file_contents.scan(/self\.requires\(\s*["']([^"']+)["']/).each do |match|
    manifest_dep = match[0]
    reference = parse_conan_reference(manifest_dep)

    # Skip entries with no name
    next if reference[:name].nil? || reference[:name].empty?

    dependencies << Dependency.new(
      name: reference[:name],
      requirement: reference[:version],
      type: "runtime",
      source: options.fetch(:filename, nil),
      platform: platform_name
    )
  end

  ParserResult.new(dependencies: dependencies)
end

.parse_conanfile_txt(file_contents, options: {}) ⇒ Object



53
54
55
56
57
58
59
60
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
# File 'lib/bibliothecary/parsers/conan.rb', line 53

def self.parse_conanfile_txt(file_contents, options: {})
  dependencies = []
  current_section = nil

  file_contents.each_line do |line|
    line = line.strip

    # Skip empty lines and comments
    next if line.empty? || line.start_with?("#")

    # Check for section headers
    if line.match?(/^\[([^\]]+)\]$/)
      current_section = line[1..-2]
      next
    end

    # Parse dependencies in [requires] and [build_requires] sections
    next unless %w[requires build_requires].include?(current_section)

    reference = parse_conan_reference(manifest_dep)
    next if reference[:name].nil? || reference[:name].empty?

    dependencies << Dependency.new(
      name: reference[:name],
      requirement: reference[:version],
      type: current_section == "requires" ? "runtime" : "development",
      source: options.fetch(:filename, nil),
      platform: platform_name
    )
  end

  ParserResult.new(dependencies: dependencies)
end

.parse_lockfile(file_contents, options: {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/bibliothecary/parsers/conan.rb', line 87

def self.parse_lockfile(file_contents, options: {})
  manifest = JSON.parse(file_contents)

  # Auto-detect lockfile format version
  if manifest.dig("graph_lock", "nodes")
    parse_v1_lockfile(manifest, options: options)
  else
    parse_v2_lockfile(manifest, options: options)
  end
end

.parse_v1_lockfile(lockfile, options: {}) ⇒ Object



98
99
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
135
136
137
138
# File 'lib/bibliothecary/parsers/conan.rb', line 98

def self.parse_v1_lockfile(lockfile, options: {})
  dependencies = []

  lockfile["graph_lock"]["nodes"].each_value do |node|
    if node["path"] && !node["path"].empty?
      # a local "conanfile.txt", skip
      next
    end

    reference = nil
    if node["pref"]
      #  old format 0.3 (conan 1.27-) lockfiles use "pref" instead of "ref"
      reference = parse_conan_reference(node["pref"])
    elsif node["ref"]
      reference = parse_conan_reference(node["ref"])
    else
      next
    end

    # skip entries with no name, they are most likely consumer's conanfiles
    # and not dependencies to be searched in a database anyway
    next if reference[:name].nil? || reference[:name].empty?

    type = case node["context"]
           when "build"
             "development"
           else
             "runtime"
           end

    dependencies << Dependency.new(
      name: reference[:name],
      requirement: reference[:version],
      type: type,
      source: options.fetch(:filename, nil),
      platform: platform_name
    )
  end

  ParserResult.new(dependencies: dependencies)
end

.parse_v2_lockfile(lockfile, options: {}) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/bibliothecary/parsers/conan.rb', line 140

def self.parse_v2_lockfile(lockfile, options: {})
  dependencies = []

  parse_conan_requires(dependencies, lockfile["requires"], "runtime", options)
  parse_conan_requires(dependencies, lockfile["build_requires"], "development", options)
  parse_conan_requires(dependencies, lockfile["python_requires"], "development", options)

  ParserResult.new(dependencies: dependencies)
end