1# Copyright (C) 2018 Open Information Security Foundation
2#
3# You can copy, redistribute or modify this Program under the terms of
4# the GNU General Public License version 2 as published by the Free
5# Software Foundation.
6#
7# This program is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10# GNU General Public License for more details.
11#
12# You should have received a copy of the GNU General Public License
13# version 2 along with this program; if not, write to the Free Software
14# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15# 02110-1301, USA.
16
17import os.path
18import sys
19
20try:
21    from urllib2 import urlopen
22except:
23    from urllib.request import urlopen
24
25import yaml
26
27DEFAULT_URL = "https://raw.githubusercontent.com/oisf/suricata-intel-index/master/index.yaml"
28
29def embed_index():
30    """Embed a copy of the index as a Python source file. We can't use a
31    datafile yet as there is no easy way to do with distutils."""
32    if len(sys.argv) > 1:
33        url = sys.argv[1]
34    else:
35        url = DEFAULT_URL
36    dist_filename = os.path.join(os.path.dirname(__file__), "index.py")
37    response = urlopen(url)
38    index = yaml.safe_load(response.read())
39
40    # Delete the version info to prevent the issue of the version info being out of
41    # date around a new release of Suricata where the index has not been updated
42    # to the latest recommended version.  The user will be asked to update their
43    # sources to run the version check.
44    del(index["versions"])
45
46    with open(dist_filename, "w") as fileobj:
47        fileobj.write("index = %s" % (str(index)))
48
49if __name__ == "__main__":
50    embed_index()
51