Skip to content

Commit

Permalink
Merge pull request #371 from VIDA-NYU/py3-only
Browse files Browse the repository at this point in the history
Remove Python 2 compatibility
  • Loading branch information
remram44 committed Apr 23, 2021
2 parents 810c8cc + 4ad2da4 commit cf4ce09
Show file tree
Hide file tree
Showing 40 changed files with 223 additions and 507 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
os: [ubuntu-18.04]
mode: [tests]
python:
- "2.7"
- "3.5"
- "3.8"
include:
- os: ubuntu-18.04
mode: coverage
Expand Down
23 changes: 11 additions & 12 deletions reprounzip-docker/reprounzip_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
parse_environment_args, interruptible_call, metadata_read, \
metadata_write, metadata_initial_iofiles, metadata_update_run, parse_ports
from reprounzip.unpackers.common.x11 import X11Handler, LocalForwarder
from reprounzip.utils import unicode_, irange, iteritems, stderr, join_root, \
download_file
from reprounzip.utils import join_root, download_file


logger = logging.getLogger('reprounzip.docker')
Expand Down Expand Up @@ -275,7 +274,7 @@ def docker_setup_create(args):
'\n'
'if [ $# = 0 ]; then\n'
' exec /busybox sh /rpz_entrypoint.sh')
for nb in irange(len(runs)):
for nb in range(len(runs)):
fp.write(' run %d' % nb)
fp.write(
'\n'
Expand Down Expand Up @@ -333,7 +332,7 @@ def docker_setup_create(args):
wd=shell_escape(run['workingdir']),
env=shell_escape(' '.join(
'%s=%s' % (shell_escape(k), shell_escape(v))
for k, v in iteritems(run['environ']))),
for k, v in run['environ'].items())),
uid=run.get('uid', 1000),
gid=run.get('gid', 1000)))
fp.write(
Expand Down Expand Up @@ -411,7 +410,7 @@ def docker_setup_build(args):
unpacked_info['docker_host'] = {
'type': 'custom',
'env': dict((k, v)
for k, v in iteritems(os.environ)
for k, v in os.environ.items()
if k.startswith('DOCKER_'))}

write_dict(target, unpacked_info)
Expand Down Expand Up @@ -454,7 +453,7 @@ def get_local_addr():
This finds the address used to connect to the Docker host by establishing a
network connection to it and reading the local address of the socket.
Returns an IP address as a unicode object, in digits-and-dots format.
Returns an IP address as a str, in digits-and-dots format.
>>> get_local_addr()
'172.17.42.1'
Expand Down Expand Up @@ -588,7 +587,7 @@ def docker_run(args):
for k in env_unset:
env.append('-u')
env.append(shell_escape(k))
for k, v in iteritems(env_set):
for k, v in env_set.items():
env.append('%s=%s' % (shell_escape(k), shell_escape(v)))
cmd.append(' '.join(env))
# FIXME : Use exec -a or something if binary != argv[0]
Expand Down Expand Up @@ -693,8 +692,8 @@ def __init__(self, target, input_files, files, unpacked_info,

def prepare_upload(self, files):
if 'current_image' not in self.unpacked_info:
stderr.write("Image doesn't exist yet, have you run "
"setup/build?\n")
print("Image doesn't exist yet, have you run setup/build?\n",
file=sys.stderr)
sys.exit(1)

self.build_directory = Path.tempdir(prefix='reprozip_build_')
Expand Down Expand Up @@ -727,8 +726,8 @@ def finalize(self):
# FIXME : spaces in filenames will probably break Docker
dockerfile.write(
'COPY \\\n %s \\\n %s\n' % (
shell_escape(unicode_(src)),
shell_escape(unicode_(target))))
shell_escape(str(src)),
shell_escape(str(target))))

for src, target in self.docker_copy:
uid = gid = None
Expand All @@ -753,7 +752,7 @@ def finalize(self):

dockerfile.write(
'RUN ["/busybox", "chown", "%d:%d", %s]\n' % (
uid, gid, json.dumps(unicode_(target)),
uid, gid, json.dumps(str(target)),
)
)

Expand Down
2 changes: 0 additions & 2 deletions reprounzip-docker/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from setuptools import setup


# pip workaround
os.chdir(os.path.abspath(os.path.dirname(__file__)))


# Need to specify encoding for PY3, which has the worst unicode handling ever
with io.open('README.rst', encoding='utf-8') as fp:
description = fp.read()
setup(name='reprounzip-docker',
Expand Down
2 changes: 0 additions & 2 deletions reprounzip-qt/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from setuptools import setup


# pip workaround
os.chdir(os.path.abspath(os.path.dirname(__file__)))


# Need to specify encoding for PY3, which has the worst unicode handling ever
with io.open('README.rst', encoding='utf-8') as fp:
description = fp.read()
setup(name='reprounzip-qt',
Expand Down
15 changes: 8 additions & 7 deletions reprounzip-vagrant/reprounzip_vagrant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
metadata_initial_iofiles, metadata_update_run, parse_ports
from reprounzip.unpackers.common.x11 import BaseX11Handler, X11Handler
from reprounzip_vagrant.run_command import IgnoreMissingKey, run_interactive
from reprounzip.utils import unicode_, iteritems, stderr, download_file
from reprounzip.utils import download_file


logger = logging.getLogger('reprounzip.vagrant')
Expand Down Expand Up @@ -308,10 +308,10 @@ def vagrant_setup_create(args):
f = f.path
dest = join_root(PosixPath('/experimentroot'), f)
fp.write('mkdir -p %s\n' %
shell_escape(unicode_(f.parent)))
shell_escape(str(f.parent)))
fp.write('cp -L %s %s\n' % (
shell_escape(unicode_(f)),
shell_escape(unicode_(dest))))
shell_escape(str(f)),
shell_escape(str(dest))))
fp.write(
'\n'
'cp /etc/resolv.conf /experimentroot/etc/resolv.conf\n')
Expand Down Expand Up @@ -488,7 +488,7 @@ def vagrant_run(args):
all_ports[host] = guest, proto
unpacked_info['ports'] = sorted(
(host, guest, proto)
for host, (guest, proto) in iteritems(all_ports))
for host, (guest, proto) in all_ports.items())

write_vagrantfile(target, unpacked_info)
logger.info("Some requested ports are not yet forwarded, running "
Expand Down Expand Up @@ -518,7 +518,7 @@ def vagrant_run(args):
environ = x11.fix_env(run['environ'])
environ = fixup_environment(environ, args)
cmd += ' '.join('%s=%s' % (shell_escape(k), shell_escape(v))
for k, v in iteritems(environ))
for k, v in environ.items())
cmd += ' '
# FIXME : Use exec -a or something if binary != argv[0]
if cmdline is None:
Expand Down Expand Up @@ -563,7 +563,8 @@ def vagrant_run(args):
cmds,
not args.no_pty,
x11.port_forward)
stderr.write("\r\n*** Command finished, status: %d\r\n" % retcode)
print("\r\n*** Command finished, status: %d\r\n" % retcode,
file=sys.stderr)

# Update input file status
metadata_update_run(config, unpacked_info, selected_runs)
Expand Down
40 changes: 19 additions & 21 deletions reprounzip-vagrant/reprounzip_vagrant/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from __future__ import division, print_function, unicode_literals

import socket
import sys

from reprounzip.utils import stdout, stdout_bytes, stderr_bytes, stdin_bytes

# windows does not have termios...
try:
Expand All @@ -47,49 +47,48 @@ def posix_shell(chan, raw):
# set signal somehow
import select

oldtty = termios.tcgetattr(stdin_bytes)
oldtty = termios.tcgetattr(sys.stdin)
try:
if raw:
tty.setraw(stdin_bytes.fileno())
tty.setcbreak(stdin_bytes.fileno())
tty.setraw(sys.stdin)
tty.setcbreak(sys.stdin)
chan.settimeout(0.0)

while True:
r, w, e = select.select([chan, stdin_bytes], [], [])
r, w, e = select.select([chan, sys.stdin.fileno()], [], [])
if chan in r:
try:
if chan.recv_stderr_ready():
x = chan.recv_stderr(1024)
if len(x) > 0:
stderr_bytes.write(x)
stderr_bytes.flush()
sys.stderr.buffer.write(x)
sys.stderr.buffer.flush()
else:
x = chan.recv(1024)
if len(x) == 0:
break
stdout_bytes.write(x)
stdout_bytes.flush()
sys.stdout.buffer.write(x)
sys.stdout.buffer.flush()
except socket.timeout:
pass
if stdin_bytes in r:
x = stdin_bytes.read(1)
if sys.stdin.fileno() in r:
x = sys.stdin.buffer.read(1)
if len(x) == 0:
break
chan.send(x)

finally:
if raw:
termios.tcsetattr(stdin_bytes, termios.TCSADRAIN, oldtty)
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)


# thanks to Mike Looijmans for this code
def windows_shell(chan):
# set signal somehow
import threading

stdout.write("*** Emulating terminal on Windows; press F6 or Ctrl+Z then "
"enter to send EOF,\r\nor at the end of the execution.\r\n")
stdout.flush()
print("*** Emulating terminal on Windows; press F6 or Ctrl+Z then "
"enter to send EOF,\nor at the end of the execution.", flush=True)

out_lock = threading.RLock()

Expand All @@ -99,12 +98,11 @@ def write(recv, std):
if not data:
if std:
with out_lock:
stdout.write(
"\r\n*** EOF reached; (press F6 or ^Z then enter "
"to end)\r\n")
stdout.flush()
print(
"\n*** EOF reached; (press F6 or ^Z then enter "
"to end)", flush=True)
break
stream = [stderr_bytes, stdout_bytes][std]
stream = [sys.stderr.buffer, sys.stdout.buffer][std]
with out_lock:
stream.write(data)
stream.flush()
Expand All @@ -114,7 +112,7 @@ def write(recv, std):

try:
while True:
d = stdin_bytes.read(1)
d = sys.stdin.buffer.read(1)
if not d:
chan.shutdown_write()
break
Expand Down
7 changes: 3 additions & 4 deletions reprounzip-vagrant/reprounzip_vagrant/run_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from reprounzip.unpackers.common import interruptible_call
from reprounzip.unpackers.common.x11 import BaseForwarder, LocalForwarder
from reprounzip_vagrant.interaction import interactive_shell
from reprounzip.utils import irange, stdout_bytes


logger = logging.getLogger('reprounzip.vagrant')
Expand All @@ -44,7 +43,7 @@ def find_ssh_executable(name='ssh'):
# or ReproUnzip\Python27\python or ReproUnzip\Python27\Scripts\something
loc = par(sys.executable)
local_dirs = []
for i in irange(3):
for i in range(3):
local_dirs.extend([loc, join(loc, 'ssh')])
loc = par(loc)
for pathdir in local_dirs + dirs:
Expand Down Expand Up @@ -167,8 +166,8 @@ def run_interactive(ssh_info, interactive, cmd, request_pty, forwarded_ports):
data = chan.recv(1024)
if len(data) == 0:
break
stdout_bytes.write(data)
stdout_bytes.flush()
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
retcode = chan.recv_exit_status()
ssh.close()
return retcode
2 changes: 0 additions & 2 deletions reprounzip-vagrant/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from setuptools import setup


# pip workaround
os.chdir(os.path.abspath(os.path.dirname(__file__)))


# Need to specify encoding for PY3, which has the worst unicode handling ever
with io.open('README.rst', encoding='utf-8') as fp:
description = fp.read()
setup(name='reprounzip-vagrant',
Expand Down
7 changes: 3 additions & 4 deletions reprounzip-vistrails/reprounzip_vistrails.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from reprounzip.common import load_config, setup_logging, record_usage
from reprounzip import signals
from reprounzip.unpackers.common import shell_escape
from reprounzip.utils import iteritems


__version__ = '1.0.16'
Expand Down Expand Up @@ -239,9 +238,9 @@ def do_vistrails(target, pack=None, **kwargs):
connect_from = d

for i, run in enumerate(config.runs):
inputs = sorted(n for n, f in iteritems(config.inputs_outputs)
inputs = sorted(n for n, f in config.inputs_outputs.items()
if i in f.read_runs)
outputs = sorted(n for n, f in iteritems(config.inputs_outputs)
outputs = sorted(n for n, f in config.inputs_outputs.items()
if i in f.write_runs)
ports = itertools.chain((('input', p) for p in inputs),
(('output', p) for p in outputs))
Expand Down Expand Up @@ -341,7 +340,7 @@ def cmd(lst, add=None):
seen_input_names.add(input_name)

# Resets the input files that are used by this run and were not given
for name, f in iteritems(config.inputs_outputs):
for name, f in config.inputs_outputs.items():
if name not in seen_input_names and int(args.run) in f.read_runs:
upload_command.append(':%s' % name)

Expand Down
5 changes: 0 additions & 5 deletions reprounzip-vistrails/setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import io
import os
from setuptools import setup
import sys


# pip workaround
os.chdir(os.path.abspath(os.path.dirname(__file__)))


# Need to specify encoding for PY3, which has the worst unicode handling ever
with io.open('README.rst', encoding='utf-8') as fp:
description = fp.read()
req = [
'reprounzip>=1.0.0',
'rpaths>=0.8']
if sys.version_info < (2, 7):
req.append('argparse')
setup(name='reprounzip-vistrails',
version='2.0.0',
py_modules=['reprounzip_vistrails'],
Expand Down

0 comments on commit cf4ce09

Please sign in to comment.