source: sasview/sansguiframe/src/sans/guiframe/CategoryInstaller.py @ 12c4933

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 12c4933 was 12c4933, checked in by Mathieu Doucet <doucetm@…>, 11 years ago

Fix RHEL6 install

  • Property mode set to 100644
File size: 6.0 KB
Line 
1"""
2Class for making sure all category stuff is installed
3and works fine.
4
5Copyright (c) Institut Laue-Langevin 2012
6
7@author kieranrcampbell@gmail.com
8@modified by NIST/MD sasview team
9"""
10
11import os
12import sys
13import shutil
14import cPickle as pickle
15from collections import defaultdict
16
17USER_FILE = 'serialized_cat.p'
18
19class CategoryInstaller:
20    """
21    Class for making sure all category stuff is installed
22
23    Note - class is entirely static!
24    """
25
26
27    def __init__(self):
28        """ initialization """
29
30    @staticmethod
31    def _get_installed_model_dir():
32        """
33        returns the dir where installed_models.txt should be
34        """
35        import sans.dataloader.readers
36        return sans.dataloader.readers.get_data_path()
37
38    @staticmethod
39    def _get_models_py_dir():
40        """
41        returns the dir where models.py should be
42        """
43        import sans.perspectives.fitting.models
44        return sans.perspectives.fitting.models.get_model_python_path()
45   
46    @staticmethod
47    def _get_default_cat_p_dir():
48        """
49        returns the dir where default_cat.p should be
50        """
51        cat_file = "default_categories.p"
52        app_dir = sys.path[0]
53
54        #TODO: Fix for RPM installation. This method should be cleaned up.
55        path = os.path.join(app_dir, 'sans', 'sansview', cat_file)
56        if os.path.isfile(path):
57            return path
58
59        if os.path.isfile(app_dir):
60            app_dir = os.path.dirname(app_dir)
61        n_dir = 12
62        for i in range(n_dir): 
63            path = os.path.join(app_dir, cat_file)
64            if os.path.isfile(path):
65                path = os.path.dirname(path)
66                return path
67            else:
68                app_dir, _ = os.path.split(app_dir)
69               
70        raise RuntimeError('Could not find the App folder')         
71
72    @staticmethod
73    def _get_home_dir():
74        """
75        returns the users sansview config dir
76        """
77        return os.path.join(os.path.expanduser("~"), ".sasview")
78
79    @staticmethod
80    def _regenerate_model_dict(master_category_dict):
81        """
82        regenerates self.by_model_dict which has each model name as the key
83        and the list of categories belonging to that model
84        along with the enabled mapping
85        returns tuplet (by_model_dict, model_enabled_dict)
86        """
87        by_model_dict = defaultdict(list)
88        model_enabled_dict = defaultdict(bool)
89       
90        for category in master_category_dict:
91            for (model, enabled) in master_category_dict[category]:
92                by_model_dict[model].append(category)
93                model_enabled_dict[model] = enabled
94   
95        return (by_model_dict, model_enabled_dict)
96
97
98    @staticmethod
99    def _regenerate_master_dict(by_model_dict, model_enabled_dict):
100        """
101        regenerates master_category_dict from by_model_dict
102        and model_enabled_dict
103        returns the master category dictionary
104        """
105        master_category_dict = defaultdict(list)
106        for model in by_model_dict:
107            for category in by_model_dict[model]:
108                master_category_dict[category].append(\
109                    (model, model_enabled_dict[model]))
110       
111        return master_category_dict
112
113    @staticmethod
114    def get_user_file():
115        """
116        returns the user data file, eg .sasview/serialized_cat.p
117        """
118        return os.path.join(CategoryInstaller._get_home_dir(),
119                            USER_FILE)
120
121    @staticmethod
122    def get_default_file():
123        """
124        returns the path of the default file
125        e.g. blahblah/default_categories.p
126        """
127        return os.path.join(\
128            CategoryInstaller._get_default_cat_p_dir(), "default_categories.p")
129       
130    @staticmethod
131    def check_install(homedir = None, model_list=None):
132        """
133        the main method of this class
134        makes sure serialized_cat.p exists and if not
135        compile it and install
136        :param homefile: Override the default home directory
137        :param model_list: List of model names except customized models
138        """
139        #model_list = []
140        default_file = CategoryInstaller.get_default_file()
141        serialized_file = None
142        master_category_dict = defaultdict(list)
143        if homedir == None:
144            serialized_file = CategoryInstaller.get_user_file()
145        else:
146            serialized_file = os.path.join(homedir, USER_FILE)
147        if os.path.isfile(serialized_file):
148            cat_file = open(serialized_file, 'rb')
149        else:
150            cat_file = open(default_file, 'rb')
151        master_category_dict = pickle.Unpickler(cat_file).load()
152        (by_model_dict, model_enabled_dict) = \
153                CategoryInstaller._regenerate_model_dict(master_category_dict)
154        cat_file.close()
155        add_list = model_list
156        del_name = False
157        for cat in master_category_dict.keys():
158            for ind in range(len(master_category_dict[cat])):
159                model_name, enabled = master_category_dict[cat][ind]
160                if model_name not in model_list:
161                    del_name = True 
162                    try:
163                        by_model_dict.pop(model_name)
164                        model_enabled_dict.pop(model_name)
165                    except:
166                        pass
167                else:
168                    add_list.remove(model_name)
169        if del_name or (len(add_list) > 0):
170            for model in add_list:
171                model_enabled_dict[model]= True
172                by_model_dict[model].append('Uncategorized')
173   
174            master_category_dict = \
175                CategoryInstaller._regenerate_master_dict(by_model_dict,
176                                                          model_enabled_dict)
177           
178            pickle.dump( master_category_dict,
179                         open(serialized_file, 'wb') )
180           
181            try:
182                #It happens only in source environment
183                shutil.copyfile(serialized_file, default_file)
184            except:
185                pass
186       
Note: See TracBrowser for help on using the repository browser.