Class: Ronin::Repos::Repository Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/repos/repository.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents an installed repository.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repository

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the repository.

Parameters:

  • path (String)

    The path to the repository.

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ronin/repos/repository.rb', line 51

def initialize(path)
  @path = File.expand_path(path)

  unless File.exist?(@path)
    raise(RepositoryNotFound,"repository does not exist: #{@path.inspect}")
  end

  unless File.directory?(@path)
    raise(RepositoryNotFound,"path is not a directory: #{@path.inspect}")
  end

  @name = File.basename(@path)
end

Instance Attribute Details

#nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of the repository.

Returns:

  • (String)


40
41
42
# File 'lib/ronin/repos/repository.rb', line 40

def name
  @name
end

#pathString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The path to the repository's directory.

Returns:

  • (String)


35
36
37
# File 'lib/ronin/repos/repository.rb', line 35

def path
  @path
end

Class Method Details

.clone(uri, path, depth: nil) ⇒ Repository

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clones a repository.

Parameters:

  • uri (String, URI::HTTPS)

    The https:// or git@HOST:PATH SSH URI

  • path (String)

    The path to where the repository will be cloned to.

  • depth (Integer, nil) (defaults to: nil)

    The number of commits to clone.

Returns:

Raises:

  • (CommandFailed)

    The git command failed.

  • (CommandNotFailed)

    The git command is not installed.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ronin/repos/repository.rb', line 86

def self.clone(uri,path, depth: nil)
  path = path.to_s
  args = []

  if depth
    args << '--depth' << depth.to_s
  end

  args << uri.to_s
  args << path.to_s

  case system('git','clone',*args)
  when nil
    raise(CommandNotInstalled,"git is not installed")
  when false
    raise(CommandFailed,"command failed: git clone #{args.join(' ')}")
  end

  return new(path)
end

.install(uri, path, branch: nil, tag: nil, **kwargs) ⇒ Repository

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clones and installs a repository from the URI and to the destination path.

Parameters:

  • uri (String, URI::HTTPS)

    The https:// or git@HOST:PATH SSH URI

  • path (String)

    The path to where the repository will be cloned to.

  • branch (String, nil) (defaults to: nil)

    The git branch to pull.

  • tag (Boolean) (defaults to: nil)

    Controls whether to pull git tags in addition to the git commits.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for clone.

Returns:

Raises:



131
132
133
134
135
136
137
138
139
# File 'lib/ronin/repos/repository.rb', line 131

def self.install(uri,path, branch: nil, tag: nil, **kwargs)
  repo = clone(uri,path, **kwargs)

  if branch || tag
    repo.checkout(branch || tag)
  end

  return repo
end

Instance Method Details

#checkout(branch_or_tag) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks out the git branch or tag.

Parameters:

  • branch_or_tag (String)

    The branch or tag name to checkout.

Returns:

  • (true)

    Indicates that the git command executed successfully.

Raises:



186
187
188
189
190
191
192
193
194
195
# File 'lib/ronin/repos/repository.rb', line 186

def checkout(branch_or_tag)
  Dir.chdir(@path) do
    case system('git','checkout',branch_or_tag)
    when nil
      raise(CommandNotInstalled,"git is not installed")
    when false
      raise(CommandFailed,"command failed: git checkout #{branch_or_tag}")
    end
  end
end

#deleteObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes the repository directory.



226
227
228
# File 'lib/ronin/repos/repository.rb', line 226

def delete
  FileUtils.rm_rf(@path)
end

#find_file(relative_path) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds a file within the repository.

Examples:

repo.find_file("wordlists/wordlist.txt")
# => "/home/user/.cache/ronin-repos/foo-repo/wordlists/wordlist.txt"

Parameters:

  • relative_path (String)

    The relative path of the file.

Returns:

  • (String, nil)

    The absolute path of the matching file or nil if no matching file could be found.



283
284
285
286
287
288
289
# File 'lib/ronin/repos/repository.rb', line 283

def find_file(relative_path)
  path = join(relative_path)

  if File.file?(path)
    return path
  end
end

#glob(pattern, &block) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds all files in the repository that matches the glob pattern.

Examples:

repo.glob("wordlists/*.txt")
# => ["/home/user/.cache/ronin-repos/foo-repo/wordlists/cities.txt",
#     "/home/user/.cache/ronin-repos/foo-repo/wordlists/states.txt"]

Parameters:

  • pattern (String)

    The file glob pattern to search for.

Returns:

  • (Array<String>)

    The absolute paths to the files that match the glob pattern.



305
306
307
308
309
310
311
312
# File 'lib/ronin/repos/repository.rb', line 305

def glob(pattern,&block)
  path    = join(pattern)
  matches = Dir.glob(path)

  if block then matches.each(&block)
  else          matches
  end
end

#has_directory?(relative_path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if the repository contains the directory.

Parameters:

  • relative_path (String)

    The relative path of the directory.

Returns:

  • (Boolean)

    Indicates whether the repository contains the directory or not.



265
266
267
# File 'lib/ronin/repos/repository.rb', line 265

def has_directory?(relative_path)
  File.directory?(join(relative_path))
end

#has_file?(relative_path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if the repository contains the file.

Parameters:

  • relative_path (String)

    The relative path of the file.

Returns:

  • (Boolean)

    Indicates whether the repository contains the file or not.



252
253
254
# File 'lib/ronin/repos/repository.rb', line 252

def has_file?(relative_path)
  File.file?(join(relative_path))
end

#join(relative_path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a relative path to an absolute path.

Parameters:

  • relative_path (String)

    The relative path of the file.

Returns:

  • (String)

    The absolute path with respect to the repository.



239
240
241
# File 'lib/ronin/repos/repository.rb', line 239

def join(relative_path)
  File.join(@path,relative_path)
end

#list_files(pattern = '{**/}*.*') ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lists the paths within the repository.

Examples:

repo.list_files('exploits/{**/}*.rb')
# => ["exploits/exploit1.rb", "exploits/exploit2.rb"]

Parameters:

  • pattern (String) (defaults to: '{**/}*.*')

    The optional glob pattern to use to list specific files.

Returns:

  • (Array<String>)

    The matching paths within the repository.



327
328
329
# File 'lib/ronin/repos/repository.rb', line 327

def list_files(pattern='{**/}*.*')
  Dir.glob(pattern, base: @path)
end

#pull(remote: 'origin', branch: nil, tags: nil) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Pulls down new git commits.

Parameters:

  • remote (String) (defaults to: 'origin')

    The git remote to pull from.

  • branch (String, nil) (defaults to: nil)

    The git branch to pull.

  • tags (Boolean) (defaults to: nil)

    Controls whether to pull git tags in addition to the git commits.

Returns:

  • (true)

    Indicates that the git command executed successfully.

Raises:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ronin/repos/repository.rb', line 158

def pull(remote: 'origin', branch: nil, tags: nil)
  args = []
  args << '--tags' if tags

  args << remote.to_s
  args << branch.to_s if branch

  Dir.chdir(@path) do
    case system('git','pull',*args)
    when nil
      raise(CommandNotInstalled,"git is not installed")
    when false
      raise(CommandFailed,"command failed: git pull #{args.join(' ')}")
    end
  end
end

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the repository to a String.

Returns:

  • (String)

    The name of the repository.



337
338
339
# File 'lib/ronin/repos/repository.rb', line 337

def to_s
  @name
end

#update(branch: nil, tag: nil, **kwargs) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Updates the repository.

Parameters:

  • branch (String, nil) (defaults to: nil)

    The optional git branch to update from.

  • tag (String, nil) (defaults to: nil)

    The optional git tag to update to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #pull.

Returns:

  • (true)

    Indicates that the git commands executed successfully.

Raises:



215
216
217
218
219
220
221
# File 'lib/ronin/repos/repository.rb', line 215

def update(branch: nil, tag: nil, **kwargs)
  pull(branch: branch, tags: branch.nil?, **kwargs)

  if branch || tag
    checkout(branch || tag)
  end
end