Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created February 10, 2012 00:31
Show Gist options
  • Save zeffii/1784791 to your computer and use it in GitHub Desktop.
Save zeffii/1784791 to your computer and use it in GitHub Desktop.
slow start.
'''
py 2.7
assumptions:
- buze is currently running in a dir located outside of program files
- the dir is either formatted like: buze-x.x.x or buze-x.x.x-revision
[x]- read externally the name of latest version and revision.
[ ]- if version and revision equal current, offer to cease now.
[x]- get current buze path, go up level.
[x]- check if dir called buze-x.x.x[-rev] exists,
[ ]- - - - if yes create a new one, with rev. (what if that exists too? timestamp? )
[x]- - - - if it does not exist already,
[x]- - - - - - - create new dir called buze-x.x.x
[x]- get
http://www.batman.no/buze/buze-x.x.x.zip
http://www.batman.no/buze/buze-x.x.x-pythonview.zip
into new dir.
[x]- unpack all .zip
'''
import urllib
import os
import zipfile
def get_version():
http_location = 'http://www.digitalaphasia.com/buze_rev/buze_revision.txt'
ufile = urllib.urlopen(http_location)
text = ufile.read()
version = text.split()[1]
revision = 'rev' # TODO
return version, revision
def get_urls_and_filenames(version):
extention = '.zip'
url_base = 'http://www.batman.no/buze/buze-'
filename1 = version + extention
filename2 = version + '-pythonview' + extention
url1 = url_base + filename1
url2 = url_base + filename2
filename1 = 'buze-'+ filename1
filename2 = 'buze-'+ filename2
urls = (url1, url2)
print_url_info(urls)
return urls, (filename1, filename2)
def print_url_info(urls):
print('download')
for url in urls:
print(url)
print('\n')
def get_path_info():
cur_path = os.getcwd()
separator = os.path.sep
print('current path')
print(cur_path)
cur_dir = cur_path.split(separator)[-1]
print('current installation directory: ' + cur_dir)
return cur_path, cur_dir
# warning, do we want to be super vigilent? probably.
# http://docs.python.org/library/zipfile.html#zipfile.ZipFile.extractall
def unzip_files(new_path):
filenames = [file for file in os.listdir(new_path) if file[-3:] == 'zip']
for filename in filenames:
relative_path_to_file = os.path.join(new_path, filename)
myzip = zipfile.ZipFile(relative_path_to_file)
myzip.extractall()
print('extracted ' + filename)
# wait for extraction? how.
def print_current_version(cur_dir):
# perhaps buze api can provide current version/revision details
print('current version installed: ' + cur_dir.split('-')[1])
def download_files(new_path, urls, filenames):
for index, payload_url in enumerate(urls):
file_path = os.path.join(new_path, filenames[index])
urllib.urlretrieve(payload_url, file_path)
def create_directory():
# make new directory if not present,
try:
os.mkdir(new_path)
return True
except OSError:
print(new_path + ' already exists')
# empty its contents?
return False
version, revision = get_version()
urls, filenames = get_urls_and_filenames(version)
original_path, cur_dir = get_path_info()
print_current_version(cur_dir)
new_path = os.path.join(os.pardir, 'buze-' + version + '-' + revision)
if create_directory():
download_files(new_path, urls, filenames)
os.chdir(new_path)
unzip_files(os.getcwd())
os.chdir(original_path)
else:
print('delete the contents of: ' + new_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment