0

This is my pre-commit file (placed in .git/hooks)

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import io
import sys


def replace_strings(filename):
    f = open(filename, 'r')
    configtext  = f.read()
    passwordpos = configtext.find("password");
    newlinepos  = configtext.find("\n", passwordpos)
    passwordold = configtext[passwordpos+10:newlinepos-2]
    passwordnew = "test0000"
    f.close()
    f = open('config.php', 'w')
    #print(passwordpos)
    #print(passwordold)
    #print(configtext)
    configtext = configtext.replace(passwordold, passwordnew)
    #print(configtext)
    f.write(configtext)
    return 1

def main(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('filenames', nargs='*', help='Filenames to fix')
    args = parser.parse_args(argv)

    retv = 0

    for filename in args.filenames:
        if (filename=='config.php'):
            return_value = replace_strings(filename)
            if return_value != 0:
                print('Fixing strings in {}'.format(filename))
            retv |= return_value

    return retv

somehow, it doesn't work. I already checked content of def replace_strings in separate .py file, it works fine enough (instead of argument filename I used "config.php" string). Any help?

TwistedSim
  • 1,960
  • 9
  • 23
Areso
  • 67
  • 1
  • 2
  • 11
  • 1
    Do you call `main()` at the bottom? – Alex Hall May 01 '18 at 17:26
  • @AlexHall No, I don't. It's full script text. I use this one as base https://github.com/pre-commit/pre-commit-hooks/blob/master/pre_commit_hooks/string_fixer.py there is no main() calling. Though https://github.com/pre-commit/pre-commit-hooks/blob/master/pre_commit_hooks/check_xml.py this example it has (probably). Is my base example is incorrect? – Areso May 01 '18 at 18:05
  • @AlexHall I added if __name__ == '__main__': sys.exit(main()) in the end, still no luck. – Areso May 01 '18 at 18:14
  • Does the file have the execute bit set? – Mort May 01 '18 at 20:27
  • @Mort Yes, it was unset. `#!/usr/bin/env python` also was required to run without crashing. – Areso May 02 '18 at 15:55

1 Answers1

0

#!/usr/bin/env python should be first line in case Python hooks.

Areso
  • 67
  • 1
  • 2
  • 11