This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
@brief test log(time=20s) | ||
""" | ||
|
||
import sys, os, unittest | ||
|
||
try : | ||
import src | ||
except ImportError : | ||
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", ".."))) | ||
if path not in sys.path : sys.path.append (path) | ||
import src | ||
|
||
try : | ||
import pyquickhelper | ||
except ImportError : | ||
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", "..", "..","pyquickhelper", "src"))) | ||
if path not in sys.path : sys.path.append (path) | ||
import pyquickhelper | ||
|
||
|
||
from src.pymyinstall.installhelper.install_custom import install_pandoc | ||
from pyquickhelper import fLOG | ||
|
||
class TestPandoc (unittest.TestCase): | ||
|
||
def test_pandoc(self) : | ||
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__") | ||
fold = os.path.abspath(os.path.split(__file__)[0]) | ||
temp = os.path.join(fold,"temp_pandoc") | ||
if not os.path.exists(temp) : os.mkdir(temp) | ||
for _ in os.listdir(temp): | ||
if ".msi" in _ : | ||
os.remove(os.path.join(temp,_)) | ||
r = install_pandoc (temp_folder = temp, fLOG = fLOG, install = False) | ||
assert os.path.exists(r) | ||
|
||
|
||
|
||
|
||
if __name__ == "__main__" : | ||
unittest.main () |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# coding: latin-1 | ||
""" | ||
@file | ||
@brief Various function to install some application such as `pandoc <http://johnmacfarlane.net/pandoc/>`_. | ||
""" | ||
import sys, re, platform, os, urllib, urllib.request, imp, zipfile,time, subprocess | ||
|
||
from .install_cmd import run_cmd | ||
|
||
def download_page(url): | ||
""" | ||
download a page from a url | ||
@param url url | ||
@return content | ||
""" | ||
try : | ||
req = urllib.request.Request(url, headers= { 'User-agent': 'Mozilla/5.0' }) | ||
u = urllib.request.urlopen(req) | ||
text = u.read() | ||
u.close() | ||
except urllib.error.HTTPError as e : | ||
raise Exception("unable to get archive from: " + zipurl) from e | ||
|
||
return str(text, encoding="utf8") | ||
|
||
def download_file(url, outfile): | ||
""" | ||
download a file from a url, the function does not download the file | ||
again if outfile already exists | ||
@param url url | ||
@param outfile outfile | ||
@return outfile | ||
""" | ||
if os.path.exists(outfile): | ||
return outfile | ||
|
||
try : | ||
req = urllib.request.Request(url, headers= { 'User-agent': 'Mozilla/5.0' }) | ||
u = urllib.request.urlopen(req) | ||
text = u.read() | ||
u.close() | ||
except urllib.error.HTTPError as e : | ||
raise Exception("unable to get archive from: " + url) from e | ||
|
||
with open(outfile,"wb") as f : | ||
f.write(text) | ||
|
||
return outfile | ||
|
||
def install_pandoc(temp_folder=".", fLOG = print, install = True): | ||
""" | ||
install `pandoc <http://johnmacfarlane.net/pandoc/>`_ | ||
@param temp_folder where to download the setup | ||
@param fLOG logging function | ||
@param install install (otherwise only download) | ||
@return temporary file | ||
""" | ||
link = "https://github.com/jgm/pandoc/releases/latest" | ||
page = download_page(link) | ||
if sys.platform.startswith("win"): | ||
reg = re.compile("href=\\\"(.*?[.]msi)\\\"") | ||
all = reg.findall(page) | ||
if len(all) == 0 : | ||
raise Exception("unable to find a link on a .msi file on page: " + url) | ||
|
||
file = all[0].split("/")[-1] | ||
filel = "https://github.com/jgm/pandoc/releases/download/{0}/pandoc-{1}-windows.msi" | ||
version = file.replace("pandoc-","").replace("-windows.msi","") | ||
fLOG("pandoc, version ", version) | ||
vershort = version.split("-")[0] | ||
full = filel.format(vershort, version) | ||
outfile = os.path.join( temp_folder, full.split("/")[-1]) | ||
fLOG("download ", full) | ||
local = download_file(full, outfile) | ||
if install : | ||
run_cmd("msiexec /i " + local,fLOG=fLOG,wait=True) | ||
return local | ||
else: | ||
raise NotImplementedError("not available on platform " + sys.platform) | ||
|