def gen_lib():
"""Build libs locally for app
Using the setup.py this method will install all required python modules locally
to be used for local testing.
"""
lib_directory = 'lib_{}.{}.{}'.format(
sys.version_info.major, sys.version_info.minor, sys.version_info.micro)
app_path = os.getcwd()
app_name = os.path.basename(app_path)
lib_path = os.path.join(app_path, lib_directory)
if not os.path.isdir(lib_path):
os.mkdir(lib_path)
os.environ['PYTHONPATH'] = '{}'.format(lib_path)
stdout = sys.stdout
stderr = sys.stderr
try:
with open(os.path.join(app_path, '{}-libs.log'.format(app_name)), 'w') as log:
sys.stdout = log
sys.stderr = log
easy_install.main(['-axZ', '-d', lib_path, str(app_path)])
except SystemExit as e:
raise Exception(str(e))
finally:
sys.stdout = stdout
sys.stderr = stderr
if os.listdir(lib_path):
err = 'Encountered error running easy_install for {}. Check log file for details.'
raise Exception(err.format(app_name))
build_path = os.path.join(app_path, 'build')
if os.access(build_path, os.W_OK):
shutil.rmtree(build_path)
temp_path = os.path.join(app_path, 'temp')
if os.access(temp_path, os.W_OK):
shutil.rmtree(temp_path)
egg_path = os.path.join(app_path, app_name + '.egg-info')
if os.access(egg_path, os.W_OK):
shutil.rmtree(egg_path)
python类main()的实例源码
def main():
parser = optparse.OptionParser()
parser.add_option('-d', '--download-dir',
help='Directory with maually downloaded python modules.'
)
opts, _ = parser.parse_args()
# Install packages.
for k, v in _PACKAGES.items():
# Test python version for module.
if k in _PY_VERSION:
# Python version too old, skip module.
if PYVER < _PY_VERSION[k]:
continue
try:
__import__(k)
print('Already installed...', k)
# Module is not available - install it.
except ImportError:
# If not url or module name then look for installers in download area.
if not v[0].startswith('http') and v[0].endswith('exe'):
files = []
# Try all file patterns.
for pattern in v:
pattern = os.path.join(opts.download_dir, pattern)
files += glob.glob(pattern)
# No file with that pattern was not found - skip it.
if not files:
print('Skipping module...', k)
continue
# Full path to installers in download directory.
v = files
print('Installing module...', k)
# Some modules might require several .exe files to install.
for f in v:
print(' ', f)
# Use --no-deps ... installing module dependencies might fail
# because easy_install tries to install the same module from
# PYPI from source code and if fails because of C code that
# that needs to be compiled.
try:
easy_install.main(['--no-deps', '--always-unzip', f])
except Exception:
print(' ', k, 'installation failed')
def test_setup_requires_honors_fetch_params(self):
"""
When easy_install installs a source distribution which specifies
setup_requires, it should honor the fetch parameters (such as
allow-hosts, index-url, and find-links).
"""
# set up a server which will simulate an alternate package index.
p_index = setuptools.tests.server.MockServer()
p_index.start()
netloc = 1
p_index_loc = urlparse(p_index.url)[netloc]
if p_index_loc.endswith(':0'):
# Some platforms (Jython) don't find a port to which to bind,
# so skip this test for them.
return
# I realize this is all-but-impossible to read, because it was
# ported from some well-factored, safe code using 'with'. If you
# need to maintain this code, consider making the changes in
# the parent revision (of this comment) and then port the changes
# back for Python 2.4 (or deprecate Python 2.4).
def install(dist_file):
def install_at(temp_install_dir):
def install_env():
ei_params = ['--index-url', p_index.url,
'--allow-hosts', p_index_loc,
'--exclude-scripts', '--install-dir', temp_install_dir,
dist_file]
def install_clean_reset():
def install_clean_argv():
# attempt to install the dist. It should fail because
# it doesn't exist.
self.assertRaises(SystemExit,
easy_install_pkg.main, ei_params)
argv_context(install_clean_argv, ['easy_install'])
reset_setup_stop_context(install_clean_reset)
environment_context(install_env, PYTHONPATH=temp_install_dir)
tempdir_context(install_at)
# create an sdist that has a build-time dependency.
self.create_sdist(install)
# there should have been two or three requests to the server
# (three happens on Python 3.3a)
self.assertTrue(2 <= len(p_index.requests) <= 3)
self.assertEqual(p_index.requests[0].path, '/does-not-exist/')
def main():
parser = optparse.OptionParser()
parser.add_option('-d', '--download-dir',
help='Directory with maually downloaded python modules.'
)
opts, _ = parser.parse_args()
# Install packages.
for k, v in _PACKAGES.items():
# Test python version for module.
if k in _PY_VERSION:
# Python version too old, skip module.
if PYVER < _PY_VERSION[k]:
continue
try:
__import__(k)
print('Already installed...', k)
# Module is not available - install it.
except ImportError:
# If not url or module name then look for installers in download area.
if not v[0].startswith('http') and v[0].endswith('exe'):
files = []
# Try all file patterns.
for pattern in v:
pattern = os.path.join(opts.download_dir, pattern)
files += glob.glob(pattern)
# No file with that pattern was not found - skip it.
if not files:
print('Skipping module...', k)
continue
# Full path to installers in download directory.
v = files
print('Installing module...', k)
# Some modules might require several .exe files to install.
for f in v:
print(' ', f)
# Use --no-deps ... installing module dependencies might fail
# because easy_install tries to install the same module from
# PYPI from source code and if fails because of C code that
# that needs to be compiled.
try:
easy_install.main(['--no-deps', '--always-unzip', f])
except Exception:
print(' ', k, 'installation failed')
tcex_local.py 文件源码
项目:threatconnect-developer-docs
作者: ThreatConnect-Inc
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def gen_lib():
"""Build libs locally for app
Using the setup.py this method will install all required python modules locally
to be used for local testing.
"""
lib_directory = 'lib_{}.{}.{}'.format(
sys.version_info.major, sys.version_info.minor, sys.version_info.micro)
app_path = os.getcwd()
app_name = os.path.basename(app_path)
lib_path = os.path.join(app_path, lib_directory)
if not os.path.isdir(lib_path):
os.mkdir(lib_path)
os.environ['PYTHONPATH'] = '{}'.format(lib_path)
stdout = sys.stdout
stderr = sys.stderr
try:
with open(os.path.join(app_path, '{}-libs.log'.format(app_name)), 'w') as log:
sys.stdout = log
sys.stderr = log
easy_install.main(['-axZ', '-d', lib_path, str(app_path)])
except SystemExit as e:
raise Exception(str(e))
finally:
sys.stdout = stdout
sys.stderr = stderr
if os.listdir(lib_path):
err = 'Encountered error running easy_install for {}. Check log file for details.'
raise Exception(err.format(app_name))
build_path = os.path.join(app_path, 'build')
if os.access(build_path, os.W_OK):
shutil.rmtree(build_path)
temp_path = os.path.join(app_path, 'temp')
if os.access(temp_path, os.W_OK):
shutil.rmtree(temp_path)
egg_path = os.path.join(app_path, app_name + '.egg-info')
if os.access(egg_path, os.W_OK):
shutil.rmtree(egg_path)