[Python-checkins] distutils2: merge konryd changes

tarek.ziade python-checkins at python.org
Sun Jul 4 11:48:39 CEST 2010


tarek.ziade pushed 0840194e5a63 to distutils2:

http://hg.python.org/distutils2/rev/0840194e5a63
changeset:   299:0840194e5a63
parent:      298:031d3246aa0d
parent:      279:ec619c341cd2
user:        Alexis Metaireau <ametaireau at gmail.com>
date:        Thu Jun 03 17:45:59 2010 +0200
summary:     merge konryd changes
files:       

diff --git a/src/distutils2/command/upload_docs.py b/src/distutils2/command/upload_docs.py
--- a/src/distutils2/command/upload_docs.py
+++ b/src/distutils2/command/upload_docs.py
@@ -1,6 +1,7 @@
 import base64, httplib, os.path, socket, tempfile, urlparse, zipfile
 from cStringIO import StringIO
 from distutils2 import log
+from distutils2.command.upload import upload
 from distutils2.core import PyPIRCCommand
 from distutils2.errors import DistutilsFileError
 
@@ -27,7 +28,6 @@
     """
     if boundary is None:
         boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
-    CRLF = '\r\n'
     l = []
     for (key, value) in fields:
         l.extend([
@@ -43,24 +43,29 @@
             value])
     l.append('--' + boundary + '--')
     l.append('')
-    body = CRLF.join(l)
+    body =  '\r\n'.join(l)
     content_type = 'multipart/form-data; boundary=%s' % boundary
     return content_type, body
 
 class upload_docs(PyPIRCCommand):
 
     user_options = [
+        ('repository=', 'r', "url of repository [default: %s]" % upload.DEFAULT_REPOSITORY),
+        ('show-response', None, 'display full response text from server'),
         ('upload-dir=', None, 'directory to upload'),
         ]
 
     def initialize_options(self):
-        self.upload_dir = None
+        PyPIRCCommand.initialize_options(self)
+        self.upload_dir = "build/docs"
 
     def finalize_options(self):
         PyPIRCCommand.finalize_options(self)
         if self.upload_dir == None:
             build = self.get_finalized_command('build')
             self.upload_dir = os.path.join(build.build_base, "docs")
+        self.announce('Using upload directory %s' % self.upload_dir)
+        self.verify_upload_dir(self.upload_dir)
         config = self._read_pypirc()
         if config != {}:
             self.username = config['username']
@@ -69,16 +74,15 @@
             self.realm = config['realm']
 
     def verify_upload_dir(self, upload_dir):
+        self.ensure_dirname('upload_dir')
         index_location = os.path.join(upload_dir, "index.html")
         if not os.path.exists(index_location):
             mesg = "No 'index.html found in docs directory (%s)"
             raise DistutilsFileError(mesg % upload_dir)
 
-
     def run(self):
         tmp_dir = tempfile.mkdtemp()
         name = self.distribution.metadata['Name']
-        self.verify_upload_dir(self.upload_dir)
         zip_file = zip_dir(self.upload_dir)
 
         fields = {':action': 'doc_upload', 'name': name}.items()
@@ -126,3 +130,6 @@
         else:
             self.announce('Upload failed (%s): %s' % (r.status, r.reason),
                           log.ERROR)
+
+        if self.show_response:
+            print "\n".join(['-'*75, r.read(), '-'*75])
diff --git a/src/distutils2/tests/test_upload_docs.py b/src/distutils2/tests/test_upload_docs.py
--- a/src/distutils2/tests/test_upload_docs.py
+++ b/src/distutils2/tests/test_upload_docs.py
@@ -1,6 +1,6 @@
 """Tests for distutils.command.upload_docs."""
 # -*- encoding: utf8 -*-
-import httplib, os, os.path, tempfile, unittest2, zipfile
+import httplib, os, os.path, shutil, sys, tempfile, unittest2, zipfile
 from cStringIO import StringIO
 
 from distutils2.command import upload_docs as upload_docs_mod
@@ -8,7 +8,7 @@
                                     encode_multipart)
 from distutils2.core import Distribution
 
-from distutils2.errors import DistutilsFileError
+from distutils2.errors import DistutilsFileError, DistutilsOptionError
 
 from distutils2.tests import support
 from distutils2.tests.pypi_server import PyPIServer, PyPIServerTestCase
@@ -54,15 +54,23 @@
         self.dist.metadata['Name'] = "distr-name"
         self.cmd = upload_docs(self.dist)
 
-    def test_generates_uploaddir_if_not_given(self):
-        self.assertEqual(self.cmd.upload_dir, None)
-        self.cmd.ensure_finalized()
-        self.assertEqual(self.cmd.upload_dir, os.path.join("build", "docs"))
+    def test_default_uploaddir(self):
+        sandbox = tempfile.mkdtemp()
+        previous = os.getcwd()
+        os.chdir(sandbox)
+        try:
+            os.mkdir("build")
+            self.prepare_sample_dir("build")
+            self.cmd.ensure_finalized()
+            self.assertEqual(self.cmd.upload_dir, os.path.join("build", "docs"))
+        finally:
+            os.chdir(previous)
 
-    def prepare_sample_dir(self):
-        sample_dir = tempfile.mkdtemp()
-        os.mkdir(os.path.join(sample_dir, "some_dir"))
-        self.write_file(os.path.join(sample_dir, "some_dir", "index.html"), "Ce mortel ennui")
+    def prepare_sample_dir(self, sample_dir=None):
+        if sample_dir is None:
+            sample_dir = tempfile.mkdtemp()
+        os.mkdir(os.path.join(sample_dir, "docs"))
+        self.write_file(os.path.join(sample_dir, "docs", "index.html"), "Ce mortel ennui")
         self.write_file(os.path.join(sample_dir, "index.html"), "Oh la la")
         return sample_dir
 
@@ -71,7 +79,7 @@
         compressed = zip_dir(source_dir)
 
         zip_f = zipfile.ZipFile(compressed)
-        self.assertEqual(zip_f.namelist(), ['index.html', 'some_dir/index.html'])
+        self.assertEqual(zip_f.namelist(), ['index.html', 'docs/index.html'])
 
     def test_encode_multipart(self):
         fields = [("a", "b"), ("c", "d")]
@@ -81,9 +89,9 @@
         self.assertEqual(body, EXPECTED_MULTIPART_OUTPUT)
 
     def prepare_command(self):
+        self.cmd.upload_dir = self.prepare_sample_dir()
         self.cmd.ensure_finalized()
         self.cmd.repository = self.pypi.full_address
-        self.cmd.upload_dir = self.prepare_sample_dir()
         self.cmd.username = "username"
         self.cmd.password = "password"
 
@@ -109,7 +117,7 @@
         # check their contents
         self.assertIn("doc_upload", action)
         self.assertIn("distr-name", name)
-        self.assertIn("some_dir/index.html", content)
+        self.assertIn("docs/index.html", content)
         self.assertIn("Ce mortel ennui", content)
 
     def test_https_connection(self):
@@ -151,14 +159,36 @@
     def test_reads_pypirc_data(self):
         self.write_file(self.rc, PYPIRC % self.pypi.full_address)
         self.cmd.repository = self.pypi.full_address
+        self.cmd.upload_dir = self.prepare_sample_dir()
         self.cmd.ensure_finalized()
         self.assertEqual(self.cmd.username, "real_slim_shady")
         self.assertEqual(self.cmd.password, "long_island")
 
     def test_checks_index_html_presence(self):
-        self.prepare_command()
+        self.cmd.upload_dir = self.prepare_sample_dir()
         os.remove(os.path.join(self.cmd.upload_dir, "index.html"))
-        self.assertRaises(DistutilsFileError, self.cmd.run)
+        self.assertRaises(DistutilsFileError, self.cmd.ensure_finalized)
+
+    def test_checks_upload_dir(self):
+        self.cmd.upload_dir = self.prepare_sample_dir()
+        shutil.rmtree(os.path.join(self.cmd.upload_dir))
+        self.assertRaises(DistutilsOptionError, self.cmd.ensure_finalized)
+
+    def test_show_response(self):
+        orig_stdout = sys.stdout
+        write_args = []
+        class MockStdIn(object):
+            def write(self, arg):
+                write_args.append(arg)
+        sys.stdout = MockStdIn()
+        try:
+            self.prepare_command()
+            self.cmd.show_response = True
+            self.cmd.run()
+        finally:
+            sys.stdout = orig_stdout
+        self.assertTrue(write_args[0], "should report the response")
+        self.assertIn("\n".join(self.pypi.default_response_data), write_args[0])
 
 def test_suite():
     return unittest2.makeSuite(UploadDocsTestCase)

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list