dockerfile_ast 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Dockerfile.sample ADDED
@@ -0,0 +1,21 @@
1
+ # this is a comment
2
+ FROM jeremiahishere/sample
3
+
4
+ MAINTAINER Jeremiah Hemphill <jeremiah@cloudspace.com>
5
+
6
+ RUN echo 'hello world'
7
+ RUN['echo', 'hello world']
8
+
9
+ CMD command param1 param2
10
+
11
+ EXPOSE 21
12
+ ENV APP_ENV production
13
+
14
+ ADD source_file.sh target_file.sh
15
+ ENTRYPOINT command param1 param2
16
+
17
+ USER daemon
18
+ VOLUME ["/data"]
19
+ WORKDIR /path/to/workdir
20
+
21
+ ONBUILD RUN echo 'hello'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dockerfile_ast.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jeremiah Hemphill
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # DockerfileAst
2
+
3
+ Convert a Dockerfile into a very basic ast, then output it as an array or string
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'dockerfile_ast'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dockerfile_ast
18
+
19
+ ## Usage
20
+
21
+ rake parse DOCKERFILE=/path/to/dockerfile
22
+
23
+ Edit DockerfileAst::Parser's parse method to return an approximation of the AST with to_array or a dockerfile with to_s.
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( http://github.com/<my-github-username>/dockerfile_ast/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'dockerfile_ast'
3
+
4
+ task :parse do
5
+ parser = DockerfileAst::Parser.new
6
+ filename = ENV.has_key?('DOCKERFILE') ? ENV['DOCKERFILE'] : 'Dockerfile.sample'
7
+ dockerfile = File.read(filename)
8
+ ast = parser.parse(dockerfile)
9
+ writer = DockerfileAst::Writer.new(ast)
10
+ puts writer.write(:string)
11
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dockerfile_ast/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dockerfile_ast"
8
+ spec.version = DockerfileAst::VERSION
9
+ spec.authors = ["Jeremiah Hemphill"]
10
+ spec.email = ["jeremiah@cloudspace.com"]
11
+ spec.summary = %q{Convert a Dockerfile into a basic AST}
12
+ spec.description = %q{Parse a Dockerfile, the write the parsed data back to a file.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency 'treetop'
25
+ end
@@ -0,0 +1,12 @@
1
+ require "dockerfile_ast/version"
2
+
3
+ require 'treetop'
4
+
5
+ require 'dockerfile_ast/nodes/named_nodes'
6
+ require 'dockerfile_ast/nodes/string_literal'
7
+ require 'dockerfile_ast/parser'
8
+ require 'dockerfile_ast/writer'
9
+
10
+ module DockerfileAst
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,87 @@
1
+ # TODO: create a parameter system the accepts a list of parameters delimited by spaces, or string in an array
2
+ grammar DockerfileGrammar
3
+ rule body
4
+ ( instruction / comment / end_of_line )* <DockerfileAst::Node::Dockerfile>
5
+ end
6
+
7
+ rule comment
8
+ space? '#' space? rest_of_line <DockerfileAst::Node::Comment>
9
+ end
10
+
11
+ rule instruction
12
+ space? instruction_type space? end_of_line? <DockerfileAst::Node::Instruction>
13
+ end
14
+
15
+ # INSTRUCTIONS
16
+
17
+ rule instruction_type
18
+ from / maintainer / run / cmd / expose / env / add / entrypoint / volume / user / workdir / onbuild
19
+ end
20
+
21
+ rule add
22
+ 'ADD' space? rest_of_line <DockerfileAst::Node::AddInstruction>
23
+ end
24
+
25
+ rule cmd
26
+ 'CMD' space? rest_of_line <DockerfileAst::Node::CmdInstruction>
27
+ end
28
+
29
+ rule entrypoint
30
+ 'ENTRYPOINT' space? rest_of_line <DockerfileAst::Node::EntrypointInstruction>
31
+ end
32
+
33
+ rule env
34
+ 'ENV' space? rest_of_line <DockerfileAst::Node::ExposeInstruction>
35
+ end
36
+
37
+ rule expose
38
+ 'EXPOSE' space? rest_of_line <DockerfileAst::Node::ExposeInstruction>
39
+ end
40
+
41
+ rule from
42
+ 'FROM' space? string_without_spaces <DockerfileAst::Node::FromInstruction>
43
+ end
44
+
45
+ rule maintainer
46
+ 'MAINTAINER' space? rest_of_line <DockerfileAst::Node::MaintainerInstruction>
47
+ end
48
+
49
+ rule onbuild
50
+ 'ONBUILD' space? instruction <DockerfileAst::Node::OnbuildInstruction>
51
+ end
52
+
53
+ rule run
54
+ 'RUN' space? rest_of_line <DockerfileAst::Node::RunInstruction>
55
+ end
56
+
57
+ rule user
58
+ 'USER' space? rest_of_line <DockerfileAst::Node::UserInstruction>
59
+ end
60
+
61
+ rule volume
62
+ 'VOLUME' space? rest_of_line <DockerfileAst::Node::VolumeInstruction>
63
+ end
64
+
65
+ rule workdir
66
+ 'WORKDIR' space? rest_of_line <DockerfileAst::Node::WorkdirInstruction>
67
+ end
68
+
69
+ # END INSTRUCTIONS
70
+
71
+ rule string_without_spaces
72
+ [\S]* <DockerfileAst::Node::StringLiteral>
73
+ end
74
+
75
+ rule rest_of_line
76
+ [^\n]* <DockerfileAst::Node::StringLiteral>
77
+ end
78
+
79
+ rule end_of_line
80
+ [\n]+
81
+ end
82
+
83
+ # allow whitespace anywhere but match it last
84
+ rule space
85
+ [\s]+
86
+ end
87
+ end
@@ -0,0 +1,96 @@
1
+ module DockerfileAst
2
+ module Node
3
+ class NamedNode < Treetop::Runtime::SyntaxNode
4
+ def title
5
+ :named_node
6
+ end
7
+
8
+ def to_array
9
+ [title] + elements.map(&:to_array)
10
+ end
11
+
12
+ def to_s
13
+ "#{title.to_s.upcase} #{elements_to_s}"
14
+ end
15
+
16
+ def elements_to_s
17
+ elements.map(&:to_s).join("\n")
18
+ end
19
+ end
20
+
21
+ class Instruction < NamedNode
22
+ def title; :instruction end
23
+
24
+ def to_s
25
+ elements_to_s
26
+ end
27
+ end
28
+
29
+ class Comment < NamedNode
30
+ def title; :comment end
31
+
32
+ def to_s
33
+ "# #{elements[0].to_s}"
34
+ end
35
+ end
36
+
37
+ class Dockerfile < NamedNode
38
+ def title; :dockerfile end
39
+
40
+ def to_s
41
+ elements_to_s
42
+ end
43
+ end
44
+
45
+
46
+ # INSTRUCTIONS
47
+
48
+ class AddInstruction < NamedNode
49
+ def title; :add end
50
+ end
51
+
52
+ class CmdInstruction < NamedNode
53
+ def title; :cmd end
54
+ end
55
+
56
+ class EntrypointInstruction < NamedNode
57
+ def title; :entrypoint end
58
+ end
59
+
60
+ class EnvInstruction < NamedNode
61
+ def title; :env end
62
+ end
63
+
64
+ class ExposeInstruction < NamedNode
65
+ def title; :expose end
66
+ end
67
+
68
+ class FromInstruction < NamedNode
69
+ def title; :from end
70
+ end
71
+
72
+ class MaintainerInstruction < NamedNode
73
+ def title; :maintainer end
74
+ end
75
+
76
+ class OnbuildInstruction < NamedNode
77
+ def title; :onbuild end
78
+ end
79
+
80
+ class RunInstruction < NamedNode
81
+ def title; :run end
82
+ end
83
+
84
+ class UserInstruction < NamedNode
85
+ def title; :user end
86
+ end
87
+
88
+ class VolumeInstruction < NamedNode
89
+ def title; :volume end
90
+ end
91
+
92
+ class WorkdirInstruction < NamedNode
93
+ def title; :workdir end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,13 @@
1
+ module DockerfileAst
2
+ module Node
3
+ class StringLiteral < Treetop::Runtime::SyntaxNode
4
+ def to_array
5
+ self.text_value
6
+ end
7
+
8
+ def to_s
9
+ self.text_value
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ module DockerfileAst
2
+ class Parser
3
+
4
+ def initialize
5
+ Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'dockerfile_grammar.treetop')))
6
+ @parser = DockerfileGrammarParser.new
7
+ end
8
+
9
+ def parse(data, return_type = :string)
10
+ tree = @parser.parse(data)
11
+
12
+ if(tree.nil?)
13
+ raise Exception, "Parse error at offset: #{@parser.index} #{@parser.failure_reason}"
14
+ end
15
+
16
+ # this edits the tree in place
17
+ clean_tree(tree)
18
+
19
+ return tree
20
+ end
21
+
22
+ def clean_tree(root_node)
23
+ return if(root_node.elements.nil?)
24
+ root_node.elements.delete_if{|node| node.class.name == "Treetop::Runtime::SyntaxNode" }
25
+ root_node.elements.each {|node| self.clean_tree(node) }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module DockerfileAst
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ module DockerfileAst
2
+ class Writer
3
+ def initialize(tree)
4
+ @tree = tree
5
+ end
6
+
7
+ def write(return_type = :string)
8
+ if return_type == :tree
9
+ return @tree
10
+ elsif return_type == :array
11
+ return @tree.to_array
12
+ else
13
+ return @tree.to_s
14
+ end
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dockerfile_ast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremiah Hemphill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: treetop
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Parse a Dockerfile, the write the parsed data back to a file.
63
+ email:
64
+ - jeremiah@cloudspace.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Dockerfile.sample
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - dockerfile_ast.gemspec
76
+ - lib/dockerfile_ast.rb
77
+ - lib/dockerfile_ast/dockerfile_grammar.treetop
78
+ - lib/dockerfile_ast/nodes/named_nodes.rb
79
+ - lib/dockerfile_ast/nodes/string_literal.rb
80
+ - lib/dockerfile_ast/parser.rb
81
+ - lib/dockerfile_ast/version.rb
82
+ - lib/dockerfile_ast/writer.rb
83
+ homepage: ''
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.25
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Convert a Dockerfile into a basic AST
108
+ test_files: []