Download-from-PyPI-and-install script

Posted by zubin71 on Stack Overflow See other posts from Stack Overflow or by zubin71
Published on 2010-04-14T05:25:46Z Indexed on 2010/04/14 5:33 UTC
Read the original article Hit count: 405

Filed under:
|
|
|
|

Hello,

I have written a script which fetches a distribution, given the URL. After downloading the distribution, it compares the md5 hashes to verify that the file has been downloaded properly.

This is how I do it.

def download(package_name, url):
    import urllib2
    downloader = urllib2.urlopen(url)
    package = downloader.read()
    package_file_path = os.path.join('/tmp', package_name)
    package_file = open(package_file_path, "w")
    package_file.write(package)
    package_file.close()

I wonder if there is any better(more pythonic) way to do what I have done using the above code snippet.

Also, once the package is downloaded this is what is done:

def install_package(package_name):
    if package_name.endswith('.tar'):
        import tarfile
        tarfile.open('/tmp/' + package_name)
        tarfile.extract('/tmp')
        import shlex
        import subprocess
        installation_cmd = 'python %ssetup.py install' %('/tmp/'+package_name)
        subprocess.Popen(shlex.split(installation_cmd)

As there are a number of imports for the install_package method, i wonder if there is a better way to do this. I`d love to have some constructive criticism and suggestions for improvement.

Also, I have only implemented the install_package method for .tar files; would there be a better manner by which I could install .tar.gz and .zip files too without having to write seperate methods for each of these?

© Stack Overflow or respective owner

Related posts about python

Related posts about subprocess