2021-03-30 16:52:20 +02:00
#!/usr/bin/env python3
2016-02-22 20:49:39 +00:00
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 Chaoyi Zha <cydrobolt@fedoraproject.org>
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
2021-03-30 16:55:21 +02:00
import grp
import hashlib
2021-03-31 15:04:21 +02:00
import logging
2021-03-30 16:55:21 +02:00
import os
2021-03-31 14:58:28 +02:00
import pwd
2021-03-30 16:55:21 +02:00
import stat
2021-03-31 14:58:28 +02:00
from pathlib import Path
2021-03-30 16:55:21 +02:00
2016-02-22 20:49:39 +00:00
from jinja2 import Template
2021-03-30 16:55:21 +02:00
2021-03-31 15:04:21 +02:00
log = logging . getLogger ( __name__ )
2016-02-22 20:49:39 +00:00
page_jinja_template = """
2016-02-23 21:06:08 +00:00
< ! DOCTYPE html >
2016-02-22 20:49:39 +00:00
< html >
< head >
< title > Fedora People < / title >
< link rel = ' stylesheet ' href = ' /static/datatables.min.css ' >
< link rel = ' stylesheet ' href = ' //getfedora.org/static/css/bootstrap-theme.css ' >
< meta name = " viewport " content = " width=device-width, initial-scale=1 " >
< style >
2021-03-31 14:37:58 +02:00
. bg - fedora - blue {
background : #3c6eb4;
}
2016-02-22 20:49:39 +00:00
. center {
text - align : center ;
}
footer {
margin - bottom : 45 px ;
}
. user - avatar {
display : inline ;
height : 20 px ;
}
< / style >
< / head >
< body >
2021-03-31 14:37:58 +02:00
< div class = " jumbotron bg-fedora-blue " >
2016-02-22 20:49:39 +00:00
< div class = " container-fluid center " >
2021-03-31 14:37:58 +02:00
< img class = " fedoralogotext " class = " img-responsive center-block " src = " //getfedora.org/static/images/fedora-logotext-white.png " alt = " Fedora Logotext " >
2016-02-22 20:49:39 +00:00
< / div >
< / div >
< div class = " container pagebody " >
< h3 > Fedora People < / h3 >
< p > A repository with web and < code > git < / code > resources from the people behind Fedora . < / p >
< p class = ' text-muted ' >
< a target = ' _blank ' href = ' //fedoraproject.org/wiki/Infrastructure/fedorapeople.org ' > FAQ < / a > on using your public space .
< / p >
< table class = ' table table-hover ' id = ' peopleTable ' >
< thead >
< tr >
< th > Name < / th >
< th > Public Resources < / th >
< / tr >
< / thead >
< tbody >
{ % for username , user in users % }
< tr >
< td >
< img class = ' user-avatar ' src = ' /static/grey.jpg ' alt = ' Avatar for {{ username}} ' data - src = ' https://seccdn.libravatar.org/avatar/ {{ user[ ' openid_hash ' ]}}?s=20&d=retro ' >
{ { user . name . strip ( ) } } < span class = ' text-muted ' > ( { { username } } ) < / span >
< / td >
< td >
{ % if user [ ' has_public_git ' ] % }
< div >
< a href = " https://fedorapeople.org/cgit/ {{ username}}/ " > Git repositories < / a >
< / div >
{ % endif % }
{ % if user [ ' has_public_html ' ] % }
< div >
< a href = " https:// {{ username}}.fedorapeople.org " > { { username } } ' s homepage</a>
< / div >
{ % endif % }
< div >
< a href = " https://fedoraproject.org/wiki/user: {{ username}} " > { { username } } ' s wiki page</a>
< / div >
< / td >
< / tr >
{ % endfor % }
< / tbody >
< / table >
< / div >
< hr >
< footer class = ' center text-muted ' >
< p class = " copy " >
& copy ; 2016 Chaoyi Zha , Red Hat , Inc . , and others .
Please send any comments or corrections to the < a href = " mailto:admin@fedoraproject.org " > infrastructure team < / a > .
< / p >
< p class = " disclaimer " >
The Fedora Project is maintained and driven by the community and sponsored by Red Hat . This is a community maintained site . Red Hat is not responsible for content .
< / p >
< a href = " http://fedoraproject.org/wiki/Legal:Main " > Legal < / a > & middot ; < a href = " http://fedoraproject.org/wiki/Legal:Trademark_guidelines " > Trademark Guidelines < / a >
< / footer >
< script src = ' /static/jquery.dataTables.min.js ' > < / script >
< script src = ' /static/jquery.unveil.js ' > < / script >
< script >
$ ( document ) . ready ( function ( ) {
$ ( ' table ' ) . DataTable ( {
' pageLength ' : 50 ,
' initComplete ' : function ( settings , json ) {
$ ( ' img ' ) . unveil ( ) ;
}
} ) ;
$ ( ' .table ' ) . on ( ' draw.dt ' , function ( ) {
/ * on each table draw * /
$ ( ' img ' ) . unveil ( ) ;
} ) ;
} ) ;
< / script >
< / body >
< / html >
"""
2021-03-31 14:58:28 +02:00
topdir = Path ( " /home/fedora " )
homedirs = sorted ( d for d in topdir . glob ( " * " ) if d . is_dir ( ) )
2016-02-22 20:49:39 +00:00
2021-03-31 14:58:28 +02:00
users = { }
2016-02-22 20:49:39 +00:00
2021-03-31 14:58:28 +02:00
for hdir in homedirs :
2021-03-31 15:04:21 +02:00
if hdir . stat ( ) . st_uid == 0 :
log . info ( " ' %s ' is owned by root. Skipping. " , hdir . name )
continue
try :
owner_name = hdir . owner ( )
except KeyError :
log . warning ( " ' %s ' is not owned by a named user. Skipping. " , hdir . name )
continue
if owner_name != hdir . name :
log . warning ( " ' %s ' is owned by ' %s ' . Skipping. " , hdir . name , owner_name )
continue
2021-03-31 14:58:28 +02:00
username = hdir . name
user = { }
2016-02-22 20:49:39 +00:00
2021-03-31 15:04:21 +02:00
try :
pwentry = pwd . getpwnam ( username )
except KeyError :
log . warning ( " User not found: %s . Skipping. " , username )
continue
2016-02-22 20:49:39 +00:00
2021-03-31 14:58:28 +02:00
user [ " name " ] = pwentry . pw_gecos
user [ " has_public_html " ] = ( hdir / " public_html " ) . is_dir ( )
user [ " has_public_git " ] = ( hdir / " public_git " ) . is_dir ( )
user [ " email_hash " ] = hashlib . md5 (
f " { user [ ' name ' ] . lower ( ) } @fedoraproject.org " . encode ( " utf-8 " )
) . hexdigest ( )
user [ " openid_hash " ] = hashlib . md5 (
f " http:// { user [ ' name ' ] . lower ( ) } .id.fedoraproject.org/ " . encode ( " utf-8 " )
) . hexdigest ( )
2016-02-22 20:49:39 +00:00
2021-03-31 15:08:16 +02:00
if user [ " has_public_html " ] or user [ " has_public_git " ] :
users [ username ] = user
2016-02-22 20:49:39 +00:00
page_jinja_template_obj = Template ( page_jinja_template )
page_output = page_jinja_template_obj . render ( users = sorted ( users . items ( ) ) )
2021-03-31 14:58:28 +02:00
out_file = Path ( " /srv/people/site/index.html " )
2016-02-22 21:11:03 +00:00
# get gid for web group
out_file_grp = grp . getgrnam ( " web " ) . gr_gid ;
2021-03-31 14:58:28 +02:00
with open ( out_file , " w " , encoding = " utf-8 " ) as handle :
handle . write ( page_output )
2016-02-22 21:11:03 +00:00
# keep current owner uid
2021-03-31 14:58:28 +02:00
st = out_file . stat ( )
out_file_uid = st . st_uid
2016-02-22 21:11:03 +00:00
# give write permissions to group
2021-03-31 14:58:28 +02:00
out_file . chmod ( st . st_mode | stat . S_IWGRP )
2016-02-22 21:11:03 +00:00
# chown out file to group
os . chown ( out_file , out_file_uid , out_file_grp )