2021-03-30 16:52:20 +02:00
#!/usr/bin/env python3
2016-02-22 20:49:39 +00:00
# -*- coding: utf-8 -*-
#
2021-03-31 15:09:36 +02:00
# Copyright © 2016 Chaoyi Zha <cydrobolt@fedoraproject.org>
# Copyright © 2021 Red Hat, Inc.
2016-02-22 20:49:39 +00:00
#
# 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 ' >
2023-04-24 13:10:10 +10:00
< ! - - < link rel = ' stylesheet ' href = ' //getfedora.org/static/css/bootstrap-theme.css ' > - - >
2016-02-22 20:49:39 +00:00
< 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 " >
2023-04-24 13:10:10 +10:00
< img class = " fedoralogotext " class = " img-responsive center-block " src = " //getfedora.org/assets/images/logos/fedora-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 >
2022-08-25 16:10:21 +10:00
< img class = ' user-avatar ' src = ' /static/grey.jpg ' alt = ' Avatar for {{ username|e}} ' data - src = ' https://seccdn.libravatar.org/avatar/ {{ user[ ' openid_hash ' ]}}?s=20&d=retro ' >
2022-08-25 17:48:45 +10:00
{ { user . name . strip ( ) | e } } < span class = ' text-muted ' > ( { { username | e } } ) < / span >
2016-02-22 20:49:39 +00:00
< / td >
< td >
{ % if user [ ' has_public_git ' ] % }
< div >
2022-08-25 16:10:21 +10:00
< a href = " https://fedorapeople.org/cgit/ {{ username|e}}/ " > Git repositories < / a >
2016-02-22 20:49:39 +00:00
< / div >
{ % endif % }
{ % if user [ ' has_public_html ' ] % }
< div >
2022-08-25 16:10:21 +10:00
< a href = " https:// {{ username|e}}.fedorapeople.org " > { { username | e } } ' s homepage</a>
2016-02-22 20:49:39 +00:00
< / div >
{ % endif % }
< div >
2022-08-25 16:10:21 +10:00
< a href = " https://fedoraproject.org/wiki/user: {{ username|e}} " > { { username | e } } ' s wiki page</a>
2016-02-22 20:49:39 +00:00
< / div >
< / td >
< / tr >
{ % endfor % }
< / tbody >
< / table >
< / div >
< hr >
< footer class = ' center text-muted ' >
< p class = " copy " >
2021-03-31 15:09:36 +02:00
& copy ; 2016 - 2021 Chaoyi Zha , Red Hat , Inc . , and others .
2016-02-22 20:49:39 +00:00
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
2021-04-07 10:50:49 +02:00
try :
user [ " has_public_html " ] = ( hdir / " public_html " ) . is_dir ( )
except PermissionError :
user [ " has_public_html " ] = False
try :
user [ " has_public_git " ] = ( hdir / " public_git " ) . is_dir ( )
except PermissionError :
user [ " has_public_git " ] = False
2021-03-31 14:58:28 +02:00
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
2021-03-31 15:09:02 +02:00
out_file_grp = grp . getgrnam ( " web " ) . gr_gid
2016-02-22 21:11:03 +00:00
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
2021-04-07 21:39:17 +02:00
# The code below was present originally, however the cron job is ran under the
# `apache` user so it is not clear what this was meant to do.
# This is being kept here for convenience in case we need to re-activate this
# code, down the line this should just be removed.
2016-02-22 21:11:03 +00:00
2021-04-07 21:39:17 +02:00
# keep current owner uid
#st = out_file.stat()
#out_file_uid = st.st_uid
#
2016-02-22 21:11:03 +00:00
# give write permissions to group
2021-04-07 21:39:17 +02:00
#out_file.chmod(st.st_mode | stat.S_IWGRP)
2016-02-22 21:11:03 +00:00
# chown out file to group
2021-04-07 21:39:17 +02:00
#os.chown(out_file, out_file_uid, out_file_grp)