From e25872187429fb6f4d2379a98a5c8fb3bc93fa12 Mon Sep 17 00:00:00 2001
From: Hamilton Chua <hchua@enterprise2.localdomain>
Date: Sun, 9 Aug 2009 12:39:12 +0800
Subject: [PATCH] allow sugar on a stick to register with an xs server

---
 src/jarabe/desktop/schoolserver.py |  106 +++++++++++++++++++++++++++++++++---
 1 files changed, 98 insertions(+), 8 deletions(-)

diff --git a/src/jarabe/desktop/schoolserver.py b/src/jarabe/desktop/schoolserver.py
index 1dd9edc..2ec5a06 100644
--- a/src/jarabe/desktop/schoolserver.py
+++ b/src/jarabe/desktop/schoolserver.py
@@ -21,26 +21,116 @@ import socket
 import os
 import gconf
 
+import string, random, time
+import subprocess
+
+import time
+import datetime
+
 from sugar.profile import get_profile
 
 REGISTER_URL = 'http://schoolserver:8080/'
 
+# SoaS: ham@solutiongrove.com
+# defs to enable registration on SoaS
+
+# utilities ********************************************************************
+
+def getEpoch():
+  t = datetime.datetime.now()
+  e = time.mktime(t.timetuple())  
+  return e
+
+# generates the serial and uuid ************************************************
+
+# to create a unique serial
+# - we randomly get 3 letters
+# - concat the above with the last 8 numbers from epoch seconds
+
+def gen_soas_serial():
+  s1 = ''.join([random.choice(string.ascii_uppercase) for y in range(3)])
+  s2 = str(int(getEpoch()))[-8:]
+  serial = s1 + s2 
+  return serial
+
+# to create a unique uuid
+
+def gen_soas_uuid():
+  u1_count = 40 
+  uuid = ''.join([random.choice(string.hexdigits + '-') for y in range(int(u1_count))])
+  return uuid
+
+# write serial and uuid to file ************************************************
+
+def write_soas_info(sn,uuid,backup_url):
+  # we presume that there is a /home/liveuser/.sugar directory
+  soas_dir = '/home/liveuser/.sugar/soas/'
+  # create the directory where we will put the files
+  if not os.path.exists(soas_dir):
+    os.mkdir(soas_dir)
+  # check if a serial file exists, it might be from a failed registration
+  # let's delete it first
+  if os.path.exists(os.path.join(soas_dir, 'sn')):
+    os.remove(os.path.join(soas_dir, 'sn'))
+  # write the serial into a file
+  serial_file = open(os.path.join(soas_dir, 'sn'),'w')
+  serial_file.write(sn)
+  serial_file.close()
+  # check if a uuid file exists, it might be from a failed registration
+  # let's delete it first
+  if os.path.exists(os.path.join(soas_dir, 'uuid')):
+    os.remove(os.path.join(soas_dir, 'uuid'))
+  # write the uuid into a file
+  uuid_file = open(os.path.join(soas_dir, 'uuid'),'w')
+  uuid_file.write(uuid)
+  uuid_file.close()
+  if os.path.exists(os.path.join(soas_dir, 'backup_url')):
+    os.remove(os.path.join(soas_dir, 'backup_url'))
+  # write the backup_url to a file
+  bu_file = open(os.path.join(soas_dir, 'backup_url'),'w')
+  bu_file.write(backup_url)
+  bu_file.close()
+
+# ******************************************************************************
+
 class RegisterError(Exception):
     pass
 
 def register_laptop(url=REGISTER_URL):
+
+    profile = get_profile()
+    client = gconf.client_get_default()
+
     if not have_ofw_tree():
-        logging.error('Registration: Cannot obtain data needed to register.')
-        raise RegisterError(_('Cannot obtain data needed for registration.'))
+        # logging.error('Registration: Cannot obtain data needed to register.')
+        # raise RegisterError(_('Cannot obtain data needed for registration.'))
 
-    sn = read_ofw('mfg-data/SN')
-    uuid = read_ofw('mfg-data/U#')
-    sn = sn or 'SHF00000000'
-    uuid = uuid or '00000000-0000-0000-0000-000000000000'
+        # SoaS  :
+        # if we did not find an ofw directory
+        # let's generate the serial and uuid from the current time 
+        # save the generated serial and uuid into files
 
-    profile = get_profile()
+        sn = gen_soas_serial()
+        uuid = gen_soas_uuid()
+
+        # we want to use the text in the jabber server as our registration url
+
+        JABBER_SERVER = client.get_string('/desktop/sugar/collaboration/jabber_server')
+
+        # we need to save sn and uuid to work with backups
+
+        write_soas_info(sn,uuid,JABBER_SERVER)
+
+        # override the registration url to use
+
+        url = 'http://'+JABBER_SERVER+':8080/'
+
+    else:
+        sn = read_ofw('mfg-data/SN')
+        uuid = read_ofw('mfg-data/U#')
+        sn = sn or 'SHF00000000'
+        uuid = uuid or '00000000-0000-0000-0000-000000000000'
 
-    client = gconf.client_get_default()
     nick = client.get_string('/desktop/sugar/user/nick')
 
     server = ServerProxy(url)
-- 
1.6.2.5

