#!/usr/bin/env python
# TODO: add custom header, footer
# TODO: symlinks or copy param
import os
import shutil
import rospy
import rospkg
rospy.init_node('roswww_static')
rospack = rospkg.RosPack()
www = rospkg.get_ros_home() + '/www'
index_file = rospy.get_param('~index_file', None)
default_package = rospy.get_param('~default_package', None)
shutil.rmtree(www, ignore_errors=True) # reset www directory content
os.mkdir(www)
packages = rospack.list()
index = '
Packages list
\n\n'
for name in packages:
path = rospack.get_path(name)
if os.path.exists(path + '/www'):
rospy.loginfo('found www path for %s package', name)
os.symlink(path + '/www', www + '/' + name)
index += '- {name}
'.format(name=name)
if default_package is not None:
redirect_html = ''.format(name=default_package)
open(www + '/index.html', 'w').write(redirect_html)
elif index_file is not None:
rospy.loginfo('symlinking index file')
os.symlink(index_file, www + '/index.html')
else:
open(www + '/index.html', 'w').write(index)