Subversion Repositories basico

Rev

Rev 57 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: utils.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Generic functions service
7
 
8
import os
32 t00mlabs 9
import sys
10
import subprocess
26 t00mlabs 11
import tarfile
12
import zipfile
53 t00mlabs 13
import shutil
26 t00mlabs 14
import urllib.request
15
import requests
40 t00mlabs 16
import webbrowser
38 t00mlabs 17
import feedparser
18
 
26 t00mlabs 19
from .service import Service
20
 
21
class Utils(Service):
22
    def initialize(self):
23
        self.uas = []
24
 
32 t00mlabs 25
 
40 t00mlabs 26
    def browse(self, lurl):
27
        if sys.platform in ['linux', 'linux2']:
28
            browser = webbrowser.get('firefox')
29
        elif sys.platform == 'win32':
30
            browser = webbrowser.get('windows-default')
31
 
32
        for url in lurl:
33
            self.log.debug("Browsing URL: %s" % url)
34
            browser.open_new_tab(url)
35
 
36
 
32 t00mlabs 37
    def which(self, program):
36 t00mlabs 38
        if sys.platform == 'win32':
39
            program = program + '.exe'
40
 
32 t00mlabs 41
        def is_exe(fpath):
42
            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
43
 
44
        fpath, fname = os.path.split(program)
45
        if fpath:
46
            if is_exe(program):
47
                return program
48
        else:
49
            for path in os.environ["PATH"].split(os.pathsep):
50
                path = path.strip('"')
51
                exe_file = os.path.join(path, program)
52
                if is_exe(exe_file):
53
                    return exe_file
54
 
55
        return None
56
 
26 t00mlabs 57
 
32 t00mlabs 58
    def install_geckodriver(self):
59
        """Get last version of Gecko webdriver from github"""
60
        self.log.debug("OS Platform: %s" % sys.platform)
61
        if sys.platform in ['linux', 'linux2']:
62
            GECKO_SOURCE = "https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-linux64.tar.gz"
36 t00mlabs 63
            GECKO_TARGET = self.get_var('TMP', scope='local') + 'gecko.tar.gz'
32 t00mlabs 64
        elif sys.platform == 'win32':
36 t00mlabs 65
            GECKO_SOURCE = "https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-win64.zip"
66
            GECKO_TARGET = self.get_var('TMP', scope='local') + 'gecko.zip'
32 t00mlabs 67
 
35 t00mlabs 68
        GECKO_INSTALL_DIR = self.get_var('DRIVERS', 'local')
32 t00mlabs 69
 
36 t00mlabs 70
 
32 t00mlabs 71
        if os.path.exists(GECKO_TARGET):
72
            self.log.debug("Gecko webdriver already downloaded")
73
            downloaded = True
74
        else:
75
            downloaded = self.download('Gecko', GECKO_SOURCE, GECKO_TARGET)
76
 
77
        if downloaded:
36 t00mlabs 78
            if sys.platform in ['linux', 'linux2']:
79
                extracted = self.extract(GECKO_TARGET, GECKO_INSTALL_DIR, 'tar.gz')
80
            elif sys.platform == 'win32':
81
                extracted = self.extract(GECKO_TARGET, GECKO_INSTALL_DIR, 'zip')
32 t00mlabs 82
            if extracted:
83
                self.log.debug("Gecko webdriver deployed successfully")
84
            else:
85
                self.log.error("Gecko could not be deployed")
86
                self.log.error("Tip: maybe %s is corrupt. Delete it" % GECKO_TARGET)
87
                #FIXME: stop application gracefully
88
                exit(-1)
89
 
90
 
28 t00mlabs 91
    def download(self, prgname, source, target):
26 t00mlabs 92
        try:
28 t00mlabs 93
            self.log.debug ("Downloading %s from: %s" % (prgname, source))
26 t00mlabs 94
            response = requests.get(source, stream=True)
95
            with open(target, 'wb') as out_file:
96
                shutil.copyfileobj(response.raw, out_file)
97
            del response
28 t00mlabs 98
            self.log.debug ("%s downloaded to %s" % (prgname, target))
26 t00mlabs 99
            return True
100
        except Exception as error:
101
            self.log.error(error)
102
            return False
103
 
28 t00mlabs 104
 
26 t00mlabs 105
    def extract(self, filename, target_path, protocol):
39 t00mlabs 106
        self.log.debug("Extracting %s to %s using protocol %s" % (filename, target_path, protocol))
32 t00mlabs 107
        if protocol in ['tar.gz', 'bz2']:
108
            try:
109
                tar = tarfile.open(filename, "r:*")
110
                tar.extractall(target_path)
111
                tar.close()
112
                self.log.debug("Extracted successfully")
113
                return True
114
            except Exception as error:
115
                self.log.error(error)
116
                return False
117
        elif protocol == 'zip':
118
            try:
119
                self.unzip(filename, target_path)
120
                self.log.debug("Extracted successfully")
121
                return True
122
            except Exception as error:
123
                self.log.error(error)
124
                return False
26 t00mlabs 125
 
53 t00mlabs 126
    def zip(self, filename, directory):
127
        # http://stackoverflow.com/a/25650295
57 t00mlabs 128
        #~ make_archive(archive_name, 'gztar', root_dir)
129
        res = shutil.make_archive(filename, 'gztar', directory)
130
        self.log.debug("%s - %s" % (filename, directory))
131
        self.log.debug("zip res: %s" % res)
26 t00mlabs 132
 
53 t00mlabs 133
 
26 t00mlabs 134
    def unzip(self, target, install_dir):
135
        zip_archive = zipfile.ZipFile(target, "r")
136
        zip_archive.extractall(path=install_dir)
137
        zip_archive.close()
138
 
139
 
36 t00mlabs 140
    def get_firefox_profile_dir(self):
141
        if sys.platform in ['linux', 'linux2']:
142
            cmd = "ls -d /home/$USER/.mozilla/firefox/*.default/"
143
            p = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
144
            FF_PRF_DIR = p.communicate()[0][0:-2]
37 t00mlabs 145
            FF_PRF_DIR_DEFAULT = str(FF_PRF_DIR,'utf-8')
36 t00mlabs 146
        elif sys.platform == 'win32':
147
            import glob
148
            APPDATA = os.getenv('APPDATA')
149
            FF_PRF_DIR = "%s\\Mozilla\\Firefox\\Profiles\\" % APPDATA
150
            PATTERN = FF_PRF_DIR + "*default*"
151
            FF_PRF_DIR_DEFAULT = glob.glob(PATTERN)[0]
26 t00mlabs 152
 
37 t00mlabs 153
        return FF_PRF_DIR_DEFAULT
26 t00mlabs 154
 
155
 
38 t00mlabs 156
    def feedparser_parse(self, thing):
157
        try:
158
            return feedparser.parse(thing)
159
        except TypeError:
160
            if 'drv_libxml2' in feedparser.PREFERRED_XML_PARSERS:
161
                feedparser.PREFERRED_XML_PARSERS.remove('drv_libxml2')
162
                return feedparser.parse(thing)
163
            else:
164
                self.log.error(self.get_traceback())
165
                return None