def move_file(src, dst, verbose=1, dry_run=0): """Move a file 'src' to 'dst'. If 'dst' is a directory, the file will be moved into it with the same name; otherwise, 'src' is just renamed to 'dst'. Return the new full name of the file. Handles cross-device moves on Unix using 'copy_file()'. What about other systems??? """ from os.path import exists, isfile, isdir, basename, dirname import errno if verbose >= 1: log.info("moving %s -> %s", src, dst) if dry_run: return dst if not isfile(src): raise DistutilsFileError("can't move '%s': not a regular file" % src) if isdir(dst): dst = os.path.join(dst, basename(src)) elif exists(dst): raise DistutilsFileError( "can't move '%s': destination '%s' already exists" % (src, dst)) if not isdir(dirname(dst)): raise DistutilsFileError( "can't move '%s': destination '%s' not a valid path" % (src, dst)) copy_it = False try: os.rename(src, dst) except OSError as e: (num, msg) = e.args if num == errno.EXDEV: copy_it = True else: raise DistutilsFileError( "couldn't move '%s' to '%s': %s" % (src, dst, msg)) if copy_it: copy_file(src, dst, verbose=verbose) try: os.unlink(src) except OSError as e: (num, msg) = e.args try: os.unlink(dst) except OSError: pass raise DistutilsFileError( "couldn't move '%s' to '%s' by copy/delete: " "delete '%s' failed: %s" % (src, dst, src, msg)) return dst
def run(self): f = None self.ensure_filename('manifest') try: try: if not self.manifest: raise DistutilsFileError( "Pass manifest with --manifest=file") f = open(self.manifest) files = [file.strip() for file in f] except IOError, e: raise DistutilsFileError("unable to open install manifest: %s", str(e)) finally: if f: f.close() for file in files: if os.path.isfile(file) or os.path.islink(file): info("removing %s" % repr(file)) if not self.dry_run: try: os.unlink(file) except OSError, e: warn("could not delete: %s" % repr(file)) elif not os.path.isdir(file): info("skipping %s" % repr(file))
def path_to_zip(path): """ Returns (pathtozip, pathinzip). If path isn't in a zipfile pathtozip will be None """ from distutils.errors import DistutilsFileError if os.path.exists(path): return (None, path) else: rest = '' while not os.path.exists(path): path, r = os.path.split(path) rest = os.path.join(r, rest) if not os.path.isfile(path): # Directory really doesn't exist raise DistutilsFileError(path) try: zf = zipfile.ZipFile(path) except zipfile.BadZipfile: raise DistutilsFileError(path) if rest.endswith('/'): rest = rest[:-1] return path, rest
def safe_copy_tree(src, dst): """Recursively copies the 'src' directory tree to 'dst' Both 'src' and 'dst' must be directories. similar to distutil.dir_util.copy_tree except it doesn't overwite an existing file and doesn't copy any file starting with "." Returns a list of copied file paths. """ if not os.path.isdir(src): raise DistutilsFileError("Cannot copy tree {}: not a directory".format(src)) try: names = os.listdir(src) except OSError as e: raise DistutilsFileError("Error listing files in {}: {}".format(src, e.strerror)) mkpath(dst) outputs = [] for name in names: src_name = os.path.join(src, name) dst_name = os.path.join(dst, name) if name.startswith("."): logger.debug("Not copying {}".format(src_name)) continue if os.path.isdir(src_name): outputs.extend(safe_copy_tree(src_name, dst_name)) else: safe_copy_file(src_name, dst_name) outputs.append(dst_name) return outputs
def check_package(self, package, package_dir): # Empty dir name means current directory, which we can probably # assume exists. Also, os.path.exists and isdir don't know about # my "empty string means current dir" convention, so we have to # circumvent them. if package_dir != "": if not os.path.exists(package_dir): raise DistutilsFileError( "package directory '%s' does not exist" % package_dir) if not os.path.isdir(package_dir): raise DistutilsFileError( "supposed package directory '%s' exists, " "but is not a directory" % package_dir) # Require __init__.py for all but the "root package" if package: init_py = os.path.join(package_dir, "__init__.py") if os.path.isfile(init_py): return init_py else: log.warn(("package init file '%s' not found " + "(or not a regular file)"), init_py) # Either not in a package at all (__init__.py not expected), or # __init__.py doesn't exist -- so don't return the filename. return None
def path_to_zip(path): """ Returns (pathtozip, pathinzip). If path isn't in a zipfile pathtozip will be None """ warnings.warn("Don't use this function", DeprecationWarning) zf = zipfile.ZipFile(path_to_zip) orig_path = path from distutils.errors import DistutilsFileError if os.path.exists(path): return (None, path) else: rest = '' while not os.path.exists(path): path, r = os.path.split(path) if not path: raise DistutilsFileError("File doesn't exist: %s" % (orig_path, )) rest = os.path.join(r, rest) if not os.path.isfile(path): # Directory really doesn't exist raise DistutilsFileError("File doesn't exist: %s" % (orig_path, )) try: zf = zipfile.ZipFile(path) except zipfile.BadZipfile: raise DistutilsFileError("File doesn't exist: %s" % (orig_path, )) if rest.endswith('/'): rest = rest[:-1] return path, rest
def check_package(self, package, package_dir): # Empty dir name means current directory, which we can probably # assume exists. Also, os.path.exists and isdir don't know about # my "empty string means current dir" convention, so we have to # circumvent them. if package_dir != "": if not os.path.exists(package_dir): raise DistutilsFileError( "package directory '%s' does not exist" % package_dir ) if not os.path.isdir(package_dir): raise DistutilsFileError( "supposed package directory '%s' exists, " "but is not a directory" % package_dir ) # Directories without __init__.py are namespace packages (PEP 420). if package: init_py = os.path.join(package_dir, "__init__.py") if os.path.isfile(init_py): return init_py # Either not in a package at all (__init__.py not expected), or # __init__.py doesn't exist -- so don't return the filename. return None
def _copy_file_contents(src, dst, buffer_size=16 * 1024): """Copy the file 'src' to 'dst'. Both must be filenames. Any error opening either file, reading from 'src', or writing to 'dst', raises DistutilsFileError. Data is read/written in chunks of 'buffer_size' bytes (default 16k). No attempt is made to handle anything apart from regular files. """ # Stolen from shutil module in the standard library, but with # custom error-handling added. fsrc = None fdst = None try: try: fsrc = open(src, 'rb') except os.error as xxx_todo_changeme3: (errno, errstr) = xxx_todo_changeme3.args raise DistutilsFileError("could not open '%s': %s" % (src, errstr)) if os.path.exists(dst): try: os.unlink(dst) except os.error as xxx_todo_changeme: (errno, errstr) = xxx_todo_changeme.args raise DistutilsFileError("could not delete '%s': %s" % (dst, errstr)) try: fdst = open(dst, 'wb') except os.error as xxx_todo_changeme4: (errno, errstr) = xxx_todo_changeme4.args raise DistutilsFileError("could not create '%s': %s" % (dst, errstr)) while 1: try: buf = fsrc.read(buffer_size) except os.error as xxx_todo_changeme1: (errno, errstr) = xxx_todo_changeme1.args raise DistutilsFileError("could not read from '%s': %s" % (src, errstr)) if not buf: break try: fdst.write(buf) except os.error as xxx_todo_changeme2: (errno, errstr) = xxx_todo_changeme2.args raise DistutilsFileError("could not write to '%s': %s" % (dst, errstr)) finally: if fdst: fdst.close() if fsrc: fsrc.close()
def copy_tree(self, infile, outfile, preserve_mode=1, preserve_times=1, preserve_symlinks=0, level=1): """Copy the build directory tree, respecting dry-run and force flags. Special treatment of libtool files. """ if not self.dry_run and not os.path.isdir(infile): raise DistutilsFileError( "cannot copy tree '%s': not a directory" % infile) try: names = os.listdir(infile) except OSError as e: if self.dry_run: names = [] else: raise DistutilsFileError( "error listing files in '%s': %s" % (infile, e.strerror)) if not self.dry_run: mkpath(outfile) outputs = [] for n in names: src_name = os.path.join(infile, n) dst_name = os.path.join(outfile, n) if n.startswith('.nfs'): # skip NFS rename files continue if n in ('.libs', '_libs'): # skip libtool directories continue if preserve_symlinks and os.path.islink(src_name): link_dest = os.readlink(src_name) log.info("linking %s -> %s", dst_name, link_dest) if not self.dry_run: os.symlink(link_dest, dst_name) outputs.append(dst_name) elif os.path.isdir(src_name): outputs.extend( self.copy_tree(src_name, dst_name, preserve_mode, preserve_times, preserve_symlinks)) elif n.endswith('.la'): spawn(self.cmd_libtool_install + [ src_name, dst_name ], dry_run=self.dry_run) else: copy_file(src_name, dst_name, preserve_mode, preserve_times, not self.force, dry_run=self.dry_run) outputs.append(dst_name) return outputs
def _copy_file_contents(src, dst, buffer_size=16 * 1024): """Copy the file 'src' to 'dst'; both must be filenames. Any error opening either file, reading from 'src', or writing to 'dst', raises DistutilsFileError. Data is read/written in chunks of 'buffer_size' bytes (default 16k). No attempt is made to handle anything apart from regular files. """ # Stolen from shutil module in the standard library, but with # custom error-handling added. fsrc = None fdst = None try: try: # fsrc = open(src, 'rb') fsrc = open(src, buffering=0, mode='rb') # TODO: Truffle revert-me except OSError as e: raise DistutilsFileError("could not open '%s': %s" % (src, e.strerror)) if os.path.exists(dst): try: os.unlink(dst) except OSError as e: raise DistutilsFileError("could not delete '%s': %s" % (dst, e.strerror)) try: # fdst = open(dst, 'wb') fdst = open(dst, buffering=0, mode='wb') # TODO: Truffle revert-me except OSError as e: raise DistutilsFileError("could not create '%s': %s" % (dst, e.strerror)) while True: try: buf = fsrc.read(buffer_size) except OSError as e: raise DistutilsFileError("could not read from '%s': %s" % (src, e.strerror)) if not buf: break try: fdst.write(buf) except OSError as e: raise DistutilsFileError("could not write to '%s': %s" % (dst, e.strerror)) finally: if fdst: fdst.close() if fsrc: fsrc.close()
def check_package(self, package, package_dir): if package_dir != '': if not os.path.exists(package_dir): raise DistutilsFileError("package directory '%s' does not exist" % package_dir) if not os.path.isdir(package_dir): raise DistutilsFileError("supposed package directory '%s' exists, but is not a directory" % package_dir) if package: init_py = os.path.join(package_dir, '__init__.py') if os.path.isfile(init_py): return init_py log.warn("package init file '%s' not found " + '(or not a regular file)', init_py) return None
def copy_tree(src, dst, preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=0, verbose=0, dry_run=0, condition=None): """ Copy an entire directory tree 'src' to a new location 'dst'. Both 'src' and 'dst' must be directory names. If 'src' is not a directory, raise DistutilsFileError. If 'dst' does not exist, it is created with 'mkpath()'. The end result of the copy is that every file in 'src' is copied to 'dst', and directories under 'src' are recursively copied to 'dst'. Return the list of files that were copied or might have been copied, using their output name. The return value is unaffected by 'update' or 'dry_run': it is simply the list of all files under 'src', with the names changed to be under 'dst'. 'preserve_mode' and 'preserve_times' are the same as for 'copy_file'; note that they only apply to regular files, not to directories. If 'preserve_symlinks' is true, symlinks will be copied as symlinks (on platforms that support them!); otherwise (the default), the destination of the symlink will be copied. 'update' and 'verbose' are the same as for 'copy_file'. """ from distutils.dir_util import mkpath from distutils.file_util import copy_file from distutils.dep_util import newer from distutils.errors import DistutilsFileError from distutils import log src = fsencoding(src) dst = fsencoding(dst) if condition is None: condition = skipjunk if not dry_run and not os.path.isdir(src): raise DistutilsFileError("cannot copy tree '%s': not a directory" % src) try: names = os.listdir(src) except os.error, (errno, errstr): if dry_run: names = [] else: raise DistutilsFileError("error listing files in '%s': %s" % (src, errstr))
def _copy_file_contents(src, dst, buffer_size=16384): fsrc = None fdst = None try: try: fsrc = open(src, 'rb') except os.error as (errno, errstr): raise DistutilsFileError("could not open '%s': %s" % (src, errstr)) if os.path.exists(dst): try: os.unlink(dst) except os.error as (errno, errstr): raise DistutilsFileError("could not delete '%s': %s" % (dst, errstr))
def read_configuration(filepath, find_others=False): """Read given configuration file and returns options from it as a dict. :param str|unicode filepath: Path to configuration file to get options from. :param bool find_others: Whether to search for other configuration files which could be on in various places. :rtype: dict """ from setuptools.dist import Distribution, _Distribution filepath = os.path.abspath(filepath) if not os.path.isfile(filepath): raise DistutilsFileError('Configuration file %s does not exist.' % filepath) current_directory = os.getcwd() os.chdir(os.path.dirname(filepath)) dist = Distribution() filenames = dist.find_config_files() if find_others else [] if filepath not in filenames: filenames.append(filepath) _Distribution.parse_config_files(dist, filenames=filenames) handlers = parse_configuration(dist, dist.command_options) os.chdir(current_directory) return configuration_to_dict(handlers)
def safe_copy_file(src, dst): """Copy a file from 'src' to 'dst'. Similar to distutils.file_util.copy_file except if the file exists in 'dst' it's not clobbered or overwritten. returns a tupple (src, val) file not copied if val = 0 else 1 """ if not os.path.isfile(src): raise DistutilsFileError( "Can't copy {}: doesn't exist or is not a regular file".format( src)) if os.path.isdir(dst): dir = dst dst = os.path.join(dst, os.path.basename(src)) else: dir = os.path.dirname(dst) if os.path.isfile(dst): logger.warning("Not updating {} (file already exists)".format(dst)) return (dst, 0) _copy_file_contents(src, dst) logger.debug("Copied {} to {}".format(src, dir)) return (dst, 1)
def cythonize_extension(self, ext): if not HAVE_CYTHON: if not ext.output_file.is_file(): raise DistutilsFileError( "expected cythonized file %r not found" % str(ext.output_file)) else: return is_newer = self.check_newer(ext.cython_source, ext.output_file) if not self.force and not is_newer: return self.announce("cythonizing %r extension" % ext.name, 2) try: ext.output_dir.mkdir(parents=True) except FileExistsError: pass options = CompilationOptions( defaults=None, output_file=str(ext.output_file), language_level=ext.language_level, cplus=ext.cplus, annotate=ext.annotate, ) source = str(ext.cython_source) output_file = str(ext.output_file) self.announce("cythonizing %r -> %r" % (source, output_file), 2) result = compile_single(source, options, ext.name)
def _apply(dist: "Distribution", filepath: _Path, other_files: Iterable[_Path] = (), ignore_option_errors: bool = False) -> Tuple["ConfigHandler", ...]: """Read configuration from ``filepath`` and applies to the ``dist`` object.""" from setuptools.dist import _Distribution filepath = os.path.abspath(filepath) if not os.path.isfile(filepath): raise DistutilsFileError('Configuration file %s does not exist.' % filepath) current_directory = os.getcwd() os.chdir(os.path.dirname(filepath)) filenames = [*other_files, filepath] try: _Distribution.parse_config_files(dist, filenames=filenames) handlers = parse_configuration( dist, dist.command_options, ignore_option_errors=ignore_option_errors) dist._finalize_license_files() finally: os.chdir(current_directory) return handlers
def run_build(self): if self.with_nvvl: if self.system_nvvl: raise DistutilsArgError( "system-nvvl and with-nvvl are mutually exclusive") libpath = os.path.join(self.with_nvvl, "libnvvl.so") if not os.path.isfile(libpath): raise DistutilsFileError("Provided with-nvvl path, but " + libpath + " doesn't exit.") for ext in self.extensions: ext.library_dirs += [self.with_nvvl] self.distribution.data_files = [('nvvl/lib', [libpath])] elif not self.system_nvvl: output_dir = os.path.join(self.build_temp, "nvvl-build") mkpath(output_dir, 0o777, dry_run=self.dry_run) cmake_cmd = ["cmake", "-B" + output_dir, "-H" + nvvl_path] spawn(cmake_cmd, dry_run=self.dry_run) make_cmd = ["make", "-C", output_dir, "-j4"] spawn(make_cmd, dry_run=self.dry_run) for ext in self.extensions: ext.library_dirs += [output_dir] ext.runtime_library_dirs = ["$ORIGIN"] self.distribution.data_files = [ ('nvvl/lib', [os.path.join(output_dir, "libnvvl.so")]) ] build_ext_orig.run(self)
def llvm_config(*args): from distutils.errors import DistutilsFileError try: print "Running `{0}`".format(" ".join([LLVM_CONFIG] + list(args))) p = Popen([LLVM_CONFIG] + list(args), stdout=PIPE) return p.communicate()[0].strip().split() except OSError, e: raise DistutilsFileError("llvm-config failed: {0}".format(e))
def _write(cls, dst, *data): from distutils.errors import DistutilsFileError if os.path.exists(dst): try: os.unlink(dst) except OSError as e: raise DistutilsFileError( "could not delete '%s': %s" % (dst, e.strerror)) try: with codecs.open(dst, 'w', 'utf-8') as fd: for datum in data: fd.write(datum) except OSError as e: raise DistutilsFileError( "could not write to '%s': %s" % (dst, e.strerror))
def _read_and_stat(cls, src): from distutils.errors import DistutilsFileError try: with codecs.open(src, 'r', 'utf-8') as fd: return fd.read(), os.fstat(fd.fileno()) except OSError as e: raise DistutilsFileError( "could not read from '%s': %s" % (src, e.strerror))
def run(self): f = None self.ensure_filename('manifest') try: try: if not self.manifest: raise DistutilsFileError( "Pass manifest with --manifest=file") f = open(self.manifest) files = [file.strip() for file in f] except IOError as e: raise DistutilsFileError("unable to open install manifest: %s", str(e)) finally: if f: f.close() for file in files: if os.path.isfile(file) or os.path.islink(file): info("removing %s" % repr(file)) if not self.dry_run: try: os.unlink(file) except OSError as e: warn("could not delete: %s" % repr(file)) elif not os.path.isdir(file): info("skipping %s" % repr(file)) dirs = set() for file in reversed(sorted(files)): dir = os.path.dirname(file) if dir not in dirs and os.path.isdir(dir) and len( os.listdir(dir)) == 0: dirs.add(dir) # Only nuke empty Python library directories, else we could destroy # e.g. locale directories we're the only app with a .mo installed for. if dir.find("site-packages/") > 0: info("removing %s" % repr(dir)) if not self.dry_run: try: os.rmdir(dir) except OSError as e: warn("could not remove directory: %s" % str(e)) else: info("skipping empty directory %s" % repr(dir))
def run(self): if not self.dry_run: ui_dir = join(self.build_lib, 'chaosplt_dashboard/ui') if not isdir(UI_ASSETS_DIR): raise DistutilsFileError( "Make sure you build the UI assets before creating this package" ) self.copy_tree(UI_ASSETS_DIR, ui_dir) setuptools.command.build_py.build_py.run(self)
def parse(self, pofile): ID = 1 STR = 2 try: lines = open(pofile).readlines() except IOError, (errno, errstr): raise DistutilsFileError("could not read from '%s': %s" % ( pofile, errstr))
def run(self) -> None: """Remove Cython C++ outputs on clean command.""" for cpp in [src('palace.cpp')]: log.info(f'removing {cpp!r}') try: unlink(cpp) except OSError as e: raise DistutilsFileError( f'could not delete {cpp!r}: {e.strerror}') super().run()
def move_file (src, dst, verbose=0, dry_run=0): """Move a file 'src' to 'dst'. If 'dst' is a directory, the file will be moved into it with the same name; otherwise, 'src' is just renamed to 'dst'. Return the new full name of the file. Handles cross-device moves on Unix using 'copy_file()'. What about other systems??? """ from os.path import exists, isfile, isdir, basename, dirname import errno if verbose: print "moving %s -> %s" % (src, dst) if dry_run: return dst if not isfile(src): raise DistutilsFileError("can't move '%s': not a regular file" % src) if isdir(dst): dst = os.path.join(dst, basename(src)) elif exists(dst): raise DistutilsFileError("can't move '%s': destination '%s' already " "exists" % (src, dst)) if not isdir(dirname(dst)): raise DistutilsFileError("can't move '%s': destination '%s' not a " "valid path" % (src, dst)) copy_it = 0 try: os.rename(src, dst) except os.error, (num, msg): if num == errno.EXDEV: copy_it = 1 else: raise DistutilsFileError("couldn't move '%s' to '%s': %s" % (src, dst, msg))
def run(self): prefixes = [] for attr in [ attr for attr in vars(self) if attr.startswith('install_') ]: prefixes.append(getattr(self, attr)) print "prefixes = ", prefixes def clean_tree(_dir): find_prefix = False for prefix in prefixes: #print "== ", prefix, ".find(",_dir,") = ",prefix.find(_dir) if prefix.find(_dir) != -1: find_prefix = True return prefix #print "find_prefix =",prefix if find_prefix: return try: if not self.dry_run: os.rmdir(_dir) log.info("remove dir {}".format(_dir)) except OSError as err: if err.errno == os.errno.ENOTEMPTY: return else: self.warn(err) return clean_tree(os.path.dirname(_dir)) try: with open(self.distribution.uninstall_files) as record_file: for path in record_file: path = os.path.normpath(path.strip()) try: if not self.dry_run: os.unlink(path) log.info("remove file {}".format(path)) except Exception as err: pass _dir = os.path.dirname(path) clean_tree(_dir) except IOError as err: msg = "Cannot unistall integron.\n" if err.errno == os.errno.ENOENT: msg += "Cannot access \"{}\": No such file".format( self.distribution.uninstall_files) elif err.errno == os.errno.EACCES: msg += "Cannot access \"{}\": Permission denied".format( self.distribution.uninstall_files) else: msg += str(err) raise DistutilsFileError(msg)
def run(self): if not self.extensions: return version = get_rust_version() if version not in MIN_VERSION: print('Rust version mismatch: required rust%s got rust%s' % (MIN_VERSION, version)) return # Make sure that if pythonXX-sys is used, it builds against the current # executing python interpreter. bindir = os.path.dirname(sys.executable) env = os.environ.copy() env.update({ # disables rust's pkg-config seeking for specified packages, # which causes pythonXX-sys to fall back to detecting the # interpreter from the path. "PYTHON_2.7_NO_PKG_CONFIG": "1", "PATH": bindir + os.pathsep + os.environ.get("PATH", "") }) for ext in self.extensions: if not os.path.exists(ext.path): raise DistutilsFileError( "Can not file rust extension project file: %s" % ext.path) features = set(ext.features) features.update( cpython_feature(ext=False, pyo3=ext.pyo3, no_binding=ext.no_binding)) # build cargo command args = ([ "cargo", "test", '--color', 'always', "--manifest-path", ext.path, "--features", " ".join(features) ] + list(ext.args or [])) # Execute cargo command print(' '.join(args)) try: subprocess.check_output(args) except subprocess.CalledProcessError as e: raise CompileError("cargo failed with code: %d\n%s" % (e.returncode, e.output.decode("utf-8"))) except OSError: raise DistutilsExecError( "Unable to execute 'cargo' - this package " "requires rust to be installed and " "cargo to be on the PATH") else: print("Test has been completed for '%s' extension" % ext.name)
def copy_to(self, file_or_dir, dst_dir): if os.path.isdir(file_or_dir): return self.copy_tree( file_or_dir, os.path.join(dst_dir, os.path.basename(file_or_dir))), 1 elif os.path.isfile(file_or_dir): return self.copy_file(file_or_dir, dst_dir) else: raise DistutilsFileError( "can't copy '%s': doesn't exist or not a regular file or a directory" % file_or_dir)
def finalize_options(self): self.parser = SafeConfigParser() if not os.path.exists(self.distribution.uninstall_prefix): raise DistutilsFileError("Cannot unistall integron_finder.\n{}: No such file".format(self.distribution.uninstall_prefix)) used_files = self.parser.read(self.distribution.uninstall_prefix) for attr in [attr for attr in vars(self) if attr.startswith('install_')]: try: value = self.parser.get('install', attr) except(NoSectionError, NoOptionError): continue setattr(self, attr, value)