Pastes

[PY3K] FindGlobals

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
################################################################################
###################### DO NOT RUN FROM AN ELEVATED SHELL! ######################
################################################################################

################################################################################
#
# Copyright (c) 2012 Robert Beam (aka lightsfuryuther)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################

import subprocess
import os
import re

# Configurable variables
luaPath = "lua"
luacPath = "luac"
findGlobalsPath = "find-globals.lua"

fileMatcher = re.compile(r'\.lua$', re.IGNORECASE)
outputMatcher = re.compile(r'\[(\d+)\][^;]*; ([^\r\n]*)')

# Make sure our path segments have seperators
def fixPath(path):
  if not (path[-1] in ['\\', '/']):
    path += '/'
  return path

# Walk a directory and enumerate its contents that match fileMatcher
def findFiles(path = '.'):
  files = []
  path = fixPath(path)
  for directory in os.walk(path):
    files += [
      fixPath(directory[0]) + file
        for file in directory[2]
          if fileMatcher.search(file)]
    if len(directory[1]) > 0:
      for newDir in directory[1]:
        files += findFiles(newDir)
  return files

# Run the find-globals scripts.
# Based on find-globals.bat by Morkser at 
# "http://www.wowace.com/addons/findglobals/#c2"
def runFindGlobals(file, lua = luaPath, luac = luacPath, findGlobals = findGlobalsPath):
  command = r'%{luac}s -p -l %{file}s | %{lua}s %{findGlobals}s %{file}s' %
    {'lua':lua, 'luac':luac, 'findGlobals':findGlobals, 'file':file}
  proc = subprocess.Popen(args=command, stdout=subprocess.PIPE, shell=True)
  return proc.stdout.read()

def searchGlobals():
  files = findFiles()
  symbols = {}
  
  def addSymbol(symbol, file, line):
    if symbol in symbols:
      symbolObject = symbols[symbol]
      if file in symbolObject['lines']:
        symbolObject['lines'][file].append(line)
      else:
        symbolObject['lines'][file] = [line]
    else:
      symbols[symbol] = {"lines": {file: [line]}}
  
  for file in files:
    proc = subprocess.Popen(args=['find-globals.bat', file],
      stdout=subprocess.PIPE)
    data = proc.stdout.read()
    matches = outputMatcher.findall(data)
    if matches:
      for match in matches:
        addSymbol(match[1], file, match[0])
  
  for symbol in symbols:
    symbolObject = symbols[symbol]
    print("Global '%s' accessed at:" % (symbol))
    for file in symbolObject['lines']:
      for line in symbolObject['lines'][file]:
        print("  %s at line %s" % (file, line))

if __name__ == "__main__":
  searchGlobals()

Facts

Date posted
Sep 06, 2012
Language
Python 3