def get_deb_depends_from_setuptools_requires(requirements, on_failure="<STR_LIT>"):
assert on_failure in ("<STR_LIT>", "<STR_LIT>", "<STR_LIT>"), on_failure<EOL>import pkg_resources<EOL>depends = [] <EOL>parsed_reqs=[]<EOL>for extra,reqs in pkg_resources.split_sections(requirements):<EOL><INDENT>if extra: continue<EOL>parsed_reqs.extend(pkg_resources.parse_requirements(reqs))<EOL><DEDENT>if not parsed_reqs:<EOL><INDENT>return depends<EOL><DEDENT>if not os.path.exists('<STR_LIT>'):<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>egginfore=("<STR_LIT>"<EOL>% '<STR_LIT:|>'.join(req.project_name.replace('<STR_LIT:->', '<STR_LIT:_>') for req in parsed_reqs))<EOL>args = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", egginfore]<EOL>if <NUM_LIT:1>:<EOL><INDENT>dry_run_args = args[:] + ['<STR_LIT>','<STR_LIT>']<EOL>cmd = subprocess.Popen(dry_run_args,stderr=subprocess.PIPE)<EOL>returncode = cmd.wait()<EOL>if returncode:<EOL><INDENT>err_output = cmd.stderr.read()<EOL>raise RuntimeError('<STR_LIT>' +<EOL>err_output.strip())<EOL><DEDENT><DEDENT>try:<EOL><INDENT>cmd = subprocess.Popen(args, stdin=subprocess.PIPE,<EOL>stdout=subprocess.PIPE,<EOL>universal_newlines=True)<EOL><DEDENT>except Exception as le:<EOL><INDENT>log.error('<STR_LIT>', '<STR_LIT:U+0020>'.join(args))<EOL>raise RuntimeError('<STR_LIT>' % (le,args))<EOL><DEDENT>returncode = cmd.wait()<EOL>if returncode:<EOL><INDENT>log.error('<STR_LIT>', '<STR_LIT:U+0020>'.join(args))<EOL>raise RuntimeError('<STR_LIT>' % (returncode,<EOL>args))<EOL><DEDENT>inlines = cmd.stdout.readlines()<EOL>dd = {} <EOL>E=re.compile(egginfore, re.I)<EOL>D=re.compile("<STR_LIT>", re.I)<EOL>eggsndebs = set()<EOL>for l in inlines:<EOL><INDENT>if l:<EOL><INDENT>emo = E.search(l)<EOL>assert emo, l<EOL>dmo = D.search(l)<EOL>assert dmo, l<EOL>eggsndebs.add((emo.group(<NUM_LIT:1>), dmo.group(<NUM_LIT:1>)))<EOL><DEDENT><DEDENT>for (egginfo, debname) in eggsndebs:<EOL><INDENT>pydist = pkg_resources.Distribution.from_filename(egginfo)<EOL>try:<EOL><INDENT>dd.setdefault(<EOL>pydist.project_name.lower(), {}).setdefault(<EOL>pydist, set()).add(debname)<EOL><DEDENT>except ValueError as le:<EOL><INDENT>log.warn("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (egginfo, debname, le,))<EOL>pass<EOL><DEDENT><DEDENT>ops = {'<STR_LIT:<>':'<STR_LIT>','<STR_LIT:>>':'<STR_LIT>','<STR_LIT>':'<STR_LIT:=>','<STR_LIT>':'<STR_LIT>','<STR_LIT>':'<STR_LIT>'}<EOL>for req in parsed_reqs:<EOL><INDENT>reqname = req.project_name.lower()<EOL>gooddebs = set()<EOL>for pydist, debs in dd.get(reqname, {}).iteritems():<EOL><INDENT>if pydist in req:<EOL><INDENT>gooddebs |= (debs)<EOL><DEDENT>else:<EOL><INDENT>log.info("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>% ('<STR_LIT:U+002CU+0020>'.join(debs), req.project_name, req))<EOL><DEDENT><DEDENT>if not gooddebs:<EOL><INDENT>if on_failure == '<STR_LIT>':<EOL><INDENT>log.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"% (req.project_name, req.specs))<EOL><DEDENT>elif on_failure == "<STR_LIT>":<EOL><INDENT>raise CantSatisfyRequirement(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (req.project_name, req.specs), req)<EOL><DEDENT>elif on_failure == "<STR_LIT>":<EOL><INDENT>log.warn("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (req.project_name,<EOL>req.specs, reqname))<EOL>gooddebs.add("<STR_LIT>" + reqname)<EOL><DEDENT><DEDENT>elif len(gooddebs) == <NUM_LIT:1>:<EOL><INDENT>log.info("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>% (req.project_name, tuple(gooddebs)[<NUM_LIT:0>], req.specs))<EOL><DEDENT>else:<EOL><INDENT>log.warn("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>% (req.project_name, '<STR_LIT:U+002CU+0020>'.join(gooddebs),))<EOL><DEDENT>alts = []<EOL>for deb in gooddebs:<EOL><INDENT>added_any_alt = False<EOL>for spec in req.specs:<EOL><INDENT>alts.append("<STR_LIT>" % (deb, ops[spec[<NUM_LIT:0>]], spec[<NUM_LIT:1>]))<EOL>added_any_alt = True<EOL><DEDENT>if not added_any_alt:<EOL><INDENT>alts.append("<STR_LIT:%s>"%deb)<EOL><DEDENT><DEDENT>if len(alts):<EOL><INDENT>depends.append('<STR_LIT>'.join(alts))<EOL><DEDENT><DEDENT>return depends<EOL>
Suppose you can't confidently figure out a .deb which satisfies a given
requirement.  If on_failure == 'warn', then log a warning.  If on_failure
== 'raise' then raise CantSatisfyRequirement exception.  If on_failure ==
'guess' then guess that python-$FOO will satisfy the dependency and that
the Python version numbers will apply to the Debian packages (in addition
to logging a warning message).