conf: prevent data loss when saving the config file
[urk.git] / install.py
blobd06a50eb32e06f60c5886beed27a4b5747acb391
1 import os
2 import shutil
3 import subprocess
5 install_path = os.curdir
6 bin_path = os.curdir
7 dirs_to_install = []
8 files_to_install = []
9 exclude_dirs = ['CVS', 'profile', '.idlerc']
10 exclude_files = ['install.py', 'urk.desktop', 'urk.nsi', 'installer.exe', 'urk.exe']
12 nsis_outfile="urk.exe"
13 nsis_make_exe=["makensis"] #for non-windows systems
15 def identify_files(path=os.curdir, prefix='', sep=os.path.join):
16 #set files to a list of the files we want to install
17 print "Searching for files to install"
18 for filename in os.listdir(path):
19 abs_file = os.path.join(path,filename)
20 rel_file = sep(prefix,filename)
21 if os.path.isfile(abs_file) and filename not in exclude_files:
22 print "Marking file %s for installation" % rel_file
23 files_to_install.append(rel_file)
24 elif os.path.isdir(abs_file) and filename not in exclude_dirs:
25 print "Marking directory %s for installation" % rel_file
26 dirs_to_install.append(rel_file)
27 identify_files(abs_file, rel_file, sep)
29 def install_files():
30 #copy .py files to the destination directory
31 print "Installing files to %s" % install_path
32 for dirname in dirs_to_install:
33 dest = os.path.join(install_path,dirname)
34 if os.access(dest,os.X_OK):
35 print "Found directory %s" % dest
36 else:
37 print "Creating new directory %s" % dest
38 os.mkdir(dest,0755)
39 for filename in files_to_install:
40 source = os.path.join(os.curdir,filename)
41 dest = os.path.join(install_path,filename)
42 print "Copying %s to %s" % (source, dest)
43 shutil.copy(source, dest)
45 def nsis_generate_script():
46 filename = os.path.join(install_path,"urk.nsi")
47 print "Generating NSIS installer script %s" % filename
48 f = file(filename,'w')
49 #header
50 f.write(r"""
51 ; This file was automatically generated by urk's install.py. Don't
52 ; even bother to modify it.
54 Name "urk"
56 OutFile "installer.exe"
58 InstallDir $PROGRAMFILES\urk
60 InstallDirRegKey HKLM "Software\urk" "Install_Dir"
63 Page components
64 Page directory
65 Page instfiles
67 UninstPage uninstConfirm
68 UninstPage instfiles
71 Function GetPythonDir
72 Var /GLOBAL python_dir
73 ReadRegStr $python_dir HKLM "Software\Python\PythonCore\2.4\InstallPath" ""
74 FunctionEnd
77 Function GetPythonVersion
78 Var /GLOBAL python_version
79 Call GetPythonDir
80 ExecWait "$python_dir\python.exe -V"
82 FunctionEnd
85 Section /o "dependencies"
86 Call GetPythonDir
87 MessageBox MB_OK $python_dir
88 SectionEnd
91 Section "urk (required)"
92 SetOutPath $INSTDIR
93 """)
94 #look for existing installation?
95 for dirname in dirs_to_install:
96 f.write(' CreateDirectory "$INSTDIR\\%s"\n' % dirname)
97 for filename in files_to_install:
98 f.write(' File "/oname=%s" "%s"\n' % (filename, filename))
99 f.write(r"""
101 WriteRegStr HKLM SOFTWARE\urk "Install_Dir" "$INSTDIR"
103 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\urk" "DisplayName" "urk IRC Client"
104 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\urk" "UninstallString" '"$INSTDIR\uninstall.exe"'
105 WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\urk" "NoModify" 1
106 WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\urk" "NoRepair" 1
107 WriteUninstaller "uninstall.exe"
108 SectionEnd
109 """)
111 #uninstaller
112 f.write(r"""
113 Section "Uninstall"
115 ; Remove registry keys
116 DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\urk"
117 DeleteRegKey HKLM SOFTWARE\urk
118 """)
119 for filename in files_to_install:
120 f.write(' Delete $INSTDIR\\%s\n' % filename)
121 for dirname in dirs_to_install[::-1]:
122 f.write(' RMDIR $INSTDIR\\%s\n' % dirname)
123 f.write(r"""
124 Delete "$INSTDIR\uninstall.exe"
125 RMDIR "$INSTDIR"
127 SectionEnd
128 """)
130 f.close()
132 def nsis_generate_exe():
133 filename = os.path.join(install_path,"urk.nsi")
134 print "Calling makensis on %s" % filename
135 try: #look for NSIS in the registry
136 import _winreg
137 key_software = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"Software")
138 key_nsis = _winreg.OpenKey(key_software,"NSIS")
139 make_exe = [os.path.join(_winreg.QueryValueEx(key_nsis,None)[0],"makensis.exe")]
140 except: #..or just use the global for it
141 make_exe = nsis_make_exe
142 subprocess.call(make_exe + [filename])
144 def install_to_nsis():
145 def sep(*args):
146 return '\\'.join(x for x in args if x)
147 identify_files(sep=sep)
148 nsis_generate_script()
149 nsis_generate_exe()
151 def install_to_linux_system():
152 identify_files()
154 pass