Skip to content

Instantly share code, notes, and snippets.

Created March 26, 2013 04:46
Show Gist options
  • Save anonymous/5243199 to your computer and use it in GitHub Desktop.
Save anonymous/5243199 to your computer and use it in GitHub Desktop.
Pypi
import urllib
import tarfile
import shutil
import console
import os
class Installer(object):
name = None
version = None
firstLetter = None
lowerName = None
tarfolder = None
tarname = None
def __init__(self, name, version):
self.name = name
self.version = version
self.firstLetter = name[0]
self.lowerName = name.lower()
self.tarfolder = self.name + '-' + self.version
self.tarname = self.tarfolder + '.tar.gz'
def install(self):
try:
self.download()
self.extract()
self.copy()
except Exception, e:
print str(e)
finally:
self.clean()
def download(self):
print 'Downloading ' + self.name + ' v' + self.version + '...'
url = 'http://pypi.python.org/packages/source/' + self.firstLetter + '/' + self.name + '/' + self.tarname
urllib.urlretrieve(url, self.tarname)
print 'Download Complete'
def extract(self):
print 'Extracting...'
t = tarfile.open(self.tarname)
t.extractall()
print 'Package extracted'
def copy(self):
# If source is a folder
if os.path.isdir(self.tarfolder + '/' + self.lowerName):
if os.path.isdir(self.lowerName):
print 'Removing old package directory...'
shutil.rmtree(self.lowerName)
print 'Installing package directory...'
shutil.move(self.tarfolder + '/' + self.lowerName, './' + self.lowerName)
# if source is a file
file = self.lowerName + '.py'
if os.path.isfile(self.tarfolder + '/' + file):
if os.path.isfile(file):
print 'Removing old package file...'
os.remove(file)
print 'Installing package file...'
shutil.move(self.tarfolder + '/' + file, './' + file)
def clean(self):
print 'Cleaning up...'
if os.path.isdir(self.tarfolder):
print 'Removing source directory...'
shutil.rmtree(self.tarfolder)
if os.path.isfile(self.tarname):
print 'Removing source tarball...'
os.remove(self.tarname)
print 'Done.'
@cclauss
Copy link

cclauss commented Jan 10, 2015

Should this be updated to use Pythonista's site-packages directory?

@ikalash762
Copy link

Downloading prettytable v2.2.1...
Download Complete
Extracting...
file could not be opened successfully
Cleaning up...
Removing source tarball...
Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment