Newer
Older
Cyril Bay
a validé
format = FORMATS[format_name]
config.format = format
config.format_regexp = re.compile(format)
# Make sure the format is compatible with the resolver.
resolver.check_format(format)
stats.count_lines_parsed.increment()
if stats.count_lines_parsed.value <= config.options.skip:
continue
match = config.format_regexp.match(line)
if not match:
invalid_line(line)
continue
hit = Hit(
filename=filename,
lineno=lineno,
status=match.group('status'),
full_path=match.group('path'),
)
# Strip query string
if config.options.strip_query_string:
hit.path = hit.full_path.split('?', 1)[0]
else:
hit.path = hit.full_path
# Parse date _with_ timezone to get an UTC timestamp.
date_string = match.group('date')
try:
tz = float(date_string[-5:])
hit.date = datetime.datetime.strptime(date_string[:-6], '%d/%b/%Y:%H:%M:%S')
except ValueError:
# Date format is incorrect, the line is probably badly formatted.
invalid_line(line)
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
continue
hit.date -= datetime.timedelta(hours=tz/100)
try:
hit.referrer = match.group('referrer')
except IndexError:
hit.referrer = ''
if hit.referrer == '-':
hit.referrer = ''
try:
hit.user_agent = match.group('user_agent')
except IndexError:
hit.user_agent = ''
hit.ip = match.group('ip')
try:
hit.length = int(match.group('length'))
except ValueError:
# Not all lines have a length (e.g. 304 redirects)
hit.length = 0
try:
hit.host = match.group('host')
except IndexError:
# Some formats have no host.
pass
# Check if the hit must be excluded.
check_methods = inspect.getmembers(self, predicate=inspect.ismethod)
if all((method(hit) for name, method in check_methods if name.startswith('check_'))):
Recorder.add_hit(hit)
def main():
"""
Start the importing process.
"""
if config.options.show_progress:
stats.start_monitor()
stats.set_time_start()
recorders = Recorder.launch(config.options.recorders)
for filename in config.filenames:
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
Recorder.wait_empty()
stats.set_time_stop()
if config.options.show_progress:
stats.stop_monitor()
try:
Recorder.invalidate_reports()
except Piwik.Error, e:
pass
stats.print_summary()
def fatal_error(error, filename=None, lineno=None):
print >> sys.stderr, 'Fatal error: %s' % error
if filename and lineno is not None:
print >> sys.stderr, (
'You can restart the import of "%s" from the point it failed by '
'specifying --skip=%d on the command line.\n' % (filename, lineno)
)
os._exit(1)
if __name__ == '__main__':
try:
piwik = Piwik()
config = Configuration()
stats = Statistics()
resolver = config.get_resolver()
parser = Parser()
main()
except KeyboardInterrupt:
pass