diff options
| author | ?ric Araujo <merwok@netwok.org> | 2011-11-20 19:45:31 +0100 |
|---|---|---|
| committer | ?ric Araujo <merwok@netwok.org> | 2011-11-20 19:45:31 +0100 |
| commit | 51a3faa30a6e4c8493bc51a2c6de3272fae2ff88 (patch) | |
| tree | 394b7ede35dfb934fa3241ca6efcadc0fe4e0510 /distutils2/command | |
| parent | 5bf02c43b4e6e8e8a23bb4b5269200b66ecfc4f2 (diff) | |
| parent | e910765752824482f8659d4f725ebd5fb84cc5ba (diff) | |
| download | disutils2-51a3faa30a6e4c8493bc51a2c6de3272fae2ff88.tar.gz | |
Branch merge
Diffstat (limited to 'distutils2/command')
| -rw-r--r-- | distutils2/command/bdist_msi.py | 10 | ||||
| -rw-r--r-- | distutils2/command/bdist_wininst.py | 37 | ||||
| -rw-r--r-- | distutils2/command/build_clib.py | 4 | ||||
| -rw-r--r-- | distutils2/command/build_ext.py | 35 | ||||
| -rw-r--r-- | distutils2/command/build_py.py | 12 | ||||
| -rw-r--r-- | distutils2/command/build_scripts.py | 18 | ||||
| -rw-r--r-- | distutils2/command/cmd.py | 12 | ||||
| -rw-r--r-- | distutils2/command/config.py | 23 | ||||
| -rw-r--r-- | distutils2/command/install_data.py | 2 | ||||
| -rw-r--r-- | distutils2/command/install_dist.py | 55 | ||||
| -rw-r--r-- | distutils2/command/install_distinfo.py | 26 | ||||
| -rw-r--r-- | distutils2/command/install_lib.py | 5 | ||||
| -rw-r--r-- | distutils2/command/install_scripts.py | 2 | ||||
| -rw-r--r-- | distutils2/command/register.py | 37 | ||||
| -rw-r--r-- | distutils2/command/sdist.py | 8 | ||||
| -rw-r--r-- | distutils2/command/upload.py | 29 | ||||
| -rw-r--r-- | distutils2/command/upload_docs.py | 18 |
17 files changed, 125 insertions, 208 deletions
diff --git a/distutils2/command/bdist_msi.py b/distutils2/command/bdist_msi.py index 16bde24..43fbe66 100644 --- a/distutils2/command/bdist_msi.py +++ b/distutils2/command/bdist_msi.py @@ -393,8 +393,7 @@ class bdist_msi(Command): # entries for each version as the above code does if self.pre_install_script: scriptfn = os.path.join(self.bdist_dir, "preinstall.bat") - f = open(scriptfn, "w") - try: + with open(scriptfn, "w") as f: # The batch file will be executed with [PYTHON], so that %1 # is the path to the Python interpreter; %0 will be the path # of the batch file. @@ -404,13 +403,8 @@ class bdist_msi(Command): # """ # <actual script> f.write('rem ="""\n%1 %0\nexit\n"""\n') - fp = open(self.pre_install_script) - try: + with open(self.pre_install_script) as fp: f.write(fp.read()) - finally: - fp.close() - finally: - f.close() add_data(self.db, "Binary", [("PreInstall", msilib.Binary(scriptfn)), ]) diff --git a/distutils2/command/bdist_wininst.py b/distutils2/command/bdist_wininst.py index 69e9484..4eab6bc 100644 --- a/distutils2/command/bdist_wininst.py +++ b/distutils2/command/bdist_wininst.py @@ -2,7 +2,6 @@ import sys import os -import codecs from distutils2.command.cmd import Command from distutils2.errors import PackagingOptionError, PackagingPlatformError @@ -246,40 +245,33 @@ class bdist_wininst(Command): logger.info("creating %s", installer_name) if bitmap: - fp = open(bitmap, "rb") - try: + with open(bitmap, "rb") as fp: bitmapdata = fp.read() - finally: - fp.close() bitmaplen = len(bitmapdata) else: bitmaplen = 0 - file = open(installer_name, "wb") - try: + with open(installer_name, "wb") as file: file.write(self.get_exe_bytes()) if bitmap: file.write(bitmapdata) # Convert cfgdata from unicode to ascii, mbcs encoded - if isinstance(cfgdata, unicode): + if isinstance(cfgdata, str): cfgdata = cfgdata.encode("mbcs") # Append the pre-install script - cfgdata = cfgdata + "\0" + cfgdata = cfgdata + b"\0" if self.pre_install_script: # We need to normalize newlines, so we open in text mode and # convert back to bytes. "latin-1" simply avoids any possible # failures. - fp = codecs.open(self.pre_install_script, encoding="latin-1") - try: + with open(self.pre_install_script, encoding="latin-1") as fp: script_data = fp.read().encode("latin-1") - finally: - fp.close() - cfgdata = cfgdata + script_data + "\n\0" + cfgdata = cfgdata + script_data + b"\n\0" else: # empty pre-install script - cfgdata = cfgdata + "\0" + cfgdata = cfgdata + b"\0" file.write(cfgdata) # The 'magic number' 0x1234567B is used to make sure that the @@ -293,13 +285,8 @@ class bdist_wininst(Command): bitmaplen, # number of bytes in bitmap ) file.write(header) - fp = open(arcname, "rb") - try: + with open(arcname, "rb") as fp: file.write(fp.read()) - finally: - fp.close() - finally: - file.close() def get_installer_filename(self, fullname): # Factored out to allow overriding in subclasses @@ -354,9 +341,5 @@ class bdist_wininst(Command): sfix = '' filename = os.path.join(directory, "wininst-%.1f%s.exe" % (bv, sfix)) - fp = open(filename, "rb") - try: - content = fp.read() - finally: - fp.close() - return content + with open(filename, "rb") as fp: + return fp.read() diff --git a/distutils2/command/build_clib.py b/distutils2/command/build_clib.py index 0a8807f..7d819d3 100644 --- a/distutils2/command/build_clib.py +++ b/distutils2/command/build_clib.py @@ -82,7 +82,7 @@ class build_clib(Command): if self.include_dirs is None: self.include_dirs = self.distribution.include_dirs or [] - if isinstance(self.include_dirs, basestring): + if isinstance(self.include_dirs, str): self.include_dirs = self.include_dirs.split(os.pathsep) # XXX same as for build_ext -- what about 'self.define' and @@ -130,7 +130,7 @@ class build_clib(Command): name, build_info = lib - if not isinstance(name, basestring): + if not isinstance(name, str): raise PackagingSetupError("first element of each tuple in 'libraries' " + \ "must be a string (the library name)") if '/' in name or (os.sep != '/' and os.sep in name): diff --git a/distutils2/command/build_ext.py b/distutils2/command/build_ext.py index f35da05..4c3eadf 100644 --- a/distutils2/command/build_ext.py +++ b/distutils2/command/build_ext.py @@ -3,6 +3,7 @@ import os import re import sys +import site from distutils2._backport import sysconfig from distutils2.util import get_platform @@ -14,12 +15,6 @@ from distutils2.util import newer_group from distutils2.compiler.extension import Extension from distutils2 import logger -import site -if sys.version_info[:2] >= (2, 6): - HAS_USER_SITE = True -else: - HAS_USER_SITE = False - if os.name == 'nt': from distutils2.compiler.msvccompiler import get_build_version MSVC_VERSION = int(get_build_version()) @@ -64,6 +59,8 @@ class build_ext(Command): ('inplace', 'i', "ignore build-lib and put compiled extensions into the source " + "directory alongside your pure Python modules"), + ('user', None, + "add user include, library and rpath"), ('include-dirs=', 'I', "list of directories to search for header files" + sep_by), ('define=', 'D', @@ -90,12 +87,8 @@ class build_ext(Command): "path to the SWIG executable"), ] - boolean_options = ['inplace', 'debug', 'force'] + boolean_options = ['inplace', 'debug', 'force', 'user'] - if HAS_USER_SITE: - user_options.append(('user', None, - "add user include, library and rpath")) - boolean_options.append('user') help_options = [ ('help-compiler', None, @@ -122,8 +115,7 @@ class build_ext(Command): self.compiler = None self.swig = None self.swig_opts = None - if HAS_USER_SITE: - self.user = None + self.user = None def finalize_options(self): self.set_undefined_options('build', @@ -158,7 +150,7 @@ class build_ext(Command): plat_py_include = sysconfig.get_path('platinclude') if self.include_dirs is None: self.include_dirs = self.distribution.include_dirs or [] - if isinstance(self.include_dirs, basestring): + if isinstance(self.include_dirs, str): self.include_dirs = self.include_dirs.split(os.pathsep) # Put the Python "system" include dir at the end, so that @@ -167,7 +159,7 @@ class build_ext(Command): if plat_py_include != py_include: self.include_dirs.append(plat_py_include) - if isinstance(self.libraries, basestring): + if isinstance(self.libraries, str): self.libraries = [self.libraries] # Life is easier if we're not forever checking for None, so @@ -176,12 +168,12 @@ class build_ext(Command): self.libraries = [] if self.library_dirs is None: self.library_dirs = [] - elif isinstance(self.library_dirs, basestring): + elif isinstance(self.library_dirs, str): self.library_dirs = self.library_dirs.split(os.pathsep) if self.rpath is None: self.rpath = [] - elif isinstance(self.rpath, basestring): + elif isinstance(self.rpath, str): self.rpath = self.rpath.split(os.pathsep) # for extensions under windows use different directories @@ -242,8 +234,7 @@ class build_ext(Command): # for extensions under Linux or Solaris with a shared Python library, # Python's library directory must be appended to library_dirs sysconfig.get_config_var('Py_ENABLE_SHARED') - if ((sys.platform.startswith('linux') or sys.platform.startswith('gnu') - or sys.platform.startswith('sunos')) + if (sys.platform.startswith(('linux', 'gnu', 'sunos')) and sysconfig.get_config_var('Py_ENABLE_SHARED')): if sys.executable.startswith(os.path.join(sys.exec_prefix, "bin")): # building third party extensions @@ -273,7 +264,7 @@ class build_ext(Command): self.swig_opts = self.swig_opts.split(' ') # Finally add the user include and library directories if requested - if HAS_USER_SITE and self.user: + if self.user: user_include = os.path.join(site.USER_BASE, "include") user_lib = os.path.join(site.USER_BASE, "lib") if os.path.isdir(user_include): @@ -356,7 +347,7 @@ class build_ext(Command): for ext in self.extensions: try: self.build_extension(ext) - except (CCompilerError, PackagingError, CompileError), e: + except (CCompilerError, PackagingError, CompileError) as e: if not ext.optional: raise logger.warning('%s: building extension %r failed: %s', @@ -644,7 +635,7 @@ class build_ext(Command): else: if sysconfig.get_config_var('Py_ENABLE_SHARED'): - template = 'python%d.%d' + template = 'python%d.%d' + getattr(sys, 'abiflags', '') pythonlib = template % sys.version_info[:2] return ext.libraries + [pythonlib] else: diff --git a/distutils2/command/build_py.py b/distutils2/command/build_py.py index 11e0442..f9b9bcd 100644 --- a/distutils2/command/build_py.py +++ b/distutils2/command/build_py.py @@ -4,10 +4,10 @@ import os from glob import glob from distutils2 import logger -from distutils2.command.cmd import Command -from distutils2.errors import PackagingOptionError, PackagingFileError from distutils2.util import convert_path -from distutils2.compat import Mixin2to3 +from distutils2.compat import Mixin2to3, cache_from_source +from distutils2.errors import PackagingOptionError, PackagingFileError +from distutils2.command.cmd import Command # marking public APIs __all__ = ['build_py'] @@ -335,9 +335,9 @@ class build_py(Command, Mixin2to3): outputs.append(filename) if include_bytecode: if self.compile: - outputs.append(filename + "c") + outputs.append(cache_from_source(filename, True)) if self.optimize: - outputs.append(filename + "o") + outputs.append(cache_from_source(filename, False)) outputs += [ os.path.join(build_dir, filename) @@ -347,7 +347,7 @@ class build_py(Command, Mixin2to3): return outputs def build_module(self, module, module_file, package): - if isinstance(package, basestring): + if isinstance(package, str): package = package.split('.') elif not isinstance(package, (list, tuple)): raise TypeError( diff --git a/distutils2/command/build_scripts.py b/distutils2/command/build_scripts.py index 300f692..e752ff1 100644 --- a/distutils2/command/build_scripts.py +++ b/distutils2/command/build_scripts.py @@ -2,17 +2,18 @@ import os import re +from tokenize import detect_encoding from distutils2.command.cmd import Command from distutils2.util import convert_path, newer from distutils2 import logger from distutils2.compat import Mixin2to3 -from distutils2.compat import detect_encoding, fsencode +from distutils2.compat import fsencode from distutils2._backport import sysconfig # check if Python is called on the first line with this expression -first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$') +first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$') class build_scripts(Command, Mixin2to3): @@ -94,7 +95,7 @@ class build_scripts(Command, Mixin2to3): match = first_line_re.match(first_line) if match: adjust = True - post_interp = match.group(1) or '' + post_interp = match.group(1) or b'' if adjust: logger.info("copying and adjusting %s -> %s", script, @@ -108,7 +109,7 @@ class build_scripts(Command, Mixin2to3): "python%s%s" % (sysconfig.get_config_var("VERSION"), sysconfig.get_config_var("EXE"))) executable = fsencode(executable) - shebang = "#!" + executable + post_interp + "\n" + shebang = b"#!" + executable + post_interp + b"\n" # Python parser starts to read a script using UTF-8 until # it gets a #coding:xxx cookie. The shebang has to be the # first line of a file, the #coding:xxx cookie cannot be @@ -130,12 +131,9 @@ class build_scripts(Command, Mixin2to3): "The shebang (%r) is not decodable " "from the script encoding (%s)" % ( shebang, encoding)) - outf = open(outfile, "wb") - try: + with open(outfile, "wb") as outf: outf.write(shebang) outf.writelines(f.readlines()) - finally: - outf.close() if f: f.close() else: @@ -148,8 +146,8 @@ class build_scripts(Command, Mixin2to3): if self.dry_run: logger.info("changing mode of %s", file) else: - oldmode = os.stat(file).st_mode & 07777 - newmode = (oldmode | 0555) & 07777 + oldmode = os.stat(file).st_mode & 0o7777 + newmode = (oldmode | 0o555) & 0o7777 if newmode != oldmode: logger.info("changing mode of %s from %o to %o", file, oldmode, newmode) diff --git a/distutils2/command/cmd.py b/distutils2/command/cmd.py index 1cdae14..6ef7554 100644 --- a/distutils2/command/cmd.py +++ b/distutils2/command/cmd.py @@ -8,7 +8,7 @@ from distutils2.errors import PackagingOptionError from distutils2._backport.shutil import copyfile, move, make_archive -class Command(object): +class Command: """Abstract base class for defining command classes, the "worker bees" of Packaging. A useful analogy for command classes is to think of them as subroutines with local variables called "options". The options @@ -216,7 +216,7 @@ class Command(object): if val is None: setattr(self, option, default) return default - elif not isinstance(val, basestring): + elif not isinstance(val, str): raise PackagingOptionError("'%s' must be a %s (got `%s`)" % (option, what, val)) return val @@ -236,14 +236,14 @@ class Command(object): val = getattr(self, option) if val is None: return - elif isinstance(val, basestring): + elif isinstance(val, str): setattr(self, option, re.split(r',\s*|\s+', val)) else: if isinstance(val, list): # checks if all elements are str ok = True for element in val: - if not isinstance(element, basestring): + if not isinstance(element, str): ok = False break else: @@ -351,7 +351,7 @@ class Command(object): def execute(self, func, args, msg=None, level=1): util.execute(func, args, msg, dry_run=self.dry_run) - def mkpath(self, name, mode=00777, dry_run=None): + def mkpath(self, name, mode=0o777, dry_run=None): if dry_run is None: dry_run = self.dry_run name = os.path.normpath(name) @@ -424,7 +424,7 @@ class Command(object): skip_msg = "skipping %s (inputs unchanged)" % outfile # Allow 'infiles' to be a single string - if isinstance(infiles, basestring): + if isinstance(infiles, str): infiles = (infiles,) elif not isinstance(infiles, (list, tuple)): raise TypeError( diff --git a/distutils2/command/config.py b/distutils2/command/config.py index e3af2e5..437cde9 100644 --- a/distutils2/command/config.py +++ b/distutils2/command/config.py @@ -67,17 +67,17 @@ class config(Command): def finalize_options(self): if self.include_dirs is None: self.include_dirs = self.distribution.include_dirs or [] - elif isinstance(self.include_dirs, basestring): + elif isinstance(self.include_dirs, str): self.include_dirs = self.include_dirs.split(os.pathsep) if self.libraries is None: self.libraries = [] - elif isinstance(self.libraries, basestring): + elif isinstance(self.libraries, str): self.libraries = [self.libraries] if self.library_dirs is None: self.library_dirs = [] - elif isinstance(self.library_dirs, basestring): + elif isinstance(self.library_dirs, str): self.library_dirs = self.library_dirs.split(os.pathsep) def run(self): @@ -110,8 +110,7 @@ class config(Command): def _gen_temp_sourcefile(self, body, headers, lang): filename = "_configtest" + LANG_EXT[lang] - file = open(filename, "w") - try: + with open(filename, "w") as file: if headers: for header in headers: file.write("#include <%s>\n" % header) @@ -119,8 +118,6 @@ class config(Command): file.write(body) if body[-1] != "\n": file.write("\n") - finally: - file.close() return filename def _preprocess(self, body, headers, include_dirs, lang): @@ -206,11 +203,10 @@ class config(Command): self._check_compiler() src, out = self._preprocess(body, headers, include_dirs, lang) - if isinstance(pattern, basestring): + if isinstance(pattern, str): pattern = re.compile(pattern) - file = open(out) - try: + with open(out) as file: match = False while True: line = file.readline() @@ -219,8 +215,6 @@ class config(Command): if pattern.search(line): match = True break - finally: - file.close() self._clean() return match @@ -351,8 +345,5 @@ def dump_file(filename, head=None): logger.info(filename) else: logger.info(head) - file = open(filename) - try: + with open(filename) as file: logger.info(file.read()) - finally: - file.close() diff --git a/distutils2/command/install_data.py b/distutils2/command/install_data.py index 3a63e20..d2a8015 100644 --- a/distutils2/command/install_data.py +++ b/distutils2/command/install_data.py @@ -48,7 +48,7 @@ class install_data(Command): self.mkpath(dir_dest) try: out = self.copy_file(_file[0], dir_dest)[0] - except Error, e: + except Error as e: logger.warning('%s: %s', self.get_command_name(), e) out = destination diff --git a/distutils2/command/install_dist.py b/distutils2/command/install_dist.py index bde77c3..8d8b675 100644 --- a/distutils2/command/install_dist.py +++ b/distutils2/command/install_dist.py @@ -13,11 +13,6 @@ from distutils2.util import write_file from distutils2.util import convert_path, change_root, get_platform from distutils2.errors import PackagingOptionError -if sys.version_info[:2] >= (2, 6): - HAS_USER_SITE = True -else: - HAS_USER_SITE = False - class install_dist(Command): @@ -29,6 +24,9 @@ class install_dist(Command): "installation prefix"), ('exec-prefix=', None, "(Unix only) prefix for platform-specific files"), + ('user', None, + "install in user site-packages directory [%s]" % + get_path('purelib', '%s_user' % os.name)), ('home=', None, "(Unix only) home directory to install under"), @@ -97,15 +95,7 @@ class install_dist(Command): ] boolean_options = ['compile', 'force', 'skip-build', 'no-distinfo', - 'requested', 'no-record'] - - if HAS_USER_SITE: - user_options.append( - ('user', None, - "install in user site-packages directory [%s]" % - get_path('purelib', '%s_user' % os.name))) - - boolean_options.append('user') + 'requested', 'no-record', 'user'] negative_opt = {'no-compile': 'compile', 'no-requested': 'requested'} @@ -115,8 +105,7 @@ class install_dist(Command): self.prefix = None self.exec_prefix = None self.home = None - if HAS_USER_SITE: - self.user = False + self.user = False # These select only the installation base; it's up to the user to # specify the installation scheme (currently, that means supplying @@ -135,9 +124,8 @@ class install_dist(Command): self.install_lib = None # set to either purelib or platlib self.install_scripts = None self.install_data = None - if HAS_USER_SITE: - self.install_userbase = get_config_var('userbase') - self.install_usersite = get_path('purelib', '%s_user' % os.name) + self.install_userbase = get_config_var('userbase') + self.install_usersite = get_path('purelib', '%s_user' % os.name) self.compile = None self.optimize = None @@ -218,9 +206,8 @@ class install_dist(Command): raise PackagingOptionError( "must supply either home or prefix/exec-prefix -- not both") - if HAS_USER_SITE and self.user and ( - self.prefix or self.exec_prefix or self.home or - self.install_base or self.install_platbase): + if self.user and (self.prefix or self.exec_prefix or self.home or + self.install_base or self.install_platbase): raise PackagingOptionError( "can't combine user with prefix/exec_prefix/home or " "install_base/install_platbase") @@ -273,11 +260,9 @@ class install_dist(Command): 'exec_prefix': exec_prefix, 'srcdir': srcdir, 'projectbase': projectbase, - } - - if HAS_USER_SITE: - self.config_vars['userbase'] = self.install_userbase - self.config_vars['usersite'] = self.install_usersite + 'userbase': self.install_userbase, + 'usersite': self.install_usersite, + } self.expand_basedirs() @@ -295,7 +280,7 @@ class install_dist(Command): self.dump_dirs("post-expand_dirs()") # Create directories under USERBASE - if HAS_USER_SITE and self.user: + if self.user: self.create_user_dirs() # Pick the actual directory to install all modules to: either @@ -310,10 +295,8 @@ class install_dist(Command): # Convert directories from Unix /-separated syntax to the local # convention. - self.convert_paths('lib', 'purelib', 'platlib', - 'scripts', 'data', 'headers') - if HAS_USER_SITE: - self.convert_paths('userbase', 'usersite') + self.convert_paths('lib', 'purelib', 'platlib', 'scripts', + 'data', 'headers', 'userbase', 'usersite') # Well, we're not actually fully completely finalized yet: we still # have to deal with 'extra_path', which is the hack for allowing @@ -354,7 +337,7 @@ class install_dist(Command): "installation scheme is incomplete") return - if HAS_USER_SITE and self.user: + if self.user: if self.install_userbase is None: raise PackagingPlatformError( "user base directory is not specified") @@ -382,7 +365,7 @@ class install_dist(Command): def finalize_other(self): """Finalize options for non-posix platforms""" - if HAS_USER_SITE and self.user: + if self.user: if self.install_userbase is None: raise PackagingPlatformError( "user base directory is not specified") @@ -463,7 +446,7 @@ class install_dist(Command): self.extra_path = self.distribution.extra_path if self.extra_path is not None: - if isinstance(self.extra_path, basestring): + if isinstance(self.extra_path, str): self.extra_path = self.extra_path.split(',') if len(self.extra_path) == 1: @@ -498,7 +481,7 @@ class install_dist(Command): home = convert_path(os.path.expanduser("~")) for name, path in self.config_vars.items(): if path.startswith(home) and not os.path.isdir(path): - os.makedirs(path, 0700) + os.makedirs(path, 0o700) # -- Command execution methods ------------------------------------- diff --git a/distutils2/command/install_distinfo.py b/distutils2/command/install_distinfo.py index fa74448..81de925 100644 --- a/distutils2/command/install_distinfo.py +++ b/distutils2/command/install_distinfo.py @@ -4,11 +4,7 @@ import os import csv -import codecs -try: - import hashlib -except ImportError: - from distutils2._backport import hashlib +import hashlib from distutils2 import logger from distutils2.command.cmd import Command @@ -89,11 +85,8 @@ class install_distinfo(Command): installer_path = os.path.join(self.install_dir, 'INSTALLER') logger.info('creating %s', installer_path) if not self.dry_run: - f = open(installer_path, 'w') - try: + with open(installer_path, 'w') as f: f.write(self.installer) - finally: - f.close() self.outfiles.append(installer_path) if self.requested: @@ -110,15 +103,12 @@ class install_distinfo(Command): 'RESOURCES') logger.info('creating %s', resources_path) if not self.dry_run: - f = open(resources_path, 'w') - try: + with open(resources_path, 'w') as f: writer = csv.writer(f, delimiter=',', lineterminator='\n', quotechar='"') for row in install_data.get_resources_out(): writer.writerow(row) - finally: - f.close() self.outfiles.append(resources_path) @@ -126,8 +116,7 @@ class install_distinfo(Command): record_path = os.path.join(self.install_dir, 'RECORD') logger.info('creating %s', record_path) if not self.dry_run: - f = codecs.open(record_path, 'w', encoding='utf-8') - try: + with open(record_path, 'w', encoding='utf-8') as f: writer = csv.writer(f, delimiter=',', lineterminator='\n', quotechar='"') @@ -140,19 +129,14 @@ class install_distinfo(Command): writer.writerow((fpath, '', '')) else: size = os.path.getsize(fpath) - fp = open(fpath, 'rb') - try: + with open(fpath, 'rb') as fp: hash = hashlib.md5() hash.update(fp.read()) - finally: - fp.close() md5sum = hash.hexdigest() writer.writerow((fpath, md5sum, size)) # add the RECORD file itself writer.writerow((record_path, '', '')) - finally: - f.close() self.outfiles.append(record_path) def get_outputs(self): diff --git a/distutils2/command/install_lib.py b/distutils2/command/install_lib.py index d3426f1..8ee1fd5 100644 --- a/distutils2/command/install_lib.py +++ b/distutils2/command/install_lib.py @@ -3,6 +3,7 @@ import os from distutils2 import logger +from distutils2.compat import cache_from_source from distutils2.command.cmd import Command from distutils2.errors import PackagingOptionError @@ -138,9 +139,9 @@ class install_lib(Command): if ext != PYTHON_SOURCE_EXTENSION: continue if self.compile: - bytecode_files.append(py_file + "c") + bytecode_files.append(cache_from_source(py_file, True)) if self.optimize: - bytecode_files.append(py_file + "o") + bytecode_files.append(cache_from_source(py_file, False)) return bytecode_files diff --git a/distutils2/command/install_scripts.py b/distutils2/command/install_scripts.py index 3bed33c..6366be2 100644 --- a/distutils2/command/install_scripts.py +++ b/distutils2/command/install_scripts.py @@ -48,7 +48,7 @@ class install_scripts(Command): if self.dry_run: logger.info("changing mode of %s", file) else: - mode = (os.stat(file).st_mode | 0555) & 07777 + mode = (os.stat(file).st_mode | 0o555) & 0o7777 logger.info("changing mode of %s to %o", file, mode) os.chmod(file, mode) diff --git a/distutils2/command/register.py b/distutils2/command/register.py index e00d574..091918c 100644 --- a/distutils2/command/register.py +++ b/distutils2/command/register.py @@ -3,8 +3,9 @@ # Contributed by Richard Jones import getpass -import urllib2 -import urlparse +import urllib.error +import urllib.parse +import urllib.request from distutils2 import logger from distutils2.util import (read_pypirc, generate_pypirc, DEFAULT_REPOSITORY, @@ -80,7 +81,7 @@ class register(Command): def classifiers(self): ''' Fetch the list of classifiers from the server. ''' - response = urllib2.urlopen(self.repository+'?:action=list_classifiers') + response = urllib.request.urlopen(self.repository+'?:action=list_classifiers') logger.info(response.read()) def verify_metadata(self): @@ -143,22 +144,22 @@ We need to know who you are, so please choose either: 4. quit Your selection [default 1]: ''') - choice = raw_input() + choice = input() if not choice: choice = '1' elif choice not in choices: - print 'Please choose one of the four options!' + print('Please choose one of the four options!') if choice == '1': # get the username and password while not username: - username = raw_input('Username: ') + username = input('Username: ') while not password: password = getpass.getpass('Password: ') # set up the authentication - auth = urllib2.HTTPPasswordMgr() - host = urlparse.urlparse(self.repository)[1] + auth = urllib.request.HTTPPasswordMgr() + host = urllib.parse.urlparse(self.repository)[1] auth.add_password(self.realm, host, username, password) # send the info to the server and report the result code, result = self.post_to_server(self.build_post_data('submit'), @@ -178,7 +179,7 @@ Your selection [default 1]: ''') get_pypirc_path()) choice = 'X' while choice.lower() not in ('y', 'n'): - choice = raw_input('Save your login (y/N)?') + choice = input('Save your login (y/N)?') if not choice: choice = 'n' if choice.lower() == 'y': @@ -189,7 +190,7 @@ Your selection [default 1]: ''') data['name'] = data['password'] = data['email'] = '' data['confirm'] = None while not data['name']: - data['name'] = raw_input('Username: ') + data['name'] = input('Username: ') while data['password'] != data['confirm']: while not data['password']: data['password'] = getpass.getpass('Password: ') @@ -198,9 +199,9 @@ Your selection [default 1]: ''') if data['password'] != data['confirm']: data['password'] = '' data['confirm'] = None - print "Password and confirm don't match!" + print("Password and confirm don't match!") while not data['email']: - data['email'] = raw_input(' EMail: ') + data['email'] = input(' EMail: ') code, result = self.post_to_server(data) if code != 200: logger.info('server response (%s): %s', code, result) @@ -211,7 +212,7 @@ Your selection [default 1]: ''') data = {':action': 'password_reset'} data['email'] = '' while not data['email']: - data['email'] = raw_input('Your email address: ') + data['email'] = input('Your email address: ') code, result = self.post_to_server(data) logger.info('server response (%s): %s', code, result) @@ -236,20 +237,20 @@ Your selection [default 1]: ''') 'Content-type': content_type, 'Content-length': str(len(body)) } - req = urllib2.Request(self.repository, body, headers) + req = urllib.request.Request(self.repository, body, headers) # handle HTTP and include the Basic Auth handler - opener = urllib2.build_opener( - urllib2.HTTPBasicAuthHandler(password_mgr=auth) + opener = urllib.request.build_opener( + urllib.request.HTTPBasicAuthHandler(password_mgr=auth) ) data = '' try: result = opener.open(req) - except urllib2.HTTPError, e: + except urllib.error.HTTPError as e: if self.show_response: data = e.fp.read() result = e.code, e.msg - except urllib2.URLError, e: + except urllib.error.URLError as e: result = 500, str(e) else: if self.show_response: diff --git a/distutils2/command/sdist.py b/distutils2/command/sdist.py index a4fa4bd..0fc990d 100644 --- a/distutils2/command/sdist.py +++ b/distutils2/command/sdist.py @@ -3,7 +3,7 @@ import os import re import sys -from StringIO import StringIO +from io import StringIO from distutils2 import logger from distutils2.util import resolve_name @@ -134,7 +134,7 @@ class sdist(Command): if self.manifest_builders is None: self.manifest_builders = [] else: - if isinstance(self.manifest_builders, basestring): + if isinstance(self.manifest_builders, str): self.manifest_builders = self.manifest_builders.split(',') builders = [] for builder in self.manifest_builders: @@ -143,7 +143,7 @@ class sdist(Command): continue try: builder = resolve_name(builder) - except ImportError, e: + except ImportError as e: raise PackagingModuleError(e) builders.append(builder) @@ -337,7 +337,7 @@ class sdist(Command): """ return self.archive_files - def create_tree(self, base_dir, files, mode=0777, dry_run=False): + def create_tree(self, base_dir, files, mode=0o777, dry_run=False): need_dir = set() for file in files: need_dir.add(os.path.join(base_dir, os.path.dirname(file))) diff --git a/distutils2/command/upload.py b/distutils2/command/upload.py index 0bdac4a..20f2be0 100644 --- a/distutils2/command/upload.py +++ b/distutils2/command/upload.py @@ -4,14 +4,11 @@ import os import socket import logging import platform -import urlparse +import urllib.parse from base64 import standard_b64encode -try: - from hashlib import md5 -except ImportError: - from distutils2._backport.hashlib import md5 -from urllib2 import HTTPError -from urllib2 import urlopen, Request +from hashlib import md5 +from urllib.error import HTTPError +from urllib.request import urlopen, Request from distutils2 import logger from distutils2.errors import PackagingOptionError @@ -87,7 +84,7 @@ class upload(Command): def upload_file(self, command, pyversion, filename): # Makes sure the repository URL is compliant scheme, netloc, url, params, query, fragments = \ - urlparse.urlparse(self.repository) + urllib.parse.urlparse(self.repository) if params or query or fragments: raise AssertionError("Incompatible url %s" % self.repository) @@ -104,11 +101,8 @@ class upload(Command): # Fill in the data - send all the metadata in case we need to # register a new release - f = open(filename, 'rb') - try: + with open(filename, 'rb') as f: content = f.read() - finally: - f.close() data = self.distribution.metadata.todict() @@ -124,11 +118,8 @@ class upload(Command): data['comment'] = 'built for %s' % platform.platform(terse=True) if self.sign: - fp = open(filename + '.asc') - try: + with open(filename + '.asc') as fp: sig = fp.read() - finally: - fp.close() data['gpg_signature'] = [ (os.path.basename(filename) + ".asc", sig)] @@ -136,7 +127,7 @@ class upload(Command): # The exact encoding of the authentication string is debated. # Anyway PyPI only accepts ascii for both username or password. user_pass = (self.username + ":" + self.password).encode('ascii') - auth = "Basic " + standard_b64encode(user_pass) + auth = b"Basic " + standard_b64encode(user_pass) # Build up the MIME payload for the POST data files = [] @@ -160,10 +151,10 @@ class upload(Command): result = urlopen(request) status = result.code reason = result.msg - except socket.error, e: + except socket.error as e: logger.error(e) return - except HTTPError, e: + except HTTPError as e: status = e.code reason = e.msg diff --git a/distutils2/command/upload_docs.py b/distutils2/command/upload_docs.py index 2f813d3..9e93fb5 100644 --- a/distutils2/command/upload_docs.py +++ b/distutils2/command/upload_docs.py @@ -5,9 +5,9 @@ import base64 import socket import zipfile import logging -import httplib -import urlparse -from StringIO import StringIO +import http.client +import urllib.parse +from io import BytesIO from distutils2 import logger from distutils2.util import (read_pypirc, DEFAULT_REPOSITORY, DEFAULT_REALM, @@ -18,7 +18,7 @@ from distutils2.command.cmd import Command def zip_dir(directory): """Compresses recursively contents of directory into a BytesIO object""" - destination = StringIO() + destination = BytesIO() zip_file = zipfile.ZipFile(destination, "w") try: for root, dirs, files in os.walk(directory): @@ -91,16 +91,16 @@ class upload_docs(Command): credentials = self.username + ':' + self.password # FIXME should use explicit encoding - auth = "Basic " + base64.encodestring(credentials.encode()).strip() + auth = b"Basic " + base64.encodebytes(credentials.encode()).strip() logger.info("Submitting documentation to %s", self.repository) - scheme, netloc, url, params, query, fragments = urlparse.urlparse( + scheme, netloc, url, params, query, fragments = urllib.parse.urlparse( self.repository) if scheme == "http": - conn = httplib.HTTPConnection(netloc) + conn = http.client.HTTPConnection(netloc) elif scheme == "https": - conn = httplib.HTTPSConnection(netloc) + conn = http.client.HTTPSConnection(netloc) else: raise AssertionError("unsupported scheme %r" % scheme) @@ -113,7 +113,7 @@ class upload_docs(Command): conn.endheaders() conn.send(body) - except socket.error, e: + except socket.error as e: logger.error(e) return |
