aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2010-07-14 13:48:41 -0300
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2010-07-14 13:48:41 -0300
commit5437c66e60a5ae5c42a508ece671bbe03f9a8722 (patch)
tree83512b1dbb88821f70634bea55432324954f374f /g_octave
parenta lot of fixes. looks like g-octave already works with py3k (diff)
downloadg-octave-5437c66e60a5ae5c42a508ece671bbe03f9a8722.tar.gz
g-octave-5437c66e60a5ae5c42a508ece671bbe03f9a8722.tar.bz2
g-octave-5437c66e60a5ae5c42a508ece671bbe03f9a8722.zip
fixed all the modules that deal with files, to work properly with py3k
Diffstat (limited to 'g_octave')
-rw-r--r--g_octave/compat.py15
-rw-r--r--g_octave/config.py2
-rw-r--r--g_octave/description.py1
-rw-r--r--g_octave/ebuild.py3
-rw-r--r--g_octave/fetch.py41
-rw-r--r--g_octave/overlay.py3
-rw-r--r--g_octave/package_manager.py1
7 files changed, 34 insertions, 32 deletions
diff --git a/g_octave/compat.py b/g_octave/compat.py
index 8c93f6f..aa45207 100644
--- a/g_octave/compat.py
+++ b/g_octave/compat.py
@@ -13,7 +13,7 @@
__all__ = [
'py3k',
- 'open_py3k',
+ 'open',
]
import codecs
@@ -21,11 +21,8 @@ import sys
py3k = sys.version_info >= (3, 0)
-#open = py3k and open or codecs.open
-
-def open_py3k(filename, mode, **kwargs):
- if 'encoding' not in kwargs:
- kwargs['encoding'] = 'utf-8'
- if py3k:
- return open(filename, mode, **kwargs)
- return codecs.open(filename, mode, **kwargs)
+def open(filename, mode='r', encoding='utf-8'):
+ try:
+ return codecs.open(filename, mode=mode, encoding=encoding)
+ except:
+ return codecs.open(filename, mode=mode, encoding='iso-8859-15')
diff --git a/g_octave/config.py b/g_octave/config.py
index 7265b7b..178f258 100644
--- a/g_octave/config.py
+++ b/g_octave/config.py
@@ -18,7 +18,7 @@ __all__ = ['Config']
import json
import os
-from .compat import py3k
+from .compat import py3k, open
from .exception import ConfigException
if py3k:
diff --git a/g_octave/description.py b/g_octave/description.py
index 6fa5f88..d3850ab 100644
--- a/g_octave/description.py
+++ b/g_octave/description.py
@@ -27,6 +27,7 @@ import os
from .config import Config
from .exception import ConfigException
+from .compat import open
from .log import Log
log = Log('g_octave.description')
diff --git a/g_octave/ebuild.py b/g_octave/ebuild.py
index 253cfc1..b7aee0d 100644
--- a/g_octave/ebuild.py
+++ b/g_octave/ebuild.py
@@ -22,6 +22,7 @@ from .config import Config
from .description import *
from .description_tree import *
from .exception import EbuildException
+from .compat import open
has_svn = True
try:
@@ -37,8 +38,6 @@ import re
import shutil
import subprocess
-from codecs import open
-
out = portage.output.EOutput()
# validating keywords (based on the keywords from the sci-mathematics/octave package)
diff --git a/g_octave/fetch.py b/g_octave/fetch.py
index 0868b93..f12974f 100644
--- a/g_octave/fetch.py
+++ b/g_octave/fetch.py
@@ -26,7 +26,7 @@ from .config import Config
conf = Config(True) # fetch phase
from .exception import FetchException
-from .compat import py3k
+from .compat import py3k, open as open_
if py3k:
import urllib.request as urllib
@@ -35,9 +35,12 @@ else:
import os
import json
import re
+import shutil
import tarfile
import portage.output
+from contextlib import closing
+
out = portage.output.EOutput()
re_files = {
@@ -54,15 +57,18 @@ def need_update():
def check_updates():
try:
- update = download_with_urllib2(conf.db_mirror + '/update.json', display_info=False)
+ update = download_with_urllib2(
+ conf.db_mirror + '/update.json',
+ display_info=False
+ ).decode('utf-8')
except Exception as error:
# if we already have a file, that's ok
if need_update():
raise FetchException(error)
- with open(os.path.join(conf.db, 'update.json')) as fp:
+ with open_(os.path.join(conf.db, 'update.json')) as fp:
update = fp.read()
else:
- with open(os.path.join(conf.db, 'update.json'), 'w') as fp:
+ with open_(os.path.join(conf.db, 'update.json'), 'w') as fp:
fp.write(update)
updated_files = json.loads(update)
@@ -91,18 +97,17 @@ def download_with_urllib2(url, dest=None, display_info=True):
if display_info:
out.ebegin('Downloading: %s' % my_file)
try:
- fp = urllib.urlopen(url)
- file_content = fp.read()
- fp.close()
if dest != None:
- if not os.path.exists(dest):
- os.makedirs(dest, 0o755)
- with open(os.path.join(dest, my_file), 'w') as fp:
- fp.write(file_content)
+ with closing(urllib.urlopen(url)) as fp:
+ if not os.path.exists(dest):
+ os.makedirs(dest, 0o755)
+ with open(os.path.join(dest, my_file), 'wb') as fp_:
+ shutil.copyfileobj(fp, fp_)
else:
- if display_info:
- out.eend(0)
- return file_content
+ with closing(urllib.urlopen(url)) as fp:
+ if display_info:
+ out.eend(0)
+ return fp.read()
except Exception as error:
if display_info:
out.eend(1)
@@ -117,7 +122,7 @@ def add_file_to_db_cache(_file):
my_file = os.path.join(conf.db, 'cache.json')
try:
- with open(my_file) as fp:
+ with open_(my_file) as fp:
files = json.load(fp)
except:
files = {'files': {}}
@@ -126,20 +131,20 @@ def add_file_to_db_cache(_file):
if re_files[f].match(_file) != None:
files['files'][f] = _file
- with open(my_file, 'w') as fp:
+ with open_(my_file, 'w') as fp:
json.dump(files, fp)
def check_db_cache():
try:
- with open(os.path.join(conf.db, 'cache.json')) as fp:
+ with open_(os.path.join(conf.db, 'cache.json')) as fp:
cache = json.load(fp)
except:
cache = {'files': {}}
try:
- with open(os.path.join(conf.db, 'update.json')) as fp:
+ with open_(os.path.join(conf.db, 'update.json')) as fp:
update = json.load(fp)
except:
my_cache = os.listdir(conf.db)
diff --git a/g_octave/overlay.py b/g_octave/overlay.py
index ec32965..79e832a 100644
--- a/g_octave/overlay.py
+++ b/g_octave/overlay.py
@@ -20,10 +20,9 @@ import sys
import shutil
import portage.output
-from codecs import open
-
from .config import Config
from .exception import ConfigException
+from .compat import open
out = portage.output.EOutput()
diff --git a/g_octave/package_manager.py b/g_octave/package_manager.py
index 0587718..c7b600d 100644
--- a/g_octave/package_manager.py
+++ b/g_octave/package_manager.py
@@ -23,6 +23,7 @@ import pwd
import subprocess
from g_octave.ebuild import Ebuild
+from g_octave.compat import open
class Base: