forked from eReader/detekt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (47 loc) · 1.82 KB
/
Copy pathutils.py
File metadata and controls
56 lines (47 loc) · 1.82 KB
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
# Copyright (C) 2014 Claudio Guarnieri.
# This file is part of Detekt - https://github.com/botherder/detekt
# See the file 'LICENSE' for copying permission.
import os
import sys
import ctypes
import hashlib
def get_resource(relative):
# First try from the local directory. This might come handy in case we want
# to provide updates or allow the user to run custom signatures.
path = os.path.join(os.getcwd(), relative)
# In case the resource doesn't exist in the current directory, we'll try
# from the actual resources.
if not os.path.exists(path):
if hasattr(sys, '_MEIPASS'):
path = os.path.join(sys._MEIPASS, relative)
return path
# Not currently used. Might use this in the future to automatically determine
# which language to use for logging messages.
#def get_language():
# return ctypes.windll.kernel32.GetUserDefaultUILanguage()
def check_connection():
# Check if there is an active Internet connection.
# This might not be 100% reliable.
if ctypes.windll.wininet.InternetGetConnectedState(None, None):
return True
else:
return False
# Get MD5 hash of a given buffer.
def get_md5(data):
md5 = hashlib.md5()
md5.update(data)
return md5.hexdigest()
# Snippet taken from:
# https://gist.github.com/sbz/1080258
def hexdump(src, length=16, maxlines=None):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
lines = []
for c in xrange(0, len(src), length):
chars = src[c:c+length]
hex = ' '.join(["%02x" % ord(x) for x in chars])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars])
lines.append("%04x %-*s %s" % (c, length*3, hex, printable))
if maxlines:
if len(lines) == maxlines:
break
return lines