Skip to content

Instantly share code, notes, and snippets.

@kgleeson
Forked from zeffii/buze_updater.py
Created February 10, 2012 19:44
Show Gist options
  • Save kgleeson/1792149 to your computer and use it in GitHub Desktop.
Save kgleeson/1792149 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
import re
def get_latest_version():
httpLocation = 'http://www.batman.no/buze/'
ufile = urllib.urlopen(httpLocation)
body = ufile.read()
latestRE = re.compile('(http:.+)?\">Download latest')
downloadLink = latestRE.search(body).group(1)
downloadViewLink = downloadLink.replace('.zip', '-pythonview.zip')
return downloadLink, downloadViewLink
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.endswith('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 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
fileToDownload = [url for url in get_path_info()]
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():
for url in fileToDownload:
urllib.urlretrieve(url, new_path)
os.chdir(new_path)
unzip_files(os.getcwd())
os.chdir(original_path)
else:
print('delete the contents of: ' + new_path)
@kgleeson
Copy link
Author

Lazy auto find newest version, no need for updating files!

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