fig 0.1.51 → 0.1.52

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,11 +2,12 @@ require 'stringio'
2
2
 
3
3
  require 'fig/backtrace'
4
4
  require 'fig/logging'
5
- require 'fig/package/command'
6
- require 'fig/package/include'
7
- require 'fig/package/path'
8
- require 'fig/package/set'
5
+ require 'fig/package'
9
6
  require 'fig/repositoryerror'
7
+ require 'fig/statement/command'
8
+ require 'fig/statement/include'
9
+ require 'fig/statement/path'
10
+ require 'fig/statement/set'
10
11
  require 'fig/userinputerror'
11
12
 
12
13
  module Fig
@@ -16,56 +17,73 @@ module Fig
16
17
  class Environment
17
18
  DEFAULT_VERSION_NAME = 'current'
18
19
 
19
- def initialize(os, repository, variables_override, retriever)
20
- @os = os
20
+ def initialize(repository, variables_override, retriever)
21
21
  @repository = repository
22
22
  @variables = variables_override || get_environment_variables
23
23
  @retrieve_vars = {}
24
24
  @packages = {}
25
- @applied_configs = {}
26
25
  @retriever = retriever
27
26
  end
28
27
 
29
28
  def get_environment_variables
30
29
  vars = {}
31
30
  ENV.each { |key,value| vars[key]=value }
31
+
32
32
  return vars
33
33
  end
34
34
 
35
35
  # Returns the value of an envirionment variable
36
36
  def [](name)
37
- @variables[name]
37
+ return @variables[name]
38
38
  end
39
39
 
40
40
  # Indicates that the values from a particular envrionment variable path
41
41
  def add_retrieve(name, path)
42
42
  @retrieve_vars[name] = path
43
+
44
+ return
43
45
  end
44
46
 
45
47
  def register_package(package)
46
48
  name = package.package_name
47
- if @packages[name]
49
+
50
+ if get_package(name)
48
51
  Logging.fatal %Q<There is already a package with the name "#{name}".>
49
52
  raise RepositoryError.new
50
53
  end
54
+
51
55
  @packages[name] = package
56
+
57
+ return
58
+ end
59
+
60
+ def get_package(package_name)
61
+ return @packages[package_name]
62
+ end
63
+
64
+ def packages
65
+ return @packages.values
52
66
  end
53
67
 
54
68
  def apply_config(package, config_name, backtrace)
55
- if (@applied_configs[package.package_name] ||= []).member?(config_name)
69
+ if package.applied_config_names.member?(config_name)
56
70
  return
57
71
  end
58
72
  new_backtrace = backtrace
59
73
 
60
74
  config = package[config_name]
61
75
  config.statements.each { |stmt| apply_config_statement(package, stmt, new_backtrace) }
62
- @applied_configs[package.package_name] << config_name
76
+ package.add_applied_config_name(config_name)
77
+
78
+ return
63
79
  end
64
80
 
65
81
  def execute_shell(command)
66
82
  with_environment do
67
83
  yield command.map{|arg| expand_command_line_argument(arg)}
68
84
  end
85
+
86
+ return
69
87
  end
70
88
 
71
89
  def execute_command(command, args, package)
@@ -77,10 +95,17 @@ module Fig
77
95
 
78
96
  yield expand_path(argument, package).split(' ')
79
97
  end
98
+
99
+ return
80
100
  end
81
101
 
82
102
  def find_config_name_in_package(package_name)
83
- return @applied_configs.key?(package_name) ? @applied_configs[package_name].first : 'default'
103
+ package = get_package(package_name)
104
+ if not package
105
+ return Package::DEFAULT_CONFIG
106
+ end
107
+
108
+ return package.primary_config_name || Package::DEFAULT_CONFIG
84
109
  end
85
110
 
86
111
  def execute_config(base_package, package_name, config_name, version_name, args, &block)
@@ -97,21 +122,25 @@ module Fig
97
122
  else
98
123
  raise UserInputError.new(%Q<The "#{package.to_s}" package with the "#{config_name}" configuration does not contain a command.>)
99
124
  end
125
+
126
+ return
100
127
  end
101
128
 
102
129
  def apply_config_statement(base_package, statement, backtrace)
103
130
  case statement
104
- when Package::Path
131
+ when Statement::Path
105
132
  append_variable(base_package, statement.name, statement.value)
106
- when Package::Set
133
+ when Statement::Set
107
134
  set_variable(base_package, statement.name, statement.value)
108
- when Package::Include
135
+ when Statement::Include
109
136
  include_config(base_package, statement.package_name, statement.config_name, statement.version_name, statement.overrides, backtrace)
110
- when Package::Command
137
+ when Statement::Command
111
138
  # ignore
112
139
  else
113
140
  fail "Unexpected statement: #{statement}"
114
141
  end
142
+
143
+ return
115
144
  end
116
145
 
117
146
  def include_config(base_package, package_name, config_name, version_name, overrides, backtrace)
@@ -126,14 +155,22 @@ module Fig
126
155
  overrides.each do |override|
127
156
  new_backtrace.add_override(override.package_name, override.version_name)
128
157
  end
129
- package = lookup_package(package_name || base_package.package_name, version_name, new_backtrace)
130
- apply_config(package, config_name || 'default', new_backtrace)
158
+ package = lookup_package(
159
+ package_name || base_package.package_name, version_name, new_backtrace
160
+ )
161
+ apply_config(
162
+ package, config_name || Package::DEFAULT_CONFIG, new_backtrace
163
+ )
164
+
165
+ return
131
166
  end
132
167
 
133
168
  private
134
169
 
135
170
  def set_variable(base_package, name, value)
136
171
  @variables[name] = expand_and_retrieve_variable_value(base_package, name, value)
172
+
173
+ return
137
174
  end
138
175
 
139
176
  def append_variable(base_package, name, value)
@@ -153,6 +190,8 @@ module Fig
153
190
  else
154
191
  @variables[name] = value
155
192
  end
193
+
194
+ return
156
195
  end
157
196
 
158
197
  def with_environment
@@ -163,12 +202,19 @@ module Fig
163
202
  ensure
164
203
  old_env.each { |key,value| ENV[key] = value }
165
204
  end
205
+
206
+ return
166
207
  end
167
208
 
168
209
  def lookup_package(package_name, version_name, backtrace)
169
- package = @packages[package_name]
210
+ package = get_package(package_name)
170
211
  if package.nil?
171
- package = @repository.load_package(package_name, version_name || DEFAULT_VERSION_NAME)
212
+ if not version_name
213
+ Logging.fatal "No version specified for #{package_name}."
214
+ raise RepositoryError.new
215
+ end
216
+
217
+ package = @repository.get_package(package_name, version_name)
172
218
  package.backtrace = backtrace
173
219
  @packages[package_name] = package
174
220
  elsif version_name && version_name != package.version_name
@@ -181,7 +227,8 @@ module Fig
181
227
  + ( stacktrace.empty? ? '' : "\n#{stacktrace}" )
182
228
  raise RepositoryError.new
183
229
  end
184
- package
230
+
231
+ return package
185
232
  end
186
233
 
187
234
  # Replace @ symbol with the package's directory, "[package]" with the
@@ -215,12 +262,14 @@ module Fig
215
262
  end
216
263
  file = target
217
264
  end
265
+
218
266
  return file
219
267
  end
220
268
 
221
269
  def expand_path(path, base_package)
222
270
  expanded_path = expand_at_sign_package_references(path, base_package)
223
271
  check_for_bad_escape(expanded_path, path)
272
+
224
273
  return expanded_path.gsub(%r< \\ ([\\@]) >x, '\1')
225
274
  end
226
275
 
@@ -235,6 +284,8 @@ module Fig
235
284
  backslashes = $1 || ''
236
285
  backslashes + base_package.directory
237
286
  end
287
+
288
+ return
238
289
  end
239
290
 
240
291
  def expand_command_line_argument(arg)
@@ -254,9 +305,9 @@ module Fig
254
305
  >x
255
306
  ) do |match|
256
307
  backslashes = $1 || ''
257
- package = @packages[$2]
308
+ package = get_package($2)
258
309
  if package.nil?
259
- raise RepositoryError.new "Package not found: #{$1}"
310
+ raise RepositoryError.new("Package not found: #{$1}")
260
311
  end
261
312
  backslashes + package.directory
262
313
  end
@@ -274,6 +325,8 @@ module Fig
274
325
  %Q<Unknown escape "#{$1}" in "#{original}">
275
326
  )
276
327
  end
328
+
329
+ return
277
330
  end
278
331
 
279
332
  def translate_retrieve_variables(base_package, name)
@@ -2,7 +2,7 @@ require 'json'
2
2
 
3
3
  require 'fig/applicationconfiguration'
4
4
  require 'fig/configfileerror'
5
- require 'fig/os'
5
+ require 'fig/operatingsystem'
6
6
 
7
7
  REPOSITORY_CONFIGURATION = '_meta/figrc'
8
8
 
@@ -64,7 +64,7 @@ module Fig
64
64
  repo_figrc_path =
65
65
  File.expand_path(File.join(fig_home, REPOSITORY_CONFIGURATION))
66
66
 
67
- os = OS.new(login)
67
+ os = OperatingSystem.new(login)
68
68
 
69
69
  repo_config_exists = nil
70
70
  begin
@@ -1,15 +1,15 @@
1
1
  require 'fig/package'
2
- require 'fig/package/archive'
3
- require 'fig/package/command'
4
- require 'fig/package/configuration'
5
- require 'fig/package/include'
6
- require 'fig/package/install'
7
- require 'fig/package/override'
8
- require 'fig/package/path'
9
- require 'fig/package/publish'
10
- require 'fig/package/resource'
11
- require 'fig/package/retrieve'
12
- require 'fig/package/set'
2
+ require 'fig/packagedescriptor'
3
+ require 'fig/statement/archive'
4
+ require 'fig/statement/command'
5
+ require 'fig/statement/configuration'
6
+ require 'fig/statement/include'
7
+ require 'fig/statement/override'
8
+ require 'fig/statement/path'
9
+ require 'fig/statement/publish'
10
+ require 'fig/statement/resource'
11
+ require 'fig/statement/retrieve'
12
+ require 'fig/statement/set'
13
13
 
14
14
  module Fig
15
15
 
@@ -34,7 +34,7 @@ grammar Fig
34
34
  rule archive
35
35
  "archive" ws url {
36
36
  def to_package_statement
37
- Package::Archive.new(url.value.text_value)
37
+ Statement::Archive.new(url.value.text_value)
38
38
  end
39
39
  }
40
40
  end
@@ -42,7 +42,7 @@ grammar Fig
42
42
  rule resource
43
43
  "resource" ws url {
44
44
  def to_package_statement
45
- Package::Resource.new(url.value.text_value)
45
+ Statement::Resource.new(url.value.text_value)
46
46
  end
47
47
  }
48
48
  end
@@ -50,7 +50,7 @@ grammar Fig
50
50
  rule retrieve
51
51
  "retrieve" ws var:retrieve_variable "->" path:retrieve_path ws {
52
52
  def to_package_statement
53
- Package::Retrieve.new(var.text_value, path.text_value)
53
+ Statement::Retrieve.new(var.text_value, path.text_value)
54
54
  end
55
55
  }
56
56
  end
@@ -63,18 +63,10 @@ grammar Fig
63
63
  [a-zA-Z0-9_/.\[\]-]+
64
64
  end
65
65
 
66
- rule install
67
- "install" ws statements:config_statement* "end" ws {
68
- def to_package_statement
69
- Package::Install.new(statements.elements.map { |statement| statement.to_config_statement })
70
- end
71
- }
72
- end
73
-
74
66
  rule config
75
67
  "config" ws config_name ws statements:config_statement* "end" ws {
76
68
  def to_package_statement
77
- Package::Configuration.new(config_name.text_value, statements.elements.map { |statement| statement.to_config_statement })
69
+ Statement::Configuration.new(config_name.text_value, statements.elements.map { |statement| statement.to_config_statement })
78
70
  end
79
71
  }
80
72
  end
@@ -86,10 +78,10 @@ grammar Fig
86
78
  rule include
87
79
  "include" ws descriptor overrides:(override*) {
88
80
  def to_config_statement
89
- package = descriptor.respond_to?(:package) ? descriptor.package.text_value : nil
90
- config = descriptor.get_config
91
- version = descriptor.get_version
92
- Package::Include.new(package, config, version, overrides.elements.map{ |e| e.to_override })
81
+ Statement::Include.new(
82
+ PackageDescriptor.parse(descriptor.text_value.strip),
83
+ overrides.elements.map{ |e| e.to_override }
84
+ )
93
85
  end
94
86
  }
95
87
  end
@@ -97,7 +89,7 @@ grammar Fig
97
89
  rule override
98
90
  "override" ws package_name "/" version_name ws {
99
91
  def to_override
100
- return Package::Override.new(package_name.text_value, version_name.text_value)
92
+ return Statement::Override.new(package_name.text_value, version_name.text_value)
101
93
  end
102
94
  }
103
95
  end
@@ -105,7 +97,7 @@ grammar Fig
105
97
  rule path
106
98
  ("append" / "path" / "add") ws name:path_name "=" value:path_value ws {
107
99
  def to_config_statement
108
- Package::Path.new(name.text_value, value.text_value)
100
+ Statement::Path.new(name.text_value, value.text_value)
109
101
  end
110
102
  }
111
103
  end
@@ -121,7 +113,7 @@ grammar Fig
121
113
  rule set
122
114
  "set" ws name:set_name "=" value:set_value ws {
123
115
  def to_config_statement
124
- Package::Set.new(name.text_value, value.text_value)
116
+ Statement::Set.new(name.text_value, value.text_value)
125
117
  end
126
118
  }
127
119
  end
@@ -137,7 +129,7 @@ grammar Fig
137
129
  rule command
138
130
  "command" ws string {
139
131
  def to_config_statement
140
- Package::Command.new(string.value.text_value)
132
+ Statement::Command.new(string.value.text_value)
141
133
  end
142
134
  }
143
135
  end
@@ -1,13 +1,15 @@
1
1
  require 'fileutils'
2
2
  # Must specify absolute path of ::Archive when using
3
- # this module to avoid conflicts with Fig::Package::Archive
3
+ # this module to avoid conflicts with Fig::Statement::Archive
4
4
  require 'libarchive_ruby' unless RUBY_PLATFORM == 'java'
5
- require 'uri'
6
5
  require 'net/http'
7
6
  require 'net/ssh'
8
7
  require 'net/sftp'
9
8
  require 'net/netrc'
9
+ require 'rbconfig'
10
10
  require 'tempfile'
11
+ require 'uri'
12
+
11
13
  require 'highline/import'
12
14
 
13
15
  require 'fig/logging'
@@ -17,7 +19,7 @@ require 'fig/notfounderror'
17
19
  module Fig
18
20
  # Does things requiring real O/S interaction, primarilly taking care of file
19
21
  # transfers and running external commands.
20
- class OS
22
+ class OperatingSystem
21
23
  def initialize(login)
22
24
  @login = login
23
25
  @username = ENV['FIG_USERNAME']
@@ -77,7 +79,7 @@ module Fig
77
79
  uri = URI.parse(url)
78
80
  rescue
79
81
  Logging.fatal %Q<Unable to parse url: "#{url}">
80
- raise NetworkError.new(%Q<Unable to parse url: "#{url}">)
82
+ raise NetworkError.new
81
83
  end
82
84
  case uri.scheme
83
85
  when 'ftp'
@@ -184,7 +186,7 @@ module Fig
184
186
  begin
185
187
  FileUtils.cp(uri.path, path)
186
188
  return true
187
- rescue Errno::ENOENT => e
189
+ rescue Errno::ENOENT
188
190
  raise NotFoundError.new
189
191
  end
190
192
  else
@@ -292,7 +294,7 @@ module Fig
292
294
 
293
295
  # Expects files_to_archive as an Array of filenames.
294
296
  def create_archive(archive_name, files_to_archive)
295
- if OS.java?
297
+ if OperatingSystem.java?
296
298
  `tar czvf #{archive_name} #{files_to_archive.join(' ')}`
297
299
  else
298
300
  # TODO: Need to verify files_to_archive exists.
@@ -318,7 +320,7 @@ module Fig
318
320
  # .zip
319
321
  def unpack_archive(dir, file)
320
322
  Dir.chdir(dir) do
321
- if OS.java?
323
+ if OperatingSystem.java?
322
324
  `tar xzvf #{file}`
323
325
  else
324
326
  ::Archive.read_open_filename(file) do |ar|
@@ -331,7 +333,7 @@ module Fig
331
333
  end
332
334
 
333
335
  def self.windows?
334
- Config::CONFIG['host_os'] =~ /mswin|mingw/
336
+ RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
335
337
  end
336
338
 
337
339
  def self.java?
@@ -343,7 +345,7 @@ module Fig
343
345
  end
344
346
 
345
347
  def shell_exec(cmd)
346
- if OS.windows?
348
+ if OperatingSystem.windows?
347
349
  Windows.shell_exec_windows(cmd)
348
350
  else
349
351
  shell_exec_unix(cmd)