Skip to content

Instantly share code, notes, and snippets.

@wolever
Created July 13, 2010 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolever/473832 to your computer and use it in GitHub Desktop.
Save wolever/473832 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Usage: fixpackages SRC_DIR [SRC_DIR ...]
Fix actionscript packages by updating the 'package <name>' statement in
applicable files and updating any import statements which reference the
old package.
To use:
- Move files to their new locations (eg, src/old/Foo.as to src/new/Foo.as)
- Run ./fixpackages src/ test/
Foo.as will have:
package old { ... }
changed to:
package new { ... }
And test/TestFoo.as will have:
import old.Foo;
changed to:
import new.Foo;
"""
from __future__ import with_statement
import os
from os import path
import sys
import re
def warn(msg):
sys.stderr.write(msg + "\n")
def log(msg):
sys.stderr.write(msg + "\n")
def fixpackage(prefix, file, dry_run=True):
fullpath = path.join(prefix, file)
with open(fullpath) as f:
data = f.read()
actual_package_match = re.search("package +([a-zA-Z0-9_.]+)", data)
if not actual_package_match:
warn("Package not found in %r - ignoring." %(fullpath, ))
return (None, None)
actual_package = actual_package_match.group(1).strip()
correct_package = ".".join(file.split("/")[:-1])
if not dry_run and actual_package != correct_package:
data = re.sub(actual_package_match.group(0),
"package " + correct_package, data, count=1)
with open(fullpath + ".tmp", "w") as f:
f.write(data)
os.rename(fullpath + ".tmp", fullpath)
return (actual_package, correct_package)
def fiximports(renames, file):
with open(file) as f:
data = f.read()
num_fixed = [0]
def do_sub(match):
impt = match.group(2)
if impt in renames:
prefix = match.group(1)
num_fixed[0] += 1
return prefix + renames[impt]
return match.group(0)
fixed_data = re.sub("([ \t]*import +)([a-zA-Z0-9_.]+)", do_sub, data)
if fixed_data != data:
with open(file + ".tmp", "w") as f:
f.write(fixed_data)
os.rename(file + ".tmp", file)
return num_fixed[0]
def main():
dry_run = False
renames = {}
dirs = [ dir + (not dir.endswith("/") and "/" or "")
for dir in sys.argv[1:] ]
if len(dirs) == 0:
print __doc__
sys.exit(1)
for dir in dirs:
for root, _, files in os.walk(dir):
for file in files:
if file.endswith(".as"):
file_name = file.split(".")[0]
file = path.join(root, file)
file = file[len(dir):]
(old, new) = fixpackage(dir, file, dry_run)
if old is None or old == new:
continue
renames[old + "." + file_name] = new + "." + file_name
log("Changing package %s --> %s in %s%s"
%(old, new, dir, file))
for dir in (not dry_run and dirs or []):
for root, _, files in os.walk(dir):
for file in files:
if file.endswith(".as") or file.endswith(".mxml"):
count = fiximports(renames, path.join(root, file))
if count > 0:
log("Fixed %d imports in %s"
%(count, path.join(root, file)))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment