File: config.py

package info (click to toggle)
pyrit 0.5.1%2Bgit20180801-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,596 kB
  • sloc: python: 4,667; ansic: 3,352; cpp: 1,490; asm: 394; lisp: 192; makefile: 15; sed: 7; sh: 6
file content (78 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download
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
# -*- coding: UTF-8 -*-
#
#    Copyright 2015, John Mora, johmora12@engineer.com
#    Original Work by Lukas Lueg (c) 2008-2011.
#
#    This file is part of Pyrit.
#
#    Pyrit 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.
#
#    Pyrit 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 Pyrit.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import with_statement

import os
import sys


def default_config():
    config = {'default_storage': 'file://',
              'use_CUDA': 'false',
              'use_OpenCL': 'false',
              'rpc_server': 'false',
              'rpc_announce': 'true',
              'rpc_announce_broadcast': 'false',
              'rpc_knownclients': '',
              'workunit_size': '75000',
              'limit_ncpus': 0}
    return config


def read_configfile(filename):
    config = default_config()
    with open(filename, 'rb') as f:
        for line in f:
            if line.startswith('#') or '=' not in line:
                continue
            option, value = map(str.strip, line.split('=', 1))
            if option in config:
                config[option] = value
            else:
                print >> sys.stderr, "WARNING: Unknown option '%s' " \
                                    "in configfile '%s'" % (option, filename)
    return config


def write_configfile(config, filename):
    with open(filename, 'wb') as f:
        for option, value in sorted(config.items()):
            f.write("%s = %s\n" % (option, value))


configpath = os.path.expanduser(os.path.join('~', '.pyrit'))
default_configfile = os.path.join(configpath, 'config')

if 'PYRIT_CONFIG_FILE' in os.environ:
    custom_config = os.environ['PYRIT_CONFIG_FILE']
    if not os.path.exists(custom_config):
        print >> sys.stderr, \
            "WARNING: custom config file %s does not exist" % custom_config
    else:
        cfg = read_configfile(custom_config)
else:
    if os.path.exists(default_configfile):
        cfg = read_configfile(default_configfile)
    else:
        cfg = default_config()
        if not os.path.exists(configpath):
            os.makedirs(configpath)
        write_configfile(cfg, default_configfile)