Skip to content

Commit

Permalink
Fixed linter issues and updated version number (0.0.5).
Browse files Browse the repository at this point in the history
  • Loading branch information
max-krichenbauer committed Jan 10, 2020
1 parent 8e5bc1e commit 0bb2e44
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 60 deletions.
4 changes: 4 additions & 0 deletions ament_cmake_virtualenv/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog for package ament_cmake_virtualenv
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0.0.5 (2020-01-10)
------------------
* fixed various bugs identified by CI

0.0.4 (2019-12-09)
------------------
* added fallbacks for different environments
Expand Down
2 changes: 1 addition & 1 deletion ament_cmake_virtualenv/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<package format="2">
<name>ament_cmake_virtualenv</name>
<version>0.0.4</version>
<version>0.0.5</version>
<description>Bundle python requirements in a ament package via virtualenv.</description>

<maintainer email="[email protected]">Max Krichenbauer</maintainer>
Expand Down
4 changes: 4 additions & 0 deletions ament_virtualenv/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog for package ament_virtualenv
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0.0.5 (2020-01-10)
------------------
* fixed various bugs identified by CI

0.0.4 (2019-12-09)
------------------
* added fallbacks for different environments
Expand Down
8 changes: 7 additions & 1 deletion ament_virtualenv/ament_virtualenv/build_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ def main(argv=sys.argv[1:]):
retries=args.retries
)

def build_venv(root_dir, python_version, requirements_filename, use_system_packages=False, extra_pip_args="", retries=0):

def build_venv(root_dir,
python_version,
requirements_filename,
use_system_packages=False,
extra_pip_args="",
retries=0):
root_dir = os.path.realpath(root_dir)
python_executable = find_python(python_version)
os.environ['DH_VIRTUALENV_INSTALL_ROOT'] = os.path.dirname(root_dir)
Expand Down
10 changes: 4 additions & 6 deletions ament_virtualenv/ament_virtualenv/combine_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

try:
from ament_virtualenv.requirements import VcsRequirement
except:
except ImportError:
try:
from requirements import VcsRequirement
except:
except ImportError:
from .requirements import VcsRequirement

comment_regex = re.compile(r'\s*#\s.*$', flags=re.MULTILINE)
Expand Down Expand Up @@ -73,10 +73,8 @@ def combine_requirements(requirements_list, output_file):
)
else:
combined_requirements[requirement.name].suppressed_set.add(
SuppressedRequirement(
requirement=requirement,
source=requirements_file.name)
)
SuppressedRequirement(requirement=requirement,
source=requirements_file.name))

for entry in combined_requirements.values():
output_file.write("{} # from {}\n".format(entry.requirement, entry.source))
Expand Down
5 changes: 3 additions & 2 deletions ament_virtualenv/ament_virtualenv/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ def __init__(self,
# Keep a copy with well-suported options only (for upgrading pip itself)
self.pip_upgrade_args = self.pip_args[:]

# Add in any user supplied pip args
extra_pip_arg = [e for e in extra_pip_arg if e != ''] # remove empty strings
# Add in any user supplied pip args,
# removing empty strings.
extra_pip_arg = [e for e in extra_pip_arg if e != '']
self.pip_args.extend(extra_pip_arg)

# (pbovbel) Set pip_upgrade_args here to keep flags like -q, disregard L111 above.
Expand Down
47 changes: 24 additions & 23 deletions ament_virtualenv/ament_virtualenv/glob_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,42 @@

try:
from ament_virtualenv.package import parse_package
except:
except ImportError:
try:
from package import parse_package
except:
except ImportError:
from .package import parse_package

try:
from queue import Queue
except:
except ImportError:
from Queue import Queue


def find_in_workspaces(project, file, workspaces=[]):
# Add default workspace search paths
ament_paths = os.environ.get('AMENT_PREFIX_PATH')
if ament_paths != None:
if ament_paths is not None:
# AMENT_PREFIX_PATH points at install/<package>
ament_paths = ament_paths.split(os.pathsep)
for path in ament_paths:
if (os.path.sep + 'install' + os.path.sep) in path or (os.path.sep + 'install_isolated' + os.path.sep) in path:
if ((os.path.sep + 'install' + os.path.sep) in path or
(os.path.sep + 'install_isolated' + os.path.sep) in path):
workspaces.append(os.path.join(path, '..'))
workspaces.append(os.path.join(path, '..', '..' , 'src'))
workspaces.append(os.path.join(path, '..', '..', 'src'))
break
if len(workspaces) == 0:
# if AMENT_PREFIX_PATH wasn't set, we can fall back on
# CMAKE_PREFIX_PATH (should contain the same information)
cmake_paths = os.environ.get('CMAKE_PREFIX_PATH')
if cmake_paths != None:
if cmake_paths is not None:
# CMAKE_PREFIX_PATH points at install/<package> or install_isolated/<package>
cmake_paths = cmake_paths.split(os.pathsep)
for path in cmake_paths:
if (os.path.sep + 'install' + os.path.sep) in path or (os.path.sep + 'install_isolated' + os.path.sep) in path:
if ((os.path.sep + 'install' + os.path.sep) in path or
(os.path.sep + 'install_isolated' + os.path.sep) in path):
workspaces.append(os.path.join(path, '..'))
workspaces.append(os.path.join(path, '..', '..' , 'src'))
workspaces.append(os.path.join(path, '..', '..', 'src'))
break
if len(workspaces) == 0:
# COLCON_PREFIX_PATH points to the `install/` directory,
Expand All @@ -70,34 +72,33 @@ def find_in_workspaces(project, file, workspaces=[]):
# but ament_cmake does not copy the files until after the
# build, which is too late. So for ament_cmake we also
# need to add the neighboring `src/` folder to the seach
# (eg.: `install/../src/`)
# (eg.: `install/../src/`)
colcon_paths = os.environ.get('COLCON_PREFIX_PATH')
if colcon_paths != None:
if colcon_paths is not None:
colcon_paths = colcon_paths.split(os.pathsep)
for path in colcon_paths:
if (os.path.sep + 'install') in path or (os.path.sep + 'install_isolated') in path:
workspaces.append(path)
workspaces.append(os.path.join(path, '..' , 'src'))
workspaces.append(os.path.join(path, '..', 'src'))
if len(workspaces) == 0:
# final fallback: use working directory (usually src/<package>)
path = os.getcwd()
if (os.path.sep + 'src') in path:
workspaces.append(path)
if len(workspaces) == 0:
raise RuntimeError(
"[ament_virtualenv] Failed to find any workspaces."
+ "\nAMENT_PREFIX_PATH=" + os.environ.get('AMENT_PREFIX_PATH', 'NOT SET')
+ "\nCMAKE_PREFIX_PATH=" + os.environ.get('CMAKE_PREFIX_PATH', 'NOT SET')
+ "\nCOLCON_PREFIX_PATH=" + os.environ.get('COLCON_PREFIX_PATH', 'NOT SET')
+ "\nCWD=" + os.getcwd()
"[ament_virtualenv] Failed to find any workspaces." +
"\nAMENT_PREFIX_PATH=" + os.environ.get('AMENT_PREFIX_PATH', 'NOT SET') +
"\nCMAKE_PREFIX_PATH=" + os.environ.get('CMAKE_PREFIX_PATH', 'NOT SET') +
"\nCOLCON_PREFIX_PATH=" + os.environ.get('COLCON_PREFIX_PATH', 'NOT SET') +
"\nCWD=" + os.getcwd()
)
# now search the workspaces
for workspace in (workspaces or []):
for d, dirs, files in os.walk(workspace, topdown=True, followlinks=True):
if ('CATKIN_IGNORE' in files or
'COLCON_IGNORE' in files or
'AMENT_IGNORE' in files
):
if (('CATKIN_IGNORE' in files) or
('COLCON_IGNORE' in files) or
('AMENT_IGNORE' in files)):
del dirs[:]
continue
dirname = os.path.basename(d)
Expand Down Expand Up @@ -146,8 +147,8 @@ def process_package(package_name, soft_fail=True):
)
if not package_path:
if not soft_fail:
raise RuntimeError("Failed to find package.xml for package "
+ package_name + ' in ' + ';'.join(workspaces))
raise RuntimeError("Failed to find package.xml for package " +
package_name + ' in ' + ';'.join(workspaces))
else:
# This is not an ament dependency
return [], []
Expand Down
39 changes: 24 additions & 15 deletions ament_virtualenv/ament_virtualenv/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@
from ament_virtualenv.combine_requirements import combine_requirements
from ament_virtualenv.build_venv import build_venv
ament_virtualenv_import_failed = False
except:
except ImportError:
ament_virtualenv_import_failed = True
#


def find_program(name='build_venv.py', package='ament_virtualenv'):
'''
"""
Find modules which are part of this package.
Helper function to find the modules that are part of this
package (glob_requirements, combine_requirements, build_venv),
in cases where a direct import failes due to python path issues.
'''
"""
ament_prefix_path = os.environ.get("AMENT_PREFIX_PATH")
if not ament_prefix_path:
return None
Expand All @@ -61,7 +64,7 @@ def find_program(name='build_venv.py', package='ament_virtualenv'):
def install_venv(install_base, package_name, python_version='2'):
venv_install_dir = os.path.join(install_base, 'venv')
bin_dir = os.path.join(install_base, 'bin')
#
#
# Build the virtual environment
python = shutil.which("python3")
if not python:
Expand All @@ -71,14 +74,15 @@ def install_venv(install_base, package_name, python_version='2'):
return 1

# glob_requirements --package-name ament_cmake_haros
if ament_virtualenv_import_failed == True:
if ament_virtualenv_import_failed is True:
# Fallback: try to find the command line script and run it
glob_requirements_py = find_program(name='glob_requirements.py', package='ament_virtualenv')
glob_requirements_py = find_program(name='glob_requirements.py',
package='ament_virtualenv')
if not glob_requirements_py:
print("ERROR: Failed to locate glob_requirements", file=sys.stderr)
return 1
cmd = [
python,
python,
glob_requirements_py,
'--package-name',
package_name
Expand All @@ -92,9 +96,11 @@ def install_venv(install_base, package_name, python_version='2'):
#
# combine_requirements --requirements-list a/requirements.txt;b/requirements.txt
# --output-file x/generated_requirements.txt
generated_requirements = os.path.join(install_base, package_name + '-generated_requirements.txt')
generated_requirements = os.path.join(install_base,
package_name + '-generated_requirements.txt')
if ament_virtualenv_import_failed:
combine_requirements_py = find_program(name='combine_requirements.py', package='ament_virtualenv')
combine_requirements_py = find_program(name='combine_requirements.py',
package='ament_virtualenv')
if not combine_requirements_py:
print("ERROR: Failed to locate combine_requirements", file=sys.stderr)
return 1
Expand Down Expand Up @@ -136,26 +142,27 @@ def install_venv(install_base, package_name, python_version='2'):
# '--use-system-packages',
'--extra-pip-args', '\"-qq\"',
]
ret = subprocess.check_output(cmd)
subprocess.check_output(cmd)
else:
ret = build_venv(
build_venv(
root_dir=venv_install_dir,
python_version=python_version,
requirements_filename=generated_requirements,
use_system_packages=False,
extra_pip_args="-qq",
retries=3
)
#
#
# Wrapper shell executables we installed
for bin_file in os.listdir(bin_dir):
if bin_file[-5:] == '-venv':
continue # possible left-over from last installation
# possible left-over from last installation
continue
# rename file from 'xxx' to 'xxx-venv'
bin_path = os.path.join(bin_dir, bin_file)
if not os.path.isfile(bin_path):
continue
os.rename(bin_path, bin_path+'-venv')
os.rename(bin_path, bin_path + '-venv')
venv_rel_path = os.path.relpath(venv_install_dir, bin_dir)
# create new file with the name of the previous file
with open(bin_path, "w") as f:
Expand All @@ -166,7 +173,8 @@ def install_venv(install_base, package_name, python_version='2'):
f.write("if __name__ == '__main__':\n")
f.write(" dir_path = os.path.dirname(os.path.realpath(__file__))\n")
f.write(" bin_path = os.path.join(dir_path, '" + bin_file + "-venv')\n")
f.write(" vpy_path = os.path.abspath(os.path.join(dir_path, '" + venv_rel_path +"'))\n")
f.write(" vpy_path = os.path.abspath(os.path.join(dir_path, '")
f.write(venv_rel_path + "'))\n")
f.write(" vpy_path = os.path.join(vpy_path, 'bin', 'python')\n")
f.write(" cmd = vpy_path + ' ' + bin_path\n")
f.write(" if len(sys.argv) > 1:\n")
Expand All @@ -178,6 +186,7 @@ def install_venv(install_base, package_name, python_version='2'):
#
return 0


def main(argv=sys.argv[1:]):
parser = argparse.ArgumentParser()
parser.add_argument('--install-base', required=True)
Expand Down
8 changes: 5 additions & 3 deletions ament_virtualenv/ament_virtualenv/wrap_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
import sys
import argparse


def wrap_module(bin_path, venv_install_dir):
if not os.path.isfile(bin_path):
return -1
bin_dir = os.path.dirname(bin_path)
bin_dir = os.path.dirname(bin_path)
bin_file = os.path.basename(bin_path)
# rename file from 'xxx' to 'xxx-venv'
os.rename(bin_path, bin_path+'-venv')
os.rename(bin_path, bin_path + '-venv')
venv_rel_path = os.path.relpath(venv_install_dir, bin_dir)
# create new file with the name of the previous file
with open(bin_path, "w") as f:
Expand All @@ -43,7 +44,8 @@ def wrap_module(bin_path, venv_install_dir):
f.write("if __name__ == '__main__':\n")
f.write(" dir_path = os.path.dirname(os.path.realpath(__file__))\n")
f.write(" bin_path = os.path.join(dir_path, '" + bin_file + "-venv')\n")
f.write(" vpy_path = os.path.abspath(os.path.join(dir_path, '" + venv_rel_path +"'))\n")
f.write(" vpy_path = os.path.abspath(os.path.join(dir_path, '")
f.write(venv_rel_path + "'))\n")
f.write(" vpy_path = os.path.join(vpy_path, 'bin', 'python')\n")
f.write(" cmd = vpy_path + ' ' + bin_path\n")
f.write(" if len(sys.argv) > 1:\n")
Expand Down
12 changes: 8 additions & 4 deletions ament_virtualenv/ament_virtualenv/wrap_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@
import sys
import argparse


def wrap_package(bin_dir, venv_install_dir):
for bin_file in os.listdir(bin_dir):
if bin_file[-3:] != '.py':
continue # only wrap python files
# only wrap python files
continue
if bin_file[-5:] == '__.py':
continue # don't wrap __init__.py etc
# don't wrap __init__.py etc
continue
bin_path = os.path.join(bin_dir, bin_file)
if not os.path.isfile(bin_path):
continue
# rename file from 'xxx.py' to 'xxx.py-venv'
os.rename(bin_path, bin_path+'-venv')
os.rename(bin_path, bin_path + '-venv')
venv_rel_path = os.path.relpath(venv_install_dir, bin_dir)
# create new file with the name of the previous file
with open(bin_path, "w") as f:
Expand All @@ -47,7 +50,8 @@ def wrap_package(bin_dir, venv_install_dir):
f.write("if __name__ == '__main__':\n")
f.write(" dir_path = os.path.dirname(os.path.realpath(__file__))\n")
f.write(" bin_path = os.path.join(dir_path, '" + bin_file + "-venv')\n")
f.write(" vpy_path = os.path.abspath(os.path.join(dir_path, '" + venv_rel_path +"'))\n")
f.write(" vpy_path = os.path.abspath(os.path.join(dir_path, '")
f.write(venv_rel_path + "'))\n")
f.write(" vpy_path = os.path.join(vpy_path, 'bin', 'python')\n")
f.write(" cmd = vpy_path + ' ' + bin_path\n")
f.write(" if len(sys.argv) > 1:\n")
Expand Down
2 changes: 1 addition & 1 deletion ament_virtualenv/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<package format="2">
<name>ament_virtualenv</name>
<version>0.0.4</version>
<version>0.0.5</version>
<description>Bundle python requirements in a ament package via virtualenv.</description>

<maintainer email="[email protected]">Max Krichenbauer</maintainer>
Expand Down
2 changes: 1 addition & 1 deletion ament_virtualenv/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name=package_name,
version='0.0.4',
version='0.0.5',
packages=find_packages(exclude=['test']),
data_files=[
('share/' + package_name, ['package.xml']),
Expand Down
4 changes: 4 additions & 0 deletions test_ament_cmake_virtualenv/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog for package test_ament_cmake_virtualenv
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0.0.5 (2020-01-10)
------------------
* fixed various bugs identified by CI

0.0.4 (2019-12-09)
------------------
* added fallbacks for different environments
Expand Down
Loading

0 comments on commit 0bb2e44

Please sign in to comment.