1 ##############################################################################
2 # Copyright (c) 2015 Todd Gaunt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
15 """Context manager for changing the current working directory"""
16 def __init__(self, new_path):
17 self.new_path = os.path.expanduser(new_path)
20 self.saved_path = os.getcwd()
21 os.chdir(self.new_path)
23 def __exit__(self, etype, value, traceback):
24 os.chdir(self.saved_path)
26 def approxsize(x, y, deviation):
27 """Approximately compares 'x' to 'y' with in % of 'deviation'"""
28 logger = logging.getLogger(__name__)
30 dev = (y * .01 * deviation)
32 if x >= round(y - dev, 0) and x <= round(y + dev, 0):
33 logger.debug("{} is approximately {}".format(x, y))
36 logger.debug("{} is not approximately {}".format(x, y))
40 """Reads from a socket until a \n character or 512 bytes have been read,
41 whichever comes first"""
45 while (c != "\n" and reads < 512):
46 # Decode bytes to str, sockets output bytes which aren't pretty
47 c = sock.recv(1).decode("utf-8")
48 #print("char: '" + c + "'") # Debugging code
54 """Reads a message prefixed with a number and a newline char, eg. "20\n"
55 then reads x lines, where x is equal to the number in the first line."""
56 # Read the socket once initially for the line count
58 buf = buf[:-1] # Cut off the '\n' character
62 for i in range(length):
63 lines.append(read_line(sock))
66 def send_msg(sock, msg):
67 """Sends a message to a socket"""
68 # Encode python str to bytes beforehand, sockets only deal in bytes
69 msg = bytes(msg, "utf-8")
71 while totalsent < len(msg):
72 sent = sock.send(msg[totalsent:])
75 totalsent = totalsent + sent
78 def get_addr(interface):
79 """Get the address of the machine that this program is running on"""
80 return netifaces.ifaddresses(interface)[netifaces.AF_INET][0]["addr"]
82 def gen_ip_range(cidr, excluded, minimum, maximum ):
83 """Takes a network cidr number, and then a min max value, and creates a list
84 of ip addresses avalable on [a,b]. Also removes "excluded" addresses
86 logger = logging.getLogger(__name__)
87 # Generate a list of available ip addresses for the dhcp server
88 ip_range = list(map(lambda x: x.exploded, ipaddress.ip_network(cidr).hosts()))
91 # Remove the value from the list, if it isn't in the list then whatever
95 logger.debug("{} not in ip_range, cannot remove".format(addr))
97 # Remove all values before the minimum usable value
98 for i in range(len(ip_range)):
99 if ip_range[i] == minimum:
100 ip_range = ip_range[i::]
102 # Remove all values after the maximum usable value
103 for i in range(len(ip_range)):
104 if ip_range[i] == maximum:
105 ip_range = ip_range[0:i+1]