3

I wrote own fixer, how can i run it? I don't find obvious way to do this.

Only this:

> cd /usr/lib/python2.7/lib2to3/fixes/
> ln -s path/to/my_fixer.py

And then run it:

> cd path/to/project
> 2to3 -f my_fixer .
Pyt
  • 1,925
  • 2
  • 13
  • 10
  • 1
    If you are not sure how to run it, you probably shouldn't be creating it. – J0HN Jul 01 '14 at 10:58
  • But i need it! I'm looking for tool for global modifying source code with saving comments, 2to3 seems have appropriate functionality. If you know a answer why you didn't say it? :( – Pyt Jul 01 '14 at 11:05
  • grep + awk + sed + xargs + 2to3? – J0HN Jul 01 '14 at 11:47
  • awk/sed - good tools, but i need calculate some logic in Python. – Pyt Jul 01 '14 at 12:23
  • Ok, what if you just teach your custom converter to operate on single file, get the filename to operate on from stdin, and then use grep/find/ln/whatever to get a list of files in a directory and than feed it to your converter through stdin? – J0HN Jul 01 '14 at 13:20
  • Why i must use custom converter when have standard tool for that? Thanks for comments. But i have found the needed answer. – Pyt Jul 01 '14 at 14:20

2 Answers2

5

I got it! (file: my2to3)

#!/usr/bin/env python2.7
import sys
from lib2to3.main import main

sys.path.append('path/to/my_own_package')
sys.exit(main('my_own_package.contained_fixers'))

And then run it:

> ./my2to3 -f my_fixer -w project
Pyt
  • 1,925
  • 2
  • 13
  • 10
3
  • Create a directory /mypath/custom_fixers
  • Generate a file /mypath/custom_fixers/fix_custom_fixers.py with this content:

    from lib2to3.fixer_base import BaseFix
    from lib2to3.pgen2 import token
    
    class FixCustomFixers(BaseFix):
    
        _accept_type = token.NAME
    
        def match(self, node):
            if node.value == 'oldname':
                return True
            return False
    
        def transform(self, node, results):
            node.value = 'newname'
            node.changed()
    
  • Create a file /mypath/myfixer.py with this content:

    import sys
    from lib2to3.main import main
    sys.exit(main('custom_fixers'))
    
  • Run PYTHONPATH=/mypath python3 /mypath/myfixer.py -f custom_fixers python_file_to_fix.py

c5782522
  • 61
  • 2