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 Various function to install some application such as :epkg:`pandoc`.
4"""
5from __future__ import print_function
6import re
7import os
8import shutil
10from ..filehelper import download, read_url
11from ..filehelper.internet_helper import ReadUrlException
12from ..filehelper.compression_helper import unzip_files
13from ..filehelper.synchelper import explore_folder_iterfile
16def download_revealjs(temp_folder=".", unzip_to=".", fLOG=print,
17 location="https://github.com/hakimel/reveal.js/releases",
18 clean=True):
19 """
20 Downloads :epkg:`reveal.js` release and unzips it.
22 @param temp_folder where to download the setup
23 @param unzip_to where to unzip the files
24 @param fLOG logging function
25 @param location location of reveal.js release
26 @param clean clean unnecessary files
27 @return list of downloaded and unzipped files
28 """
29 link = location
30 page = read_url(link, encoding="utf8")
31 reg = re.compile("href=\\\"(.*?[0-9.]+?[.]zip)\\\"")
32 alls = reg.findall(page)
33 if len(alls) == 0:
34 raise Exception(
35 "unable to find a link on a .zip file on page: " +
36 page)
38 filename = alls[0].split("/")[-1]
39 filel = location.replace("releases", "").rstrip(
40 '/') + "/archive/{0}".format(filename)
41 outfile = os.path.join(temp_folder, "reveal.js." + filename)
42 fLOG("download ", filel, "to", outfile)
43 local = download(filel, temp_folder, fLOG=fLOG)
44 fLOG("local file", local)
45 unzip_files(local, where_to=unzip_to)
47 # we rename
48 sub = [_ for _ in os.listdir(unzip_to) if "reveal" in _]
49 if len(sub) != 1:
50 raise FileNotFoundError(
51 "several version exists or noe of them:\n{0}".format(", ".join(sub)))
52 sub = sub[0]
53 master = os.path.join(unzip_to, sub)
54 if not os.path.exists(master):
55 raise FileNotFoundError("unable to find: " + master)
56 new_master = os.path.join(unzip_to, "reveal.js")
57 os.rename(master, new_master)
59 if clean:
60 # we clean some files
61 res = explore_folder_iterfile(new_master)
62 keep = []
63 for r in res:
64 if os.path.isdir(r):
65 continue
66 if (".gitignore" in r or ".travis.yml" in r or "index.html" in r or
67 ".appveyor.yml" in r or "requirement" in r or "README" in r or
68 "CONTRIBUTING.md" in r):
69 os.remove(r)
70 elif "/test/" in r.replace("\\", "/"):
71 os.remove(r)
72 else:
73 keep.append(r)
75 # we clean the downloaded file
76 os.remove(local)
78 return keep
81def download_requirejs(to=".", fLOG=print,
82 location="http://requirejs.org/docs/download.html",
83 clean=True):
84 """
85 Downloads `require.js
86 <http://requirejs.org/docs/download.html>`_
87 release.
89 @param to where to unzip the files
90 @param fLOG logging function
91 @param location location of require.js release
92 @param clean clean unnecessary files
93 @return list of downloaded and unzipped files
95 *require.js* can be locally obtained
96 if :epkg:`notebook` is installed.
97 """
98 if location is None:
99 from notebook import __file__ as local_location
100 dirname = os.path.dirname(local_location)
101 location = os.path.join(
102 dirname, "static", "components", "requirejs", "require.js")
103 if not os.path.exists(location):
104 raise FileNotFoundError(
105 "Unable to find requirejs in '{0}'".format(location))
106 shutil.copy(location, to)
107 return [os.path.join(to, "require.js")]
108 else:
109 link = location
110 try:
111 page = read_url(link, encoding="utf8")
112 except ReadUrlException:
113 if fLOG:
114 fLOG(
115 "[download_requirejs] unable to read '{0}'".format(location))
116 return download_requirejs(to=to, fLOG=fLOG, location=None, clean=clean)
118 reg = re.compile("href=\\\"(.*?minified/require[.]js)\\\"")
119 alls = reg.findall(page)
120 if len(alls) == 0:
121 raise Exception(
122 "unable to find a link on require.js file on page: " +
123 page)
125 filename = alls[0]
127 try:
128 local = download(filename, to, fLOG=fLOG)
129 except ReadUrlException as e:
130 # We implement a backup plan.
131 new_filename = "http://www.xavierdupre.fr/enseignement/setup/require.js/2.3.6/require.js"
132 try:
133 local = download(new_filename, to, fLOG=fLOG)
134 except ReadUrlException:
135 raise ReadUrlException("Unable to download '{0}' or '{1}'".format(
136 filename, new_filename)) from e
138 fLOG("[download_requirejs] local file", local)
139 return [local]