prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  download_only w/path fails to use path argument when checking for file
  When `download_only` checks to see if the file already exists before copying it, it ignores the path argument. This means that:
  
  ```
  retriever download MoM2003 -p testdata
  ```
  
  will keep overwriting the file in `testdata` if it exists, and it will not copy the file to `testdata` if the file exists in `.`.
  
  Fixes this is probably just a little logic improvement in the `final_cleanup` function of `download_only`.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `engines/download_only.py`
  Content:
  ```
  1 import os
  2 import platform
  3 import shutil
  4 import inspect
  5 from retriever.lib.engine import filename_from_url
  6 from retriever.lib.models import Engine, no_cleanup
  7 from retriever import DATA_DIR, HOME_DIR
  8 
  9 class DummyConnection:
  10     def cursor(self):
  11         pass
  12     def commit(self):
  13         pass
  14     def rollback(self):
  15         pass
  16     def close(self):
  17         pass
  18 
  19 class DummyCursor(DummyConnection):
  20     pass
  21 
  22 
  23 class engine(Engine):
  24     """Engine instance for writing data to a CSV file."""
  25     name = "Download Only"
  26     abbreviation = "download"
  27     required_opts = [("path",
  28                       "File path to copy data files",
  29                       "./"),
  30                      ("subdir",
  31                       "Keep the subdirectories for archived files",
  32                       False)
  33                      ]
  34 
  35     def table_exists(self, dbname, tablename):
  36         try:
  37             tablename = self.table_name(name=tablename, dbname=dbname)
  38             return os.path.exists(tablename)
  39         except:
  40             return False
  41 
  42     def get_connection(self):
  43         """Gets the db connection."""
  44         self.get_input()
  45         return DummyConnection()
  46 
  47     def final_cleanup(self):
  48         if hasattr(self, "all_files"):
  49             for file_name in self.all_files:
  50                 file_path, file_name_nopath = os.path.split(file_name)
  51                 subdir = os.path.split(file_path)[1] if self.opts['subdir'] else ''
  52                 dest_path = os.path.join(self.opts['path'], subdir)
  53                 if os.path.abspath(file_path) == os.path.abspath(os.path.join(DATA_DIR, subdir)):
  54                     print ("%s is already in the working directory" % file_name_nopath)
  55                     print("Keeping existing copy.")
  56                 else:
  57                     print("Copying %s from %s" % (file_name_nopath, file_path))
  58                     if os.path.isdir(dest_path):
  59                         try:
  60                             shutil.copy(file_name, dest_path)
  61                         except:
  62                             print("Couldn't copy file to %s" % dest_path)
  63                     else:
  64                         try:
  65                             print("Creating directory %s" % dest_path)
  66                             os.makedirs(dest_path)
  67                             shutil.copy(file_name, dest_path)
  68                         except:
  69                             print("Couldn't create directory %s" % dest_path)
  70         self.all_files = set()
  71 
  72     def auto_create_table(self, table, url=None, filename=None, pk=None):
  73         if url and not filename:
  74             filename = filename_from_url(url)
  75 
  76         if url and not self.find_file(filename):
  77             # If the file doesn't exist, download it
  78             self.download_file(url, filename)
  79 
  80     def insert_data_from_url(self, url):
  81         filename = filename_from_url(url)
  82         find = self.find_file(filename)
  83         if not find:
  84             self.create_raw_data_dir()
  85             self.download_file(url, filename)
  86 
  87     def find_file(self, filename):
  88         result = Engine.find_file(self, filename)
  89         if not hasattr(self, "all_files"): self.all_files = set()
  90         if result: self.all_files.add(result)
  91         return result
  92 
  93     def register_files(self, filenames):
  94         """Identify a list of files to be moved by the download
  95 
  96         When downloading archives with multiple files the engine needs to be
  97         informed of all of the file names so that it can move them.
  98 
  99         """
  100         full_filenames = {self.find_file(filename) for filename in filenames
  101                           if self.find_file(filename)}
  102         self.all_files = self.all_files.union(full_filenames)
  103 
  104 
  105 # replace all other methods with a function that does nothing
  106 def dummy_method(self, *args, **kwargs):
  107     pass
  108 methods = inspect.getmembers(engine, predicate=inspect.ismethod)
  109 keep_methods = {'table_exists',
  110                 'get_connection',
  111                 'final_cleanup',
  112                 'auto_create_table',
  113                 'insert_data_from_url',
  114                 }
  115 remove_methods = ['insert_data_from_file']
  116 for name, method in methods:
  117     if (not name in keep_methods
  118         and not 'download' in name
  119         and not 'file' in name
  120         and not 'dir' in name):
  121 
  122         setattr(engine, name, dummy_method)
  123 for name in remove_methods:
  124     setattr(engine, name, dummy_method)
  125
  ```
  --- 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/engines/download_only.py b/engines/download_only.py
  --- a/engines/download_only.py
  +++ b/engines/download_only.py
  @@ -48,14 +48,17 @@
           if hasattr(self, "all_files"):
               for file_name in self.all_files:
                   file_path, file_name_nopath = os.path.split(file_name)
  +                full_path = os.path.join(self.opts['path'], file_name_nopath)
                   subdir = os.path.split(file_path)[1] if self.opts['subdir'] else ''
                   dest_path = os.path.join(self.opts['path'], subdir)
                   if os.path.abspath(file_path) == os.path.abspath(os.path.join(DATA_DIR, subdir)):
                       print ("%s is already in the working directory" % file_name_nopath)
                       print("Keeping existing copy.")
  +                elif os.path.exists(full_path):
  +                    print ("File already exists at specified location")
                   else:
  -                    print("Copying %s from %s" % (file_name_nopath, file_path))
                       if os.path.isdir(dest_path):
  +                        print("Copying %s from %s" % (file_name_nopath, file_path))
                           try:
                               shutil.copy(file_name, dest_path)
                           except:
  @@ -64,6 +67,7 @@
                           try:
                               print("Creating directory %s" % dest_path)
                               os.makedirs(dest_path)
  +                            print("Copying %s from %s" % (file_name_nopath, file_path))
                               shutil.copy(file_name, dest_path)
                           except:
                               print("Couldn't create directory %s" % dest_path)
  @@ -122,3 +126,4 @@
           setattr(engine, name, dummy_method)
   for name in remove_methods:
       setattr(engine, name, dummy_method)
  +
  
verification_info: {"golden_diff": "diff --git a/engines/download_only.py b/engines/download_only.py\n--- a/engines/download_only.py\n+++ b/engines/download_only.py\n@@ -48,14 +48,17 @@\n         if hasattr(self, \"all_files\"):\n             for file_name in self.all_files:\n                 file_path, file_name_nopath = os.path.split(file_name)\n+                full_path = os.path.join(self.opts['path'], file_name_nopath)\n                 subdir = os.path.split(file_path)[1] if self.opts['subdir'] else ''\n                 dest_path = os.path.join(self.opts['path'], subdir)\n                 if os.path.abspath(file_path) == os.path.abspath(os.path.join(DATA_DIR, subdir)):\n                     print (\"%s is already in the working directory\" % file_name_nopath)\n                     print(\"Keeping existing copy.\")\n+                elif os.path.exists(full_path):\n+                    print (\"File already exists at specified location\")\n                 else:\n-                    print(\"Copying %s from %s\" % (file_name_nopath, file_path))\n                     if os.path.isdir(dest_path):\n+                        print(\"Copying %s from %s\" % (file_name_nopath, file_path))\n                         try:\n                             shutil.copy(file_name, dest_path)\n                         except:\n@@ -64,6 +67,7 @@\n                         try:\n                             print(\"Creating directory %s\" % dest_path)\n                             os.makedirs(dest_path)\n+                            print(\"Copying %s from %s\" % (file_name_nopath, file_path))\n                             shutil.copy(file_name, dest_path)\n                         except:\n                             print(\"Couldn't create directory %s\" % dest_path)\n@@ -122,3 +126,4 @@\n         setattr(engine, name, dummy_method)\n for name in remove_methods:\n     setattr(engine, name, dummy_method)\n+\n", "issue": "download_only w/path fails to use path argument when checking for file\nWhen `download_only` checks to see if the file already exists before copying it, it ignores the path argument. This means that:\n\n```\nretriever download MoM2003 -p testdata\n```\n\nwill keep overwriting the file in `testdata` if it exists, and it will not copy the file to `testdata` if the file exists in `.`.\n\nFixes this is probably just a little logic improvement in the `final_cleanup` function of `download_only`.\n\n", "before_files": [{"content": "import os\nimport platform\nimport shutil\nimport inspect\nfrom retriever.lib.engine import filename_from_url\nfrom retriever.lib.models import Engine, no_cleanup\nfrom retriever import DATA_DIR, HOME_DIR\n\nclass DummyConnection:\n    def cursor(self):\n        pass\n    def commit(self):\n        pass\n    def rollback(self):\n        pass\n    def close(self):\n        pass\n\nclass DummyCursor(DummyConnection):\n    pass\n\n\nclass engine(Engine):\n    \"\"\"Engine instance for writing data to a CSV file.\"\"\"\n    name = \"Download Only\"\n    abbreviation = \"download\"\n    required_opts = [(\"path\",\n                      \"File path to copy data files\",\n                      \"./\"),\n                     (\"subdir\",\n                      \"Keep the subdirectories for archived files\",\n                      False)\n                     ]\n\n    def table_exists(self, dbname, tablename):\n        try:\n            tablename = self.table_name(name=tablename, dbname=dbname)\n            return os.path.exists(tablename)\n        except:\n            return False\n\n    def get_connection(self):\n        \"\"\"Gets the db connection.\"\"\"\n        self.get_input()\n        return DummyConnection()\n\n    def final_cleanup(self):\n        if hasattr(self, \"all_files\"):\n            for file_name in self.all_files:\n                file_path, file_name_nopath = os.path.split(file_name)\n                subdir = os.path.split(file_path)[1] if self.opts['subdir'] else ''\n                dest_path = os.path.join(self.opts['path'], subdir)\n                if os.path.abspath(file_path) == os.path.abspath(os.path.join(DATA_DIR, subdir)):\n                    print (\"%s is already in the working directory\" % file_name_nopath)\n                    print(\"Keeping existing copy.\")\n                else:\n                    print(\"Copying %s from %s\" % (file_name_nopath, file_path))\n                    if os.path.isdir(dest_path):\n                        try:\n                            shutil.copy(file_name, dest_path)\n                        except:\n                            print(\"Couldn't copy file to %s\" % dest_path)\n                    else:\n                        try:\n                            print(\"Creating directory %s\" % dest_path)\n                            os.makedirs(dest_path)\n                            shutil.copy(file_name, dest_path)\n                        except:\n                            print(\"Couldn't create directory %s\" % dest_path)\n        self.all_files = set()\n\n    def auto_create_table(self, table, url=None, filename=None, pk=None):\n        if url and not filename:\n            filename = filename_from_url(url)\n\n        if url and not self.find_file(filename):\n            # If the file doesn't exist, download it\n            self.download_file(url, filename)\n\n    def insert_data_from_url(self, url):\n        filename = filename_from_url(url)\n        find = self.find_file(filename)\n        if not find:\n            self.create_raw_data_dir()\n            self.download_file(url, filename)\n\n    def find_file(self, filename):\n        result = Engine.find_file(self, filename)\n        if not hasattr(self, \"all_files\"): self.all_files = set()\n        if result: self.all_files.add(result)\n        return result\n\n    def register_files(self, filenames):\n        \"\"\"Identify a list of files to be moved by the download\n\n        When downloading archives with multiple files the engine needs to be\n        informed of all of the file names so that it can move them.\n\n        \"\"\"\n        full_filenames = {self.find_file(filename) for filename in filenames\n                          if self.find_file(filename)}\n        self.all_files = self.all_files.union(full_filenames)\n\n\n# replace all other methods with a function that does nothing\ndef dummy_method(self, *args, **kwargs):\n    pass\nmethods = inspect.getmembers(engine, predicate=inspect.ismethod)\nkeep_methods = {'table_exists',\n                'get_connection',\n                'final_cleanup',\n                'auto_create_table',\n                'insert_data_from_url',\n                }\nremove_methods = ['insert_data_from_file']\nfor name, method in methods:\n    if (not name in keep_methods\n        and not 'download' in name\n        and not 'file' in name\n        and not 'dir' in name):\n\n        setattr(engine, name, dummy_method)\nfor name in remove_methods:\n    setattr(engine, name, dummy_method)\n", "path": "engines/download_only.py"}], "after_files": [{"content": "import os\nimport platform\nimport shutil\nimport inspect\nfrom retriever.lib.engine import filename_from_url\nfrom retriever.lib.models import Engine, no_cleanup\nfrom retriever import DATA_DIR, HOME_DIR\n\nclass DummyConnection:\n    def cursor(self):\n        pass\n    def commit(self):\n        pass\n    def rollback(self):\n        pass\n    def close(self):\n        pass\n\nclass DummyCursor(DummyConnection):\n    pass\n\n\nclass engine(Engine):\n    \"\"\"Engine instance for writing data to a CSV file.\"\"\"\n    name = \"Download Only\"\n    abbreviation = \"download\"\n    required_opts = [(\"path\",\n                      \"File path to copy data files\",\n                      \"./\"),\n                     (\"subdir\",\n                      \"Keep the subdirectories for archived files\",\n                      False)\n                     ]\n\n    def table_exists(self, dbname, tablename):\n        try:\n            tablename = self.table_name(name=tablename, dbname=dbname)\n            return os.path.exists(tablename)\n        except:\n            return False\n\n    def get_connection(self):\n        \"\"\"Gets the db connection.\"\"\"\n        self.get_input()\n        return DummyConnection()\n\n    def final_cleanup(self):\n        if hasattr(self, \"all_files\"):\n            for file_name in self.all_files:\n                file_path, file_name_nopath = os.path.split(file_name)\n                full_path = os.path.join(self.opts['path'], file_name_nopath)\n                subdir = os.path.split(file_path)[1] if self.opts['subdir'] else ''\n                dest_path = os.path.join(self.opts['path'], subdir)\n                if os.path.abspath(file_path) == os.path.abspath(os.path.join(DATA_DIR, subdir)):\n                    print (\"%s is already in the working directory\" % file_name_nopath)\n                    print(\"Keeping existing copy.\")\n                elif os.path.exists(full_path):\n                    print (\"File already exists at specified location\")\n                else:\n                    if os.path.isdir(dest_path):\n                        print(\"Copying %s from %s\" % (file_name_nopath, file_path))\n                        try:\n                            shutil.copy(file_name, dest_path)\n                        except:\n                            print(\"Couldn't copy file to %s\" % dest_path)\n                    else:\n                        try:\n                            print(\"Creating directory %s\" % dest_path)\n                            os.makedirs(dest_path)\n                            print(\"Copying %s from %s\" % (file_name_nopath, file_path))\n                            shutil.copy(file_name, dest_path)\n                        except:\n                            print(\"Couldn't create directory %s\" % dest_path)\n        self.all_files = set()\n\n    def auto_create_table(self, table, url=None, filename=None, pk=None):\n        if url and not filename:\n            filename = filename_from_url(url)\n\n        if url and not self.find_file(filename):\n            # If the file doesn't exist, download it\n            self.download_file(url, filename)\n\n    def insert_data_from_url(self, url):\n        filename = filename_from_url(url)\n        find = self.find_file(filename)\n        if not find:\n            self.create_raw_data_dir()\n            self.download_file(url, filename)\n\n    def find_file(self, filename):\n        result = Engine.find_file(self, filename)\n        if not hasattr(self, \"all_files\"): self.all_files = set()\n        if result: self.all_files.add(result)\n        return result\n\n    def register_files(self, filenames):\n        \"\"\"Identify a list of files to be moved by the download\n\n        When downloading archives with multiple files the engine needs to be\n        informed of all of the file names so that it can move them.\n\n        \"\"\"\n        full_filenames = {self.find_file(filename) for filename in filenames\n                          if self.find_file(filename)}\n        self.all_files = self.all_files.union(full_filenames)\n\n\n# replace all other methods with a function that does nothing\ndef dummy_method(self, *args, **kwargs):\n    pass\nmethods = inspect.getmembers(engine, predicate=inspect.ismethod)\nkeep_methods = {'table_exists',\n                'get_connection',\n                'final_cleanup',\n                'auto_create_table',\n                'insert_data_from_url',\n                }\nremove_methods = ['insert_data_from_file']\nfor name, method in methods:\n    if (not name in keep_methods\n        and not 'download' in name\n        and not 'file' in name\n        and not 'dir' in name):\n\n        setattr(engine, name, dummy_method)\nfor name in remove_methods:\n    setattr(engine, name, dummy_method)\n\n", "path": "engines/download_only.py"}]}