Skip to content

Commit

Permalink
merge of python support
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfeathers committed Nov 27, 2016
2 parents bbd6cf2 + 83bd274 commit 420e833
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.scythe_probe
todo
*.pyc
__pycache__
9 changes: 5 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ about whether to attempt to delete code or do further analysis.

== FEATURES/PROBLEMS:

* Currently only supports Ruby
* Currently only supports Ruby and Python 3.x

== SYNOPSIS:

To use Scythe, place calls in your code of the following form
To use Scythe, place calls in your Ruby/Python code of the following form

scythe_probe("probe_name")

Expand All @@ -34,7 +34,7 @@ with the gather option (-g) and a directory path:

scythe -g .

Scythe will recursively interate over all Ruby files in the specified
Scythe will recursively iterate over all Ruby and Python files in the specified
directory and record the addition of any probe calls. Ideally, scythe -g
should be run as part of your build.

Expand All @@ -53,7 +53,7 @@ errors.

== FUTURE DIRECTIONS:

* Support for other languages (Javascript, Java, Python, etc)
* Support for other languages (Javascript, Java, C etc)
* Record number of calls and stack traces
* statsd support

Expand All @@ -80,6 +80,7 @@ Run scythe -h to see command line options
== REQUIREMENTS:

* Ruby >= 2.30
* OPTIONAL: Python 3.x to use the python probe

== LICENSE:

Expand Down
10 changes: 7 additions & 3 deletions lib/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

require 'find'

RUBY_MARKER_PATTERN = /scythe_probe\s*\(\s*\"(\w+)\"\s*\)/
# Note: RUBY_MARKER_PATTERN also works for python
RUBY_MARKER_PATTERN = /scythe_probe\s*\s*\("(\w+)\"\)s*/
# matches ruby functions of the form scythe_probe "param" (without parentheses)
RUBY_ALT_MARKER_PATTERN = /scythe_probe\s*\s*\"(\w+)\"\s*/
PROBE_EXT_PATTERN = /\.scythe_probe$/
PROBE_EXT = ".scythe_probe"


def probe_env_var
ENV["SCYTHE_PROBE_DIR"]
end
Expand All @@ -25,7 +27,9 @@ def file_names file_spec
end

def markers fn
IO.read(fn).scan(RUBY_MARKER_PATTERN).flatten
matching = IO.read(fn).scan(RUBY_ALT_MARKER_PATTERN).flatten
matching += IO.read(fn).scan(RUBY_MARKER_PATTERN).flatten
return matching
rescue
[]
end
Expand Down
14 changes: 13 additions & 1 deletion lib/probe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def days_silent? date_now
return (seconds_silent?(date_now) / 86400).to_i
end

def hours_silent? date_now
return (seconds_silent?(date_now) / 3600).to_i
end

def seconds_silent? date_now
check_epoch = Time.at(date_now)
mod_epoch = Time.at(@mod_date)
Expand All @@ -24,7 +28,15 @@ def seconds_silent? date_now
end

def silent? date_now, interval
interval == :seconds ? seconds_silent?(date_now) : days_silent?(date_now)
#interval == :seconds ? seconds_silent?(date_now) : days_silent?(date_now)
case interval
when :seconds
seconds_silent?(date_now)
when :hours
hours_silent?(date_now)
else
days_silent?(date_now)
end
end

end
24 changes: 18 additions & 6 deletions lib/scythe
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ OptionParser.new do |opts|

project_dir = File.expand_path(dir)

# regex now considers .py files
probe_markers =
file_names(project_dir).grep(/\.rb$/)
file_names(project_dir).grep(/[\.rb$ \.py$]/)
.flat_map {|fn| markers(fn) }

#puts probe_markers

duplicated_markers =
probe_markers.group_by {|m| m }
Expand Down Expand Up @@ -77,9 +80,11 @@ OptionParser.new do |opts|

end

opts.on("-s [DURATION]",
"--status [DURATION]",
"show status of probes") do |format|
opts.on("-s [secs | h]",
"--status [secs | h]",
"shows the elapsed time since the last call for each probe (in days).",
"secs: show time in seconds",
"h: show time in hours") do |format|


unless probe_env_var && File.directory?(probe_env_var)
Expand All @@ -90,8 +95,15 @@ OptionParser.new do |opts|

now = Time.now.to_i

reporting_interval = format == "secs" ? :seconds : :days

#reporting_interval = (format == "secs") ? :seconds : :days
case format
when "secs"
reporting_interval = :seconds
when "h"
reporting_interval = :hours
else
reporting_interval = :days
end
probes = get_probes(probe_dir)

probes.map {|probe| [probe.name, probe.silent?(now, reporting_interval)]}
Expand Down
21 changes: 21 additions & 0 deletions probes/python_probe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os

def scythe_probe(marker):
# make sure the environ is set
try:
directory = os.environ["SCYTHE_PROBE_DIR"]
except:
return

# make sure the directory is valid
if not os.path.isdir(directory):
return

filename = os.path.join(directory, marker + ".scythe_probe")
if not os.path.exists(filename):
#print(filename + " invalid")
return

os.utime(filename)


2 changes: 0 additions & 2 deletions probes/ruby_probe.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


require 'fileutils'

def scythe_probe marker
Expand Down
13 changes: 13 additions & 0 deletions spec/data/bad/duplicate_markers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys
sys.path.append("../../../probes")

from python_probe import scythe_probe

if True:
scythe_probe("marker0_py")

scythe_probe("marker0_py")
scythe_probe("marker1_py")
scythe_probe("marker1_py")

scythe_probe("marker2_py")
2 changes: 2 additions & 0 deletions spec/data/bad/duplicate_markers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ def scythe_probe name

if 1 < 2
scythe_probe("marker0")
scythe_probe "marker2"
else
scythe_probe("marker0")
scythe_probe("marker0")
scythe_probe("marker1")
scythe_probe("marker1")
scythe_probe "marker2"
end
9 changes: 9 additions & 0 deletions spec/data/good/simple_branch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sys
sys.path.append("../../../probes")

from python_probe import scythe_probe

if 1 * 2 == 2:
scythe_probe("marker0_py")
else:
scythe_probe("marker1_py")
3 changes: 3 additions & 0 deletions spec/data/good/simple_branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

if ARGV.empty?
scythe_probe("program_path")
scythe_probe "paran_less"
else
scythe_probe("non_program_path")
scythe_probe "paran_less2"
end


0 comments on commit 420e833

Please sign in to comment.