KERNEL: add win32 compile
authorced <>
Mon, 11 Jun 2007 11:10:47 +0000 (11:10 +0000)
committerced <>
Mon, 11 Jun 2007 11:10:47 +0000 (11:10 +0000)
bzr revid: ced-5040a2a33469b094dd0fa2a7ca9a3e60d3547708

bin/tools/config.py
setup.nsi
win32/TinyERPServerService.py [new file with mode: 0644]
win32/setup.py [new file with mode: 0644]
win32/start.bat [new file with mode: 0644]
win32/stop.bat [new file with mode: 0644]

index 3a0f38c..33ef69d 100644 (file)
@@ -125,7 +125,7 @@ class configmanager(object):
                # if the server is run by an unprivileged user, he has to specify location of a config file where he has the rights to write,
                # else he won't be able to save the configurations, or even to start the server...
                if os.name == 'nt':
-                       rcfilepath = os.path.join(os.path.split(os.path.split(os.path.abspath(sys.argv[0]))[0])[0], 'etc', 'tinyerp-server.conf')
+                       rcfilepath = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), 'tinyerp-server.conf')
                else:
                        rcfilepath = os.path.expanduser('~/.terp_serverrc')
 
index c15b48c..5923536 100644 (file)
--- a/setup.nsi
+++ b/setup.nsi
   !insertmacro MUI_PAGE_INSTFILES
 
   !define MUI_FINISHPAGE_NOAUTOCLOSE
+  !define MUI_FINISHPAGE_RUN
+  !define MUI_FINISHPAGE_RUN_CHECKED
+  !define MUI_FINISHPAGE_RUN_TEXT "Start TinyERP Server"
+  !define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
   !define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED
   !define MUI_FINISHPAGE_SHOWREADME $INSTDIR\README.txt
   !insertmacro MUI_PAGE_FINISH
 
 Section "TinyERP Server" SecTinyERPServer
 
+  nsExec::Exec "net stop tinyerp-service"
+  sleep 2
+
   SetOutPath "$INSTDIR"
   
   ;ADD YOUR OWN FILES HERE...
   File /r "dist\\*"
 
+  SetOutPath "$INSTDIR\service"
+  File /r "win32\\dist\\*"
+  File "win32\\start.bat"
+  File "win32\\stop.bat"
+
   ;Store installation folder
   WriteRegStr HKCU "Software\TinyERP Server" "" $INSTDIR
   
   ;Create uninstaller
   WriteUninstaller "$INSTDIR\Uninstall.exe"
   
+
   !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
     
     ;Create shortcuts
     CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
     CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\TinyERP Server.lnk" "$INSTDIR\tinyerp-server.exe"
     CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
+    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Start service.lnk" "$INSTDIR\service\start.bat"
+    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Stop service.lnk" "$INSTDIR\service\stop.bat"
+    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Edit config.lnk" "notepad.exe" "$INSTDIR\tinyerp-server.conf"
+    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Edit log.lnk" "notepad.exe" "$INSTDIR\tinyerp-server.log"
   
   !insertmacro MUI_STARTMENU_WRITE_END
 
+  nsExec::Exec '"$INSTDIR\\tinyerp-server.exe" --stop-after-init --logfile "$INSTDIR\\tinyerp-server.log" -s'
+  nsExec::Exec '"$INSTDIR\\service\\TinyERPServerService.exe" -auto -install'
+
 SectionEnd
 
 ;Descriptions
@@ -108,11 +128,17 @@ SectionEnd
 
 Section "Uninstall"
 
+  nsExec::Exec "net stop tinyerp-service"
+  sleep 2
+  nsExec::Exec '"$INSTDIR\\service\\TinyERPServerService.exe" -remove'
+  sleep 2
+
   ;ADD YOUR OWN FILES HERE...
   Delete "$INSTDIR\Uninstall.exe"
   Push "$INSTDIR\addons"
   Push ""
   Call un.RmFilesButOne
+  Delete "$INSTDIR\service\*"
   Delete "$INSTDIR\*"
   Delete "$INSTDIR\Uninstall.exe"
 
@@ -120,12 +146,15 @@ Section "Uninstall"
   Push ""
   Call un.RmDirsButOne
   RMDir "$INSTDIR\addons"
+  RMDir "$INSTDIR\service"
   RMDir "$INSTDIR"
   
   !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
     
   Delete "$SMPROGRAMS\$MUI_TEMP\TinyERP Server.lnk"
   Delete "$SMPROGRAMS\$MUI_TEMP\Uninstall.lnk"
+  Delete "$SMPROGRAMS\$MUI_TEMP\Start service.lnk"
+  Delete "$SMPROGRAMS\$MUI_TEMP\Stop service.lnk"
   
   ;Delete empty start menu parent diretories
   StrCpy $MUI_TEMP "$SMPROGRAMS\$MUI_TEMP"
@@ -144,6 +173,10 @@ Section "Uninstall"
 
 SectionEnd
 
+Function LaunchLink
+  nsExec::Exec "net start tinyerp-service"
+FunctionEnd
+
 Function un.RmDirsButOne
  Exch $R0 ; exclude dir
  Exch
diff --git a/win32/TinyERPServerService.py b/win32/TinyERPServerService.py
new file mode 100644 (file)
index 0000000..06687c0
--- /dev/null
@@ -0,0 +1,80 @@
+#\r
+# TinyERPServerService.py\r
+#\r
+# Script installing Tiny ERP as Windows service\r
+\r
+# Win32 python extensions modules\r
+import win32serviceutil\r
+import win32service\r
+import win32event\r
+import win32api\r
+import win32process\r
+import servicemanager\r
+\r
+import sys\r
+import subprocess\r
+import os\r
+import thread\r
+\r
+class TinyERPServerService(win32serviceutil.ServiceFramework):\r
+    # required info\r
+    _svc_name_ = "tinyerp-service"\r
+    _svc_display_name_ = "Tiny ERP Server"\r
+    # optionnal info\r
+    _svc_description_ = "Tiny ERP Server service"\r
+\r
+    def __init__(self, args):\r
+        win32serviceutil.ServiceFramework.__init__(self, args)\r
+        # Create an event which we will use to wait on.\r
+        # The "service stop" request will set this event.\r
+        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)\r
+        # a reference to the server's process\r
+        self.terpprocess = None\r
+        # info if the service terminates correctly or if the server crashed\r
+        self.stopping = False\r
+\r
+\r
+    def SvcStop(self):\r
+        # Before we do anything, tell the SCM we are starting the stop process.\r
+        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)\r
+        # stop the running TERP Server: say it's a normal exit\r
+        win32api.TerminateProcess(int(self.terpprocess._handle), 0)\r
+        servicemanager.LogInfoMsg("Tiny ERP Server stopped correctly")\r
+        # And set my event.\r
+        win32event.SetEvent(self.hWaitStop)\r
+\r
+\r
+    def StartTERP(self):\r
+       # The server finds now its configuration automatically on Windows\r
+       # We start the ERP Server as an independent process, but we keep its handle\r
+       # The server's binary must be one directory above the service's binary (when py2exe'd the python libraries shouldn' mix)\r
+       service_dir = os.path.dirname(sys.argv[0])\r
+       server_dir = os.path.split(service_dir)[0]\r
+       server_path = os.path.join(server_dir, 'tinyerp-server.exe')\r
+       self.terpprocess = subprocess.Popen([server_path], \\r
+                                            cwd=server_dir,\r
+                                           creationflags=win32process.CREATE_NO_WINDOW)\r
+\r
+\r
+    def StartControl(self,ws):\r
+        # this listens to the Service Manager's events\r
+        win32event.WaitForSingleObject(ws, win32event.INFINITE)\r
+        self.stopping = True\r
+\r
+    def SvcDoRun(self):\r
+       # Start Tiny ERP Server itself\r
+        self.StartTERP()\r
+        # start the loop waiting for the Service Manager's stop signal\r
+       thread.start_new_thread(self.StartControl, (self.hWaitStop,))\r
+       # Log a info message that the server is running\r
+       servicemanager.LogInfoMsg("Tiny ERP Server up and running")\r
+       # verification if the server is really running, else quit with an error\r
+        self.terpprocess.wait()\r
+               if not self.stopping:\r
+            sys.exit("Tiny ERP Server check: server not running, check the logfile for more info")\r
+\r
+\r
+\r
+if __name__=='__main__':\r
+    # Do with the service whatever option is passed in the command line\r
+    win32serviceutil.HandleCommandLine(TinyERPServerService)\r
diff --git a/win32/setup.py b/win32/setup.py
new file mode 100644 (file)
index 0000000..d61a4c6
--- /dev/null
@@ -0,0 +1,12 @@
+# setup.py\r
+from distutils.core import setup\r
+import py2exe\r
+\r
+\r
+setup(service=["TinyERPServerService"],\r
+      options={"py2exe":{"excludes":["Tkconstants","Tkinter","tcl",\r
+                                     "_imagingtk","PIL._imagingtk",\r
+                                     "ImageTk", "PIL.ImageTk",\r
+                                     "FixTk"],\r
+                         "compressed": 1}}\r
+      )\r
diff --git a/win32/start.bat b/win32/start.bat
new file mode 100644 (file)
index 0000000..5605687
--- /dev/null
@@ -0,0 +1,7 @@
+@PATH=%WINDIR%\system32;%WINDIR%;%WINDIR%\System32\Wbem;.\r
+\r
+@net stop tinyerp-service\r
+\r
+@net start tinyerp-service\r
+\r
+cls\r
diff --git a/win32/stop.bat b/win32/stop.bat
new file mode 100644 (file)
index 0000000..8640bab
--- /dev/null
@@ -0,0 +1,5 @@
+@PATH=%WINDIR%\system32;%WINDIR%;%WINDIR%\System32\Wbem;.\r
+\r
+@net stop tinyerp-service\r
+\r
+cls\r