~nvalcarcel/ubuntu/lucid/puppet/fix-546677

« back to all changes in this revision

Viewing changes to lib/puppet/application/pi.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'puppet'
 
2
require 'puppet/application'
 
3
 
 
4
class Formatter
 
5
 
 
6
    def initialize(width)
 
7
        @width = width
 
8
    end
 
9
 
 
10
    def wrap(txt, opts)
 
11
        return "" unless txt && !txt.empty?
 
12
        work = (opts[:scrub] ? scrub(txt) : txt)
 
13
        indent = (opts[:indent] ? opts[:indent] : 0)
 
14
        textLen = @width - indent
 
15
        patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
 
16
        prefix = " " * indent
 
17
 
 
18
        res = []
 
19
 
 
20
        while work.length > textLen
 
21
            if work =~ patt
 
22
                res << $1
 
23
                work.slice!(0, $&.length)
 
24
            else
 
25
                res << work.slice!(0, textLen)
 
26
            end
 
27
        end
 
28
        res << work if work.length.nonzero?
 
29
        return prefix + res.join("\n" + prefix)
 
30
    end
 
31
 
 
32
    def header(txt, sep = "-")
 
33
        "\n#{txt}\n" + sep * txt.size
 
34
    end
 
35
 
 
36
    private
 
37
 
 
38
    def scrub(text)
 
39
        # For text with no carriage returns, there's nothing to do.
 
40
        if text !~ /\n/
 
41
            return text
 
42
        end
 
43
        indent = nil
 
44
 
 
45
        # If we can match an indentation, then just remove that same level of
 
46
        # indent from every line.
 
47
        if text =~ /^(\s+)/
 
48
            indent = $1
 
49
            return text.gsub(/^#{indent}/,'')
 
50
        else
 
51
            return text
 
52
        end
 
53
    end
 
54
 
 
55
end
 
56
 
 
57
class TypeDoc
 
58
 
 
59
    def initialize
 
60
        @format = Formatter.new(76)
 
61
        @types = {}
 
62
        Puppet::Type.loadall
 
63
        Puppet::Type.eachtype { |type|
 
64
            next if type.name == :component
 
65
            @types[type.name] = type
 
66
        }
 
67
    end
 
68
 
 
69
    def list_types
 
70
        puts "These are the types known to puppet:\n"
 
71
        @types.keys.sort { |a, b|
 
72
            a.to_s <=> b.to_s
 
73
        }.each do |name|
 
74
            type = @types[name]
 
75
            s = type.doc.gsub(/\s+/, " ")
 
76
            n = s.index(".")
 
77
            if n.nil?
 
78
                s = ".. no documentation .."
 
79
            elsif n > 45
 
80
                s = s[0, 45] + " ..."
 
81
            else
 
82
                s = s[0, n]
 
83
            end
 
84
            printf "%-15s - %s\n", name, s
 
85
        end
 
86
    end
 
87
 
 
88
    def format_type(name, opts)
 
89
        name = name.to_sym
 
90
        unless @types.has_key?(name)
 
91
            puts "Unknown type #{name}"
 
92
            return
 
93
        end
 
94
        type = @types[name]
 
95
        puts @format.header(name.to_s, "=")
 
96
        puts @format.wrap(type.doc, :indent => 0, :scrub => true) + "\n\n"
 
97
 
 
98
        puts @format.header("Parameters")
 
99
        if opts[:parameters]
 
100
            format_attrs(type, [:property, :param])
 
101
        else
 
102
            list_attrs(type, [:property, :param])
 
103
        end
 
104
 
 
105
        if opts[:meta]
 
106
            puts @format.header("Meta Parameters")
 
107
            if opts[:parameters]
 
108
                format_attrs(type, [:meta])
 
109
            else
 
110
                list_attrs(type, [:meta])
 
111
            end
 
112
        end
 
113
 
 
114
        if type.providers.size > 0
 
115
            puts @format.header("Providers")
 
116
            if opts[:providers]
 
117
                format_providers(type)
 
118
            else
 
119
                list_providers(type)
 
120
            end
 
121
        end
 
122
    end
 
123
 
 
124
    # List details about attributes
 
125
    def format_attrs(type, attrs)
 
126
        docs = {}
 
127
        type.allattrs.each do |name|
 
128
            kind = type.attrtype(name)
 
129
            if attrs.include?(kind) && name != :provider
 
130
                docs[name] = type.attrclass(name).doc
 
131
            end
 
132
        end
 
133
 
 
134
        docs.sort { |a,b|
 
135
            a[0].to_s <=> b[0].to_s
 
136
        }.each { |name, doc|
 
137
            print "\n- **%s**" % name
 
138
            if type.namevar == name and name != :name
 
139
                puts " (*namevar*)"
 
140
            else
 
141
                puts ""
 
142
            end
 
143
            puts @format.wrap(doc, :indent => 4, :scrub => true)
 
144
        }
 
145
    end
 
146
 
 
147
    # List the names of attributes
 
148
    def list_attrs(type, attrs)
 
149
        params = []
 
150
        type.allattrs.each do |name|
 
151
            kind = type.attrtype(name)
 
152
            if attrs.include?(kind) && name != :provider
 
153
                params << name.to_s
 
154
            end
 
155
        end
 
156
        puts @format.wrap(params.sort.join(", "), :indent => 4)
 
157
    end
 
158
 
 
159
    def format_providers(type)
 
160
        type.providers.sort { |a,b|
 
161
            a.to_s <=> b.to_s
 
162
        }.each { |prov|
 
163
            puts "\n- **%s**" % prov
 
164
            puts @format.wrap(type.provider(prov).doc,
 
165
                              :indent => 4, :scrub => true)
 
166
        }
 
167
    end
 
168
 
 
169
    def list_providers(type)
 
170
        list = type.providers.sort { |a,b|
 
171
            a.to_s <=> b.to_s
 
172
        }.join(", ")
 
173
        puts @format.wrap(list, :indent => 4)
 
174
    end
 
175
 
 
176
end
 
177
 
 
178
Puppet::Application.new(:pi,"#{$0} [options] [type]") do
 
179
 
 
180
    should_not_parse_config
 
181
 
 
182
    option("--short", "-s", "Only list parameters without detail") do |arg|
 
183
        options[:parameters] = false
 
184
    end
 
185
 
 
186
    option("--providers","-p")
 
187
    option("--list", "-l")
 
188
    option("--meta","-m")
 
189
 
 
190
    preinit do
 
191
        options[:parameters] = true
 
192
    end
 
193
 
 
194
    command(:main) do
 
195
        doc = TypeDoc.new
 
196
 
 
197
        if options[:list]
 
198
            doc.list_types
 
199
        else
 
200
            options[:types].each { |name| doc.format_type(name, options) }
 
201
        end
 
202
    end
 
203
 
 
204
    setup do
 
205
        options[:types] = ARGV.dup
 
206
        unless options[:list] || options[:types].size > 0
 
207
            handle_help(nil)
 
208
        end
 
209
        if options[:list] && options[:types].size > 0
 
210
            $stderr.puts "Warning: ignoring types when listing all types"
 
211
        end
 
212
    end
 
213
 
 
214
end