1# -*- coding: utf-8 -*-
2
3import sys
4import os
5import re
6from glob import glob
7from distutils import log
8from distutils import sysconfig
9
10from configuration.util import execute, less_than_version, get_function, move_tree, \
11                           remove_tree, remove_empty_dirs, tick
12
13
14def remove_1_7(prefix, dest, version):
15   d_f = ['ASTK', 'aster_profile.sh'] \
16       + [os.path.join('outils', l) for l in ('as_run', 'show', 'get', 'getop', 'astk', 'bsf')]
17   move_tree(prefix, d_f, dest, preserve_symlinks=True)
18
19
20def remove_last(prefix, dest, version):
21   remove_1_7(prefix, dest, version)
22
23   python_lib = re.sub(prefix + '/*', '', sysconfig.get_python_lib(prefix=prefix))
24   d_f = [os.path.join('bin', f) for f in ('as_run', 'astk', 'bsf', 'mpirun_template', \
25                                           'parallel_cp', 'show', 'get', 'showop', 'getop',
26                                           'auto_update.cron')] \
27       + ['etc/codeaster', 'share/codeaster/asrun', 'share/codeaster/GPL.txt', 'lib/astk'] \
28       + [os.path.join(python_lib, 'asrun'), os.path.join(python_lib, 'astk-%s.egg-info' % version)]
29   tick()
30   move_tree(prefix, d_f, dest, preserve_symlinks=True, same_device=True)
31   tick()
32
33
34def remove_previous_main(prefix, version):
35   """Backup previous installation."""
36   if not version:
37      version = 'unknown'
38   destination = os.path.join(prefix, 'share', 'codeaster', 'backup', 'backup_%s' % version)
39   if os.path.exists(destination):
40      tick()
41      remove_tree(destination)
42   os.makedirs(destination)
43   log.info("backup previous installation into %s", destination)
44
45   functions = (
46      ('0.0.0', remove_1_7),
47      ('1.8.0', remove_last),
48   )
49   tick()
50   func = get_function(functions, version)
51   tick()
52   func(prefix, destination, version)
53   tick()
54   remove_empty_dirs(prefix)
55   tick()
56   return destination
57