Coverage for pyquickhelper/helpgen/install_custom.py: 94%

63 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 02:21 +0200

1""" 

2@file 

3@brief Various function to install some application such as :epkg:`pandoc`. 

4""" 

5import re 

6import os 

7import shutil 

8from ..filehelper import download, read_url 

9from ..filehelper.internet_helper import ReadUrlException 

10from ..filehelper.compression_helper import unzip_files 

11from ..filehelper.synchelper import explore_folder_iterfile 

12 

13 

14def download_revealjs(temp_folder=".", unzip_to=".", fLOG=print, 

15 location="https://github.com/hakimel/reveal.js/releases", 

16 clean=True): 

17 """ 

18 Downloads :epkg:`reveal.js` release and unzips it. 

19 

20 @param temp_folder where to download the setup 

21 @param unzip_to where to unzip the files 

22 @param fLOG logging function 

23 @param location location of reveal.js release 

24 @param clean clean unnecessary files 

25 @return list of downloaded and unzipped files 

26 """ 

27 link = location 

28 page = read_url(link, encoding="utf8") 

29 reg = re.compile("href=\\\"(.*?[0-9.]+?[.]zip)\\\"") 

30 alls = reg.findall(page) 

31 if len(alls) == 0: 

32 # Fall back to one release 

33 filel = "https://github.com/hakimel/reveal.js/archive/refs/tags/4.3.1.zip" 

34 outfile = os.path.join(temp_folder, "reveal.js.4.3.1.zip") 

35 else: 

36 filename = alls[0].split("/")[-1] 

37 filel = location.replace("releases", "").rstrip( 

38 '/') + f"/archive/{filename}" 

39 outfile = os.path.join(temp_folder, "reveal.js." + filename) 

40 fLOG("download ", filel, "to", outfile) 

41 local = download(filel, temp_folder, fLOG=fLOG) 

42 fLOG("local file", local) 

43 unzip_files(local, where_to=unzip_to) 

44 

45 # we rename 

46 sub = [_ for _ in os.listdir(unzip_to) if "reveal" in _] 

47 if len(sub) != 1: 

48 raise FileNotFoundError( # pragma: no cover 

49 f"Several version exists or none of them:\n{', '.join(sub)}") 

50 sub = sub[0] 

51 master = os.path.join(unzip_to, sub) 

52 if not os.path.exists(master): 

53 raise FileNotFoundError( # pragma: no cover 

54 f"Unable to find {master!r}.") 

55 new_master = os.path.join(unzip_to, "reveal.js") 

56 os.rename(master, new_master) 

57 

58 if clean: 

59 # we clean some files 

60 res = explore_folder_iterfile(new_master) 

61 keep = [] 

62 for r in res: 

63 if os.path.isdir(r): 

64 continue 

65 if (".gitignore" in r or ".travis.yml" in r or "index.html" in r or 

66 ".appveyor.yml" in r or "requirement" in r or "README" in r or 

67 "CONTRIBUTING.md" in r): 

68 os.remove(r) 

69 elif "/test/" in r.replace("\\", "/"): 

70 os.remove(r) 

71 else: 

72 keep.append(r) 

73 

74 # we clean the downloaded file 

75 os.remove(local) 

76 

77 return keep 

78 

79 

80def download_requirejs(to=".", fLOG=print, 

81 location="http://requirejs.org/docs/download.html", 

82 clean=True): 

83 """ 

84 Downloads `require.js 

85 <http://requirejs.org/docs/download.html>`_ 

86 release. 

87 

88 @param to where to unzip the files 

89 @param fLOG logging function 

90 @param location location of require.js release 

91 @param clean clean unnecessary files 

92 @return list of downloaded and unzipped files 

93 

94 *require.js* can be locally obtained 

95 if :epkg:`notebook` is installed. 

96 """ 

97 if location is None: 

98 from notebook import __file__ as local_location 

99 dirname = os.path.dirname(local_location) 

100 locations = [ 

101 os.path.join( 

102 dirname, "static", "components", "requirejs", "require.js"), 

103 os.path.join( 

104 dirname, "..", "nbclassic", "static", "components", "requirejs", 

105 "require.js"), 

106 os.path.join(os.path.dirname(__file__), "require.js")] 

107 elocations = [loc for loc in locations if os.path.exists(loc)] 

108 if len(elocations) == 0: 

109 raise FileNotFoundError( # pragma: no cover 

110 f"Unable to find requirejs in '{locations}'") 

111 location = elocations[0] 

112 shutil.copy(location, to) 

113 return [os.path.join(to, "require.js")] 

114 else: 

115 link = location 

116 try: 

117 page = read_url(link, encoding="utf8") 

118 except ReadUrlException: # pragma: no cover 

119 if fLOG: 

120 fLOG( 

121 f"[download_requirejs] unable to read '{location}'") 

122 return download_requirejs(to=to, fLOG=fLOG, location=None, clean=clean) 

123 

124 reg = re.compile("href=\\\"(.*?minified/require[.]js)\\\"") 

125 alls = reg.findall(page) 

126 if len(alls) == 0: 

127 raise RuntimeError( # pragma: no cover 

128 f"Unable to find a link on require.js file on page {page!r}.") 

129 

130 filename = alls[0] 

131 

132 try: 

133 local = download(filename, to, fLOG=fLOG) 

134 except ReadUrlException as e: # pragma: no cover 

135 # We implement a backup plan. 

136 new_filename = "http://www.xavierdupre.fr/enseignement/setup/require.js/2.3.6/require.js" 

137 try: 

138 local = download(new_filename, to, fLOG=fLOG) 

139 except ReadUrlException: 

140 raise ReadUrlException("Unable to download '{0}' or '{1}'".format( 

141 filename, new_filename)) from e 

142 

143 fLOG("[download_requirejs] local file", local) 

144 return [local]