Refactoring.

This commit is contained in:
Achim D. Brucker 2017-07-29 09:46:28 +01:00
parent 10cce2859d
commit b68eb42f96
1 changed files with 22 additions and 30 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
#
# Copyright (C) 2017 The University of Sheffield, UK
#
#
# 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
@ -13,31 +13,26 @@
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
"""Tool for extracting crx file from a tar archive."""
import os
import sys
import glob
import re
import requests
from time import sleep
from random import randint
import getopt
import tarfile
import datetime
from ExtensionCrawler.discover import *
from ExtensionCrawler.archive import *
from ExtensionCrawler.util import *
from ExtensionCrawler.discover import *
import dateutil
import dateutil.parser
import time
import getopt
from ExtensionCrawler.archive import last_crx, get_local_archive_dir
# Script should run with python 3.4 or 3.5
assert sys.version_info >= (3, 4) and sys.version_info < (3, 6)
def help():
def helpmsg():
"""Print help message."""
print("extract-crx [OPTION] extid")
print(" -h print this help text")
print(" -s silent (no log messages)")
@ -47,21 +42,20 @@ def help():
def main(argv):
today = datetime.datetime.now(datetime.timezone.utc).isoformat()
"""Main function of the extension crawler."""
basedir = "archive"
verbose = True
date = None
extid = ""
outputds = ""
discover = False
output = ""
try:
opts, args = getopt.getopt(argv, "hsd:a:o:", ["date=","archive=","output="])
opts, args = getopt.getopt(argv, "hsd:a:o:", ["date=", "archive=", "output="])
except getopt.GetoptError:
help()
helpmsg()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
help()
helpmsg()
sys.exit()
elif opt in ("-a", "--archive"):
basedir = arg
@ -71,8 +65,6 @@ def main(argv):
output = arg
elif opt == '-s':
verbose = False
elif opt == '-d':
discover = True
if len(args) > 0:
extid = args[0]
@ -81,10 +73,10 @@ def main(argv):
sys.exit()
if date is not None:
d = dateutil.parser.parse(date)
if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
d = d.replace(tzinfo=datetime.timezone.utc)
last = last_crx(os.path.join(basedir, "data"), extid, d)
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 = last_crx(os.path.join(basedir, "data"), extid, dateobj)
else:
last = last_crx(os.path.join(basedir, "data"), extid)
@ -92,10 +84,10 @@ def main(argv):
tar = os.path.join(basedir, "data",
get_local_archive_dir(extid), extid + ".tar")
if os.path.exists(tar):
t = tarfile.open(tar, 'r')
if verbose:
print("Extracting "+os.path.join(output,last))
t.extract(last,output)
print("Extracting "+os.path.join(output, last))
with tarfile.open(tar, 'r') as archive:
archive.extract(last, output)
if __name__ == "__main__":
main(sys.argv[1:])