def quiet(): old_stdout, old_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = StringIO(), StringIO() try: yield finally: sys.stdout, sys.stderr = old_stdout, old_stderr
def write_requirements(cmd, basename, filename): dist = cmd.distribution data = StringIO() _write_requirements(data, dist.install_requires) extras_require = dist.extras_require or {} for extra in sorted(extras_require): data.write('\n[{extra}]\n'.format(**vars())) _write_requirements(data, extras_require[extra]) cmd.write_or_delete_file("requirements", filename, data.getvalue())
def test_setup_requires(self): """Regression test for Distribute issue #318 Ensure that a package with setup_requires can be installed when setuptools is installed in the user site-packages without causing a SandboxViolation. """ test_setup_attrs = { 'name': 'test_pkg', 'version': '0.0', 'setup_requires': ['foobar'], 'dependency_links': [os.path.abspath(self.dir)] } test_pkg = os.path.join(self.dir, 'test_pkg') test_setup_py = os.path.join(test_pkg, 'setup.py') test_setup_cfg = os.path.join(test_pkg, 'setup.cfg') os.mkdir(test_pkg) f = open(test_setup_py, 'w') f.write( textwrap.dedent("""\ import setuptools setuptools.setup(**%r) """ % test_setup_attrs)) f.close() foobar_path = os.path.join(self.dir, 'foobar-0.1.tar.gz') make_trivial_sdist( foobar_path, textwrap.dedent("""\ import setuptools setuptools.setup( name='foobar', version='0.1' ) """)) old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = StringIO() sys.stderr = StringIO() try: try: reset_setup_stop_context( lambda: run_setup(test_setup_py, ['install'])) except SandboxViolation: self.fail('Installation caused SandboxViolation') finally: sys.stdout = old_stdout sys.stderr = old_stderr
def local_open(url): """Read a local path, with special support for directories""" scheme, server, path, param, query, frag = urlparse(url) filename = url2pathname(path) if os.path.isfile(filename): return urllib2.urlopen(url) elif path.endswith('/') and os.path.isdir(filename): files = [] for f in os.listdir(filename): if f == 'index.html': with open(os.path.join(filename, f), 'r') as fp: body = fp.read() break elif os.path.isdir(os.path.join(filename, f)): f += '/' files.append("<a href=%r>%s</a>" % (f, f)) else: body = ("<html><head><title>%s</title>" % url) + \ "</head><body>%s</body></html>" % '\n'.join(files) status, message = 200, "OK" else: status, message, body = 404, "Path not found", "Not found" headers = {'content-type': 'text/html'} return HTTPError(url, status, message, headers, StringIO(body))
def test_test(self): if sys.version < "2.6" or hasattr(sys, 'real_prefix'): return dist = Distribution( dict( name='foo', packages=['name', 'name.space', 'name.space.tests'], namespace_packages=['name'], test_suite='name.space.tests.test_suite', use_2to3=True, )) dist.script_name = 'setup.py' cmd = test(dist) cmd.user = 1 cmd.ensure_finalized() cmd.install_dir = site.USER_SITE cmd.user = 1 old_stdout = sys.stdout sys.stdout = StringIO() try: try: # try/except/finally doesn't work in Python 2.4, so we need nested try-statements. cmd.run() except SystemExit: # The test runner calls sys.exit, stop that making an error. pass finally: sys.stdout = old_stdout
def quiet_context(): """ Redirect stdout/stderr to StringIO objects to prevent console output from distutils commands. """ old_stdout = sys.stdout old_stderr = sys.stderr new_stdout = sys.stdout = StringIO() new_stderr = sys.stderr = StringIO() try: yield new_stdout, new_stderr finally: new_stdout.seek(0) new_stderr.seek(0) sys.stdout = old_stdout sys.stderr = old_stderr
def test_get_script_header_jython_workaround(self): # This test doesn't work with Python 3 in some locales if (sys.version_info >= (3, ) and os.environ.get("LC_CTYPE") in (None, "C", "POSIX")): return class java: class lang: class System: @staticmethod def getProperty(property): return "" sys.modules["java"] = java platform = sys.platform sys.platform = 'java1.5.0_13' stdout, stderr = sys.stdout, sys.stderr try: # A mock sys.executable that uses a shebang line (this file) exe = os.path.normpath(os.path.splitext(__file__)[0] + '.py') self.assertEqual( get_script_header('#!/usr/local/bin/python', executable=exe), '#!/usr/bin/env %s\n' % exe) # Ensure we generate what is basically a broken shebang line # when there's options, with a warning emitted sys.stdout = sys.stderr = StringIO() self.assertEqual( get_script_header('#!/usr/bin/python -x', executable=exe), '#!%s -x\n' % exe) self.assertTrue( 'Unable to adapt shebang line' in sys.stdout.getvalue()) sys.stdout = sys.stderr = StringIO() self.assertEqual( get_script_header('#!/usr/bin/python', executable=self.non_ascii_exe), '#!%s -x\n' % self.non_ascii_exe) self.assertTrue( 'Unable to adapt shebang line' in sys.stdout.getvalue()) finally: del sys.modules["java"] sys.platform = platform sys.stdout, sys.stderr = stdout, stderr
def test_bdist_egg(self): dist = Distribution( dict(script_name='setup.py', script_args=['bdist_egg'], name='foo', py_modules=['hi'])) os.makedirs(os.path.join('build', 'src')) old_stdout = sys.stdout sys.stdout = o = StringIO() try: dist.parse_command_line() dist.run_commands() finally: sys.stdout = old_stdout # let's see if we got our egg link at the right place [content] = os.listdir('dist') self.assertTrue(re.match('foo-0.0.0-py[23].\d.egg$', content))
def write_setup_requirements(cmd, basename, filename): data = StringIO() _write_requirements(data, cmd.distribution.setup_requires) cmd.write_or_delete_file("setup-requirements", filename, data.getvalue())
def quiet(): global old_stdout, old_stderr old_stdout, old_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = StringIO(), StringIO()
script_args=['bdist_egg'], name='foo', py_modules=['hi'] )) os.makedirs(os.path.join('build', 'src')) <<<<<<< HEAD with contexts.quiet(): dist.parse_command_line() dist.run_commands() # let's see if we got our egg link at the right place [content] = os.listdir('dist') assert re.match('foo-0.0.0-py[23].\d.egg$', content) ======= old_stdout = sys.stdout sys.stdout = o = StringIO() try: dist.parse_command_line() dist.run_commands() finally: sys.stdout = old_stdout # let's see if we got our egg link at the right place [content] = os.listdir('dist') self.assertTrue(re.match('foo-0.0.0-py[23].\d.egg$', content)) def test_suite(): return unittest.makeSuite(TestDevelopTest) >>>>>>> e4baf504ede925f4f1e07d823c9b20b3d0dbe14c
def upload_file(self, command, pyversion, filename): # Sign if requested if self.sign: gpg_args = ["gpg", "--detach-sign", "-a", filename] if self.identity: gpg_args[2:2] = ["--local-user", self.identity] spawn(gpg_args, dry_run=self.dry_run) # Fill in the data f = open(filename, 'rb') content = f.read() f.close() basename = os.path.basename(filename) comment = '' if command == 'bdist_egg' and self.distribution.has_ext_modules(): comment = "built on %s" % platform.platform(terse=1) data = { ':action': 'file_upload', 'protocol_version': '1', 'name': self.distribution.get_name(), 'version': self.distribution.get_version(), 'content': (basename, content), 'filetype': command, 'pyversion': pyversion, 'md5_digest': md5(content).hexdigest(), } if command == 'bdist_rpm': dist, version, id = platform.dist() if dist: comment = 'built for %s %s' % (dist, version) elif command == 'bdist_dumb': comment = 'built for %s' % platform.platform(terse=1) data['comment'] = comment if self.sign: asc_file = open(filename + ".asc") data['gpg_signature'] = (os.path.basename(filename) + ".asc", asc_file.read()) asc_file.close() # set up the authentication auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip() # Build up the MIME payload for the POST data boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' sep_boundary = '\n--' + boundary end_boundary = sep_boundary + '--' body = StringIO() for key, value in data.items(): # handle multiple entries for the same name if not isinstance(value, list): value = [value] for value in value: if type(value) is tuple: fn = ';filename="%s"' % value[0] value = value[1] else: fn = "" value = str(value) body.write(sep_boundary) body.write('\nContent-Disposition: form-data; name="%s"' % key) body.write(fn) body.write("\n\n") body.write(value) if value and value[-1] == '\r': body.write('\n') # write an extra newline (lurve Macs) body.write(end_boundary) body.write("\n") body = body.getvalue() self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO) # build the Request # We can't use urllib2 since we need to send the Basic # auth right with the first request schema, netloc, url, params, query, fragments = \ urlparse(self.repository) assert not params and not query and not fragments if schema == 'http': http = httplib.HTTPConnection(netloc) elif schema == 'https': http = httplib.HTTPSConnection(netloc) else: raise AssertionError("unsupported schema " + schema) data = '' try: http.connect() http.putrequest("POST", url) http.putheader('Content-type', 'multipart/form-data; boundary=%s' % boundary) http.putheader('Content-length', str(len(body))) http.putheader('Authorization', auth) http.endheaders() http.send(body) except socket.error: e = sys.exc_info()[1] self.announce(str(e), log.ERROR) return r = http.getresponse() if r.status == 200: self.announce('Server response (%s): %s' % (r.status, r.reason), log.INFO) else: self.announce('Upload failed (%s): %s' % (r.status, r.reason), log.ERROR) if self.show_response: print('-' * 75, r.read(), '-' * 75)
"""Read a local path, with special support for directories""" scheme, server, path, param, query, frag = urlparse(url) filename = url2pathname(path) if os.path.isfile(filename): return urllib2.urlopen(url) elif path.endswith('/') and os.path.isdir(filename): files = [] for f in os.listdir(filename): if f=='index.html': <<<<<<< HEAD with open(os.path.join(filename,f),'r') as fp: body = fp.read() ======= fp = open(os.path.join(filename,f),'r') body = fp.read() fp.close() >>>>>>> e4baf504ede925f4f1e07d823c9b20b3d0dbe14c break elif os.path.isdir(os.path.join(filename,f)): f+='/' files.append("<a href=%r>%s</a>" % (f,f)) else: body = ("<html><head><title>%s</title>" % url) + \ "</head><body>%s</body></html>" % '\n'.join(files) status, message = 200, "OK" else: status, message, body = 404, "Path not found", "Not found" headers = {'content-type': 'text/html'} return HTTPError(url, status, message, headers, StringIO(body))
def upload_file(self, command, pyversion, filename): # Sign if requested if self.sign: gpg_args = ["gpg", "--detach-sign", "-a", filename] if self.identity: gpg_args[2:2] = ["--local-user", self.identity] spawn(gpg_args, dry_run=self.dry_run) # Fill in the data f = open(filename,'rb') content = f.read() f.close() basename = os.path.basename(filename) comment = '' if command=='bdist_egg' and self.distribution.has_ext_modules(): comment = "built on %s" % platform.platform(terse=1) data = { ':action':'file_upload', 'protocol_version':'1', 'name':self.distribution.get_name(), 'version':self.distribution.get_version(), 'content':(basename,content), 'filetype':command, 'pyversion':pyversion, 'md5_digest':md5(content).hexdigest(), } if command == 'bdist_rpm': dist, version, id = platform.dist() if dist: comment = 'built for %s %s' % (dist, version) elif command == 'bdist_dumb': comment = 'built for %s' % platform.platform(terse=1) data['comment'] = comment if self.sign: asc_file = open(filename + ".asc") data['gpg_signature'] = (os.path.basename(filename) + ".asc", asc_file.read()) asc_file.close() # set up the authentication auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip() # Build up the MIME payload for the POST data boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' sep_boundary = '\n--' + boundary end_boundary = sep_boundary + '--' body = StringIO() for key, value in data.items(): # handle multiple entries for the same name if isinstance(value, list): value = [value] for value in value: if type(value) is tuple: fn = ';filename="%s"' % value[0] value = value[1] else: fn = "" value = str(value) body.write(sep_boundary) body.write('\nContent-Disposition: form-data; name="%s"'%key) body.write(fn) body.write("\n\n") body.write(value) if value and value[-1] == '\r': body.write('\n') # write an extra newline (lurve Macs) body.write(end_boundary) body.write("\n") body = body.getvalue() self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO) # build the Request # We can't use urllib2 since we need to send the Basic # auth right with the first request schema, netloc, url, params, query, fragments = \ urlparse(self.repository) assert not params and not query and not fragments if schema == 'http': http = httplib.HTTPConnection(netloc) elif schema == 'https': http = httplib.HTTPSConnection(netloc) else: raise AssertionError("unsupported schema " + schema) data = '' try: http.connect() http.putrequest("POST", url) http.putheader('Content-type', 'multipart/form-data; boundary=%s'%boundary) http.putheader('Content-length', str(len(body))) http.putheader('Authorization', auth) http.endheaders() http.send(body) except socket.error: e = sys.exc_info()[1] self.announce(str(e), log.ERROR) return r = http.getresponse() if r.status == 200: self.announce('Server response (%s): %s' % (r.status, r.reason), log.INFO) else: self.announce('Upload failed (%s): %s' % (r.status, r.reason), log.ERROR) if self.show_response: print('-'*75, r.read(), '-'*75)