Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
add a function to install pandoc
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Jun 4, 2014
1 parent 009af11 commit a6b88b3
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
42 changes: 42 additions & 0 deletions _unittests/ut_custom_install/test_pandoc.py
@@ -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 ()
83 changes: 83 additions & 0 deletions src/pymyinstall/installhelper/install_custom.py
@@ -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)

0 comments on commit a6b88b3

Please sign in to comment.