-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathdetect.py
167 lines (140 loc) · 5.29 KB
/
detect.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import logging
import re
import chardet
yaml_fn_dict = {}
def yaml_visible(fn):
"""Decorator, which allows us to point to function names in YAML-files.
Example: fingerprint: detect_general
"""
yaml_fn_dict[fn.__name__] = fn
return fn
def grep_from_file(version_file, regexp):
"""Grepping file with predefined regexp to find a version. This returns
m.group from regexp: (?P<version>foo)
"""
with open(version_file, 'r', encoding='utf-8') as version_file:
try:
source = version_file.readlines()
except UnicodeDecodeError:
with open(version_file.name, 'rb') as handle:
res = chardet.detect(handle.read())
source = handle.read().decode(res['encoding'])
handle.close()
prog = re.compile(regexp)
for line in source:
match = prog.match(line)
try:
found_match = match.group('version')
return found_match
except re.error:
logging.error('Invalid regular expression: %s', regexp)
except AttributeError:
pass
@yaml_visible
def detect_general(source_file, regexp):
"""Detects from source file if it contains version information. Uses first
regexp-match.
"""
if not (os.path.isfile(source_file) and regexp):
return
return grep_from_file(source_file, regexp[0])
@yaml_visible
def detect_joomla(source_file, regexp):
"""Detects from source file if it contains version information of Joomla"""
if not (os.path.isfile(source_file) and regexp):
return
logging.debug('Dectecting Joomla from: %s', source_file)
release_version = grep_from_file(source_file, regexp[0])
if not release_version:
logging.debug('Could not find release version from: %s', source_file)
return
logging.debug('Release version: %s', release_version)
dev_level_version = grep_from_file(source_file, regexp[1])
if not dev_level_version:
logging.debug('Could not find development version from: %s', source_file)
return
logging.debug('Development level version: %s', dev_level_version)
return release_version + "." + dev_level_version
@yaml_visible
def detect_wikkawiki(source_file, regexp):
"""Detects from file if the file has version information of WikkaWiki.
Wikka-1.3.2-p7/version.php:
$svn_version = '1.3.2';
if (!defined('WAKKA_VERSION')) define('WAKKA_VERSION', $svn_version);
if(!defined('WIKKA_PATCH_LEVEL')) define('WIKKA_PATCH_LEVEL', '7');
"""
if not (os.path.isfile(source_file) and regexp):
return
logging.debug('Dectecting WikkaWiki from: %s', source_file)
version = grep_from_file(source_file, regexp[0])
if not version:
logging.debug('Could not find version from: %s', source_file)
return
logging.debug('Version: %s', version)
patch_level = grep_from_file(source_file, regexp[1])
if not patch_level:
logging.debug('Could not find patch level from: %s', patch_level)
return
logging.debug('Patch level: %s', patch_level)
if version and patch_level:
return version + "-p" + patch_level
@yaml_visible
def detect_gallery(source_file, regexp):
"""Detects from source file if it contains version information of Gallery.
Also ignores Git-versions.
"""
if not (os.path.isfile(source_file) and regexp):
return
logging.debug('Dectecting Gallery from: %s', source_file)
version = grep_from_file(source_file, regexp[0])
if not version:
logging.debug('Could not find version from: %s', source_file)
return
logging.debug('Gallery version %s %s' % (version, source_file))
git_version = grep_from_file(source_file,
'.*?const.*?RELEASE_CHANNEL.*?(?P<version>(git))')
if git_version:
logging.debug('Not reporting Gallery Git-version %s', source_file)
return
else:
return version
@yaml_visible
def detect_redmine(source_file, regexp):
"""Detects from source file if it contains version information. Uses first
regexp-match.
"""
if not (os.path.isfile(source_file) and regexp):
return
with open(source_file) as redmine_changelog:
if not 'Redmine changelog' in redmine_changelog.read():
return
return grep_from_file(source_file, regexp[0])
@yaml_visible
def detect_zenario(source_file, regexp):
if not (os.path.isfile(source_file) and regexp):
return
with open(source_file) as zenario:
if not 'Zenario' in zenario.read():
return
return grep_from_file(source_file, regexp[0])
@yaml_visible
def detect_withoutnewlines(source_file, regexp):
"""Strips newlines from source file."""
if not (os.path.isfile(source_file) and regexp):
return
with open(source_file, 'r') as f:
try:
source = f.read().replace('\n', '')
f.close()
except UnicodeDecodeError:
with open(f.name, 'rb') as handle:
res = chardet.detect(handle.read())
source = handle.read().decode(res['encoding']).replace('\n', '')
handle.close()
try:
return re.compile(regexp[0]).match(source).group('version')
except re.error:
logging.error('Invalid regular expression: %s', regexp)
except AttributeError:
pass