mtree 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e07ec004e1b016ffad3ce93f09d80009859db4d73afbc6aa0657514a032d1a55
4
+ data.tar.gz: d33eea6de1e3fc2934d404d0d7b684f78b5c99e0ee63d4a98326429320e9d05e
5
+ SHA512:
6
+ metadata.gz: 3f26621da0b4658b23199632b7722d3b62fc5375933f82679c86092867e9bf89158f053bdae016ba4ff822c39aa182ac149a03a5a3691843f9430b84ed27c5d9
7
+ data.tar.gz: 61048a3f80af78a046aac04b0ef2886c13ad9a933e8d4b5e3aee50b4027f670163777ef5d6f96380e5e67d7584aadff4a2f0521020d67bd624fb1102ac037faa
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ /parser.output
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'lib/mtree/parser.tab.rb'
4
+
5
+ Metrics/LineLength:
6
+ Enabled: false
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - 'spec/**/*.rb'
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Style/TrailingCommaInHashLiteral:
16
+ Enabled: false
@@ -0,0 +1,9 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3
7
+ - 2.4
8
+ - 2.5
9
+ before_install: gem install bundler -v 1.16.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at romain@opus-codium.fr. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mtree.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Romain Tartière
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # Mtree
2
+
3
+ [![Build Status](https://travis-ci.com/opus-codium/mtree.svg?branch=master)](https://travis-ci.com/opus-codium/mtree)
4
+
5
+ This gem allows mtree styled hierarchy specifications — as found in the BSD world — to be parsed and updated from a Ruby program. It also provide a `mtree` command with is backward compatible with [FreeBSD's `mtree(1)`](https://www.freebsd.org/cgi/man.cgi?query=mtree).
6
+
7
+ Not all the flags of FreeBSD `mtree(1)` are currently supported, the effor being driven by opus-labs' requirements. Pull requests are accepted though, and we will be happy to merge your contribution!
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'mtree'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install mtree
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/opus-codium/mtree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
42
+
43
+ ## Code of Conduct
44
+
45
+ Everyone interacting in the Mtree project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/smortex/mtree/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+
8
+ desc "Generate the mtree parser's code"
9
+ task :parser do
10
+ sh 'racc lib/mtree/parser.y'
11
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mtree'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mtree'
4
+
5
+ require 'optparse'
6
+
7
+ options = {
8
+ action: :check,
9
+ group_mapping: {},
10
+ user_mapping: {},
11
+ }
12
+
13
+ OptionParser.new do |opts|
14
+ opts.banner = 'usage: mtree options'
15
+
16
+ opts.on('-e', 'Noop. For compatibility only') do
17
+ end
18
+ opts.on('-f', '--file=FILE', 'Read the specification from FILE instead of standard input') do |specification|
19
+ options[:specification] = specification
20
+ end
21
+
22
+ opts.on('-p', '--path=PATH', 'Use the file hierarchy rooted in PATH, instead of the current directory') do |path|
23
+ options[:root] = path
24
+ end
25
+
26
+ opts.on('-j', 'Indent the output 4 spaces each time a directory level is descended') do
27
+ options[:indent] = true
28
+ end
29
+
30
+ opts.on('-u', '--update') do
31
+ options[:action] = :update
32
+ end
33
+
34
+ # Extensions
35
+
36
+ opts.on('--check', 'Checks that the files hierarchy match the specification') do
37
+ options[:action] = :check
38
+ end
39
+
40
+ opts.on('--print', 'Print the transformed specification') do
41
+ options[:action] = :print
42
+ end
43
+
44
+ opts.on('--leaves', 'Only consider leaves of the specification') do
45
+ options[:leaves] = true
46
+ end
47
+
48
+ opts.on('--map-gname=MAPPING', 'Apply from:to MAPPING to the gname field') do |mapping|
49
+ mapping.split(',').each do |tuple|
50
+ from, to = tuple.split(':', 2)
51
+ options[:group_mapping][from] = to
52
+ end
53
+ end
54
+
55
+ opts.on('--map-uname=MAPPING', 'Apply from:to MAPPING to the uname field') do |mapping|
56
+ mapping.split(',').each do |tuple|
57
+ from, to = tuple.split(':', 2)
58
+ options[:user_mapping][from] = to
59
+ end
60
+ end
61
+
62
+ opts.on('--symlink-to=DESTINATION', '') do |destination|
63
+ options[:symlink_to] = destination
64
+ end
65
+ end.parse!
66
+
67
+ parser = Mtree::Parser.new
68
+ parser.parse_file(options[:specification])
69
+ specifications = parser.specifications
70
+
71
+ specifications.leaves! if options[:leaves]
72
+
73
+ options[:group_mapping].each do |from, to|
74
+ specifications.each do |specification|
75
+ specification.gname = to if specification.gname == from
76
+ end
77
+ end
78
+
79
+ options[:user_mapping].each do |from, to|
80
+ specifications.each do |specification|
81
+ specification.uname = to if specification.uname == from
82
+ end
83
+ end
84
+
85
+ if options[:symlink_to]
86
+ specifications.symlink_to!(options[:symlink_to])
87
+ end
88
+
89
+ case options[:action]
90
+ when :update
91
+ specifications.enforce(options[:root] || '.')
92
+ when :check
93
+ exit specifications.match?(options[:root] || '.') ? 0 : 2
94
+ when :print
95
+ puts specifications.to_s(indent: options[:indent])
96
+ end
@@ -0,0 +1,7 @@
1
+ require 'mtree/file_specification'
2
+ require 'mtree/parser.tab'
3
+ require 'mtree/version'
4
+
5
+ module Mtree
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,200 @@
1
+ module Mtree
2
+ class FileSpecification
3
+ # VALID_ATTRIBUTES = %i[cksum device flags gid gname link md5 mode nlink nochange optional rmd160 sha1 sha256 sha384 sha512 size tags time type uid uname].freeze
4
+ VALID_ATTRIBUTES = %i[gid gname link mode nochange optional type uid uname].freeze
5
+
6
+ VALID_ATTRIBUTES.each do |attr|
7
+ define_method(attr) do
8
+ @attributes[attr]
9
+ end
10
+
11
+ define_method("#{attr}=") do |value|
12
+ @attributes[attr] = value
13
+ end
14
+ end
15
+
16
+ attr_accessor :filename
17
+ attr_accessor :relative_path
18
+ attr_accessor :children
19
+
20
+ def initialize(filename, attributes = {})
21
+ @filename = filename
22
+ @relative_path = filename
23
+ if (invalid_keys = attributes.keys - VALID_ATTRIBUTES) != []
24
+ raise "Unsupported attribute: #{invalid_keys.first}"
25
+ end
26
+ @attributes = attributes
27
+ @children = []
28
+ end
29
+
30
+ def match?(root)
31
+ res = true
32
+ problems = []
33
+
34
+ begin
35
+ @attributes.each do |attr, expected|
36
+ actual = send("current_#{attr}", root)
37
+
38
+ if expected != actual
39
+ problems << { attr: attr, expected: expected, actual: actual }
40
+ end
41
+ end
42
+ rescue Errno::ENOENT
43
+ puts "#{relative_path} missing"
44
+ res = false
45
+ end
46
+
47
+ if problems.any? && !nochange
48
+ puts("#{relative_path}:")
49
+ puts(problems.map { |problem| format("\t%<attr>s (%<expected>s, %<actual>s)", problem) })
50
+ res = false
51
+ end
52
+
53
+ children.each do |child|
54
+ res &= child.match?(root)
55
+ end
56
+
57
+ res
58
+ end
59
+
60
+ def each(&block)
61
+ yield(self)
62
+
63
+ children.each do |child|
64
+ child.each(&block)
65
+ end
66
+ end
67
+
68
+ def enforce(root)
69
+ if exist?(root)
70
+ update(root) unless nochange
71
+ else
72
+ create(root)
73
+ end
74
+
75
+ children.each do |child|
76
+ child.enforce(root)
77
+ end
78
+ end
79
+
80
+ def exist?(root)
81
+ current_type(root) == type
82
+ end
83
+
84
+ def create(root)
85
+ case type
86
+ when 'dir' then FileUtils.mkdir_p(full_filename(root))
87
+ when 'file' then FileUtils.touch(full_filename(root))
88
+ when 'link' then FileUtils.ln_s(link, full_filename(root), force: true)
89
+ end
90
+ update(root)
91
+ end
92
+
93
+ def update(root)
94
+ FileUtils.chown(uname, gname, full_filename(root)) if uname || gname
95
+ FileUtils.chmod(mode, full_filename(root)) if mode
96
+ end
97
+
98
+ def <<(child)
99
+ children << child
100
+
101
+ child.relative_path = File.join(relative_path, child.filename)
102
+ end
103
+
104
+ def leaves!
105
+ if children.any?
106
+ self.nochange = true
107
+ children.each(&:leaves!)
108
+ end
109
+ self
110
+ end
111
+
112
+ def symlink_to!(destination)
113
+ unless nochange
114
+ @attributes = {
115
+ type: 'link',
116
+ link: File.join(destination, relative_path).sub(%r{/\.$}, '').sub(%r{/\./}, '/'),
117
+ }
118
+ end
119
+ children.each do |child|
120
+ child.symlink_to!(destination)
121
+ end
122
+ self
123
+ end
124
+
125
+ def to_s(options = {})
126
+ descendent = ''
127
+ if children.any?
128
+ descendent = children.map do |child|
129
+ child.to_s(options)
130
+ end.join
131
+ descendent.gsub!(/^/, ' ') if options[:indent]
132
+ end
133
+
134
+ "#{filename} #{attributes}\n#{descendent}..\n"
135
+ end
136
+
137
+ def attributes
138
+ parts = []
139
+
140
+ parts << format('gid=%d', gid) if gid
141
+ parts << format('gname=%s', gname) if gname
142
+ parts << format('link=%s', link) if link
143
+ parts << format('mode=0%o', mode) if mode
144
+ parts << 'nochange' if nochange
145
+ parts << 'optional' if optional
146
+ parts << format('type=%s', type) if type
147
+ parts << format('uid=%d', uid) if uid
148
+ parts << format('uname=%s', uname) if uname
149
+
150
+ parts.join(' ')
151
+ end
152
+
153
+ private
154
+
155
+ def full_filename(root)
156
+ File.join(root, @relative_path)
157
+ end
158
+
159
+ def file_stat(root)
160
+ File.stat(full_filename(root))
161
+ end
162
+
163
+ def current_gid(root)
164
+ file_stat(root).gid
165
+ end
166
+
167
+ def current_gname(root)
168
+ Etc.getgrgid(current_gid(root)).name
169
+ end
170
+
171
+ def current_mode(root)
172
+ file_stat(root).mode & 0o7777
173
+ end
174
+
175
+ def current_nochange(_root)
176
+ true
177
+ end
178
+
179
+ def current_type(root)
180
+ t = file_stat(root).ftype
181
+
182
+ case t
183
+ when 'blockSpecial' then 'block'
184
+ when 'characterSpecial' then 'char'
185
+ when 'directory' then 'dir'
186
+ else t
187
+ end
188
+ rescue Errno::ENOENT
189
+ nil
190
+ end
191
+
192
+ def current_uid(root)
193
+ file_stat(root).uid
194
+ end
195
+
196
+ def current_uname(root)
197
+ Etc.getpwuid(current_uid(root)).name
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,303 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.4.14
4
+ # from Racc grammer file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+
10
+ require 'strscan'
11
+
12
+ module Mtree
13
+ class Parser < Racc::Parser
14
+
15
+ module_eval(<<'...end parser.y/module_eval...', 'parser.y', 33)
16
+
17
+ attr_accessor :yydebug
18
+ attr_accessor :defaults
19
+
20
+ attr_reader :specifications
21
+
22
+ def initialize
23
+ super
24
+ @defaults = {}
25
+ @path_components = []
26
+ @specifications = nil
27
+ end
28
+
29
+ def parse(text)
30
+ s = StringScanner.new(text)
31
+
32
+ tokens = []
33
+ until s.eos? do
34
+ case
35
+ when s.scan(/#.*/); # Ignore comment
36
+ when s.scan(/[[:blank:]]+/); # Ignore blanks
37
+ when s.scan(/\n/); tokens << [:LINE_BREAK, s.matched]
38
+ when s.scan(/\.\./); tokens << [:DOT_DOT, s.matched]
39
+ when s.scan(/\/set\b/); tokens << [:SET, s.matched]
40
+ when s.scan(/gid=([[:digit:]]+)/); tokens << [:GID, s[1]]
41
+ when s.scan(/gname=([[:alnum:]]+)/); tokens << [:GNAME, s[1]]
42
+ when s.scan(/mode=([[:digit:]]+)/); tokens << [:MODE, s[1]]
43
+ when s.scan(/nochange\b/); tokens << [:NOCHANGE, s[1]]
44
+ when s.scan(/tags=([[:print:]]+)\b/); tokens << [:TAGS, s[1]]
45
+ when s.scan(/type=(dir|file)/); tokens << [:TYPE, s[1]]
46
+ when s.scan(/uid=([[:digit:]]+)/); tokens << [:UID, s[1]]
47
+ when s.scan(/uname=([[:alnum:]]+)/); tokens << [:UNAME, s[1]]
48
+ when s.scan(/[^[:space:]]+/); tokens << [:IDENTIFIER, s.matched]
49
+ else
50
+ raise "No match: #{s.rest}"
51
+ end
52
+ end
53
+
54
+ define_singleton_method(:next_token) { tokens.shift }
55
+
56
+ tokens << [false, false]
57
+
58
+ # tokens.each do |t|
59
+ # puts t.inspect
60
+ # end
61
+
62
+ do_parse
63
+ end
64
+
65
+ def parse_file(filename)
66
+ parse(File.read(filename))
67
+ end
68
+ ...end parser.y/module_eval...
69
+ ##### State transition tables begin ###
70
+
71
+ racc_action_table = [
72
+ 14, 12, 8, 16, 17, 18, 19, 20, 21, 22,
73
+ 23, 24, 13, nil, 16, 17, 18, 19, 20, 21,
74
+ 22, 23, 4, 7, 5, 6, 4, 7, 5, 6 ]
75
+
76
+ racc_action_check = [
77
+ 10, 6, 1, 10, 10, 10, 10, 10, 10, 10,
78
+ 10, 11, 8, nil, 11, 11, 11, 11, 11, 11,
79
+ 11, 11, 2, 2, 2, 2, 0, 0, 0, 0 ]
80
+
81
+ racc_action_pointer = [
82
+ 24, 2, 20, nil, nil, nil, -2, nil, 12, nil,
83
+ -3, 8, nil, nil, nil, nil, nil, nil, nil, nil,
84
+ nil, nil, nil, nil, nil ]
85
+
86
+ racc_action_default = [
87
+ -2, -19, -1, -4, -10, -10, -19, -8, -19, -3,
88
+ -19, -19, -7, 25, -5, -9, -11, -12, -13, -14,
89
+ -15, -16, -17, -18, -6 ]
90
+
91
+ racc_goto_table = [
92
+ 3, 1, 9, 10, 11, 2 ]
93
+
94
+ racc_goto_check = [
95
+ 3, 1, 3, 4, 4, 2 ]
96
+
97
+ racc_goto_pointer = [
98
+ nil, 1, 5, 0, -1, nil ]
99
+
100
+ racc_goto_default = [
101
+ nil, nil, nil, nil, nil, 15 ]
102
+
103
+ racc_reduce_table = [
104
+ 0, 0, :racc_error,
105
+ 1, 15, :_reduce_1,
106
+ 0, 15, :_reduce_none,
107
+ 2, 16, :_reduce_none,
108
+ 1, 16, :_reduce_none,
109
+ 3, 17, :_reduce_5,
110
+ 3, 17, :_reduce_6,
111
+ 2, 17, :_reduce_7,
112
+ 1, 17, :_reduce_none,
113
+ 2, 18, :_reduce_9,
114
+ 0, 18, :_reduce_10,
115
+ 1, 19, :_reduce_11,
116
+ 1, 19, :_reduce_12,
117
+ 1, 19, :_reduce_13,
118
+ 1, 19, :_reduce_14,
119
+ 1, 19, :_reduce_15,
120
+ 1, 19, :_reduce_16,
121
+ 1, 19, :_reduce_17,
122
+ 1, 19, :_reduce_18 ]
123
+
124
+ racc_reduce_n = 19
125
+
126
+ racc_shift_n = 25
127
+
128
+ racc_token_table = {
129
+ false => 0,
130
+ :error => 1,
131
+ :SET => 2,
132
+ :LINE_BREAK => 3,
133
+ :IDENTIFIER => 4,
134
+ :DOT_DOT => 5,
135
+ :GID => 6,
136
+ :GNAME => 7,
137
+ :MODE => 8,
138
+ :NOCHANGE => 9,
139
+ :TAGS => 10,
140
+ :TYPE => 11,
141
+ :UID => 12,
142
+ :UNAME => 13 }
143
+
144
+ racc_nt_base = 14
145
+
146
+ racc_use_result_var = true
147
+
148
+ Racc_arg = [
149
+ racc_action_table,
150
+ racc_action_check,
151
+ racc_action_default,
152
+ racc_action_pointer,
153
+ racc_goto_table,
154
+ racc_goto_check,
155
+ racc_goto_default,
156
+ racc_goto_pointer,
157
+ racc_nt_base,
158
+ racc_reduce_table,
159
+ racc_token_table,
160
+ racc_shift_n,
161
+ racc_reduce_n,
162
+ racc_use_result_var ]
163
+
164
+ Racc_token_to_s_table = [
165
+ "$end",
166
+ "error",
167
+ "SET",
168
+ "LINE_BREAK",
169
+ "IDENTIFIER",
170
+ "DOT_DOT",
171
+ "GID",
172
+ "GNAME",
173
+ "MODE",
174
+ "NOCHANGE",
175
+ "TAGS",
176
+ "TYPE",
177
+ "UID",
178
+ "UNAME",
179
+ "$start",
180
+ "target",
181
+ "expressions",
182
+ "expression",
183
+ "attributes",
184
+ "attribute" ]
185
+
186
+ Racc_debug_parser = false
187
+
188
+ ##### State transition tables end #####
189
+
190
+ # reduce 0 omitted
191
+
192
+ module_eval(<<'.,.,', 'parser.y', 2)
193
+ def _reduce_1(val, _values, result)
194
+ if @path_components.any? then raise 'Malformed specifications' end
195
+ result
196
+ end
197
+ .,.,
198
+
199
+ # reduce 2 omitted
200
+
201
+ # reduce 3 omitted
202
+
203
+ # reduce 4 omitted
204
+
205
+ module_eval(<<'.,.,', 'parser.y', 8)
206
+ def _reduce_5(val, _values, result)
207
+ @defaults.merge!(val[1])
208
+ result
209
+ end
210
+ .,.,
211
+
212
+ module_eval(<<'.,.,', 'parser.y', 9)
213
+ def _reduce_6(val, _values, result)
214
+ spec = Mtree::FileSpecification.new(val[0], @defaults.merge(val[1])); if @specifications.nil? then @specifications = spec end; if @path_components.last then @path_components.last << spec end; @path_components.push(spec)
215
+ result
216
+ end
217
+ .,.,
218
+
219
+ module_eval(<<'.,.,', 'parser.y', 10)
220
+ def _reduce_7(val, _values, result)
221
+ @path_components.pop
222
+ result
223
+ end
224
+ .,.,
225
+
226
+ # reduce 8 omitted
227
+
228
+ module_eval(<<'.,.,', 'parser.y', 13)
229
+ def _reduce_9(val, _values, result)
230
+ result = val[0].merge(val[1])
231
+ result
232
+ end
233
+ .,.,
234
+
235
+ module_eval(<<'.,.,', 'parser.y', 14)
236
+ def _reduce_10(val, _values, result)
237
+ result = {}
238
+ result
239
+ end
240
+ .,.,
241
+
242
+ module_eval(<<'.,.,', 'parser.y', 16)
243
+ def _reduce_11(val, _values, result)
244
+ result = { gid: Integer(val[0]) }
245
+ result
246
+ end
247
+ .,.,
248
+
249
+ module_eval(<<'.,.,', 'parser.y', 17)
250
+ def _reduce_12(val, _values, result)
251
+ result = { gname: val[0] }
252
+ result
253
+ end
254
+ .,.,
255
+
256
+ module_eval(<<'.,.,', 'parser.y', 18)
257
+ def _reduce_13(val, _values, result)
258
+ result = { mode: Integer(val[0], 8) }
259
+ result
260
+ end
261
+ .,.,
262
+
263
+ module_eval(<<'.,.,', 'parser.y', 19)
264
+ def _reduce_14(val, _values, result)
265
+ result = { nochange: true }
266
+ result
267
+ end
268
+ .,.,
269
+
270
+ module_eval(<<'.,.,', 'parser.y', 20)
271
+ def _reduce_15(val, _values, result)
272
+ result = {}
273
+ result
274
+ end
275
+ .,.,
276
+
277
+ module_eval(<<'.,.,', 'parser.y', 21)
278
+ def _reduce_16(val, _values, result)
279
+ result = { type: val[0] }
280
+ result
281
+ end
282
+ .,.,
283
+
284
+ module_eval(<<'.,.,', 'parser.y', 22)
285
+ def _reduce_17(val, _values, result)
286
+ result = { uid: Integer(val[0]) }
287
+ result
288
+ end
289
+ .,.,
290
+
291
+ module_eval(<<'.,.,', 'parser.y', 23)
292
+ def _reduce_18(val, _values, result)
293
+ result = { uname: val[0] }
294
+ result
295
+ end
296
+ .,.,
297
+
298
+ def _reduce_none(val, _values, result)
299
+ val[0]
300
+ end
301
+
302
+ end # class Parser
303
+ end # module Mtree
@@ -0,0 +1,84 @@
1
+ class Mtree::Parser
2
+ rule
3
+ target: expressions { if @path_components.any? then raise 'Malformed specifications' end }
4
+ |
5
+
6
+ expressions: expressions expression
7
+ | expression
8
+
9
+ expression: SET attributes LINE_BREAK { @defaults.merge!(val[1]) }
10
+ | IDENTIFIER attributes LINE_BREAK { spec = Mtree::FileSpecification.new(val[0], @defaults.merge(val[1])); if @specifications.nil? then @specifications = spec end; if @path_components.last then @path_components.last << spec end; @path_components.push(spec) } # @path_components.push(val[0]); @specifications.push() }
11
+ | DOT_DOT LINE_BREAK { @path_components.pop }
12
+ | LINE_BREAK
13
+
14
+ attributes: attributes attribute { result = val[0].merge(val[1]) }
15
+ | { result = {} }
16
+
17
+ attribute: GID { result = { gid: Integer(val[0]) } }
18
+ | GNAME { result = { gname: val[0] } }
19
+ | MODE { result = { mode: Integer(val[0], 8) } }
20
+ | NOCHANGE { result = { nochange: true } }
21
+ | TAGS { result = {} }
22
+ | TYPE { result = { type: val[0] } }
23
+ | UID { result = { uid: Integer(val[0]) } }
24
+ | UNAME { result = { uname: val[0] } }
25
+ end
26
+
27
+
28
+ ---- header
29
+
30
+ require 'strscan'
31
+
32
+ ---- inner
33
+
34
+ attr_accessor :yydebug
35
+ attr_accessor :defaults
36
+
37
+ attr_reader :specifications
38
+
39
+ def initialize
40
+ super
41
+ @defaults = {}
42
+ @path_components = []
43
+ @specifications = nil
44
+ end
45
+
46
+ def parse(text)
47
+ s = StringScanner.new(text)
48
+
49
+ tokens = []
50
+ until s.eos? do
51
+ case
52
+ when s.scan(/#.*/); # Ignore comment
53
+ when s.scan(/[[:blank:]]+/); # Ignore blanks
54
+ when s.scan(/\n/); tokens << [:LINE_BREAK, s.matched]
55
+ when s.scan(/\.\./); tokens << [:DOT_DOT, s.matched]
56
+ when s.scan(/\/set\b/); tokens << [:SET, s.matched]
57
+ when s.scan(/gid=([[:digit:]]+)/); tokens << [:GID, s[1]]
58
+ when s.scan(/gname=([[:alnum:]]+)/); tokens << [:GNAME, s[1]]
59
+ when s.scan(/mode=([[:digit:]]+)/); tokens << [:MODE, s[1]]
60
+ when s.scan(/nochange\b/); tokens << [:NOCHANGE, s[1]]
61
+ when s.scan(/tags=([[:print:]]+)\b/); tokens << [:TAGS, s[1]]
62
+ when s.scan(/type=(dir|file)/); tokens << [:TYPE, s[1]]
63
+ when s.scan(/uid=([[:digit:]]+)/); tokens << [:UID, s[1]]
64
+ when s.scan(/uname=([[:alnum:]]+)/); tokens << [:UNAME, s[1]]
65
+ when s.scan(/[^[:space:]]+/); tokens << [:IDENTIFIER, s.matched]
66
+ else
67
+ raise "No match: #{s.rest}"
68
+ end
69
+ end
70
+
71
+ define_singleton_method(:next_token) { tokens.shift }
72
+
73
+ tokens << [false, false]
74
+
75
+ # tokens.each do |t|
76
+ # puts t.inspect
77
+ # end
78
+
79
+ do_parse
80
+ end
81
+
82
+ def parse_file(filename)
83
+ parse(File.read(filename))
84
+ end
@@ -0,0 +1,3 @@
1
+ module Mtree
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'mtree/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'mtree'
7
+ spec.version = Mtree::VERSION
8
+ spec.authors = ['Romain Tartière']
9
+ spec.email = ['romain@opus-codium.org']
10
+
11
+ spec.summary = 'Manage a directory hierarchy'
12
+ spec.description = 'This is a ruby utility and library providing the mtree(1) features found on BSDs.'
13
+ spec.homepage = 'https://github.com/opus-codium/mtree'
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'racc'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'simplecov'
30
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mtree
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Romain Tartière
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: racc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This is a ruby utility and library providing the mtree(1) features found
84
+ on BSDs.
85
+ email:
86
+ - romain@opus-codium.org
87
+ executables:
88
+ - mtree
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".rubocop.yml"
95
+ - ".travis.yml"
96
+ - CODE_OF_CONDUCT.md
97
+ - Gemfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - exe/mtree
104
+ - lib/mtree.rb
105
+ - lib/mtree/file_specification.rb
106
+ - lib/mtree/parser.tab.rb
107
+ - lib/mtree/parser.y
108
+ - lib/mtree/version.rb
109
+ - mtree.gemspec
110
+ homepage: https://github.com/opus-codium/mtree
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.7
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Manage a directory hierarchy
134
+ test_files: []