def find_missing_requirements(self): """ Find the required packages that are not in the requirements.txt file. :return: set of missing packages. :rtype: set[Requirement] """ requirements = self._reduce_by_version(self._get_requirements_dict_from_py_files()) debug('requirements:') debug(pformat(requirements)) needed = [] needed.extend(sorted(compress_list(unique_list(requirements)))) filename = 'requirements.txt' if not os.path.exists(filename): debug("Missing: " + filename) diff = sorted(set(needed)) else: with open(filename) as in_file: existing_requirements = [] for line in [line.strip() for line in in_file.readlines()]: if line and not line.startswith('#'): existing_requirements.append(Requirement(line)) existing = sorted(compress_list(unique_list(existing_requirements))) difference = [req for req in needed if req not in existing] diff = sorted(set([req for req in difference if not req.markers or Requirement(req.package) not in needed])) debug("find_missing_requirements.needed: {pkgs}".format(pkgs=pformat(needed))) debug("find_missing_requirements.diff: {pkgs}".format(pkgs=pformat(diff))) return diff
def find_missing_requirements(self, lib_files=None): """ Find the required packages that are not in the requirements.txt file. :param lib_files: list of filenames to scan for requirement comments. Set to None to scan all py files in the herringlib directory and the herringfile. :type lib_files: None|list[str] :return: key is requirement filename and value is the set of missing packages. :rtype: dict[str,set[Requirement]] """ requirements_dict = self._get_requirements_dict_from_py_files(lib_files=lib_files) diff_dict = {} for requirement_filename in requirements_dict.keys(): requirements = self._reduce_by_version(requirements_dict[requirement_filename]) debug('requirements:') debug(pformat(requirements)) needed = [] needed.extend(sorted(compress_list(unique_list(requirements)))) if not os.path.exists(requirement_filename): debug("Missing: " + requirement_filename) diff_dict[requirement_filename] = sorted(set(needed)) else: with open(requirement_filename) as in_file: existing_requirements = [] for line in [line.strip() for line in in_file.readlines()]: if line and not line.startswith('#'): existing_requirements.append(Requirement(line)) existing = sorted(compress_list(unique_list(existing_requirements))) difference = [req for req in needed if req not in existing] diff_dict[requirement_filename] = sorted(set([req for req in difference if not req.markers or Requirement(req.package) not in needed])) debug("find_missing_requirements.needed: {pkgs}".format(pkgs=pformat(needed))) debug("find_missing_requirements.diff: {pkgs}".format(pkgs=pformat(diff_dict[requirement_filename]))) return diff_dict
def test_compress_list(): """ compress_list removes empty or None elements from a list """ # incompressible lists assert compress_list([]) == [] assert compress_list([1]) == [1] assert compress_list([1, 2]) == [1, 2] # remove None element assert compress_list([None]) == [] assert compress_list([None, 1]) == [1] assert compress_list([None, 1, 2]) == [1, 2] assert compress_list([1, None, 2]) == [1, 2] assert compress_list([1, 2, None]) == [1, 2] # remove empty strings assert compress_list(['']) == [] assert compress_list(['', 1]) == [1] assert compress_list(['', 1, 2]) == [1, 2] assert compress_list([1, '', 2]) == [1, 2] assert compress_list([1, 2, '']) == [1, 2] # remove empty lists assert compress_list([[]]) == [] assert compress_list([[], 1]) == [1] assert compress_list([[], 1, 2]) == [1, 2] assert compress_list([1, [], 2]) == [1, 2] assert compress_list([1, 2, []]) == [1, 2]