Module: Bits

Defined in:
lib/bits.rb,
lib/bits/bit.rb,
lib/bits/user.rb,
lib/bits/spawn.rb,
lib/bits/backend.rb,
lib/bits/command.rb,
lib/bits/logging.rb,
lib/bits/package.rb,
lib/bits/version.rb,
lib/bits/manifest.rb,
lib/bits/provider.rb,
lib/bits/bit_reader.rb,
lib/bits/exceptions.rb,
lib/bits/repository.rb,
lib/bits/backend/join.rb,
lib/bits/provider/apt.rb,
lib/bits/backend/local.rb,
lib/bits/commands/show.rb,
lib/bits/commands/sync.rb,
lib/bits/package_proxy.rb,
lib/bits/commands/setup.rb,
lib/bits/bit_declaration.rb,
lib/bits/commands/remove.rb,
lib/bits/execute_context.rb,
lib/bits/provider/python.rb,
lib/bits/bit_reader/local.rb,
lib/bits/command_provider.rb,
lib/bits/commands/install.rb,
lib/bits/provider/portage.rb,
lib/bits/commands/manifest.rb,
lib/bits/provider/homebrew.rb,
lib/bits/provider/rubygems.rb,
lib/bits/external_interface.rb,
lib/bits/provider_reporting.rb,
lib/bits/commands/provider_sync.rb,
lib/bits/commands/provider_query.rb

Defined Under Namespace

Modules: Cache, CommandProvider, ExternalInterface, InstallerMixin, Logging, ProviderReporting Classes: Backend, Bit, BitDeclaration, BitDependency, BitParameters, BitReader, BitReaderLocal, BitReference, Command, CommandException, ExecuteContext, InvalidArgument, JoinBackend, LocalBackend, Manifest, MissingBit, MissingDependencies, MissingPackage, MissingProvidedPackage, PPP, Package, PackageProxy, PosixUser, Provider, ProviderException, Repository, User

Constant Summary collapse

DEFAULT_COMMAND =
'manifest'
ENOENT =
0x7f
PIPE =
0x01
NULL =
0x02
DEV_NULL =
'/dev/null'
VERSION =
'0.3.3'

Class Method Summary collapse

Class Method Details

.commandsObject



22
23
24
# File 'lib/bits/command.rb', line 22

def commands
  @commands ||= {}
end

.define_command(command_id, params = {}, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bits/command.rb', line 26

def define_command(command_id, params={}, &block)
  if commands[command_id]
    raise "Command already defined: #{command_id}" 
  end

  desc = params[:desc] || "(no description)"
  switch = params[:switch] || command_id.to_s

  klass = Class.new(Command) do
    @command_id = command_id
    @name = command_id.to_s.capitalize
    @desc = desc
    @switch = switch

    def switch
      self.class.switch
    end

    def to_s
      self.class.to_s
    end

    class << self
      attr_reader :command_id, :name, :desc, :switch

      def to_s
        "Bits::Command::#{@name}"
      end
    end
  end

  klass.class_eval(&block)

  commands[command_id] = klass
end

.define_provider(provider_id, params = {}, &block) ⇒ Object



51
52
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
# File 'lib/bits/provider.rb', line 51

def define_provider(provider_id, params={}, &block)
  if providers[provider_id]
    raise "Provider already defined: #{provider_id}"
  end

  desc = params[:desc] || "(no description)"

  klass = Class.new Provider do
    @provider_id = provider_id
    @name = provider_id.to_s.capitalize
    @desc = desc

    def to_s
      self.class.to_s
    end

    def provider_id
      self.class.provider_id
    end

    class << self
      attr_reader :provider_id, :desc, :name

      def to_s
        "Bits::Provider::#{@name}"
      end
    end
  end

  klass.class_eval(&block)

  providers[provider_id] = klass
end

.handle_child_file(type, global, fds) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/bits/spawn.rb', line 39

def handle_child_file(type, global, fds)
  case type
  when PIPE then
    fds[0].close
    global.reopen fds[1]
  else
    global.reopen fds unless fds === global
  end
end

.handle_exit(pid, fd_cache, ignore_exitcode) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bits/spawn.rb', line 11

def handle_exit(pid, fd_cache, ignore_exitcode)
  fd_cache.each do |key, fd|
    fd.close
  end

  Process.waitpid pid

  exitstatus = $?.exitstatus

  if exitstatus == ENOENT then
    raise Errno::ENOENT
  end

  if not ignore_exitcode and exitstatus != 0 then
    raise "Bad exit status: #{exitstatus}"
  end

  exitstatus
end

.handle_parent_file(type, fds) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/bits/spawn.rb', line 49

def handle_parent_file(type, fds)
  case type
  when PIPE then
    fds[1].close
    fds[0]
  else fds
  end
end

.main(args) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bits.rb', line 145

def main(args)
  @log = setup_logging

  args, command, command_parser = parse_options(args)

  begin
    command.entry args
  rescue InvalidArgument => e
    $stderr.puts "Argument Error: #{e}"
    $stderr.puts command_parser.help
    return 1
  ensure
    Bits::ExternalInterface.close_interfaces
  end

  return 0
end

.parse_options(args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bits.rb', line 30

def parse_options(args)
  ns = {}

  avail_commands = Bits.commands.values.sort_by{|c| c.command_id.to_s}
  avail_providers = Bits.providers.values.sort_by{|p| p.provider_id.to_s}

  commands = Hash.new

  global = OptionParser.new do |global_opts|
    global_opts.banner = "Usage: bits <command> [options]"

    global_opts.on("-d", "--debug", "Enable debug logging") do |v|
      @log.level = Log4r::DEBUG
    end

    global_opts.separator ""
    global_opts.separator "Available commands:"

    avail_commands.each do |klass|
      global_opts.separator "  #{klass.switch}: #{klass.desc}"
      commands[klass.switch] = klass
    end

    global_opts.separator ""

    global_opts.separator "Providers:"

    avail_providers.each do |klass|
      global_opts.separator "  #{klass.provider_id}: #{klass.desc}"
    end
  end

  global.order!
  command = ARGV.shift

  checked_providers = avail_providers.select do |klass|
    @log.debug "Checking provider: #{klass.provider_id}"
    klass.check
  end

  command = DEFAULT_COMMAND if command.nil?

  if command.nil? then
    $stderr.puts global.help
    exit 0
  end

  if commands[command].nil? then
    $stderr.puts "No such command: #{command}"
    $stderr.puts global.help
    exit 0
  end

  bits_dir = setup_bits_dir
  repo_dir = setup_repo_dir bits_dir
  backend = setup_backend repo_dir

  ns[:user] = setup_user
  ns[:bits_dir] = bits_dir
  ns[:repo_dir] = repo_dir

  providers = checked_providers.collect do |klass|
    provider = klass.new ns
    provider.setup
    provider
  end

  repository = Bits::Repository.new(providers, backend)

  ns[:providers] = providers
  ns[:repository] = repository

  command_klass = commands[command]
  command = command_klass.new ns

  command_parser = OptionParser.new do |opts|
    command.setup opts
  end

  command_parser.order!

  return ARGV, command, command_parser
end

.providersObject



47
48
49
# File 'lib/bits/provider.rb', line 47

def providers
  @providers ||= {}
end

.setup_backend(local_dir) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/bits.rb', line 134

def setup_backend(local_dir)
  cwd_dir = File.join Dir.pwd, 'bits'

  backends = Array.new

  backends << LocalBackend.new(cwd_dir) if File.directory? cwd_dir
  backends << LocalBackend.new(local_dir) if File.directory? local_dir

  JoinBackend.new backends
end

.setup_bits_dirObject



114
115
116
117
118
119
120
# File 'lib/bits.rb', line 114

def setup_bits_dir
  home = ENV['HOME']
  raise "HOME environment variable not defined" if home.nil?
  bits_dir = File.join home, '.bits'
  FileUtils.mkdir_p bits_dir unless File.directory? bits_dir
  bits_dir
end

.setup_file(type, global, fd_cache) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/bits/spawn.rb', line 31

def setup_file(type, global, fd_cache)
  case type
    when PIPE then IO.pipe
    when NULL then (fd_cache[:null] ||= File.open(DEV_NULL, 'w'))
    else type
  end
end

.setup_loggingObject



127
128
129
130
131
132
# File 'lib/bits.rb', line 127

def setup_logging
  log = Log4r::Logger.new 'Bits'
  log.outputters << Log4r::Outputter.stdout
  log.level = Log4r::INFO
  log
end

.setup_repo_dir(bits_dir) ⇒ Object

Setup the path to the local repository directory.



123
124
125
# File 'lib/bits.rb', line 123

def setup_repo_dir(bits_dir)
  File.join bits_dir, 'bits'
end

.setup_userObject



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

def self.setup_user
  if RUBY_PLATFORM.include? 'darwin'
    kind = PosixUser
  elsif RUBY_PLATFORM.include? 'linux'
    kind = PosixUser
  elsif RUBY_PLATFORM.include? 'win32'
    kind = Win32User
  else
    raise "Unsupported platform: #{RUBY_PLATFORM}"
  end

  kind.new
end

.spawn(args, params = {}) {|[out, err]| ... } ⇒ Object

Yields:

  • ([out, err])


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
86
87
# File 'lib/bits/spawn.rb', line 58

def spawn(args, params={})
  fd_cache = {}

  stdout = (params[:stdout] || $stdout)
  stderr = (params[:stderr] || $stderr)
  ignore_exitcode = params[:ignore_exitcode] || false

  out = setup_file stdout, $stdout, fd_cache
  err = setup_file stderr, $stderr, fd_cache

  pid = fork do
    handle_child_file stdout, $stdout, out
    handle_child_file stderr, $stderr, err

    begin
      exec(*args)
    rescue Errno::ENOENT
      exit ENOENT
    rescue
      exit 1
    end
  end

  out = handle_parent_file stdout, out
  err = handle_parent_file stderr, err

  return handle_exit pid, fd_cache, ignore_exitcode unless block_given?
  yield [out, err]
  handle_exit pid, fd_cache, ignore_exitcode
end