forked from yahuarkuntur/gdimm2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdater.py
144 lines (101 loc) · 4.31 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
###
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
#
# 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
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
###
import urllib2
import os, glob
import shutil
from zipfile import ZipFile
from distutils import dir_util
from time import strptime, strftime, mktime
from datetime import date, datetime
from lxml import etree
""" Actualizador del sistema. """
class gDimmUpdater:
download_url = 'http://descargas.sri.gov.ec/download/declaraciones/ActuDimm.zip'
download_dest_file_path = os.path.join('updates', 'update.zip')
unzipped_dest_path = os.path.join('updates', 'update')
lastupdate_xml_file = os.path.join('updates', 'last_update.xml')
last_update = None
def __init__(self):
pass
def need_update(self):
""" Verifica si es necesario actualizar el sistema """
remotefile = urllib2.urlopen(self.download_url)
file_info = remotefile.info()
last_mod = file_info['Last-Modified']
date_object = strptime(last_mod, "%a, %d %b %Y %H:%M:%S GMT")
remote_file_date = date(date_object.tm_year, date_object.tm_mon, date_object.tm_mday).isoformat()
if not os.path.isfile(self.lastupdate_xml_file):
self._create_last_update_file(str(date.today()))
parser = etree.XMLParser(remove_comments=True, encoding='utf8')
xml = etree.parse(self.lastupdate_xml_file, parser)
root = xml.getroot()
last_update = root.attrib.get('last_update')
if remote_file_date != last_update:
print remote_file_date, '!=', last_update, 'update needed!'
self.last_update = remote_file_date
return True
return False
def download(self):
""" Descarga el archivo de actualizacion """
if os.path.isfile(self.download_dest_file_path):
os.remove(self.download_dest_file_path)
mysock = urllib2.urlopen(self.download_url)
fileToSave = mysock.read()
oFile = open(self.download_dest_file_path, 'wb')
oFile.write(fileToSave)
oFile.close()
def unzip(self):
""" Descomprime los archivos """
zipfile = ZipFile(self.download_dest_file_path)
if not os.path.isdir(self.unzipped_dest_path):
os.makedirs(self.unzipped_dest_path, mode=0700)
zipfile.extractall(self.unzipped_dest_path)
def copy_files(self):
""" Reemplaza los nuevos archivos XML """
dir_util.copy_tree(os.path.join(self.unzipped_dest_path, 'XML'), os.path.join('XML'))
dir_util.copy_tree(os.path.join(self.unzipped_dest_path, 'XSL'), os.path.join('XSL'))
shutil.rmtree(self.unzipped_dest_path)
# ajustar las extensiones .XML --> .xml
self._update_extensions()
def update(self):
""" Ejecuta la actualizacion de los XML """
if not self.need_update():
return
self.download()
self.unzip()
self.copy_files()
self._create_last_update_file()
def _create_last_update_file(self, update_date=None):
""" Crea el archivo de referencia con la ultima actualizacion realizada """
if update_date is None:
update_date = self.last_update
root = etree.Element("root")
root.set('last_update', update_date)
f = open(self.lastupdate_xml_file, 'w+')
f.write(etree.tostring(root, encoding='utf8', pretty_print=True))
f.close()
def _update_extensions(self):
files = glob.glob(os.path.join('XML', '*.XML'))
files += glob.glob(os.path.join('XSL', '*.XML'))
for filepath in files:
(root, ext) = os.path.splitext(filepath)
os.rename(root + ext, root + ext.lower())
# tests
if __name__ == '__main__':
updater = gDimmUpdater()
updater.update()