Ticket #916: schoolserver072509.py.patch

File schoolserver072509.py.patch, 4.1 KB (added by hamiltonchua, 8 months ago)
  • schoolserver.py

    old new  
    2121import os 
    2222import gconf 
    2323 
     24import string, random, time 
     25import subprocess 
     26 
     27import time 
     28import datetime 
     29 
    2430from sugar.profile import get_profile 
    2531 
    26 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() 
     39REGISTER_URL = 'http://'+client.get_string('/desktop/sugar/collaboration/jabber_server')+':8080/' 
     40 
     41# utilities ******************************************************************** 
     42 
     43def getEpoch(): 
     44  t = datetime.datetime.now() 
     45  e = time.mktime(t.timetuple())   
     46  return e 
     47 
     48def getMacAddress(): 
     49  for line in os.popen("/sbin/ifconfig"): 
     50    if line.find('Ether') > -1: 
     51      mac = line.split()[4] 
     52      break 
     53  return mac  
     54 
     55# generates the serial and uuid ************************************************ 
     56 
     57# to create a unique serial 
     58# - we randomly get 3 letters 
     59# - concat the above with the last 8 numbers from epoch seconds 
     60 
     61def gen_soas_serial(): 
     62  s1 = ''.join([random.choice(string.ascii_uppercase) for y in range(3)]) 
     63  s2 = str(int(getEpoch()))[-8:] 
     64  serial = s1 + s2  
     65  return serial 
     66 
     67# to create a unique uuid 
     68# - create uuid using random hexdigits up to 40 characters 
     69 
     70def gen_soas_uuid(): 
     71  u1_count = 40  
     72  uuid = ''.join([random.choice(string.hexdigits + '-') for y in range(int(u1_count))]) 
     73  return uuid 
     74 
     75# write serial and uuid to file ************************************************ 
     76 
     77def write_soas_info(sn,uuid): 
     78  # we presume that there is a /home/liveuser/.sugar directory 
     79  soas_dir = '/home/liveuser/.sugar/soas/' 
     80  # create the directory where we will put the files 
     81  if not os.path.exists(soas_dir): 
     82    os.mkdir(soas_dir) 
     83  # check if a serial file exists, it might be from a failed registration 
     84  # let's delete it first 
     85  if os.path.exists(os.path.join(soas_dir, 'sn')): 
     86    os.remove(os.path.join(soas_dir, 'sn')) 
     87  # write the serial into a file 
     88  serial_file = open(os.path.join(soas_dir, 'sn'),'w') 
     89  serial_file.write(sn) 
     90  serial_file.close() 
     91  # check if a uuid file exists, it might be from a failed registration 
     92  # let's delete it first 
     93  if os.path.exists(os.path.join(soas_dir, 'uuid')): 
     94    os.remove(os.path.join(soas_dir, 'uuid')) 
     95  # write the uuid into a file 
     96  uuid_file = open(os.path.join(soas_dir, 'uuid'),'w') 
     97  uuid_file.write(uuid) 
     98  uuid_file.close() 
     99 
     100# ****************************************************************************** 
    27101 
    28102class RegisterError(Exception): 
    29103    pass 
    30104 
    31105def register_laptop(url=REGISTER_URL): 
    32     if not have_ofw_tree(): 
    33         logging.error('Registration: Cannot obtain data needed to register.') 
    34         raise RegisterError(_('Cannot obtain data needed for registration.')) 
    35  
    36     sn = read_ofw('mfg-data/SN') 
    37     uuid = read_ofw('mfg-data/U#') 
    38     sn = sn or 'SHF00000000' 
    39     uuid = uuid or '00000000-0000-0000-0000-000000000000' 
    40106 
    41107    profile = get_profile() 
    42108 
    43109    client = gconf.client_get_default() 
     110 
     111    if not have_ofw_tree(): 
     112        # logging.error('Registration: Cannot obtain data needed to register.') 
     113        # raise RegisterError(_('Cannot obtain data needed for registration.')) 
     114 
     115        # HAM : SoaS  
     116        # if we did not find an ofw directory 
     117        # let's generate the serial and uuid from the current time and mac address 
     118        # save the generated serial and uuid into gconf 
     119 
     120        sn = gen_soas_serial() 
     121        uuid = gen_soas_uuid() 
     122 
     123        # we need to save these for later use 
     124        # we need them to work with backups 
     125        # client.set_string('/desktop/sugar/soas_serial', sn) 
     126        # client.set_string('/desktop/sugar/soas_uuid', uuid) 
     127 
     128        write_soas_info(sn,uuid) 
     129 
     130    else: 
     131        sn = read_ofw('mfg-data/SN') 
     132        uuid = read_ofw('mfg-data/U#') 
     133        sn = sn or 'SHF00000000' 
     134        uuid = uuid or '00000000-0000-0000-0000-000000000000' 
     135 
    44136    nick = client.get_string('/desktop/sugar/user/nick') 
    45137 
    46138    server = ServerProxy(url)