1#!/usr/bin/python
2
3import urllib2
4import os
5import sys
6from subprocess import call
7
8CHROMIUM_VERSION_TRACKING_URL = "https://omahaproxy.appspot.com/all"
9CHROMIUM_BUILD_TYPE = "stable"
10CHROMIUM_OS = "android"
11
12CHROMIUM_SOURCE_URL = "https://chromium.googlesource.com/chromium/src/+/refs/tags"
13CHROMIUM_DEPS_FILE = "DEPS"
14
15PDFIUM_GIT_REPO = "https://pdfium.googlesource.com/pdfium.git"
16
17MAKE_FILES = ["third_party/pdfiumopenjpeg.mk",
18              "third_party/pdfiumlcms.mk",
19              "third_party/pdfiumjpeg.mk",
20              "third_party/pdfiumagg23.mk",
21              "third_party/pdfiumzlib.mk",
22              "third_party/pdfiumbigint.mk",
23              "third_party/Android.mk",
24              "core/pdfiumfpdftext.mk",
25              "core/pdfiumfpdfdoc.mk",
26              "core/pdfiumfdrm.mk",
27              "core/pdfiumfxcodec.mk",
28              "core/pdfiumfpdfapi.mk",
29              "core/pdfiumfxcrt.mk",
30              "core/pdfiumfxge.mk",
31              "core/Android.mk",
32              "fpdfsdk/pdfiumjavascript.mk",
33              "fpdfsdk/pdfiumformfiller.mk",
34              "fpdfsdk/pdfiumfxedit.mk",
35              "fpdfsdk/pdfiumpdfwindow.mk",
36              "fpdfsdk/pdfium.mk",
37              "fpdfsdk/Android.mk"]
38
39COPY_FILES = [os.path.basename(__file__), ".git", "MODULE_LICENSE_BSD", "NOTICE"] + MAKE_FILES
40REMOVE_FILES = [os.path.basename(__file__), ".git", ".gitignore"]
41
42def getStableChromiumVersion():
43   """ :return the latest chromium version """
44
45   chromiumVersions = urllib2.urlopen(CHROMIUM_VERSION_TRACKING_URL)
46
47   for chromiumVersionStr in chromiumVersions.read().split("\n"):
48       chromiumVersion = chromiumVersionStr.split(",")
49
50       if chromiumVersion[0] == CHROMIUM_OS and chromiumVersion[1] == CHROMIUM_BUILD_TYPE:
51           return chromiumVersion[2]
52
53   raise Exception("Could not find latest %s chromium version for %s at %s"
54                   % (CHROMIUM_BUILD_TYPE, CHROMIUM_OS, CHROMIUM_VERSION_TRACKING_URL))
55
56
57def getPdfiumRevision():
58    """ :return the pdfium version used by the latest chromium version """
59
60    try:
61        deps = urllib2.urlopen("%s/%s/%s" % (CHROMIUM_SOURCE_URL, getStableChromiumVersion(),
62                                             CHROMIUM_DEPS_FILE))
63
64        # I seem to not be able to get the raw file, hence grep the html file
65        return deps.read().split("pdfium_revision&")[1].split("'")[1]
66    except Exception as e:
67        raise Exception("Could not extract pdfium revision from %s/%s/%s: %s"
68                       % (CHROMIUM_SOURCE_URL, getStableChromiumVersion(), CHROMIUM_DEPS_FILE, e))
69
70
71def downloadPdfium(newDir, rev):
72    """ Download the newest version of pdfium to the new directory
73
74    :param newDir: The new files
75    :param rev: The revision to change to
76    """
77
78    call(["git", "clone", PDFIUM_GIT_REPO, newDir])
79    os.chdir(newDir)
80    call(["git", "reset", "--hard", rev])
81
82
83def removeFiles(newDir):
84    """ Remove files that should not be checked in from the original download
85
86    :param newDir: The new files
87    """
88
89    for fileName in REMOVE_FILES:
90        call(["rm", "-rf", os.path.join(newDir, fileName)])
91
92
93def copyFiles(currentDir, newDir):
94    """ Copy files needed to make pdfium work with android
95
96    :param currentDir: The current files
97    :param newDir: The new files
98    """
99
100    for fileName in COPY_FILES:
101        call(["cp", "-r", os.path.join(currentDir, fileName), os.path.join(newDir, fileName)])
102
103
104def exchange(currentDir, newDir, oldDir):
105    """ Update current to new and save current in old.
106
107    :param currentDir: The current files
108    :param newDir: The new files
109    :param oldDir: The old files
110    """
111
112    call(["mv", currentDir, oldDir])
113    call(["mv", newDir, currentDir])
114
115
116if __name__ == "__main__":
117   rev = getPdfiumRevision()
118   targetDir = os.path.dirname(os.path.realpath(__file__))
119   newDir = targetDir + ".new"
120   oldDir = targetDir + ".old"
121
122   try:
123       downloadPdfium(newDir, rev)
124       removeFiles(newDir)
125       copyFiles(targetDir, newDir)
126       exchange(targetDir, newDir, oldDir)
127       print("Updated pdfium to " + rev + ". Old files are in " + oldDir + ". Please verify if "
128             "build files need to be updated.")
129
130       sys.exit(0)
131   except:
132       call(["rm", "-rf", newDir])
133       sys.exit(1)
134
135