2014-11-29 11:39:25 +01:00
|
|
|
#!/usr/bin/env python
|
2014-11-26 09:09:19 +01:00
|
|
|
# encoding: utf-8
|
2016-03-30 08:14:58 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-06-11 10:07:21 +02:00
|
|
|
"""
|
2014-11-26 09:09:19 +01:00
|
|
|
makePyLoT -- build and install PyLoT
|
|
|
|
|
|
|
|
makePyLoT is a python make file in order to establish the folder structure and
|
|
|
|
meet requisites
|
|
|
|
|
|
|
|
It defines
|
|
|
|
:class CLIError:
|
|
|
|
:method main:
|
|
|
|
|
|
|
|
:author: Sebastian Wehling-Benatelli
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
:copyright: 2014 MAGS2 EP3 Working Group. All rights reserved.
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
:license: GNU Lesser General Public License, Version 3
|
|
|
|
(http://www.gnu.org/copyleft/lesser.html)
|
|
|
|
|
|
|
|
:contact: sebastian.wehling@rub.de
|
|
|
|
|
|
|
|
updated: Updated
|
2015-06-11 10:07:21 +02:00
|
|
|
"""
|
2014-11-26 09:09:19 +01:00
|
|
|
|
2014-11-29 11:39:25 +01:00
|
|
|
import glob
|
2014-11-26 09:09:19 +01:00
|
|
|
import os
|
2014-11-29 11:39:25 +01:00
|
|
|
import sys
|
2016-04-29 11:50:28 +02:00
|
|
|
import shutil
|
|
|
|
import copy
|
2014-11-26 09:09:19 +01:00
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from argparse import RawDescriptionHelpFormatter
|
|
|
|
|
|
|
|
__all__ = []
|
|
|
|
__version__ = 0.1
|
|
|
|
__date__ = '2014-11-26'
|
2016-04-28 15:20:46 +02:00
|
|
|
__updated__ = '2016-04-28'
|
2014-11-26 09:09:19 +01:00
|
|
|
|
2014-11-27 11:51:40 +01:00
|
|
|
DEBUG = 0
|
2014-11-26 09:09:19 +01:00
|
|
|
TESTRUN = 0
|
|
|
|
PROFILE = 0
|
|
|
|
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
class CLIError(Exception):
|
2015-06-11 10:07:21 +02:00
|
|
|
"""Generic exception to raise and log different fatal errors."""
|
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
def __init__(self, msg):
|
|
|
|
super(CLIError).__init__(type(self))
|
|
|
|
self.msg = "E: %s" % msg
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.msg
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.msg
|
|
|
|
|
2015-06-11 10:07:21 +02:00
|
|
|
|
|
|
|
def main(argv=None): # IGNORE:C0111
|
2014-11-26 09:09:19 +01:00
|
|
|
'''Command line options.'''
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv
|
|
|
|
else:
|
|
|
|
sys.argv.extend(argv)
|
|
|
|
|
|
|
|
program_name = os.path.basename(sys.argv[0])
|
|
|
|
program_version = "v%s" % __version__
|
|
|
|
program_build_date = str(__updated__)
|
2015-06-11 10:07:21 +02:00
|
|
|
program_version_message = 'makePyLoT %s (%s)' % (
|
|
|
|
program_version, program_build_date)
|
2014-11-26 09:09:19 +01:00
|
|
|
program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
|
2016-04-28 15:20:46 +02:00
|
|
|
program_license = '''{0:s}
|
2014-11-26 09:09:19 +01:00
|
|
|
|
2016-04-28 15:20:46 +02:00
|
|
|
Created by Sebastian Wehling-Benatelli on {1:s}.
|
|
|
|
Copyright 2014 MAGS2 EP3 Working Group. All rights reserved.
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2016-04-28 15:20:46 +02:00
|
|
|
GNU Lesser General Public License, Version 3
|
2014-11-26 09:09:19 +01:00
|
|
|
(http://www.gnu.org/copyleft/lesser.html)
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2016-04-28 15:20:46 +02:00
|
|
|
Distributed on an "AS IS" basis without warranties
|
|
|
|
or conditions of any kind, either express or implied.
|
2014-11-26 09:09:19 +01:00
|
|
|
|
2016-04-28 15:20:46 +02:00
|
|
|
USAGE
|
|
|
|
'''.format(program_shortdesc, str(__date__))
|
2014-11-26 09:09:19 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Setup argument parser
|
2015-06-11 10:07:21 +02:00
|
|
|
parser = ArgumentParser(description=program_license,
|
|
|
|
formatter_class=RawDescriptionHelpFormatter)
|
|
|
|
parser.add_argument("-b", "--build", dest="build", action="store_true",
|
|
|
|
help="build PyLoT")
|
|
|
|
parser.add_argument("-v", "--verbose", dest="verbose", action="count",
|
|
|
|
help="set verbosity level")
|
|
|
|
parser.add_argument("-i", "--install", dest="install",
|
|
|
|
action="store_true",
|
|
|
|
help="install PyLoT on the system")
|
|
|
|
parser.add_argument("-d", "--directory", dest="directory",
|
|
|
|
help="installation directory", metavar="RE")
|
|
|
|
parser.add_argument('-V', '--version', action='version',
|
|
|
|
version=program_version_message)
|
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
# Process arguments
|
|
|
|
args = parser.parse_args()
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
verbose = args.verbose
|
2014-11-27 11:51:40 +01:00
|
|
|
build = args.build
|
|
|
|
install = args.install
|
|
|
|
directory = args.directory
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
if verbose > 0:
|
|
|
|
print("Verbose mode on")
|
2016-04-28 15:20:46 +02:00
|
|
|
if install and not directory:
|
|
|
|
raise CLIError("""Trying to install without appropriate
|
|
|
|
destination; please specify an installation
|
|
|
|
directory!""")
|
|
|
|
if build and install:
|
|
|
|
print("Building and installing PyLoT ...\n")
|
|
|
|
buildPyLoT(verbose)
|
|
|
|
installPyLoT(verbose)
|
|
|
|
elif build and not install:
|
|
|
|
print("Building PyLoT without installing! Please wait ...\n")
|
|
|
|
buildPyLoT(verbose)
|
2014-11-27 11:51:40 +01:00
|
|
|
cleanUp()
|
2014-11-26 09:09:19 +01:00
|
|
|
return 0
|
|
|
|
except KeyboardInterrupt:
|
2016-04-29 11:50:28 +02:00
|
|
|
cleanUp(1)
|
2014-11-26 09:09:19 +01:00
|
|
|
return 0
|
2016-03-30 08:14:58 +02:00
|
|
|
except Exception as e:
|
2014-11-26 09:09:19 +01:00
|
|
|
if DEBUG or TESTRUN:
|
2015-06-11 10:07:21 +02:00
|
|
|
raise e
|
2014-11-26 09:09:19 +01:00
|
|
|
indent = len(program_name) * " "
|
|
|
|
sys.stderr.write(program_name + ": " + repr(e) + "\n")
|
|
|
|
sys.stderr.write(indent + " for help use --help")
|
|
|
|
return 2
|
|
|
|
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-12-02 12:07:02 +01:00
|
|
|
def buildPyLoT(verbosity=None):
|
|
|
|
system = sys.platform
|
|
|
|
if verbosity > 1:
|
|
|
|
msg = ("... on system: {0}\n"
|
|
|
|
"\n"
|
|
|
|
" Current working directory: {1}\n"
|
2015-06-11 10:07:21 +02:00
|
|
|
).format(system, os.getcwd())
|
2016-03-30 08:14:58 +02:00
|
|
|
print(msg)
|
2014-12-02 12:07:02 +01:00
|
|
|
if system.startswith(('win', 'microsoft')):
|
2015-06-11 10:07:21 +02:00
|
|
|
raise CLIError(
|
|
|
|
"building on Windows system not tested yet; implementation pending")
|
2014-12-02 12:07:02 +01:00
|
|
|
elif system == 'darwin':
|
2014-11-29 11:39:25 +01:00
|
|
|
# create a symbolic link to the desired python interpreter in order to
|
|
|
|
# display the right application name
|
|
|
|
for path in os.getenv('PATH').split(':'):
|
|
|
|
found = glob.glob(os.path.join(path, 'python'))
|
|
|
|
if found:
|
2014-12-02 12:07:02 +01:00
|
|
|
os.symlink(found, './PyLoT')
|
2014-11-29 11:39:25 +01:00
|
|
|
break
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-27 11:51:40 +01:00
|
|
|
|
2014-12-02 12:07:02 +01:00
|
|
|
def installPyLoT(verbosity=None):
|
2017-09-21 15:19:15 +02:00
|
|
|
files_to_copy = {'pylot_local.in': ['~', '.pylot'],
|
|
|
|
'pylot_regional.in': ['~', '.pylot'],
|
2017-09-21 15:03:14 +02:00
|
|
|
'pylot_global.in': ['~', '.pylot']}
|
2016-04-29 11:50:28 +02:00
|
|
|
if verbosity > 0:
|
2017-08-03 09:41:54 +02:00
|
|
|
print('starting installation of PyLoT ...')
|
2016-04-29 11:50:28 +02:00
|
|
|
if verbosity > 1:
|
2017-08-03 09:41:54 +02:00
|
|
|
print('copying input files into destination folder ...')
|
2016-04-29 11:50:28 +02:00
|
|
|
ans = input('please specify scope of interest '
|
2020-10-06 10:44:27 +02:00
|
|
|
'([0]=local, 1=regional, 2=global, 3=active) :') or 0
|
2016-04-29 11:50:28 +02:00
|
|
|
if not isinstance(ans, int):
|
|
|
|
ans = int(ans)
|
2017-09-21 15:19:15 +02:00
|
|
|
if ans == 0:
|
|
|
|
ans = 'local'
|
|
|
|
elif ans == 1:
|
|
|
|
ans = 'regional'
|
|
|
|
elif ans == 2:
|
|
|
|
ans = 'global'
|
2020-10-06 10:44:27 +02:00
|
|
|
elif ans == 3:
|
|
|
|
ans = 'active'
|
2016-04-29 11:50:28 +02:00
|
|
|
link_dest = []
|
|
|
|
for file, destination in files_to_copy.items():
|
|
|
|
link_file = ans in file
|
|
|
|
if link_file:
|
|
|
|
link_dest = copy.deepcopy(destination)
|
2017-09-21 15:03:14 +02:00
|
|
|
link_dest.append('pylot.in')
|
2016-04-29 11:50:28 +02:00
|
|
|
link_dest = os.path.join(*link_dest)
|
|
|
|
destination.append(file)
|
|
|
|
destination = os.path.join(*destination)
|
|
|
|
srcfile = os.path.join('input', file)
|
|
|
|
assert not os.path.isabs(srcfile), 'source files seem to be ' \
|
|
|
|
'corrupted ...'
|
|
|
|
if verbosity > 1:
|
2017-08-03 09:41:54 +02:00
|
|
|
print('copying file {file} to folder {dest}'.format(file=file, dest=destination))
|
2016-04-29 11:50:28 +02:00
|
|
|
shutil.copyfile(srcfile, destination)
|
|
|
|
if link_file:
|
|
|
|
if verbosity:
|
|
|
|
print('linking input file for autoPyLoT ...')
|
|
|
|
os.symlink(destination, link_dest)
|
|
|
|
|
|
|
|
|
2014-12-02 12:07:02 +01:00
|
|
|
def cleanUp(verbosity=None):
|
2016-04-29 11:50:28 +02:00
|
|
|
if verbosity >= 1:
|
|
|
|
print('cleaning up build files...')
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
os.remove('./PyLoT')
|
2014-11-27 11:51:40 +01:00
|
|
|
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if DEBUG:
|
|
|
|
sys.argv.append("-h")
|
|
|
|
sys.argv.append("-v")
|
|
|
|
if TESTRUN:
|
|
|
|
import doctest
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
doctest.testmod()
|
|
|
|
if PROFILE:
|
|
|
|
import cProfile
|
|
|
|
import pstats
|
2015-06-11 10:07:21 +02:00
|
|
|
|
2014-11-26 09:09:19 +01:00
|
|
|
profile_filename = 'makePyLoT_profile.txt'
|
|
|
|
cProfile.run('main()', profile_filename)
|
|
|
|
statsfile = open("profile_stats.txt", "wb")
|
|
|
|
p = pstats.Stats(profile_filename, stream=statsfile)
|
|
|
|
stats = p.strip_dirs().sort_stats('cumulative')
|
|
|
|
stats.print_stats()
|
|
|
|
statsfile.close()
|
|
|
|
sys.exit(0)
|
2015-06-11 10:07:21 +02:00
|
|
|
sys.exit(main())
|