Skip to content

Commit

Permalink
URL redirect on plugin installation
Browse files Browse the repository at this point in the history
Use redirected filename when downloading scripts from the internet.
  • Loading branch information
pslacerda committed Aug 13, 2024
1 parent 03d7a7f commit f62db64
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions modules/pymol/plugins/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,21 +309,24 @@ def fetchscript(title, dest=None, run=1, quiet=1):
filename = url = title
rawscript = 1

filename = os.path.join(dest, filename.rsplit('/')[-1])
if not quiet:
print('Downloading', url)

# get page content
try:
from . import pref_get
timeout = pref_get('network_timeout', 10.0)
handle = urllib2.urlopen(url, timeout=timeout)
content = handle.read().decode('utf-8')
filename = handle.url
except IOError as e:
raise CmdException(e, "Plugin-Error")

filename = os.path.join(dest, filename.rsplit('/')[-1])
if os.path.exists(filename):
if not quiet:
print('File "%s" exists, will not redownload')
else:
if not quiet:
print('Downloading', url)

# get page content
try:
content = urlreadstr(url, None if rawscript else 'utf-8')
except IOError as e:
raise CmdException(e, "Plugin-Error")

if not rawscript:
# redirect
redirect = re.match(r'\s*#REDIRECT\s*\[\[(.*?)\]\]', content)
Expand Down Expand Up @@ -356,9 +359,8 @@ def fetchscript(title, dest=None, run=1, quiet=1):
with open(filename, 'w') as handle:
handle.write(content)
else:
with open(filename, 'wb') as handle:
with open(filename, 'w') as handle:
handle.write(content)

if int(run):
cmd.do("run " + filename, echo=not quiet)

Expand Down

0 comments on commit f62db64

Please sign in to comment.