def translate_markers(pipfile_entry): """Take a pipfile entry and normalize its markers Provide a pipfile entry which may have 'markers' as a key or it may have any valid key from `packaging.markers.marker_context.keys()` and standardize the format into {'markers': 'key == "some_value"'}. :param pipfile_entry: A dictionariy of keys and values representing a pipfile entry :type pipfile_entry: dict :returns: A normalized dictionary with cleaned marker entries """ if not isinstance(pipfile_entry, Mapping): raise TypeError("Entry is not a pipfile formatted mapping.") from notpip._vendor.distlib.markers import DEFAULT_CONTEXT as marker_context allowed_marker_keys = ["markers"] + [k for k in marker_context.keys()] provided_keys = list(pipfile_entry.keys()) if hasattr(pipfile_entry, "keys") else [] pipfile_marker = next((k for k in provided_keys if k in allowed_marker_keys), None) new_pipfile = dict(pipfile_entry).copy() if pipfile_marker: entry = "{0}".format(pipfile_entry[pipfile_marker]) if pipfile_marker != "markers": entry = "{0} {1}".format(pipfile_marker, entry) new_pipfile.pop(pipfile_marker) new_pipfile["markers"] = entry return new_pipfile
def translate_markers(pipfile_entry): """Take a pipfile entry and normalize its markers Provide a pipfile entry which may have 'markers' as a key or it may have any valid key from `packaging.markers.marker_context.keys()` and standardize the format into {'markers': 'key == "some_value"'}. :param pipfile_entry: A dictionariy of keys and values representing a pipfile entry :type pipfile_entry: dict :returns: A normalized dictionary with cleaned marker entries """ if not isinstance(pipfile_entry, Mapping): raise TypeError("Entry is not a pipfile formatted mapping.") from notpip._vendor.distlib.markers import DEFAULT_CONTEXT as marker_context from .vendor.packaging.markers import Marker from .vendor.vistir.misc import dedup allowed_marker_keys = ["markers"] + [k for k in marker_context.keys()] provided_keys = list(pipfile_entry.keys()) if hasattr(pipfile_entry, "keys") else [] pipfile_markers = [k for k in provided_keys if k in allowed_marker_keys] new_pipfile = dict(pipfile_entry).copy() marker_set = set() if "markers" in new_pipfile: marker_set.add(str(Marker(new_pipfile.get("markers")))) for m in pipfile_markers: entry = "{0}".format(pipfile_entry[m]) if m != "markers": marker_set.add(str(Marker("{0}{1}".format(m, entry)))) new_pipfile.pop(m) if marker_set: new_pipfile["markers"] = str(Marker(" or ".join(["{0}".format(s) if " and " in s else s for s in sorted(dedup(marker_set))]))).replace('"', "'") return new_pipfile
def clean_resolved_dep(dep, is_top_level=False, pipfile_entry=None): from notpip._vendor.distlib.markers import DEFAULT_CONTEXT as marker_context allowed_marker_keys = ['markers'] + [k for k in marker_context.keys()] name = pep423_name(dep['name']) # We use this to determine if there are any markers on top level packages # So we can make sure those win out during resolution if the packages reoccur dep_keys = [k for k in getattr(pipfile_entry, 'keys', list)() ] if is_top_level else [] lockfile = { 'version': '=={0}'.format(dep['version']), } for key in ['hashes', 'index', 'extras']: if key in dep: lockfile[key] = dep[key] # In case we lock a uri or a file when the user supplied a path # remove the uri or file keys from the entry and keep the path if pipfile_entry and any(k in pipfile_entry for k in ['file', 'path']): fs_key = next((k for k in ['path', 'file'] if k in pipfile_entry), None) lockfile_key = next( (k for k in ['uri', 'file', 'path'] if k in lockfile), None) if fs_key != lockfile_key: try: del lockfile[lockfile_key] except KeyError: # pass when there is no lock file, usually because it's the first time pass lockfile[fs_key] = pipfile_entry[fs_key] # If a package is **PRESENT** in the pipfile but has no markers, make sure we # **NEVER** include markers in the lockfile if 'markers' in dep: # First, handle the case where there is no top level dependency in the pipfile if not is_top_level: lockfile['markers'] = dep['markers'] # otherwise make sure we are prioritizing whatever the pipfile says about the markers # If the pipfile says nothing, then we should put nothing in the lockfile else: pipfile_marker = next( (k for k in dep_keys if k in allowed_marker_keys), None) if pipfile_marker: entry = "{0}".format(pipfile_entry[pipfile_marker]) if pipfile_marker != 'markers': entry = "{0} {1}".format(pipfile_marker, entry) lockfile['markers'] = entry return {name: lockfile}