bugzyrb 0.1.0

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.
@@ -0,0 +1,280 @@
1
+ #!/usr/bin/env ruby -w
2
+ =begin
3
+ * Name : cmdapp.rb
4
+ * Description : some basic command line things
5
+ * : Moving some methods from todorb.rb here
6
+ * Author : rkumar
7
+ * Date : 2010-06-20 11:18
8
+ * License: Ruby License
9
+
10
+ =end
11
+ require 'common/sed'
12
+
13
+ ERRCODE = 1
14
+
15
+ module Cmdapp
16
+
17
+ ##
18
+ # external dependencies:
19
+ # @app_default_action - action to run if none specified
20
+ # @app_file_path - data file we are backing up, or reading into array
21
+ # @app_serial_path - serial_number file
22
+ ##
23
+ # check whether this action is mapped to some alias and *changes*
24
+ # variables@action and @argv if true.
25
+ # @param [String] action asked by user
26
+ # @param [Array] rest of args on command line
27
+ # @return [Boolean] whether it is mapped or not.
28
+ #
29
+ def check_aliases action, args
30
+ return false unless @aliases
31
+ ret = @aliases[action]
32
+ if ret
33
+ a = ret.shift
34
+ b = [*ret, *args]
35
+ @action = a
36
+ @argv = b
37
+ #puts " #{@action} ; argv: #{@argv} "
38
+ return true
39
+ end
40
+ return false
41
+ end
42
+ ##
43
+ # runs method after checking if valid or alias.
44
+ # If not found prints help.
45
+ # @return [0, ERRCODE] success 0.
46
+ def run
47
+ @action = @argv[0] || @app_default_action
48
+ @action = @action.downcase
49
+
50
+
51
+ ret = 0
52
+ @argv.shift
53
+ if respond_to? @action
54
+ ret = send(@action, @argv)
55
+ else
56
+ # check aliases
57
+ if check_aliases @action, @argv
58
+ ret = send(@action, @argv)
59
+ else
60
+ help @argv
61
+ ret = ERRCODE
62
+ end
63
+ end
64
+ ret ||= 0
65
+ ret = 0 if ret != ERRCODE
66
+ return ret
67
+ end
68
+ # not required if using Subcommand
69
+ def help args
70
+ if @actions.nil?
71
+ if defined? @commands
72
+ unless @commands.empty?
73
+ @actions = @commands
74
+ end
75
+ end
76
+ end
77
+ if @actions
78
+ puts "Actions are "
79
+ @actions.each_pair { |name, val| puts "#{name}\t#{val}" }
80
+ end
81
+ puts " "
82
+ if @aliases
83
+ puts "Aliases are "
84
+ @aliases.each_pair { |name, val| puts "#{name}:\t#{val.join(' ')}" }
85
+ end
86
+ 0
87
+ end
88
+ ##
89
+ def alias_command name, *args
90
+ @aliases ||= {}
91
+ @aliases[name.to_s] = args
92
+ end
93
+ def add_action name, descr
94
+ @actions ||= {}
95
+ @actions[name.to_s] = desc
96
+ end
97
+
98
+ ##
99
+ # reads serial_number file, returns serialno for this app
100
+ # and increments the serial number and writes back.
101
+ def _get_serial_number
102
+ require 'fileutils'
103
+ appname = @appname
104
+ filename = @app_serial_path || "serial_numbers"
105
+ h = {}
106
+ # check if serial file existing in curr dir. Else create
107
+ if File.exists?(filename)
108
+ File.open(filename).each { |line|
109
+ #sn = $1 if line.match regex
110
+ x = line.split ":"
111
+ h[x[0]] = x[1].chomp
112
+ }
113
+ end
114
+ sn = h[appname] || 1
115
+ # update the sn in file
116
+ nsn = sn.to_i + 1
117
+ # this will create if not exists in addition to storing if it does
118
+ h[appname] = nsn
119
+ # write back to file
120
+ File.open(filename, "w") do |f|
121
+ h.each_pair {|k,v| f.print "#{k}:#{v}\n"}
122
+ end
123
+ return sn
124
+ end
125
+ ##
126
+ # After doing a redo of the numbering, we need to reset the numbers for that app
127
+ def _set_serial_number number
128
+ appname = @appname
129
+ pattern = Regexp.new "^#{appname}:.*$"
130
+ filename = @app_serial_path || "serial_numbers"
131
+ # during testing redo this file does not exist, so i get errors
132
+ if !File.exists? filename
133
+ _get_serial_number
134
+ end
135
+ _backup filename
136
+ change_row filename, pattern, "#{appname}:#{number}"
137
+ end
138
+
139
+ def _backup filename=@app_file_path
140
+ require 'fileutils'
141
+ FileUtils.cp filename, "#{filename}.org"
142
+ end
143
+ def die text
144
+ $stderr.puts text
145
+ exit ERRCODE
146
+ end
147
+ # prints messages to stderr
148
+ # All messages should go to stderr.
149
+ # Keep stdout only for output which can be used by other programs
150
+ def message text
151
+ $stderr.puts text
152
+ end
153
+ # print to stderr only if verbose set
154
+ def verbose text
155
+ message(text) if @options[:verbose]
156
+ end
157
+ # print to stderr only if verbose set
158
+ def warning text
159
+ print_red("WARNING: #{text}")
160
+ end
161
+ def print_red text
162
+ message "#{RED}#{text}#{CLEAR}"
163
+ end
164
+ def print_green text
165
+ message "#{GREEN}#{text}#{CLEAR}"
166
+ end
167
+
168
+ ##
169
+ # load data into array as item and task
170
+ # @see save_array to write
171
+ def load_array
172
+ #return if $valid_array
173
+ $valid_array = false
174
+ @data = []
175
+ File.open(@app_file_path).each do |line|
176
+ # FIXME: use @app_delim
177
+ row = line.chomp.split "\t"
178
+ @data << row
179
+ end
180
+ $valid_array = true
181
+ end
182
+ ##
183
+ # saves the task array to disk
184
+ # Please use load_array to load, and not populate
185
+ def save_array
186
+ raise "Cannot save array! Please use load_array to load" if $valid_array == false
187
+
188
+ File.open(@app_file_path, "w") do |file|
189
+ # FIXME: use join with @app_delim
190
+ @data.each { |row| file.puts "#{row[0]}\t#{row[1]}" }
191
+ end
192
+ end
193
+ ##
194
+ # retrieve version info updated by jeweler.
195
+ # Typically used by --version option of any command.
196
+ # @return [String, nil] version as string, or nil if file not found
197
+ def version_info
198
+ # thanks to Roger Pack on ruby-forum for how to get to the version
199
+ # file
200
+ filename = File.open(File.dirname(__FILE__) + "/../../VERSION")
201
+ v = nil
202
+ if File.exists?(filename)
203
+ v = File.open(filename).read.chomp if File.exists?(filename)
204
+ #else
205
+ #$stderr.puts "could not locate file #{filename}. "
206
+ #puts `pwd`
207
+ end
208
+ v
209
+ end
210
+
211
+ # reads multiple lines of input until EOF (Ctrl-d)
212
+ # and returns as a string.
213
+ # Add newline after each line
214
+ # @return [String, nil] newline delimited string, or nil
215
+ def get_lines
216
+ lines = nil
217
+ #$stdin.flush
218
+ $stdin.each_line do |line|
219
+ line.chomp!
220
+ if line =~ /^bye$/
221
+ break
222
+ end
223
+ if lines
224
+ lines << "\n" + line
225
+ else
226
+ lines = line
227
+ end
228
+ end
229
+ return lines
230
+ end
231
+
232
+
233
+ # edits given text using EDITOR
234
+ # @param [String] text to edit
235
+ # @return [String, nil] edited string, or nil if no change
236
+ def edit_text text
237
+ # 2010-06-29 10:24
238
+ require 'fileutils'
239
+ require 'tempfile'
240
+ ed = ENV['EDITOR'] || "vim"
241
+ temp = Tempfile.new "tmp"
242
+ File.open(temp,"w"){ |f| f.write text }
243
+ mtime = File.mtime(temp.path)
244
+ #system("#{ed} #{temp.path}")
245
+ system(ed, temp.path)
246
+
247
+ newmtime = File.mtime(temp.path)
248
+ newstr = nil
249
+ if mtime < newmtime
250
+ # check timestamp, if updated ..
251
+ #newstr = ""
252
+ #File.open(temp,"r"){ |f| f.each {|r| newstr << r } }
253
+ newstr = File.read(temp)
254
+ #puts "I got: #{newstr}"
255
+ else
256
+ #puts "user quit without saving"
257
+ end
258
+ return newstr
259
+ end
260
+
261
+ ##
262
+ # reads up template, and substirutes values from myhash
263
+ # @param [String] template text
264
+ # @param [Hash] values to replace in template
265
+ # @return [String] template output
266
+ # NOTE: probably better to use rdoc/template which can handle arrays as well.
267
+ def template_replace template, myhash
268
+ #tmpltext=File::read(template);
269
+
270
+ t = template.dup
271
+ t.gsub!( /##(.*?)##/ ) {
272
+ #raise "Key '#{$1}' found in template but the value has not been set" unless ( myhash.has_key?( $1 ) )
273
+ myhash[ $1 ].to_s
274
+ }
275
+ t
276
+ end
277
+
278
+
279
+
280
+ end
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Color constants so we can print onto console using print or puts
4
+ # @example
5
+ # string = "hello ruby"
6
+ # puts " #{RED}#{BOLD}#{UNDERLINE}#{string}#{CLEAR}"
7
+ #
8
+ # http://wiki.bash-hackers.org/scripting/terminalcodes
9
+ # ripped off highline gem
10
+ module ColorConstants
11
+
12
+ # Embed in a String to clear all previous ANSI sequences. This *MUST* be
13
+ # done before the program exits!
14
+ #
15
+ CLEAR = "\e[0m"
16
+ # An alias for CLEAR.
17
+ RESET = CLEAR
18
+ NORMAL = CLEAR
19
+ # Erase the current line of terminal output.
20
+ ERASE_LINE = "\e[K"
21
+ # Erase the character under the cursor.
22
+ ERASE_CHAR = "\e[P"
23
+ # The start of an ANSI bold sequence.
24
+ BOLD = "\e[1m"
25
+ # The start of an ANSI dark sequence. (Terminal support uncommon.)
26
+ DARK = "\e[2m"
27
+ DIM = DARK
28
+ # The start of an ANSI underline sequence.
29
+ UNDERLINE = "\e[4m"
30
+ # An alias for UNDERLINE.
31
+ UNDERSCORE = UNDERLINE
32
+ # The start of an ANSI blink sequence. (Terminal support uncommon.)
33
+ BLINK = "\e[5m"
34
+ # The start of an ANSI reverse sequence.
35
+ REVERSE = "\e[7m"
36
+ # The start of an ANSI concealed sequence. (Terminal support uncommon.)
37
+ CONCEALED = "\e[8m"
38
+
39
+ # added from http://understudy.net/custom.html
40
+ BOLD_OFF = "\e[22m"
41
+ UNDERILNE_OFF = "\e[24m"
42
+ BLINK_OFF = "\e[25m"
43
+ REVERSE_OFF = "\e[27m"
44
+
45
+ # Set the terminal's foreground ANSI color to black.
46
+ BLACK = "\e[30m"
47
+ # Set the terminal's foreground ANSI color to red.
48
+ RED = "\e[31m"
49
+ # Set the terminal's foreground ANSI color to green.
50
+ GREEN = "\e[32m"
51
+ # Set the terminal's foreground ANSI color to yellow.
52
+ YELLOW = "\e[33m"
53
+ # Set the terminal's foreground ANSI color to blue.
54
+ BLUE = "\e[34m"
55
+ # Set the terminal's foreground ANSI color to magenta.
56
+ MAGENTA = "\e[35m"
57
+ # Set the terminal's foreground ANSI color to cyan.
58
+ CYAN = "\e[36m"
59
+ # Set the terminal's foreground ANSI color to white.
60
+ WHITE = "\e[37m"
61
+
62
+ # Set the terminal's background ANSI color to black.
63
+ ON_BLACK = "\e[40m"
64
+ # Set the terminal's background ANSI color to red.
65
+ ON_RED = "\e[41m"
66
+ # Set the terminal's background ANSI color to green.
67
+ ON_GREEN = "\e[42m"
68
+ # Set the terminal's background ANSI color to yellow.
69
+ ON_YELLOW = "\e[43m"
70
+ # Set the terminal's background ANSI color to blue.
71
+ ON_BLUE = "\e[44m"
72
+ # Set the terminal's background ANSI color to magenta.
73
+ ON_MAGENTA = "\e[45m"
74
+ # Set the terminal's background ANSI color to cyan.
75
+ ON_CYAN = "\e[46m"
76
+ # Set the terminal's background ANSI color to white.
77
+ ON_WHITE = "\e[47m"
78
+
79
+ end
data/lib/common/db.rb ADDED
@@ -0,0 +1,249 @@
1
+ #!/usr/bin/env ruby -w
2
+ require 'rubygems'
3
+ require 'sqlite3'
4
+ require 'pp'
5
+ require 'arrayfields'
6
+ module Database
7
+ class DB
8
+ attr_accessor :db
9
+
10
+ def initialize dbname="bugzy.sqlite"
11
+ raise "#{dbname} does not exist. Try --help" unless File.exists? dbname
12
+ @db = SQLite3::Database.new(dbname)
13
+ $now = Time.now
14
+ $num = rand(100)
15
+ $default_user = ENV['LOGNAME'] || ENV['USER']
16
+ end
17
+
18
+ # returns many rows
19
+ # @param [Fixnum] id for table with 1:n rows
20
+ # @return [Array, nil] array if rows, else nil
21
+ def select table
22
+ #puts " --- #{table} --- "
23
+ @db.type_translation = true
24
+ rows = []
25
+ @db.execute( "select * from #{table} " ) do |row|
26
+ if block_given?
27
+ yield row
28
+ else
29
+ rows << row
30
+ end
31
+ end
32
+ return nil if rows.empty?
33
+ return rows
34
+ end
35
+ # returns many rows
36
+ # @param [String] sql statement
37
+ # @return [Array, nil] array if rows, else nil
38
+ def run text
39
+ #puts " --- #{text} --- "
40
+ @db.type_translation = true
41
+ rows = []
42
+ @db.execute( text ) do |row|
43
+ if block_given?
44
+ yield row
45
+ else
46
+ rows << row
47
+ end
48
+ end
49
+ return nil if rows.empty?
50
+ return rows
51
+ end
52
+ def select_where table, *wherecond
53
+ #puts " --- #{table} --- #{wherecond} "
54
+ @db.type_translation = true
55
+ wherestr = nil
56
+ rows = []
57
+ if wherecond and !wherecond.empty?
58
+ fields, values = separate_field_values wherecond
59
+ #wherestr = "" unless wherestr
60
+ wherestr = " where #{fields.join(" and ")} "
61
+ if wherestr
62
+ #puts " wherestr #{wherestr}, #{values} "
63
+ #stmt = @db.prepare("select * from #{table} #{wherestr} ", *values)
64
+ @db.execute( "select * from #{table} #{wherestr}", *values ) do |row|
65
+ if block_given?
66
+ yield row
67
+ else
68
+ rows << row
69
+ end
70
+ end
71
+ end
72
+ end
73
+ return nil if rows.empty?
74
+ return rows
75
+ end
76
+ ##
77
+ # insert a issue or bug report into the database
78
+ # @params
79
+ # @return [Fixnum] last row id
80
+ def bugs_insert(status, severity, type, assigned_to, start_date, due_date, comment_count, priority, title, description, fix, created_by = $default_user)
81
+ # id = $num
82
+ # status = "CODE"
83
+ # severity = "CODE"
84
+ # type = "CODE"
85
+ # assigned_to = "CODE"
86
+ # start_date = $now
87
+ # due_date = $now
88
+ # comment_count = $num
89
+ # priority = "CODE"
90
+ # title = "CODE"
91
+ # description = "Some long text"
92
+ # fix = "Some long text"
93
+ # date_created = $now
94
+ # date_modified = $now
95
+ @db.execute(" insert into bugs ( status, severity, type, assigned_to, start_date, due_date, comment_count, priority, title, description, fix, created_by ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
96
+ status, severity, type, assigned_to, start_date, due_date, comment_count, priority, title, description, fix, created_by )
97
+ rowid = @db.get_first_value( "select last_insert_rowid();")
98
+ return rowid
99
+ end
100
+ ## takes a hash and creates an insert statement for table and inserts data.
101
+ # Advantage is that as we add columns, this will add the column to the insert, so we
102
+ # don't need to keep modifying in many places.
103
+ # @param [String] name of table to insert data into
104
+ # @param [Hash] values to insert, keys must be table column names
105
+ # @return [Fixnum] newly inserted rowid
106
+ def table_insert_hash table, hash
107
+ str = "INSERT INTO #{table} ("
108
+ qstr = [] # question marks
109
+ fields = [] # field names
110
+ bind_vars = [] # values to insert
111
+ hash.each_pair { |name, val|
112
+ fields << name
113
+ bind_vars << val
114
+ qstr << "?"
115
+ }
116
+ fstr = fields.join(",")
117
+ str << fstr
118
+ str << ") values ("
119
+ str << qstr.join(",")
120
+ str << ")"
121
+ puts str
122
+ @db.execute(str, *bind_vars)
123
+ rowid = @db.get_first_value( "select last_insert_rowid();")
124
+ return rowid
125
+ end
126
+ def max_bug_id
127
+ id = @db.get_first_value( "select max(id) from bugs;")
128
+ return id
129
+ end
130
+ def sql_comments_insert id, comment, created_by = $default_user
131
+ #date_created = date_cr | Time.now
132
+ @db.execute("insert into comments (id, comment, created_by) values (?,?,?)", id, comment, created_by )
133
+ rowid = @db.get_first_value( "select last_insert_rowid();")
134
+ return rowid
135
+ end
136
+ def sql_logs_insert id, field, log, created_by = $default_user
137
+ #date_created = date_cr | Time.now
138
+ @db.execute("insert into log (id, field, log, created_by) values (?,?,?,?)", id, field, log, created_by )
139
+ end
140
+ def sql_delete_bug id
141
+ message "deleting #{id}"
142
+ @db.execute( "delete from bugs where id = ?", id )
143
+ @db.execute( "delete from comments where id = ?", id )
144
+ @db.execute( "delete from log where id = ?", id )
145
+ end
146
+
147
+
148
+ ##
149
+ # update a row from bugs based on id, giving one fieldname and value
150
+ # @param [Fixnum] id unique key
151
+ # @param [String] fieldname
152
+ # @param [String] value to update
153
+ # @example sql_bugs_update 9, :name, "Roger"
154
+ def sql_update table, id, field, value
155
+ @db.execute( "update #{table} set #{field} = ?, date_modified = ? where id = ?", value,$now, id)
156
+ end
157
+
158
+ # update a row from bugs based on id, can give multiple fieldnames and values
159
+ # @param [Fixnum] id unique key
160
+ # @return [Array] alternating fieldname and value
161
+ # @example sql_bugs_update_mult 9, :name, "Roger", :age, 29, :country, "SWI"
162
+ def sql_bugs_update_mult id, *fv
163
+ fields = []
164
+ values = []
165
+ fv << "date_modified"
166
+ fv << $now
167
+ fv.each_with_index do |f, i|
168
+ if i % 2 == 0
169
+ fields << "#{f} = ?"
170
+ else
171
+ values << f
172
+ end
173
+ end
174
+
175
+ print( "update bugs set #{fields.join(" ,")} where id = ?", *values, id)
176
+ @db.execute( "update bugs set #{fields.join(" ,")} where id = ?", *values, id)
177
+ end
178
+ #
179
+ # return a single row from table based on rowid
180
+ # @param [String] table name
181
+ # @param [Fixnum] rowid
182
+ # @return [Array] resultset (based on arrayfield)
183
+ def sql_select_rowid table, id
184
+ # @db.results_as_hash = true
185
+ return nil if id.nil? or table.nil?
186
+ row = @db.get_first_row( "select * from #{table} where rowid = ?", id )
187
+ return row
188
+ end
189
+
190
+ def separate_field_values array
191
+ fields = []
192
+ values = []
193
+ array.each_with_index do |f, i|
194
+ if i % 2 == 0
195
+ fields << "#{f} = ?"
196
+ else
197
+ values << f
198
+ end
199
+ end
200
+ return fields, values
201
+ end
202
+ def dummy
203
+ id = $num
204
+ status = "OPEN"
205
+ severity = "CRI"
206
+ type = "BUG"
207
+ assigned_to = "rahul"
208
+ start_date = $now
209
+ due_date = $now
210
+ comment_count = 0
211
+ priority = "A"
212
+ title = "some title"
213
+ description = "Some long text fro this bug too"
214
+ fix = nil #"Some long text"
215
+ date_created = $now
216
+ date_modified = $now
217
+ created_by = $default_user
218
+ bugs_insert(status, severity, type, assigned_to, start_date, due_date, comment_count, priority, title, description, fix, created_by)
219
+ #bugs_insert(id, status, severity, type, assigned_to, start_date, due_date, comment_count, priority, title, description, fix, date_created, date_modified)
220
+ end
221
+
222
+ end # class
223
+ end # module
224
+ if __FILE__ == $PROGRAM_NAME
225
+ include Database
226
+ # some tests. change bugs with real name
227
+ db = DB.new
228
+ db.dummy
229
+ puts "\n------------ all row -----\n"
230
+ #db.select "bugs" do |r|
231
+ #puts r
232
+ ##puts r.join(" | ")
233
+ #end
234
+ res = db.select "bugs"
235
+ if res
236
+ puts "count: #{res.count}"
237
+ #puts res.public_methods
238
+ res.each do |e|
239
+ puts e.join(" | ")
240
+ end
241
+ end
242
+ db.sql_update "bugs", 1, "fix", "A fix added at #{$now}"
243
+ db.sql_bugs_update_mult 1, "title", "a new title #{$num}", "description", "a new description #{$num}"
244
+ puts "\n------------ one row -----\n "
245
+ db.select_where "bugs", "id", 1 do |r|
246
+ puts r.join(" | ")
247
+ end
248
+
249
+ end # if