Subversion Repositories basico

Rev

Rev 207 | Go to most recent revision | 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
207 t00m 18
from datetime import datetime
38 t00mlabs 19
 
26 t00mlabs 20
from .service import Service
21
 
22
class Utils(Service):
23
    def initialize(self):
24
        self.uas = []
25
 
207 t00m 26
    def timestamp(self):
27
        now = datetime.now()
217 t00m 28
        timestamp = "%4d%02d%02d_%02d%02d%02d" % (now.year, now.month, now.day, now.hour, now.minute, now.second)
32 t00mlabs 29
 
207 t00m 30
        return timestamp
31
 
32
 
33
    def get_datetime(self, timestamp):
217 t00m 34
        adate = datetime.strptime(timestamp, "%Y%m%d_%H%M%S")
207 t00m 35
        return adate
36
 
37
 
38
    def fuzzy_date_from_timestamp(self, timestamp):
39
        # ~ date = self.get_datetime(timestamp)
40
        d1 = self.get_datetime(timestamp)
41
        d2 = datetime.now()
42
        rdate = d2 - d1 # DateTimeDelta
43
        if int(rdate.days) > 0:
44
            if (rdate.days >= 365):
45
                return "%d years ago" % int((rdate.days/365))
46
            else:
47
                return "%d days ago" % int(rdate.days)
48
 
49
        hours = rdate.seconds / 3600
50
        if int(hours) > 0:
51
            return "%d hours ago" % int(hours)
52
 
53
        minutes = rdate.seconds / 60
54
        if int(minutes) > 0:
55
            return "%d minutes ago" % int(minutes)
56
 
57
        if int(rdate.seconds) > 0:
58
            return "%d seconds ago" % int(rdate.seconds)
59
 
60
 
174 t00m 61
    def browse(self, url):
40 t00mlabs 62
        if sys.platform in ['linux', 'linux2']:
63
            browser = webbrowser.get('firefox')
64
        elif sys.platform == 'win32':
65
            browser = webbrowser.get('windows-default')
66
 
174 t00m 67
        browser.open_new_tab(url)
40 t00mlabs 68
 
69
 
32 t00mlabs 70
    def which(self, program):
36 t00mlabs 71
        if sys.platform == 'win32':
72
            program = program + '.exe'
73
 
32 t00mlabs 74
        def is_exe(fpath):
75
            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
76
 
77
        fpath, fname = os.path.split(program)
78
        if fpath:
79
            if is_exe(program):
80
                return program
81
        else:
82
            for path in os.environ["PATH"].split(os.pathsep):
83
                path = path.strip('"')
84
                exe_file = os.path.join(path, program)
85
                if is_exe(exe_file):
86
                    return exe_file
87
 
88
        return None
89
 
26 t00mlabs 90
 
32 t00mlabs 91
    def install_geckodriver(self):
92
        """Get last version of Gecko webdriver from github"""
133 t00mlabs 93
        self.debug("OS Platform: %s" % sys.platform)
32 t00mlabs 94
        if sys.platform in ['linux', 'linux2']:
145 t00mlabs 95
            GECKO_SOURCE = "https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz"
36 t00mlabs 96
            GECKO_TARGET = self.get_var('TMP', scope='local') + 'gecko.tar.gz'
32 t00mlabs 97
        elif sys.platform == 'win32':
145 t00mlabs 98
            GECKO_SOURCE = "https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-win64.zip"
36 t00mlabs 99
            GECKO_TARGET = self.get_var('TMP', scope='local') + 'gecko.zip'
32 t00mlabs 100
 
35 t00mlabs 101
        GECKO_INSTALL_DIR = self.get_var('DRIVERS', 'local')
32 t00mlabs 102
 
36 t00mlabs 103
 
32 t00mlabs 104
        if os.path.exists(GECKO_TARGET):
133 t00mlabs 105
            self.debug("Gecko webdriver already downloaded")
32 t00mlabs 106
            downloaded = True
107
        else:
108
            downloaded = self.download('Gecko', GECKO_SOURCE, GECKO_TARGET)
109
 
110
        if downloaded:
36 t00mlabs 111
            if sys.platform in ['linux', 'linux2']:
112
                extracted = self.extract(GECKO_TARGET, GECKO_INSTALL_DIR, 'tar.gz')
113
            elif sys.platform == 'win32':
114
                extracted = self.extract(GECKO_TARGET, GECKO_INSTALL_DIR, 'zip')
32 t00mlabs 115
            if extracted:
133 t00mlabs 116
                self.debug("Gecko webdriver deployed successfully")
32 t00mlabs 117
            else:
118
                self.log.error("Gecko could not be deployed")
119
                self.log.error("Tip: maybe %s is corrupt. Delete it" % GECKO_TARGET)
120
                #FIXME: stop application gracefully
121
                exit(-1)
122
 
123
 
28 t00mlabs 124
    def download(self, prgname, source, target):
26 t00mlabs 125
        try:
133 t00mlabs 126
            self.debug ("Downloading %s from: %s" % (prgname, source))
26 t00mlabs 127
            response = requests.get(source, stream=True)
128
            with open(target, 'wb') as out_file:
129
                shutil.copyfileobj(response.raw, out_file)
130
            del response
133 t00mlabs 131
            self.debug ("%s downloaded to %s" % (prgname, target))
26 t00mlabs 132
            return True
133
        except Exception as error:
134
            self.log.error(error)
135
            return False
136
 
28 t00mlabs 137
 
26 t00mlabs 138
    def extract(self, filename, target_path, protocol):
133 t00mlabs 139
        self.debug("Extracting %s to %s using protocol %s" % (filename, target_path, protocol))
32 t00mlabs 140
        if protocol in ['tar.gz', 'bz2']:
141
            try:
142
                tar = tarfile.open(filename, "r:*")
143
                tar.extractall(target_path)
144
                tar.close()
133 t00mlabs 145
                self.debug("Extracted successfully")
32 t00mlabs 146
                return True
147
            except Exception as error:
148
                self.log.error(error)
149
                return False
150
        elif protocol == 'zip':
151
            try:
152
                self.unzip(filename, target_path)
133 t00mlabs 153
                self.debug("Extracted successfully")
32 t00mlabs 154
                return True
155
            except Exception as error:
156
                self.log.error(error)
157
                return False
26 t00mlabs 158
 
53 t00mlabs 159
    def zip(self, filename, directory):
160
        # http://stackoverflow.com/a/25650295
57 t00mlabs 161
        #~ make_archive(archive_name, 'gztar', root_dir)
162
        res = shutil.make_archive(filename, 'gztar', directory)
133 t00mlabs 163
        self.debug("%s - %s" % (filename, directory))
164
        self.debug("zip res: %s" % res)
26 t00mlabs 165
 
53 t00mlabs 166
 
26 t00mlabs 167
    def unzip(self, target, install_dir):
168
        zip_archive = zipfile.ZipFile(target, "r")
169
        zip_archive.extractall(path=install_dir)
170
        zip_archive.close()
171
 
172
 
36 t00mlabs 173
    def get_firefox_profile_dir(self):
174
        if sys.platform in ['linux', 'linux2']:
175
            cmd = "ls -d /home/$USER/.mozilla/firefox/*.default/"
176
            p = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
177
            FF_PRF_DIR = p.communicate()[0][0:-2]
37 t00mlabs 178
            FF_PRF_DIR_DEFAULT = str(FF_PRF_DIR,'utf-8')
36 t00mlabs 179
        elif sys.platform == 'win32':
180
            import glob
181
            APPDATA = os.getenv('APPDATA')
182
            FF_PRF_DIR = "%s\\Mozilla\\Firefox\\Profiles\\" % APPDATA
183
            PATTERN = FF_PRF_DIR + "*default*"
184
            FF_PRF_DIR_DEFAULT = glob.glob(PATTERN)[0]
26 t00mlabs 185
 
37 t00mlabs 186
        return FF_PRF_DIR_DEFAULT
26 t00mlabs 187
 
188
 
38 t00mlabs 189
    def feedparser_parse(self, thing):
190
        try:
191
            return feedparser.parse(thing)
192
        except TypeError:
193
            if 'drv_libxml2' in feedparser.PREFERRED_XML_PARSERS:
194
                feedparser.PREFERRED_XML_PARSERS.remove('drv_libxml2')
195
                return feedparser.parse(thing)
196
            else:
197
                self.log.error(self.get_traceback())
198
                return None