-
Notifications
You must be signed in to change notification settings - Fork 0
/
v.py
77 lines (65 loc) · 2.11 KB
/
v.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Print all py source codes and versions
"""
import os
import re
from collections import defaultdict
__version__ = '1.0.21'
re_vnr = re.compile('(\d{1,3}\.\d{1,3}\.\d{1,3})')
re_version = re.compile('(?i)version')
re_ftype = re.compile('(\.\w+)$')
include_ftypes = ['.py', '.json', '.html', '.sh']
def get_allfiles(path, extension=''):
'''get all files'''
allfiles = list()
for dirpath, dirnames, filenames in os.walk(path):
files = [file for file in filenames if file.endswith(extension)]
allfiles.extend([os.path.join(dirpath, file) for file in files])
allfiles.sort()
return allfiles
def get_ftype(fname):
'''Return file type'''
try:
return re.search(re_ftype, fname).group()
except AttributeError:
return ''
return
def check_file(ffname) -> str:
'''Open file and read version'''
txt, res = None, None
with open(ffname, 'rb') as file:
for line in file:
try:
txt = line.decode('utf8').lower()
except UnicodeDecodeError:
return ffname, None
if re.search(re_version, txt):
try:
res = re.search(re_vnr, txt).group()
except AttributeError:
pass
else:
break
return res, get_ftype(ffname)
if __name__ == '__main__':
cwd = os.getcwd()
res = defaultdict(list)
files = get_allfiles(cwd)
len_cwd = len(cwd) + 1
files = [file for file in files if get_ftype(file) in include_ftypes]
for file in files:
vnr, ftype = check_file(file)
if vnr is None:
vnr = 'Missing'
if ftype and vnr:
res[ftype].append((file[len_cwd:], vnr))
n = 80
os.system('cls' if os.name=='nt' else 'clear')
print()
print(f'v.py {__version__} '.center(n, '-'))
for key in res.keys():
print(f' {key} '.center(n, '-'))
for fn, vnr in res[key]:
print(f' {vnr:9s} : {fn}')
print(f'-'.center(n, '-'))