Ticket #916: schoolserver080109.py.patch

File schoolserver080109.py.patch, 5.5 KB (added by hamiltonchua, 15 years ago)
  • schoolserver.py

    old new  
     1# Copyright (C) 2007, 2008 One Laptop Per Child
     2#
     3# This program is free software; you can redistribute it and/or modify
     4# it under the terms of the GNU General Public License as published by
     5# the Free Software Foundation; either version 2 of the License, or
     6# (at your option) any later version.
     7#
     8# This program is distributed in the hope that it will be useful,
     9# but WITHOUT ANY WARRANTY; without even the implied warranty of
     10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11# GNU General Public License for more details.
     12#
     13# You should have received a copy of the GNU General Public License
     14# along with this program; if not, write to the Free Software
     15# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     16
    117import logging
    218from gettext import gettext as _
    319from xmlrpclib import ServerProxy, Error
     
    521import os
    622import gconf
    723
     24import string, random, time
     25import subprocess
     26
     27import time
     28import datetime
     29
    830from sugar.profile import get_profile
    931
    10 REGISTER_URL = 'http://schoolserver:8080/'
     32# HAM : SoaS
     33# (ham@solutiongrove.com)
     34# defs to enable registration on SoaS
     35
     36# use whatever is specified in the jabber server field for REGISTER_URL
     37# REGISTER_URL = 'http://schoolserver:8080/'
     38client = gconf.client_get_default()
     39JABBER_SERVER = client.get_string('/desktop/sugar/collaboration/jabber_server')
     40REGISTER_URL = 'http://'+JABBER_SERVER+':8080/'
     41
     42# utilities ********************************************************************
     43
     44def getEpoch():
     45  t = datetime.datetime.now()
     46  e = time.mktime(t.timetuple()) 
     47  return e
     48
     49def getMacAddress():
     50  for line in os.popen("/sbin/ifconfig"):
     51    if line.find('Ether') > -1:
     52      mac = line.split()[4]
     53      break
     54  return mac
     55
     56# generates the serial and uuid ************************************************
     57
     58# to create a unique serial
     59# - we randomly get 3 letters
     60# - concat the above with the last 8 numbers from epoch seconds
     61
     62def gen_soas_serial():
     63  s1 = ''.join([random.choice(string.ascii_uppercase) for y in range(3)])
     64  s2 = str(int(getEpoch()))[-8:]
     65  serial = s1 + s2
     66  return serial
     67
     68# to create a unique uuid
     69# - create uuid using random hexdigits up to 40 characters
     70
     71def gen_soas_uuid():
     72  u1_count = 40
     73  uuid = ''.join([random.choice(string.hexdigits + '-') for y in range(int(u1_count))])
     74  return uuid
     75
     76# write serial and uuid to file ************************************************
     77
     78def write_soas_info(sn,uuid,backup_url):
     79  # we presume that there is a /home/liveuser/.sugar directory
     80  soas_dir = '/home/liveuser/.sugar/soas/'
     81  # create the directory where we will put the files
     82  if not os.path.exists(soas_dir):
     83    os.mkdir(soas_dir)
     84  # check if a serial file exists, it might be from a failed registration
     85  # let's delete it first
     86  if os.path.exists(os.path.join(soas_dir, 'sn')):
     87    os.remove(os.path.join(soas_dir, 'sn'))
     88  # write the serial into a file
     89  serial_file = open(os.path.join(soas_dir, 'sn'),'w')
     90  serial_file.write(sn)
     91  serial_file.close()
     92  # check if a uuid file exists, it might be from a failed registration
     93  # let's delete it first
     94  if os.path.exists(os.path.join(soas_dir, 'uuid')):
     95    os.remove(os.path.join(soas_dir, 'uuid'))
     96  # write the uuid into a file
     97  uuid_file = open(os.path.join(soas_dir, 'uuid'),'w')
     98  uuid_file.write(uuid)
     99  uuid_file.close()
     100  if os.path.exists(os.path.join(soas_dir, 'backup_url')):
     101    os.remove(os.path.join(soas_dir, 'backup_url'))
     102  # write the backup_url to a file
     103  bu_file = open(os.path.join(soas_dir, 'backup_url'),'w')
     104  bu_file.write(backup_url)
     105  bu_file.close()
     106
     107# ******************************************************************************
    11108
    12109class RegisterError(Exception):
    13110    pass
    14111
    15112def register_laptop(url=REGISTER_URL):
    16     if not have_ofw_tree():
    17         logging.error('Registration: Cannot obtain data needed to register.')
    18         raise RegisterError(_('Cannot obtain data needed for registration.'))
    19 
    20     sn = read_ofw('mfg-data/SN')
    21     uuid = read_ofw('mfg-data/U#')
    22     sn = sn or 'SHF00000000'
    23     uuid = uuid or '00000000-0000-0000-0000-000000000000'
    24113
    25114    profile = get_profile()
    26115
    27116    client = gconf.client_get_default()
     117    JABBER_SERVER = client.get_string('/desktop/sugar/collaboration/jabber_server')
     118
     119    if not have_ofw_tree():
     120        # logging.error('Registration: Cannot obtain data needed to register.')
     121        # raise RegisterError(_('Cannot obtain data needed for registration.'))
     122
     123        # HAM : SoaS
     124        # if we did not find an ofw directory
     125        # let's generate the serial and uuid from the current time and mac address
     126        # save the generated serial and uuid into gconf
     127
     128        sn = gen_soas_serial()
     129        uuid = gen_soas_uuid()
     130
     131        # we need to save these for later use
     132        # we need them to work with backups
     133        # client.set_string('/desktop/sugar/soas_serial', sn)
     134        # client.set_string('/desktop/sugar/soas_uuid', uuid)
     135
     136        write_soas_info(sn,uuid,JABBER_SERVER)
     137
     138    else:
     139        sn = read_ofw('mfg-data/SN')
     140        uuid = read_ofw('mfg-data/U#')
     141        sn = sn or 'SHF00000000'
     142        uuid = uuid or '00000000-0000-0000-0000-000000000000'
     143
    28144    nick = client.get_string('/desktop/sugar/user/nick')
    29145
    30146    server = ServerProxy(url)
     
    56172    data = fh.read().rstrip('\0\n')
    57173    fh.close()
    58174    return data
    59