[PATCH 12 of 15] Change use of global sys.stdout, sys.stdin, os.environ into method-local

vincent at ricardis.tudelft.nl vincent at ricardis.tudelft.nl
Wed Aug 24 16:49:20 CDT 2005


variables. Changed the run method of hgweb and hgwebdir class. This is
needed for the FastCGI environment patch. It also looks cleaner to me.


# HG changeset patch
# User Vincent Wagelaar <vincent at ricardis.tudelft.nl>
# Node ID db5cb7b9b646e0f7f2a822b0d269f3b598f76c23
# Parent  bf9f130d6fb2a3cb7d059d1a3dbf0235f1958b79
Change use of global sys.stdout, sys.stdin, os.environ into method-local
variables. Changed the run method of hgweb and hgwebdir class. This is
needed for the FastCGI environment patch. It also looks cleaner to me.

diff -r bf9f130d6fb2 -r db5cb7b9b646 mercurial/hgweb.py
--- a/mercurial/hgweb.py	Tue Aug 23 22:36:37 2005
+++ b/mercurial/hgweb.py	Tue Aug 23 22:57:34 2005
@@ -53,16 +53,16 @@
         return "/"
     return up + "/"
 
-def httphdr(type):
-    sys.stdout.write('Content-type: %s\n\n' % type)
-
-def write(*things):
+def httphdr(fd, type):
+    fd.write('Content-type: %s\n\n' % type)
+
+def write(stdout, fd, *things):
     for thing in things:
         if hasattr(thing, "__iter__"):
             for part in thing:
-                write(part)
+                write(fd, part)
         else:
-            sys.stdout.write(str(thing))
+            fd.write(str(thing))
 
 class templater:
     def __init__(self, mapfile, filters = {}, defaults = {}):
@@ -615,7 +615,7 @@
     # tags -> list of changesets corresponding to tags
     # find tag, changeset, file
 
-    def run(self):
+    def run(self, stdin=sys.stdin, stdout=sys.stdout, env=os.environ):
         def header(**map):
             yield self.t("header", **map)
 
@@ -623,7 +623,7 @@
             yield self.t("footer", **map)
 
         self.refresh()
-        args = cgi.parse()
+        args = cgi.parse(stdin, env)
 
         t = self.repo.ui.config("web", "templates", templatepath())
         m = os.path.join(t, "map")
@@ -635,11 +635,11 @@
             p = os.path.join(t, b)
             if os.path.isfile(p): m = p
 
-        port = os.environ["SERVER_PORT"]
+        port = env["SERVER_PORT"]
         port = port != "80" and (":" + port) or ""
-        uri = os.environ["REQUEST_URI"]
+        uri = env["REQUEST_URI"]
         if "?" in uri: uri = uri.split("?")[0]
-        url = "http://%s%s%s" % (os.environ["SERVER_NAME"], port, uri)
+        url = "http://%s%s%s" % (env["SERVER_NAME"], port, uri)
 
         self.t = templater(m, common_filters,
                            {"url":url,
@@ -659,56 +659,56 @@
                 try:
                     hi = self.repo.changelog.rev(self.repo.lookup(hi))
                 except RepoError:
-                    write(self.search(hi))
+                    write(stdout, self.search(hi))
                     return
 
-            write(self.changelog(hi))
+            write(stdout, self.changelog(hi))
 
         elif args['cmd'][0] == 'changeset':
-            write(self.changeset(args['node'][0]))
+            write(stdout, self.changeset(args['node'][0]))
 
         elif args['cmd'][0] == 'manifest':
-            write(self.manifest(args['manifest'][0], args['path'][0]))
+            write(stdout, self.manifest(args['manifest'][0], args['path'][0]))
 
         elif args['cmd'][0] == 'tags':
-            write(self.tags())
+            write(stdout, self.tags())
 
         elif args['cmd'][0] == 'filediff':
-            write(self.filediff(args['file'][0], args['node'][0]))
+            write(stdout, self.filediff(args['file'][0], args['node'][0]))
 
         elif args['cmd'][0] == 'file':
-            write(self.filerevision(args['file'][0], args['filenode'][0]))
+            write(stdout, self.filerevision(args['file'][0], args['filenode'][0]))
 
         elif args['cmd'][0] == 'annotate':
-            write(self.fileannotate(args['file'][0], args['filenode'][0]))
+            write(stdout, self.fileannotate(args['file'][0], args['filenode'][0]))
 
         elif args['cmd'][0] == 'filelog':
-            write(self.filelog(args['file'][0], args['filenode'][0]))
+            write(stdout, self.filelog(args['file'][0], args['filenode'][0]))
 
         elif args['cmd'][0] == 'heads':
-            httphdr("application/mercurial-0.1")
+            httphdr(stdout, "application/mercurial-0.1")
             h = self.repo.heads()
-            sys.stdout.write(" ".join(map(hex, h)) + "\n")
+            stdout.write(" ".join(map(hex, h)) + "\n")
 
         elif args['cmd'][0] == 'branches':
-            httphdr("application/mercurial-0.1")
+            httphdr(stdout, "application/mercurial-0.1")
             nodes = []
             if args.has_key('nodes'):
                 nodes = map(bin, args['nodes'][0].split(" "))
             for b in self.repo.branches(nodes):
-                sys.stdout.write(" ".join(map(hex, b)) + "\n")
+                stdout.write(" ".join(map(hex, b)) + "\n")
 
         elif args['cmd'][0] == 'between':
-            httphdr("application/mercurial-0.1")
+            httphdr(stdout, "application/mercurial-0.1")
             nodes = []
             if args.has_key('pairs'):
                 pairs = [ map(bin, p.split("-"))
                           for p in args['pairs'][0].split(" ") ]
             for b in self.repo.between(pairs):
-                sys.stdout.write(" ".join(map(hex, b)) + "\n")
+                stdout.write(" ".join(map(hex, b)) + "\n")
 
         elif args['cmd'][0] == 'changegroup':
-            httphdr("application/mercurial-0.1")
+            httphdr(stdout, "application/mercurial-0.1")
             nodes = []
             if not self.allowpull:
                 return
@@ -721,12 +721,12 @@
             while 1:
                 chunk = f.read(4096)
                 if not chunk: break
-                sys.stdout.write(z.compress(chunk))
-
-            sys.stdout.write(z.flush())
+                stdout.write(z.compress(chunk))
+
+            stdout.write(z.flush())
 
         else:
-            write(self.t("error"))
+            write(stdout, self.t("error"))
 
 def create_server(repo):
 
@@ -838,9 +838,9 @@
         self.cp = ConfigParser.SafeConfigParser()
         self.cp.read(config)
 
-    def run(self):
+    def run(self, stdin=sys.stdin, stdout=sys.stdout, env=os.environ):
         try:
-            virtual = os.environ["PATH_INFO"]
+            virtual = env["PATH_INFO"]
         except:
             virtual = ""
 
@@ -877,7 +877,7 @@
 
                 yield dict(author = get("web", "author", "unknown"),
                            name = get("web", "name", v),
-                           url = os.environ["REQUEST_URI"] + "/" + v,
+                           url = env["REQUEST_URI"] + "/" + v,
                            parity = parity,
                            shortdesc = get("web", "description", "unknown"),
                            lastupdate = os.stat(os.path.join(r, ".hg",
@@ -885,4 +885,4 @@
 
                 parity = 1 - parity
 
-        write(tmpl("index", entries = entries))
+        write(stdout, tmpl("index", entries = entries))


More information about the Mercurial mailing list