-
Notifications
You must be signed in to change notification settings - Fork 1
/
github-tree.py
executable file
·187 lines (155 loc) · 5.86 KB
/
github-tree.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016-2018 Libresoft, GSyC (URJC).
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA.
#
# Authors:
# Miguel Angel Fernandez Sanchez <[email protected]>
# Gregorio Robles Martinez <[email protected]>
#
import argparse
import json
import logging
import os
import sys
import yaml
DESC_MSG = 'Look for patterns and heuristics into Git-trees and return a list of positive results'
def main(args):
logger.info('GitHub-Tree starts...')
with open(os.path.abspath(args.heuristics_file), 'r') as hfile:
try:
heuristics = yaml.load(hfile)
except yaml.YAMLError as e:
logger.error(e)
raise SystemExit
logger.info("Looking for JSON files into: %s" % args.trees_path)
repo_jsons = os.listdir(args.trees_path)
with open(args.out_file, 'w') as ofile:
for jsonfile_path in repo_jsons:
jsonfile = "%s/%s" % (os.path.abspath(args.trees_path), jsonfile_path)
(owner_id, repo_id) = jsonfile_path.split(":")
repo_id = repo_id[:-5]
logger.debug("Opening %s" % jsonfile)
with open(jsonfile, 'r') as data_file:
data = json.load(data_file)
try:
tree = data["tree"]
except KeyError:
logger.warning("KeyError in file: %s" % jsonfile)
continue
for file_dict in tree:
if file_dict["type"] != "tree":
try:
if ("path" in file_dict) and ("url" in file_dict):
if interesting(file_dict["path"], heuristics):
ofile.write("%s, %s\r\n" %(file_dict["path"], file_dict["url"]))
else:
pass
except UnicodeEncodeError:
logger.error("UnicodeEncodeError in file: %s" % jsonfile)
def interesting(path, heuristics):
ext = extension(path)
if ext in heuristics['level-one_exts']:
return 1
if ext in heuristics['level-two_exts']:
for keyword in heuristics['keywords']:
if keyword in filename(path):
return 1
return 0
else:
return 0
def extension(path):
""""
Given a path, return its extension
"""
tmp_list = path.split('.')
if len(tmp_list) > 1:
return tmp_list[-1].lower()
else:
return ""
def filename(path):
""""
Given a path, return its filename (without extension)
"""
tmp_list = path.split('/')
if len(tmp_list) > 1:
full_name = tmp_list[-1] # with extension
if '.' in full_name:
full_name = '.'.join(full_name.split('.')[:-1])
return full_name.lower()
else:
return ""
def tree(path):
""""
Given a path, return its tree (without the final filename)
"""
tmp_list = path.split('/')
if len(tmp_list) > 1:
return '/'.join(tmp_list[:-1])
else:
return ""
logger = logging.getLogger(__name__)
def configure_logging(log_file, debug_mode_on=False):
"""Set up the logging and returns a list with the file descriptors
:param log_file: Path for the log file
:param debug_mode_on: If True, the level of the logger will be DEBUG
:return: List with logging file descriptors
"""
if debug_mode_on:
logging_mode = logging.DEBUG
else:
logging_mode = logging.INFO
logger = logging.getLogger()
logger.setLevel(logging_mode)
# redirect logging to our log file
fh = logging.FileHandler(log_file, 'a')
fh.setLevel(logging_mode)
# create console handler
ch = logging.StreamHandler()
ch.setLevel(logging_mode)
# create formatter and add it to the handlers
formatter = logging.Formatter("[%(asctime)s - %(levelname)s] %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
keep_fds = [fh.stream.fileno()]
return keep_fds
def parse_args():
"""Parse arguments from the command line"""
parser = argparse.ArgumentParser(description=DESC_MSG)
parser.add_argument('--heuristics-file', dest='heuristics_file', required=True,
help='File with patterns and other heuristics')
parser.add_argument('--trees-path', dest='trees_path', required=True,
help='Path to folder containing trees information')
parser.add_argument('--log-file', dest='log_file', default='github-tree.log',
required=False, help='Log file')
parser.add_argument('--output-file', dest='out_file', default='hits.txt',
required=False, help='Path to output hits file')
parser.add_argument('-g', '--debug', dest='debug_mode_on', action='store_true',
default=False, help='Enables debug mode')
return parser.parse_args()
if __name__ == '__main__':
try:
args = parse_args()
keep_fds = configure_logging(args.log_file, args.debug_mode_on)
main(args)
except Exception as e:
logger.exception("Exception message:")
s = "Error: %s github-tree is exiting now." % str(e)
logger.error(s)
sys.exit(1)