# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (C) 2005 The Trustees of Princeton University
#
-# $Id: gen-static-content.py,v 1.35.2.1 2007/02/07 03:27:50 mlhuang Exp $
-#
import os, sys, shutil
import time
import string
import codecs
import socket
-import urllib2
+import urllib.request, urllib.error, urllib.parse
import csv
SCRIPT_PID_FILE= "/var/run/gen-static-content.pid"
f.write( "$node_list_generated_time= '%s';\n" % time_generated )
# Nodes with primary IP addresses in boot state
- nodes_in_boot = filter(lambda node: node['boot_state'] == "boot" and node['ip'],
- all_nodes)
+ nodes_in_boot = [node for node in all_nodes if node['boot_state'] == "boot" and node['ip']]
# Hostnames
all_hosts = [node['hostname'] for node in nodes_in_boot]
f.write( "}\n" )
for group in ['Alpha', 'Beta']:
- if not node_group_nodes.has_key(group):
+ if group not in node_group_nodes:
node_group_nodes[group] = []
# Group nodes with primary IP addresses in boot state
- group_nodes_in_boot = filter(lambda node: node['boot_state'] == "boot" and node['ip'],
- node_group_nodes[group])
+ group_nodes_in_boot = [node for node in node_group_nodes[group] if node['boot_state'] == "boot" and node['ip']]
# Group hostnames
group_hosts = [node['hostname'] for node in group_nodes_in_boot]
f.write( "}\n" )
# All production nodes (nodes not in Alpha or Beta)
- production_nodes_in_boot = filter(lambda node: node not in node_group_nodes['Alpha'] and \
- node not in node_group_nodes['Beta'],
- nodes_in_boot)
+ production_nodes_in_boot = [node for node in nodes_in_boot if node not in node_group_nodes['Alpha'] and \
+ node not in node_group_nodes['Beta']]
production_hosts = [node['hostname'] for node in production_nodes_in_boot]
f.write( "elseif( $which_node_list == 'production_hosts' )\n" )
url = url + '/slice.php?csv=1&start_time=2+days+ago'
if slices:
url = url + '&slices[]=' + '&slices[]='.join(slices)
- stats = urllib2.urlopen(url)
+ stats = urllib.request.urlopen(url)
fields = ['slice', 'flows', 'packets', 'bytes', 'src_ips',
'dst_ips', 'top_dst_ip', 'top_dst_ip_bytes']
rows = csv.DictReader(stats, fields)
f.write("<?php\n")
f.write("$planetflow = array(\n")
for row in rows:
- if row.has_key('slice'):
+ if 'slice' in row:
f.write("'%s' => array(\n" % row['slice'])
for field in fields:
- if row.has_key(field) and \
+ if field in row and \
row[field] is not None and \
row[field] != "":
if type(row[field]) == type(0):
if os.access(SCRIPT_PID_FILE, os.R_OK):
pid= string.strip(file(SCRIPT_PID_FILE).readline())
- if pid <> "":
+ if pid != "":
if os.system("/bin/kill -0 %s > /dev/null 2>&1" % pid) == 0:
sys.exit(0)
if nodenetwork['is_primary']:
node['ip'] = nodenetwork['ip']
break
- except IndexError, KeyError:
+ except IndexError as KeyError:
continue
# Get list of nodes in each node group
for group in all_groups:
- nodes_in_group = filter(lambda node: node['node_id'] in group['node_ids'], all_nodes)
+ nodes_in_group = [node for node in all_nodes if node['node_id'] in group['node_ids']]
node_group_nodes[group['tagname']] = nodes_in_group
# generate the static content files
output_file_path= "%s/%s" % (GENERATED_OUTPUT_PATH,file_name)
tmp_output_file_path= output_file_path + '.tmp'
tmp_output_file= codecs.open( tmp_output_file_path, encoding = 'utf-8', mode = "w" )
- except IOError, err:
- print( "Unable to open file %s for writing." % output_file_path )
+ except IOError as err:
+ print(( "Unable to open file %s for writing." % output_file_path ))
continue
try:
func(tmp_output_file)
tmp_output_file.flush()
shutil.copyfile( tmp_output_file_path, output_file_path )
- except Exception, e:
- print "Unable to get content for file: %s" % file_name, e
+ except Exception as e:
+ print("Unable to get content for file: %s" % file_name, e)
import traceback
traceback.print_exc()