Created
December 1, 2011 20:39
-
-
Save omsai/1419675 to your computer and use it in GitHub Desktop.
Creates a zip file containing 'Kinetic Imaging' iQ settings folder and 'kiconfig.ini' configurations file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Zip Andor iQ settings | |
Creates a zip file on the desktop containing: | |
- iQ settings folder | |
C:\Documents and Settings\All Users\Application Data\Kinetic Imaging\ | |
C:\ProgramData\Kinetic Imaging\ | |
- Configurations file | |
C:\Program Files\Andor Bioimaging\Common Files\kiconfig.ini | |
C:\Program Files (x86)\Andor Bioimaging\Common Files\kiconfig.ini | |
Pariksheet Nanda <p.nanda@andor.com> | |
''' | |
# User variables | |
ZIP_FILE_STEM = 'Settings-of-iQ' | |
ZIP_SAVE_HOME_FOLDER = r'Desktop' | |
LOG_FILE_NAME = r'zip_iQ_settings_log.txt' | |
# Program variables | |
APP_DATA_FOLDER = r'Kinetic Imaging' | |
COMMON_FILES_FOLDER = r'Andor Bioimaging\\Common Files' | |
KICONFIG_NAME = r'kiconfig.ini' | |
IQ_EXE = [ | |
'Andor iQ.exe', | |
'Andor iQ2.exe', | |
] | |
from os import path, listdir, sep, getenv | |
from zipfile import ZipFile | |
import logging | |
from datetime import datetime | |
from win32api import GetFileVersionInfo, LOWORD, HIWORD | |
logging.basicConfig(filename=LOG_FILE_NAME, level=logging.DEBUG) | |
def existspath(mypath): | |
''' | |
Return 1 if file or path exists, else return 0 | |
''' | |
if path.exists(mypath): | |
logging.info('Found path: %s' % mypath) | |
return 1 | |
else: | |
logging.error('Path not found: %s' % mypath) | |
return 0 | |
def recursive_zip(zipf, directory, folder = ""): | |
''' | |
Zips all contents of directory into ZipFile object | |
Copied from: | |
http://bytes.com/topic/python/answers/851018-how-zip-directory-python-using-zipfile#post3614508 | |
''' | |
for item in listdir(directory): | |
node = path.join(directory, item) | |
if path.isfile(node): | |
zipf.write(node, folder + sep + item) | |
elif path.isdir(node): | |
recursive_zip(zipf, node, folder + sep + item) | |
def get_version_number(filename): | |
''' | |
Extracts Windows file version tag | |
Copied from: | |
http://stackoverflow.com/questions/580924/python-windows-file-version-attribute | |
''' | |
try: | |
info = GetFileVersionInfo (filename, "\\") | |
ms = info['ProductVersionMS'] | |
ls = info['ProductVersionLS'] | |
return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls) | |
except: | |
return 0,0,0,0 | |
def main(start_time): | |
''' | |
Creates a zip file of iQ settings | |
''' | |
# Expand paths | |
logging.info('Expand and test paths...') | |
save_path = path.join(getenv('USERPROFILE'), ZIP_SAVE_HOME_FOLDER) | |
existspath(save_path) | |
app_data_path = getenv('ALLUSERSPROFILE') | |
existspath(app_data_path) | |
prog_files_path = getenv('PROGRAMFILES(X86)') | |
# Adjust paths for 64-bit system | |
if prog_files_path == None: | |
prog_files_path = getenv('PROGRAMFILES') | |
existspath(prog_files_path) | |
app_data_path = path.join(app_data_path, r'Application Data') | |
else: | |
existspath(prog_files_path) | |
existspath(app_data_path) | |
kinetic_path = path.join(app_data_path, APP_DATA_FOLDER) | |
existspath(kinetic_path) | |
common_files_path = path.join(prog_files_path, COMMON_FILES_FOLDER) | |
existspath(common_files_path) | |
try: | |
while 1: | |
iq_path = path.join(common_files_path, IQ_EXE.pop() ) | |
if existspath(iq_path): | |
break | |
except IndexError: | |
logging.info('No iQ executable found! Cannot determine iQ version') | |
# Create zip file name | |
logging.debug('Creating zip filename') | |
pc_name = getenv('COMPUTERNAME') | |
iq_version = '_'.join ([str (i) for i in get_version_number(iq_path)]) | |
ZIP_FILE = path.join(save_path, ZIP_FILE_STEM) | |
ZIP_FILE = '-'.join([ZIP_FILE, | |
iq_version, | |
pc_name, | |
start_time, | |
'.zip']) | |
logging.info('Saving settings into zip file: ' + ZIP_FILE) | |
# Populate zip file | |
logging.debug('Populating new zip file...') | |
zfile = ZipFile(ZIP_FILE, 'w') | |
recursive_zip(zfile, kinetic_path) | |
zfile.write(path.join(common_files_path, KICONFIG_NAME), KICONFIG_NAME) | |
zfile.close() | |
logging.debug('Closed zip file') | |
# boilerplate to call main function | |
if __name__ == '__main__': | |
print 'Zipping iQ settings files to desktop...' | |
t = datetime.now().strftime('%d%b%Y %H%M%S') | |
logging.info('---' + t + ' Start of program---') | |
main(t) | |
logging.info('--- End of program ---') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment