prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  "pip freeze -r" returns ValueError() if requirements file contains --find-links
  It seems that pip freeze should not pass --find-links or --index-url to distribute/setuptools when inlined in a requirements file. Here is an easy way to repro the problem:
  
  1) echo "--find-links http://foo.bar/" > /tmp/req.txt
  2) pip freeze -r /tmp/req.txt
  
  """
  Exception:
  Traceback (most recent call last):
    File "/Users/hpfennig/work/pip/pip/basecommand.py", line 95, in main
      self.run(options, args)
    File "/Users/hpfennig/work/pip/pip/commands/freeze.py", line 92, in run
      line_req = InstallRequirement.from_line(line)
    File "/Users/hpfennig/work/pip/pip/req.py", line 105, in from_line
      return cls(req, comes_from, url=url)
    File "/Users/hpfennig/work/pip/pip/req.py", line 39, in **init**
      req = pkg_resources.Requirement.parse(req)
    File "/Users/hpfennig/.virtualenvs/pip-bug-virt-env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2510, in parse
      reqs = list(parse_requirements(s))
    File "/Users/hpfennig/.virtualenvs/pip-bug-virt-env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2436, in parse_requirements
      line, p, specs = scan_list(VERSION,LINE_END,line,p,(1,2),"version spec")
    File "/Users/hpfennig/.virtualenvs/pip-bug-virt-env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2404, in scan_list
      raise ValueError("Expected "+item_name+" in",line,"at",line[p:])
  ValueError: ('Expected version spec in', '--find-links http://foo.bar/', 'at', ' http://foo.bar/')
  
  Storing complete log in /Users/hpfennig/.pip/pip.log
  """
  
  I have a fix, but I am not sure this is the correct way to go about it. Would be happy to do an official pull request if needed.
  
  diff --git a/pip/commands/freeze.py b/pip/commands/freeze.py
  index 01b5df9..03ac80f 100644
  --- a/pip/commands/freeze.py
  +++ b/pip/commands/freeze.py
  @@ -85,7 +85,9 @@ class FreezeCommand(Command):
                   elif (line.startswith('-r') or line.startswith('--requirement')
                         or line.startswith('-Z') or line.startswith('--always-unzip')
                         or line.startswith('-f') or line.startswith('-i')
  -                      or line.startswith('--extra-index-url')):
  -                      or line.startswith('--extra-index-url')
  -                      or line.startswith('--find-links')
  -                      or line.startswith('--index-url')):
                     f.write(line)
                     continue
                 else:
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `pip/commands/freeze.py`
  Content:
  ```
  1 import re
  2 import sys
  3 import pkg_resources
  4 import pip
  5 from pip.req import InstallRequirement
  6 from pip.log import logger
  7 from pip.basecommand import Command
  8 from pip.util import get_installed_distributions
  9 
  10 
  11 class FreezeCommand(Command):
  12     name = 'freeze'
  13     usage = '%prog [OPTIONS]'
  14     summary = 'Output all currently installed packages (exact versions) to stdout'
  15 
  16     def __init__(self):
  17         super(FreezeCommand, self).__init__()
  18         self.parser.add_option(
  19             '-r', '--requirement',
  20             dest='requirement',
  21             action='store',
  22             default=None,
  23             metavar='FILENAME',
  24             help='Use the given requirements file as a hint about how to generate the new frozen requirements')
  25         self.parser.add_option(
  26             '-f', '--find-links',
  27             dest='find_links',
  28             action='append',
  29             default=[],
  30             metavar='URL',
  31             help='URL for finding packages, which will be added to the frozen requirements file')
  32         self.parser.add_option(
  33             '-l', '--local',
  34             dest='local',
  35             action='store_true',
  36             default=False,
  37             help='If in a virtualenv, do not report globally-installed packages')
  38 
  39     def setup_logging(self):
  40         logger.move_stdout_to_stderr()
  41 
  42     def run(self, options, args):
  43         requirement = options.requirement
  44         find_links = options.find_links or []
  45         local_only = options.local
  46         ## FIXME: Obviously this should be settable:
  47         find_tags = False
  48         skip_match = None
  49 
  50         skip_regex = options.skip_requirements_regex
  51         if skip_regex:
  52             skip_match = re.compile(skip_regex)
  53 
  54         dependency_links = []
  55 
  56         f = sys.stdout
  57 
  58         for dist in pkg_resources.working_set:
  59             if dist.has_metadata('dependency_links.txt'):
  60                 dependency_links.extend(dist.get_metadata_lines('dependency_links.txt'))
  61         for link in find_links:
  62             if '#egg=' in link:
  63                 dependency_links.append(link)
  64         for link in find_links:
  65             f.write('-f %s\n' % link)
  66         installations = {}
  67         for dist in get_installed_distributions(local_only=local_only):
  68             req = pip.FrozenRequirement.from_dist(dist, dependency_links, find_tags=find_tags)
  69             installations[req.name] = req
  70         if requirement:
  71             req_f = open(requirement)
  72             for line in req_f:
  73                 if not line.strip() or line.strip().startswith('#'):
  74                     f.write(line)
  75                     continue
  76                 if skip_match and skip_match.search(line):
  77                     f.write(line)
  78                     continue
  79                 elif line.startswith('-e') or line.startswith('--editable'):
  80                     if line.startswith('-e'):
  81                         line = line[2:].strip()
  82                     else:
  83                         line = line[len('--editable'):].strip().lstrip('=')
  84                     line_req = InstallRequirement.from_editable(line, default_vcs=options.default_vcs)
  85                 elif (line.startswith('-r') or line.startswith('--requirement')
  86                       or line.startswith('-Z') or line.startswith('--always-unzip')
  87                       or line.startswith('-f') or line.startswith('-i')
  88                       or line.startswith('--extra-index-url')):
  89                     f.write(line)
  90                     continue
  91                 else:
  92                     line_req = InstallRequirement.from_line(line)
  93                 if not line_req.name:
  94                     logger.notify("Skipping line because it's not clear what it would install: %s"
  95                                   % line.strip())
  96                     logger.notify("  (add #egg=PackageName to the URL to avoid this warning)")
  97                     continue
  98                 if line_req.name not in installations:
  99                     logger.warn("Requirement file contains %s, but that package is not installed"
  100                                 % line.strip())
  101                     continue
  102                 f.write(str(installations[line_req.name]))
  103                 del installations[line_req.name]
  104             f.write('## The following requirements were added by pip --freeze:\n')
  105         for installation in sorted(installations.values(), key=lambda x: x.name):
  106             f.write(str(installation))
  107 
  108 
  109 FreezeCommand()
  110
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/pip/commands/freeze.py b/pip/commands/freeze.py
  --- a/pip/commands/freeze.py
  +++ b/pip/commands/freeze.py
  @@ -85,7 +85,9 @@
                   elif (line.startswith('-r') or line.startswith('--requirement')
                         or line.startswith('-Z') or line.startswith('--always-unzip')
                         or line.startswith('-f') or line.startswith('-i')
  -                      or line.startswith('--extra-index-url')):
  +                      or line.startswith('--extra-index-url')
  +                      or line.startswith('--find-links')
  +                      or line.startswith('--index-url')):
                       f.write(line)
                       continue
                   else:
  
verification_info: {"golden_diff": "diff --git a/pip/commands/freeze.py b/pip/commands/freeze.py\n--- a/pip/commands/freeze.py\n+++ b/pip/commands/freeze.py\n@@ -85,7 +85,9 @@\n                 elif (line.startswith('-r') or line.startswith('--requirement')\n                       or line.startswith('-Z') or line.startswith('--always-unzip')\n                       or line.startswith('-f') or line.startswith('-i')\n-                      or line.startswith('--extra-index-url')):\n+                      or line.startswith('--extra-index-url')\n+                      or line.startswith('--find-links')\n+                      or line.startswith('--index-url')):\n                     f.write(line)\n                     continue\n                 else:\n", "issue": "\"pip freeze -r\" returns ValueError() if requirements file contains --find-links\nIt seems that pip freeze should not pass --find-links or --index-url to distribute/setuptools when inlined in a requirements file. Here is an easy way to repro the problem:\n\n1) echo \"--find-links http://foo.bar/\" > /tmp/req.txt\n2) pip freeze -r /tmp/req.txt\n\n\"\"\"\nException:\nTraceback (most recent call last):\n  File \"/Users/hpfennig/work/pip/pip/basecommand.py\", line 95, in main\n    self.run(options, args)\n  File \"/Users/hpfennig/work/pip/pip/commands/freeze.py\", line 92, in run\n    line_req = InstallRequirement.from_line(line)\n  File \"/Users/hpfennig/work/pip/pip/req.py\", line 105, in from_line\n    return cls(req, comes_from, url=url)\n  File \"/Users/hpfennig/work/pip/pip/req.py\", line 39, in **init**\n    req = pkg_resources.Requirement.parse(req)\n  File \"/Users/hpfennig/.virtualenvs/pip-bug-virt-env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py\", line 2510, in parse\n    reqs = list(parse_requirements(s))\n  File \"/Users/hpfennig/.virtualenvs/pip-bug-virt-env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py\", line 2436, in parse_requirements\n    line, p, specs = scan_list(VERSION,LINE_END,line,p,(1,2),\"version spec\")\n  File \"/Users/hpfennig/.virtualenvs/pip-bug-virt-env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py\", line 2404, in scan_list\n    raise ValueError(\"Expected \"+item_name+\" in\",line,\"at\",line[p:])\nValueError: ('Expected version spec in', '--find-links http://foo.bar/', 'at', ' http://foo.bar/')\n\nStoring complete log in /Users/hpfennig/.pip/pip.log\n\"\"\"\n\nI have a fix, but I am not sure this is the correct way to go about it. Would be happy to do an official pull request if needed.\n\ndiff --git a/pip/commands/freeze.py b/pip/commands/freeze.py\nindex 01b5df9..03ac80f 100644\n--- a/pip/commands/freeze.py\n+++ b/pip/commands/freeze.py\n@@ -85,7 +85,9 @@ class FreezeCommand(Command):\n                 elif (line.startswith('-r') or line.startswith('--requirement')\n                       or line.startswith('-Z') or line.startswith('--always-unzip')\n                       or line.startswith('-f') or line.startswith('-i')\n-                      or line.startswith('--extra-index-url')):\n-                      or line.startswith('--extra-index-url')\n-                      or line.startswith('--find-links')\n-                      or line.startswith('--index-url')):\n                   f.write(line)\n                   continue\n               else:\n\n", "before_files": [{"content": "import re\nimport sys\nimport pkg_resources\nimport pip\nfrom pip.req import InstallRequirement\nfrom pip.log import logger\nfrom pip.basecommand import Command\nfrom pip.util import get_installed_distributions\n\n\nclass FreezeCommand(Command):\n    name = 'freeze'\n    usage = '%prog [OPTIONS]'\n    summary = 'Output all currently installed packages (exact versions) to stdout'\n\n    def __init__(self):\n        super(FreezeCommand, self).__init__()\n        self.parser.add_option(\n            '-r', '--requirement',\n            dest='requirement',\n            action='store',\n            default=None,\n            metavar='FILENAME',\n            help='Use the given requirements file as a hint about how to generate the new frozen requirements')\n        self.parser.add_option(\n            '-f', '--find-links',\n            dest='find_links',\n            action='append',\n            default=[],\n            metavar='URL',\n            help='URL for finding packages, which will be added to the frozen requirements file')\n        self.parser.add_option(\n            '-l', '--local',\n            dest='local',\n            action='store_true',\n            default=False,\n            help='If in a virtualenv, do not report globally-installed packages')\n\n    def setup_logging(self):\n        logger.move_stdout_to_stderr()\n\n    def run(self, options, args):\n        requirement = options.requirement\n        find_links = options.find_links or []\n        local_only = options.local\n        ## FIXME: Obviously this should be settable:\n        find_tags = False\n        skip_match = None\n\n        skip_regex = options.skip_requirements_regex\n        if skip_regex:\n            skip_match = re.compile(skip_regex)\n\n        dependency_links = []\n\n        f = sys.stdout\n\n        for dist in pkg_resources.working_set:\n            if dist.has_metadata('dependency_links.txt'):\n                dependency_links.extend(dist.get_metadata_lines('dependency_links.txt'))\n        for link in find_links:\n            if '#egg=' in link:\n                dependency_links.append(link)\n        for link in find_links:\n            f.write('-f %s\\n' % link)\n        installations = {}\n        for dist in get_installed_distributions(local_only=local_only):\n            req = pip.FrozenRequirement.from_dist(dist, dependency_links, find_tags=find_tags)\n            installations[req.name] = req\n        if requirement:\n            req_f = open(requirement)\n            for line in req_f:\n                if not line.strip() or line.strip().startswith('#'):\n                    f.write(line)\n                    continue\n                if skip_match and skip_match.search(line):\n                    f.write(line)\n                    continue\n                elif line.startswith('-e') or line.startswith('--editable'):\n                    if line.startswith('-e'):\n                        line = line[2:].strip()\n                    else:\n                        line = line[len('--editable'):].strip().lstrip('=')\n                    line_req = InstallRequirement.from_editable(line, default_vcs=options.default_vcs)\n                elif (line.startswith('-r') or line.startswith('--requirement')\n                      or line.startswith('-Z') or line.startswith('--always-unzip')\n                      or line.startswith('-f') or line.startswith('-i')\n                      or line.startswith('--extra-index-url')):\n                    f.write(line)\n                    continue\n                else:\n                    line_req = InstallRequirement.from_line(line)\n                if not line_req.name:\n                    logger.notify(\"Skipping line because it's not clear what it would install: %s\"\n                                  % line.strip())\n                    logger.notify(\"  (add #egg=PackageName to the URL to avoid this warning)\")\n                    continue\n                if line_req.name not in installations:\n                    logger.warn(\"Requirement file contains %s, but that package is not installed\"\n                                % line.strip())\n                    continue\n                f.write(str(installations[line_req.name]))\n                del installations[line_req.name]\n            f.write('## The following requirements were added by pip --freeze:\\n')\n        for installation in sorted(installations.values(), key=lambda x: x.name):\n            f.write(str(installation))\n\n\nFreezeCommand()\n", "path": "pip/commands/freeze.py"}], "after_files": [{"content": "import re\nimport sys\nimport pkg_resources\nimport pip\nfrom pip.req import InstallRequirement\nfrom pip.log import logger\nfrom pip.basecommand import Command\nfrom pip.util import get_installed_distributions\n\n\nclass FreezeCommand(Command):\n    name = 'freeze'\n    usage = '%prog [OPTIONS]'\n    summary = 'Output all currently installed packages (exact versions) to stdout'\n\n    def __init__(self):\n        super(FreezeCommand, self).__init__()\n        self.parser.add_option(\n            '-r', '--requirement',\n            dest='requirement',\n            action='store',\n            default=None,\n            metavar='FILENAME',\n            help='Use the given requirements file as a hint about how to generate the new frozen requirements')\n        self.parser.add_option(\n            '-f', '--find-links',\n            dest='find_links',\n            action='append',\n            default=[],\n            metavar='URL',\n            help='URL for finding packages, which will be added to the frozen requirements file')\n        self.parser.add_option(\n            '-l', '--local',\n            dest='local',\n            action='store_true',\n            default=False,\n            help='If in a virtualenv, do not report globally-installed packages')\n\n    def setup_logging(self):\n        logger.move_stdout_to_stderr()\n\n    def run(self, options, args):\n        requirement = options.requirement\n        find_links = options.find_links or []\n        local_only = options.local\n        ## FIXME: Obviously this should be settable:\n        find_tags = False\n        skip_match = None\n\n        skip_regex = options.skip_requirements_regex\n        if skip_regex:\n            skip_match = re.compile(skip_regex)\n\n        dependency_links = []\n\n        f = sys.stdout\n\n        for dist in pkg_resources.working_set:\n            if dist.has_metadata('dependency_links.txt'):\n                dependency_links.extend(dist.get_metadata_lines('dependency_links.txt'))\n        for link in find_links:\n            if '#egg=' in link:\n                dependency_links.append(link)\n        for link in find_links:\n            f.write('-f %s\\n' % link)\n        installations = {}\n        for dist in get_installed_distributions(local_only=local_only):\n            req = pip.FrozenRequirement.from_dist(dist, dependency_links, find_tags=find_tags)\n            installations[req.name] = req\n        if requirement:\n            req_f = open(requirement)\n            for line in req_f:\n                if not line.strip() or line.strip().startswith('#'):\n                    f.write(line)\n                    continue\n                if skip_match and skip_match.search(line):\n                    f.write(line)\n                    continue\n                elif line.startswith('-e') or line.startswith('--editable'):\n                    if line.startswith('-e'):\n                        line = line[2:].strip()\n                    else:\n                        line = line[len('--editable'):].strip().lstrip('=')\n                    line_req = InstallRequirement.from_editable(line, default_vcs=options.default_vcs)\n                elif (line.startswith('-r') or line.startswith('--requirement')\n                      or line.startswith('-Z') or line.startswith('--always-unzip')\n                      or line.startswith('-f') or line.startswith('-i')\n                      or line.startswith('--extra-index-url')\n                      or line.startswith('--find-links')\n                      or line.startswith('--index-url')):\n                    f.write(line)\n                    continue\n                else:\n                    line_req = InstallRequirement.from_line(line)\n                if not line_req.name:\n                    logger.notify(\"Skipping line because it's not clear what it would install: %s\"\n                                  % line.strip())\n                    logger.notify(\"  (add #egg=PackageName to the URL to avoid this warning)\")\n                    continue\n                if line_req.name not in installations:\n                    logger.warn(\"Requirement file contains %s, but that package is not installed\"\n                                % line.strip())\n                    continue\n                f.write(str(installations[line_req.name]))\n                del installations[line_req.name]\n            f.write('## The following requirements were added by pip --freeze:\\n')\n        for installation in sorted(installations.values(), key=lambda x: x.name):\n            f.write(str(installation))\n\n\nFreezeCommand()\n", "path": "pip/commands/freeze.py"}]}