Repository: kblin/ncbi-genome-download

Issue:
OSError: [Errno 17] File exists
Been getting this a bit when I restart jobs that failed due to unreachable network:

```
NFO: Starting new HTTPS connection (1): ftp.ncbi.nlm.nih.gov
DEBUG: "GET /genomes/all/GCA_001443705.1_ASM144370v1/GCA_001443705.1_ASM144370v1_genomic.gbff.gz HTTP/1.1" 200 185238057
Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/bin/ncbi-genome-download", line 11, in <module>
    sys.exit(main())
  File "/home/linuxbrew/.linuxbrew/Cellar/python/2.7.12_1/lib/python2.7/site-packages/ncbi_genome_download/__main__.py", line 78, in main
INFO: Starting new HTTPS connection (1): ftp.ncbi.nlm.nih.gov
    ret = ncbi_genome_download.download(args)
  File "/home/linuxbrew/.linuxbrew/Cellar/python/2.7.12_1/lib/python2.7/site-packages/ncbi_genome_download/core.py", line 60, in download
    args.taxid, args.human_readable, args.parallel)
  File "/home/linuxbrew/.linuxbrew/Cellar/python/2.7.12_1/lib/python2.7/site-packages/ncbi_genome_download/core.py", line 94, in _download
    pool.map(worker, download_jobs)
  File "/home/linuxbrew/.linuxbrew/Cellar/python/2.7.12_1/lib/python2.7/multiprocessing/pool.py", line 251, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/home/linuxbrew/.linuxbrew/Cellar/python/2.7.12_1/lib/python2.7/multiprocessing/pool.py", line 567, in get
    raise self._value
OSError: [Errno 17] File exists
```


Discussion:

Patch:
diff --git a/ncbi_genome_download/core.py b/ncbi_genome_download/core.py
index 2a47d97..f2389df 100644
--- a/ncbi_genome_download/core.py
+++ b/ncbi_genome_download/core.py
@@ -286,6 +286,8 @@ def save_and_check(response, local_file, expected_checksum, symlink_path):
         return False
 
     if symlink_path is not None:
+        if os.path.lexists(symlink_path):
+            os.unlink(symlink_path)
         os.symlink(os.path.abspath(local_file), symlink_path)
 
     return True


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index d372c91..3c17e9a 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -1,3 +1,4 @@
+import os
 import pytest
 import requests_mock
 from argparse import Namespace
@@ -508,6 +509,21 @@ def test_download_file_symlink_path(req, tmpdir):
     symlink = symlink_dir.join('fake_genomic.gbff.gz')
     assert symlink.check()
 
+def test_download_file_symlink_path_existed(req, tmpdir):
+    entry = {'ftp_path': 'ftp://fake/path'}
+    fake_file = tmpdir.join('fake_genomic.gbff.gz')
+    fake_file.write('foo')
+    assert fake_file.check()
+    checksum = core.md5sum(str(fake_file))
+    checksums = [{'checksum': checksum, 'file': fake_file.basename}]
+    dl_dir = tmpdir.mkdir('download')
+    symlink_dir = tmpdir.mkdir('symlink')
+    symlink = symlink_dir.join('fake_genomic.gbff.gz')
+    os.symlink("/foo/bar", str(symlink))
+    req.get('https://fake/path/fake_genomic.gbff.gz', text=fake_file.read())
+
+    assert core.worker(core.download_file(entry, str(dl_dir), checksums, symlink_path=str(symlink_dir)))
+    assert symlink.check()
 
 def test_get_genus_label():
     fake_entry = {'organism_name': 'Example species ABC 1234'}