-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcd.py
63 lines (53 loc) · 2.07 KB
/
pcd.py
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
#!/usr/bin/env python3
import argparse
from os import walk
from os import path
from sys import exit
def dir_is_subdir(curdir, dirs):
for d in dirs:
if d in curdir:
return True
return False
parser = argparse.ArgumentParser(description='PHP4 Constructor Detector')
parser.add_argument('-r', '-R', '--recursive', action='store_true', help='Recursively scan directory')
parser.add_argument('-i', '--ignore', help='Directories to ignore')
parser.add_argument('dir', help='Directory to scan')
args = parser.parse_args()
if not path.isdir(args.dir):
print('%s is not a valid directory' % args.dir)
exit()
args.ignore = args.ignore.strip(' \t\n\r\\/').split(',') if args.ignore else []
files_using_php4_constructors = []
for (dirpath, dirnames, filenames) in walk(args.dir):
if dir_is_subdir(dirpath, args.ignore):
continue
print('\nScanning directory: %s' % dirpath)
for fl in filenames:
if not '.php' in fl:
continue
try:
with open(dirpath + '\\' + fl) as f:
class_name = ''
for line in f:
l = line.strip()
if l.startswith('class '):
class_name = l.replace('class ', '')
class_name = class_name[:class_name.find(' ')]
elif class_name and l.startswith('function '):
function_name = l.replace('function ', '')
function_name = function_name[:function_name.find('(')]
if function_name == class_name:
files_using_php4_constructors.append(dirpath + '\\' + fl)
except FileNotFoundError:
print('Could not find file %s. Skipping...' % (dirpath + '\\' + fl))
except UnicodeDecodeError:
continue
if not args.recursive:
break
print('\n\nFiles using PHP4 constructors')
print('-----------------------------')
if not files_using_php4_constructors:
print('None')
else:
for f in set(files_using_php4_constructors):
print(f)