Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def find_one_file(directory, pattern):
"""
Use :func:`find_files()` to find a file and make sure a single file is matched.
:param directory: The pathname of the directory to be searched (a string).
:param pattern: The filename pattern to match (a string).
:returns: The matched pathname (a string).
:raises: :exc:`~exceptions.AssertionError` when no file or more than one
file is matched.
"""
matches = list(find_files(directory, pattern))
if len(matches) == 1:
return matches[0]
elif matches:
msg = "More than one file matched %r pattern in directory %r! (matches: %s)"
raise Exception(msg % (pattern, directory, concatenate(matches)))
else:
msg = "Failed to find file matching %r pattern in directory %r! (available files: %s)"
raise Exception(msg % (pattern, directory, concatenate(find_files(directory, '*'))))
:param directory: The pathname of the directory to be searched (a string).
:param pattern: The filename pattern to match (a string).
:returns: The matched pathname (a string).
:raises: :exc:`~exceptions.AssertionError` when no file or more than one
file is matched.
"""
matches = list(find_files(directory, pattern))
if len(matches) == 1:
return matches[0]
elif matches:
msg = "More than one file matched %r pattern in directory %r! (matches: %s)"
raise Exception(msg % (pattern, directory, concatenate(matches)))
else:
msg = "Failed to find file matching %r pattern in directory %r! (available files: %s)"
raise Exception(msg % (pattern, directory, concatenate(find_files(directory, '*'))))
supported_command = parser.get('commands', 'supported')
logger.debug("Checking if configuration is supported: %s", supported_command)
with open(os.devnull, 'wb') as null_device:
if subprocess.call(supported_command, shell=True,
stdout=null_device,
stderr=subprocess.STDOUT) == 0:
logger.debug("System package manager configuration is supported!")
# Get the commands to list and install system packages.
self.list_command = parser.get('commands', 'list')
self.install_command = parser.get('commands', 'install')
# Get the known dependencies.
self.dependencies = dict((n.lower(), v.split()) for n, v
in parser.items('dependencies'))
logger.debug("Loaded dependencies of %s: %s",
pluralize(len(self.dependencies), "Python package"),
concatenate(sorted(self.dependencies)))
else:
logger.debug("Command failed, assuming configuration doesn't apply ..")
"""
Find pip metadata files in unpacked source distributions.
When pip unpacks a source distribution archive it creates a directory
``pip-egg-info`` which contains the package metadata in a declarative
and easy to parse format. This method finds such metadata files.
:param pattern: The :mod:`glob` pattern to search for (a string).
:returns: A list of matched filenames (strings).
"""
full_pattern = os.path.join(self.requirement.source_directory, 'pip-egg-info', '*.egg-info', pattern)
logger.debug("Looking for %r file(s) using pattern %r ..", pattern, full_pattern)
matches = glob.glob(full_pattern)
if len(matches) > 1:
msg = "Source distribution directory of %s (%s) contains multiple *.egg-info directories: %s"
raise Exception(msg % (self.requirement.project_name, self.requirement.version, concatenate(matches)))
elif matches:
logger.debug("Matched %s: %s.", pluralize(len(matches), "file", "files"), concatenate(matches))
return matches[0]
else:
logger.debug("No matching %r files found.", pattern)
def install():
# Make sure stdeb is not installed.
logger.info("Making sure python-stdeb is not installed (py2deb contains a fork of stdeb).")
execute('apt-get', 'purge', '--yes', 'python-stdeb', sudo=True, logger=logger)
# Install Debian package dependencies of py2deb and deb-pkg-tools.
dependencies = set(py2deb_dependencies) | set(deb_pkg_tools_dependencies)
logger.info("Installing prerequisites (%s) ..",
concatenate(sorted(dependencies)))
execute('apt-get', 'install', '--yes', *sorted(dependencies), sudo=True, logger=logger)
# Initialize the apt-file cache.
logger.info("Initializing apt-file cache ..")
execute('apt-file', 'update', sudo=True, logger=logger)
# Prepare a temporary directory tree to hold the generated packages.
directory = tempfile.mkdtemp()
try:
# Use py2deb to convert itself and its dependencies to Debian packages.
logger.info("Converting py2deb and dependencies to Debian packages ..")
convert(['py2deb'], repository=directory)
# Convert the directory of packages to a repository.
update_repository(directory)
# Make it possible to install packages from the repository.
activate_repository(directory)
try:
# Try to install the generated packages from the repository.
When pip unpacks a source distribution archive it creates a directory
``pip-egg-info`` which contains the package metadata in a declarative
and easy to parse format. This method finds such metadata files.
:param pattern: The :mod:`glob` pattern to search for (a string).
:returns: A list of matched filenames (strings).
"""
full_pattern = os.path.join(self.requirement.source_directory, 'pip-egg-info', '*.egg-info', pattern)
logger.debug("Looking for %r file(s) using pattern %r ..", pattern, full_pattern)
matches = glob.glob(full_pattern)
if len(matches) > 1:
msg = "Source distribution directory of %s (%s) contains multiple *.egg-info directories: %s"
raise Exception(msg % (self.requirement.project_name, self.requirement.version, concatenate(matches)))
elif matches:
logger.debug("Matched %s: %s.", pluralize(len(matches), "file", "files"), concatenate(matches))
return matches[0]
else:
logger.debug("No matching %r files found.", pattern)
def install_requirements(self, requirements, **kw):
"""
Manually install a requirement set from binary and/or wheel distributions.
:param requirements: A list of :class:`pip_accel.req.Requirement` objects.
:param kw: Any keyword arguments are passed on to
:func:`~pip_accel.bdist.BinaryDistributionManager.install_binary_dist()`.
:returns: The number of packages that were just installed (an integer).
"""
install_timer = Timer()
install_types = []
if any(not req.is_wheel for req in requirements):
install_types.append('binary')
if any(req.is_wheel for req in requirements):
install_types.append('wheel')
logger.info("Installing from %s distributions ..", concatenate(install_types))
# Track installed files by default (unless the caller specifically opted out).
kw.setdefault('track_installed_files', True)
num_installed = 0
for requirement in requirements:
# When installing setuptools we need to uninstall distribute,
# otherwise distribute will shadow setuptools and all sorts of
# strange issues can occur (e.g. upgrading to the latest
# setuptools to gain wheel support and then having everything
# blow up because distribute doesn't know about wheels).
if requirement.name == 'setuptools' and is_installed('distribute'):
uninstall('distribute')
if requirement.is_editable:
logger.debug("Installing %s in editable form using pip.", requirement)
with TransactionalUpdate(requirement):
command = InstallCommand()
opts, args = command.parse_args(['--no-deps', '--editable', requirement.source_directory])
def find_known_dependencies(self, requirement):
"""
Find the known dependencies of a Python package.
:param requirement: A :class:`.Requirement` object.
:returns: A list of strings with system package names.
"""
logger.info("Checking for known dependencies of %s ..", requirement.name)
known_dependencies = sorted(self.dependencies.get(requirement.name.lower(), []))
if known_dependencies:
logger.info("Found %s: %s", pluralize(len(known_dependencies), "known dependency", "known dependencies"),
concatenate(known_dependencies))
else:
logger.info("No known dependencies... Maybe you have a suggestion?")
return known_dependencies
def find_missing_dependencies(self, requirement):
"""
Find missing dependencies of a Python package.
:param requirement: A :class:`.Requirement` object.
:returns: A list of strings with system package names.
"""
known_dependencies = self.find_known_dependencies(requirement)
if known_dependencies:
installed_packages = self.find_installed_packages()
logger.debug("Checking for missing dependencies of %s ..", requirement.name)
missing_dependencies = sorted(set(known_dependencies).difference(installed_packages))
if missing_dependencies:
logger.debug("Found %s: %s",
pluralize(len(missing_dependencies), "missing dependency", "missing dependencies"),
concatenate(missing_dependencies))
else:
logger.info("All known dependencies are already installed.")
return missing_dependencies
# If the build reported an error we'll try to provide the user with
# some hints about what went wrong.
if build.returncode != 0:
raise BuildFailed("Failed to build {name} ({version}) binary distribution!",
name=requirement.name, version=requirement.version)
# Check if the build created the `dist' directory (the os.listdir()
# call below will raise an exception if we don't check for this).
if not os.path.isdir(dist_directory):
raise NoBuildOutput("Build of {name} ({version}) did not produce a binary distribution archive!",
name=requirement.name, version=requirement.version)
# Check if we can find the binary distribution archive.
filenames = os.listdir(dist_directory)
if len(filenames) != 1:
variables = dict(name=requirement.name,
version=requirement.version,
filenames=concatenate(sorted(filenames)))
raise NoBuildOutput("""
Build of {name} ({version}) produced more than one
distribution archive! (matches: {filenames})
""", **variables)
except Exception as e:
# Decorate the exception with the output of the failed build.
with open(temporary_file) as handle:
build_output = handle.read()
enhanced_message = compact("""
{message}
Please check the build output because it will probably
provide a hint about what went wrong.
Build output: