Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Functions to prepare a setup on Windows, use InnoSetup 

4""" 

5from __future__ import print_function 

6 

7import os 

8import sys 

9from ..installhelper.install_cmd_helper import run_cmd 

10from .win_ipy_kernels import install_kernels 

11 

12if sys.version_info[0] == 2: 

13 from codecs import open 

14 FileNotFoundError = Exception 

15 

16 

17class InnoSetupException(Exception): 

18 

19 """ 

20 Exception happening with InnoSetup 

21 """ 

22 pass 

23 

24 

25def find_innosetup(): 

26 """ 

27 find InnoSetup executable 

28 

29 @return executable 

30 """ 

31 exe = r"C:\Program Files (x86)\Inno Setup 5\compil32.exe" 

32 if not os.path.exists(exe): 

33 raise FileNotFoundError(exe) 

34 return exe 

35 

36 

37def run_innosetup(new_script, innosetup=None, log_script=None, temp_folder=".", fLOG=print): 

38 """ 

39 run InnotSetup for a script 

40 

41 @param new_script script to run 

42 @param innosetup location of InnoSetup (if None, use default location) 

43 @param log_script output logs to this file 

44 @param temp_folder where to copy the modified script 

45 @param fLOG logging function 

46 @return output 

47 """ 

48 if innosetup is None: 

49 innosetup = find_innosetup() 

50 

51 cmd = [innosetup, "/cc", new_script] 

52 if log_script is not None: 

53 raise NotImplementedError() 

54 # cmd.append('/LOG="{0}"'.format(log_script)) 

55 

56 fLOG("[pymy] ISS script", new_script) 

57 fLOG("[pymy] CMD", cmd) 

58 out, err = run_cmd(" ".join(cmd), wait=True, fLOG=fLOG) 

59 if err is not None and len(err) > 0: 

60 raise InnoSetupException( 

61 "CMD:\n{0}\nOUT:\n{1}\nERR-5:\n{2}".format(cmd, out, err)) 

62 return out 

63 

64 

65def innosetup_replacements(script=None, innosetup=None, replacements=None, log_script=None, temp_folder=".", fLOG=print): 

66 """ 

67 run InnotSetup for a script 

68 

69 @param script script to run, if None, use the default script assuming you want to build a Python Distribution 

70 @param innosetup location of InnoSetup (if None, use default location) 

71 @param replacements replace to make in the script (dictionary) 

72 @param log_script output logs to this file 

73 @param temp_folder where to copy the modified script 

74 @param fLOG logging function 

75 @return new script 

76 """ 

77 if script is None: 

78 script = os.path.abspath( 

79 os.path.join(os.path.dirname(__file__), "innosetup_script.iss")) 

80 

81 if replacements is None: 

82 replacements = dict() 

83 

84 with open(script, "r", encoding="utf8") as f: 

85 content = f.read() 

86 

87 for k, v in replacements.items(): 

88 content = content.replace(k, v) 

89 

90 new_script = os.path.join(os.path.abspath(temp_folder), 

91 os.path.split(script)[-1].replace(".iss", ".temp.iss")) 

92 with open(new_script, "w", encoding="utf8") as f: 

93 f.write(content) 

94 

95 return new_script 

96 

97 

98def inno_install_kernels(root, suffix): 

99 """ 

100 install kernels for Jupyter notebooks 

101 

102 @param root root folder 

103 @param suffix suffix 

104 """ 

105 if root in os.environ: 

106 path = os.environ[root] 

107 tools = os.path.join(path, "tools") 

108 if not os.path.exists(tools): 

109 tools = os.path.normpath(os.path.join(path, "..", "tools")) 

110 python = os.path.normpath(os.path.join(path, "..", "python")) 

111 else: 

112 python = os.path.join(path, "python") 

113 

114 else: 

115 tools = os.path.join(root, "tools") 

116 python = os.path.join(root, "python") 

117 

118 if not os.path.exists(tools): 

119 raise FileNotFoundError(tools) 

120 if not os.path.exists(python): 

121 raise FileNotFoundError(python) 

122 

123 install_kernels(tools, python, suffix)