releases/openstack_releases/xstaticutils.py
Jeremy Stanley 7936adc658 Bust import caching of xstatic package namespaces
When iterating over xstatic packages which haven't had the benefit
of pip install creating explicit namespace packages for them, we
reset the implicit namespaces which may linger from earlier imports.

Change-Id: Ie6fe282c9d06f0147e37bbe2cf6de9d9c3f0eb39
2020-03-26 21:08:08 +00:00

52 lines
1.6 KiB
Python

# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import importlib
import os
import sys
def get_versions(workdir, repo):
"""Get the package versions from packages."""
versions = []
# Switch to the workdir
start_path = os.getcwd()
repo_dir = '%s/%s' % (workdir, repo)
os.chdir(repo_dir)
# Add the repo to the PYTHONPATH so we can import its contents
sys.path.append(repo_dir)
# Extract PACAKGE_VERSION from any xstatic packages found
try:
for name in os.listdir('xstatic/pkg'):
if '__' in name:
continue
if os.path.isdir('xstatic/pkg/%s' % name):
# import cache buster, since these namespaces can linger
import xstatic
import xstatic.pkg
importlib.reload(xstatic)
importlib.reload(xstatic.pkg)
xs = importlib.import_module('xstatic.pkg.%s' % name)
versions.append(xs.PACKAGE_VERSION)
finally:
# Get back to our original state
sys.path.pop()
os.chdir(start_path)
return versions