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