Permalink
Browse files

Added function get_local_file_name()

  • Loading branch information...
1 parent 322fe16 commit 572fd29efb08f80b3696bf959cf1fbb6eed54934 @dvarrazzo committed Aug 29, 2012
Showing with 17 additions and 9 deletions.
  1. +2 −9 pgxnclient/commands/install.py
  2. +15 −0 pgxnclient/network.py
@@ -20,7 +20,7 @@
from pgxnclient.i18n import _, N_
from pgxnclient.utils import sha1, b
from pgxnclient.errors import BadChecksum, PgxnClientException, InsufficientPrivileges
-from pgxnclient.network import download
+from pgxnclient.network import download, get_local_file_name
from pgxnclient.commands import Command, WithDatabase, WithMake, WithPgConfig
from pgxnclient.commands import WithSpec, WithSpecLocal, WithSudo
from pgxnclient.utils.zip import unpack
@@ -80,14 +80,7 @@ def verify_checksum(self, fn, chk):
raise BadChecksum(_("bad sha1 in downloaded file"))
def _get_local_file_name(self, url):
- from urlparse import urlsplit
- if os.path.isdir(self.opts.target):
- basename = urlsplit(url)[2].rsplit('/', 1)[-1]
- fn = os.path.join(self.opts.target, basename)
- else:
- fn = self.opts.target
-
- return os.path.abspath(fn)
+ return get_local_file_name(self.opts.target, url)
class InstallUninstall(WithMake, WithSpecLocal, Command):
View
@@ -8,6 +8,7 @@
import os
import urllib2
+from urlparse import urlsplit
from itertools import count
from contextlib import closing
@@ -39,6 +40,20 @@ def get_file(url):
except urllib2.URLError, e:
raise NetworkError(_("network error: %s") % e.reason)
+def get_local_file_name(target, url):
+ """Return a good name for a local file.
+
+ If *target* is a dir, make a name out of the url. Otherwise return target
+ itself. Always return an absolute path.
+ """
+ if os.path.isdir(target):
+ basename = urlsplit(url)[2].rsplit('/', 1)[-1]
+ fn = os.path.join(target, basename)
+ else:
+ fn = target
+
+ return os.path.abspath(fn)
+
def download(f, fn, rename=True):
"""Download a file locally.

0 comments on commit 572fd29

Please sign in to comment.