Modify

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#11584 closed enhancement (fixed)

update to Trac-1.0

Reported by: Mark Mikofski Owned by: Mark Mikofski
Priority: high Component: WindowsServiceScript
Severity: major Keywords:
Cc: Trac Release: 1.0

Description

Great hack! Works perfect on Windows 7 with Trac-1.0, but had to update API. Here is a patch!

--- a/tracservice.py
+++ b/tracservice.py
@@ -1,5 +1,5 @@
-#!C:\programme\python\2.3\python.exe
-# -*- coding: iso8859-1 -*-
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
 #
 # Author: Florent Xicluna <laxyf@yahoo.fr>
 
@@ -33,32 +33,50 @@
 import locale
 import sys
 import os
+from distutils import sysconfig
 
 import win32serviceutil
 import win32service
 
-from trac.web.standalone import BasicAuth, DigestAuth, TracHTTPServer
+from trac.web.standalone import BasicAuthentication as BasicAuth, \
+    DigestAuthentication as DigestAuth, TracHTTPServer, \
+    AuthenticationMiddleware, BasePathMiddleware, TracEnvironMiddleware
+
+from trac.web.main import dispatch_request
 
 # ==  Editable CONSTANTS SECTION  ============================================
 
-PYTHON = r'C:\Python23\python.exe'
-INSTANCE_HOME = r'c:\path\to\instance\trac'
+TRAC_PROJECT = 'c:\\path\\to\\Trac\\Projects'
 
-# Trac options (see C:\Python23\Script\tracd)
+# Trac options (see tracd --help)
+#  -a DIGESTAUTH, --auth=DIGESTAUTH
+#                        [projectdir],[htdigest_file],[realm]
+#  --basic-auth=BASICAUTH
+#                        [projectdir],[htpasswd_file],[realm]
+#  -p PORT, --port=PORT  the port number to bind to
+#  -b HOSTNAME, --hostname=HOSTNAME
+#                        the host name or IP address to bind to
+#  -e PARENTDIR, --env-parent-dir=PARENTDIR
+#                        parent directory of the project environments
+#  --base-path=BASE_PATH
+#                        the initial portion of the request URL's "path"
+#  -s, --single-env      only serve a single project without the project list
 OPTS = [
-  ( '--auth', ('trac,%s\conf\htdigest,TracRealm' % INSTANCE_HOME) ),
-  ( '--port', '80' ),
+    ('--hostname', 'mycomputer.mydomain.com'),
+    ('--single-env', True),
+    ('--auth', ('trac,c:\\path\\to\\pswd\\file,TracRealm')),
+    ('--port', '80'),
 ]
 
 # ==  End of CONSTANTS SECTION  ==============================================
 
 # Other constants
-PYTHONDIR = os.path.split(PYTHON)[0]
-PYTHONSERVICE_EXE=r'%s\Lib\site-packages\win32\pythonservice.exe' % PYTHONDIR
-LOG_DIR = r'%s\log' % INSTANCE_HOME
+PYTHONDIR = sysconfig.get_python_lib()  # gets site-packages folder
+PYTHONSERVICE_EXE=os.path.join(PYTHONDIR, 'win32', 'pythonservice.exe')
+LOG_DIR = os.path.join(TRAC_PROJECT, log)
 
 # Trac instance(s)
-ARGS = [ INSTANCE_HOME, ]
+ARGS = [TRAC_PROJECT]
 
 def add_auth(auths, vals, cls):
     info = vals.split(',', 3)
@@ -81,8 +99,8 @@
     prompt.
     """
 
-    _svc_name_ = 'Trac_%s' % str(hash(INSTANCE_HOME))
-    _svc_display_name_ = 'Trac instance at %s' % INSTANCE_HOME
+    _svc_name_ = 'Trac_%s' % str(hash(TRAC_PROJECT))
+    _svc_display_name_ = 'Trac instance at %s' % TRAC_PROJECT
     _exe_name_ = PYTHONSERVICE_EXE
 
     def SvcDoRun(self):
@@ -111,7 +129,6 @@
         port = 80
         hostname = ''
         auths = {}
-        daemonize = 0
         env_parent_dir = None
 
         for o, a in OPTS:
@@ -125,15 +142,32 @@
                 hostname = a
             if o in ("-e", "--env-parent-dir"):
                 env_parent_dir = a
+            if o in ("-s", "--single-env"):
+                single_env = a
+            if o in ("--base-path"):
+                base_path = a
 
         if not env_parent_dir and not ARGS:
             raise ValueError("""No Trac project specified""")
+
+        wsgi_app = TracEnvironMiddleware(dispatch_request,
+                                         env_parent_dir, ARGS,
+                                         single_env)
+        if auths:
+            if single_env:
+                project_name = os.path.basename(ARGS[0])
+                wsgi_app = AuthenticationMiddleware(wsgi_app, auths, project_name)
+            else:
+                wsgi_app = AuthenticationMiddleware(wsgi_app, auths)
+        base_path = base_path.strip('/').strip('\\')
+        if base_path:
+            wsgi_app = BasePathMiddleware(wsgi_app, base_path)
 
         sys.stdout = open(os.path.join(LOG_DIR, 'stdout.log'),'a')
         sys.stderr = open(os.path.join(LOG_DIR, 'stderr.log'),'a')
 
         server_address = (hostname, port)
-        return TracHTTPServer(server_address, env_parent_dir, ARGS, auths)
+        return TracHTTPServer(server_address, wsgi_app, env_parent_dir, ARGS)
 
 if __name__ == '__main__':
     # The following are the most common command-line arguments that are used

Attachments (2)

tracservice.patch (4.4 KB) - added by Mark Mikofski 11 years ago.
patch to update to Trac-1.0
tracservice_r13672.patch (1.6 KB) - added by Mark Mikofski 11 years ago.
patch to 13672

Download all attachments as: .zip

Change History (10)

Changed 11 years ago by Mark Mikofski

Attachment: tracservice.patch added

patch to update to Trac-1.0

comment:2 Changed 11 years ago by Ryan J Ollos

Resolution: fixed
Status: newclosed

In 13672:

Applied Trac 1.0 compatibility patch. Fixes #11584. Patch by bwanamarko.

Last edited 11 years ago by Ryan J Ollos (previous) (diff)

comment:1 Changed 11 years ago by Ryan J Ollos

Owner: changed from Florent to Ryan J Ollos
Status: newaccepted
Type: defectenhancement

Changed 11 years ago by Mark Mikofski

Attachment: tracservice_r13672.patch added

patch to 13672

comment:2 Changed 11 years ago by Mark Mikofski

Resolution: fixed
Status: closedreopened

Hi @rjollos,

Oops! There were a few typos. I had tested an earlier version. I am so sorry! This version is now tested and running on Windows 7 with no errors. A new patch for r13672 is attached, with these changes:

  1. On L67 in OPTS each part of auth tuple should be enclosed in separate sets of quotes.
  2. Forgot to add quotes around string 'log' on L76.
  3. Since using tuple for auth, in add_auths() on L81 remove L82 and change input arg vals to info.
  4. base_path never had any default, so set its default to None after L132.
  5. Can't split None so move L162 into if block
  6. These are non-POSIX paths, so don't need to strip forward slashes, only backslashes.
--- a/tracservice.py
+++ b/tracservice.py
@@ -64,7 +64,7 @@
 OPTS = [
     ('--hostname', 'mycomputer.mydomain.com'),
     ('--single-env', True),
-    ('--auth', ('trac,c:\\path\\to\\pswd\\file,TracRealm')),
+    ('--auth', ('TracProj', 'c:\\path\\to\\pswd\\file', 'TracRealm')),
     ('--port', '80'),
 ]
 
@@ -73,16 +73,15 @@
 # Other constants
 PYTHONDIR = sysconfig.get_python_lib()  # gets site-packages folder
 PYTHONSERVICE_EXE=os.path.join(PYTHONDIR, 'win32', 'pythonservice.exe')
-LOG_DIR = os.path.join(TRAC_PROJECT, log)
+LOG_DIR = os.path.join(TRAC_PROJECT, 'log')
 
 # Trac instance(s)
 ARGS = [TRAC_PROJECT]
 
-def add_auth(auths, vals, cls):
-    info = vals.split(',', 3)
+def add_auth(auths, info, cls):
     p, h, r = info
     if auths.has_key(p):
-        print >>sys.stderr, 'Ignoring duplicate authentication option for ' \
+        print >> sys.stderr, 'Ignoring duplicate authentication option for ' \
                             'project: %s' % p
     else:
         auths[p] = cls(h, r)
@@ -130,6 +129,7 @@
         hostname = ''
         auths = {}
         env_parent_dir = None
+        base_path = None
 
         for o, a in OPTS:
             if o in ("-a", "--auth"):
@@ -159,8 +159,8 @@
                 wsgi_app = AuthenticationMiddleware(wsgi_app, auths, project_name)
             else:
                 wsgi_app = AuthenticationMiddleware(wsgi_app, auths)
-        base_path = base_path.strip('/').strip('\\')
         if base_path:
+            base_path = base_path.strip('\\')
             wsgi_app = BasePathMiddleware(wsgi_app, base_path)
 
         sys.stdout = open(os.path.join(LOG_DIR, 'stdout.log'),'a')

comment:3 Changed 11 years ago by Ryan J Ollos

This ticket has some issues with comment ordering that have been reported in trac:#11491.

comment:4 in reply to:  2 Changed 11 years ago by Ryan J Ollos

Replying to bwanamarko:

Hi @rjollos,

Oops! There were a few typos. I had tested an earlier version. I am so sorry! This version is now tested and running on Windows 7 with no errors.

Thank you for the follow-on patch. I'll commit the changes now.

comment:5 Changed 11 years ago by Ryan J Ollos

In 13691:

Applied follow-on to [13672] from bwanamarko. Refs #11584.

  1. On L67 in OPTS each part of auth tuple should be enclosed in separate sets of quotes.
  2. Forgot to add quotes around string 'log' on L76.
  3. Since using tuple for auth, in add_auths() on L81 remove L82 and change input arg vals to info.
  4. base_path never had any default, so set its default to None after L132.
  5. Can't split None so move L162 into if block
  6. These are non-POSIX paths, so don't need to strip forward slashes, only backslashes.

comment:6 Changed 11 years ago by Ryan J Ollos

Resolution: fixed
Status: reopenedclosed

bwanamarko: If you'd ever like to take over maintenance of this script, we can grant you commit access. Thanks for your contributions!

comment:7 Changed 11 years ago by Ryan J Ollos

Owner: changed from Ryan J Ollos to Mark Mikofski

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Mark Mikofski.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.