#!/usr/bin/python1###############################################################################2#3# CoCalc: Collaborative Calculation in the Cloud4#5# Copyright (C) 2016, Sagemath Inc.6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with this program. If not, see <http://www.gnu.org/licenses/>.19#20###############################################################################21222324import os, sys2526path = sys.argv[1]27if not os.path.exists(path):28raise RuntimeError("no such directory -- %s"%path)2930dot_ssh = os.path.join(path, '.ssh')3132if os.path.exists(dot_ssh) and not os.path.isdir(dot_ssh):33os.unlink(dot_ssh)3435if not os.path.exists(dot_ssh):36os.makedirs(dot_ssh)3738target = os.path.join(dot_ssh, 'authorized_keys')39authorized_keys = '\n' + open(sys.argv[2]).read() + '\n'4041if not os.path.exists(target) or authorized_keys not in open(target).read():42open(target,'w').write(authorized_keys)4344s = os.stat(path)4546if os.system('chown -R %s:%s %s'%(s.st_uid, s.st_gid, dot_ssh)):47raise RuntimeError("failed to chown")4849if os.system('chmod og-rwx -R %s'%dot_ssh):50raise RuntimeError("failed to chmod")515253