ExtensionCrawler/crx-extract

138 lines
4.6 KiB
Plaintext
Raw Permalink Normal View History

2019-01-23 00:01:00 +00:00
#!/usr/bin/env python3.7
#
2018-09-01 20:48:14 +00:00
# Copyright (C) 2017-2018 The University of Sheffield, UK
2017-07-29 08:46:28 +00:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2017-07-29 08:46:28 +00:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
2018-09-01 20:48:14 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
2017-07-29 08:46:28 +00:00
"""Tool for extracting crx file from a tar archive."""
import os
import sys
2018-09-01 20:47:19 +00:00
import glob
2017-07-29 08:46:28 +00:00
import getopt
import tarfile
import datetime
import dateutil
import dateutil.parser
from ExtensionCrawler.archive import last_crx, get_local_archive_dir
2017-11-04 19:27:27 +00:00
from ExtensionCrawler.config import const_basedir
2017-07-29 08:46:28 +00:00
def helpmsg():
"""Print help message."""
2017-08-29 21:06:18 +00:00
print("crx-extract [OPTION] extid")
print(" -h print this help text")
print(" -s silent (no log messages)")
2017-08-18 12:09:35 +00:00
print(" -e use etag instead of date in outoput")
print(" -w avoid ':' in filenames (useful on Windows)")
print(" -d=<DATE> date")
print(" -o=<DIR> output directory")
print(" -a=<DIR> archive directory")
2017-11-04 19:27:27 +00:00
def get_tarinfo(members, name, winfs=False, etag=None):
2017-08-16 07:13:32 +00:00
"""Select tarinfo object with a specified path/name."""
for tarinfo in members:
2017-08-16 07:13:32 +00:00
if tarinfo.name == name:
if winfs:
2017-11-04 19:27:27 +00:00
tarinfo.name = name.replace(":", "-")
if etag is not None:
(path, crx) = os.path.split(tarinfo.name)
(path, _) = os.path.split(path)
2017-11-04 19:27:27 +00:00
tarinfo.name = os.path.join(path, etag, crx)
yield tarinfo
def main(argv):
2017-07-29 08:46:28 +00:00
"""Main function of the extension crawler."""
basedir = const_basedir()
verbose = True
date = None
useetag = False
2017-07-29 08:46:28 +00:00
output = ""
winfs = False
try:
2017-11-04 19:27:27 +00:00
opts, args = getopt.getopt(argv, "hsed:a:o:w",
["date=", "archive=", "output="])
except getopt.GetoptError:
2017-07-29 08:46:28 +00:00
helpmsg()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
2017-07-29 08:46:28 +00:00
helpmsg()
sys.exit()
elif opt in ("-a", "--archive"):
basedir = arg
elif opt in ("-d", "--date"):
date = arg
elif opt in ("-o", "--output"):
output = arg
elif opt in ("-w", "--winfs"):
winfs = True
elif opt in ("-e", "--etag"):
useetag = True
elif opt == '-s':
verbose = False
if len(args) > 0:
extid = args[0]
else:
helpmsg()
sys.exit()
if date is not None:
2017-07-29 08:46:28 +00:00
dateobj = dateutil.parser.parse(date)
if dateobj.tzinfo is None or dateobj.tzinfo.utcoffset(dateobj) is None:
dateobj = dateobj.replace(tzinfo=datetime.timezone.utc)
last, etag = last_crx(os.path.join(basedir, "data"), extid, dateobj)
else:
last, etag = last_crx(os.path.join(basedir, "data"), extid)
if not useetag:
2017-11-04 19:27:27 +00:00
etag = None
2018-09-01 20:47:19 +00:00
basetar = os.path.join(basedir, "data",
get_local_archive_dir(extid), extid)
tar = basetar+".tar"
if last != "":
if os.path.exists(tar):
2018-09-01 20:47:19 +00:00
files = None
if verbose:
2018-09-01 20:47:19 +00:00
print("Extracting " + os.path.join(output, last) + " from " + tar)
2017-07-29 08:46:28 +00:00
with tarfile.open(tar, 'r') as archive:
2018-09-01 20:47:19 +00:00
files = archive.extractall(
path=output,
members=get_tarinfo(archive, last, winfs, etag))
archivetars = sorted(glob.glob(basetar+".[0-9][0-9][0-9].tar.xz"))
while (not files and archivetars):
tar = archivetars.pop()
if verbose:
print("Extracting " + os.path.join(output, last) + " from " + tar)
with tarfile.open(tar, 'r:xz') as archive:
files = archive.extractall(
path=output,
members=get_tarinfo(archive, last, winfs, etag))
elif verbose:
print("Cannot find archive " + tar)
elif verbose:
if os.path.exists(tar):
print("CRX not in archive" + tar)
else:
print("CRX does not exist: cannot find archive " + tar)
2017-07-29 08:46:28 +00:00
2017-11-04 19:27:27 +00:00
if __name__ == "__main__":
main(sys.argv[1:])