Coverage for larch/site_config.py: 74%
68 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-10-16 21:04 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2024-10-16 21:04 +0000
1#!/usr/bin/env python
2"""
3site configuration for larch:
5 init_files: list of larch files run (in order) on startup
6 history_file:
7"""
8import sys
9import os
10from pathlib import Path
12from subprocess import check_call, CalledProcessError, TimeoutExpired
14from packaging.version import parse as version_parse
16from .utils import (uname, get_homedir, log_warning, log_error)
17from .version import __version__, __release_version__
19larch_version = __version__
20larch_release_version = __release_version__
22# lists of recommended packages that are not installed by default
23# but may be installed if several of the larch apps are run.
25def pjoin(*args):
26 "simple join"
27 return Path(*args).absolute().as_posix()
29def update_larch(with_larix=True):
30 "pip upgrade larch"
31 target = 'xraylarch'
32 if with_larix:
33 target = 'xraylarch[larix]'
34 check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', target])
36# = get_homedir() + 'larch' (#win)
37home_dir = get_homedir()
39icondir = Path(Path(__file__).parent, 'icons').absolute()
41user_larchdir = pjoin(home_dir, '.larch')
43if 'LARCHDIR' in os.environ:
44 user_larchdir = Path(os.environ['LARCHDIR']).absolute().as_posix()
46# on Linux, check for HOME/.local/share,
47# make with mode=711 if needed
48if uname in ('linux', 'darwin') and os.getuid() > 0:
49 lshare = Path(home_dir, '.local', 'share').absolute()
50 if Path(home_dir).is_dir():
51 lshare.mkdir(mode=457, parents=True, exist_ok=True) # for octal 711
54# initialization larch files to be run on startup
55init_files = [pjoin(user_larchdir, 'init.lar')]
57if 'LARCHSTARTUP' in os.environ:
58 startup = Path(os.environ['LARCHSTARTUP'])
59 if startup.exists():
60 init_files = [startup.as_posix()]
62# history file:
63history_file = pjoin(user_larchdir, 'history.lar')
65def make_user_larchdirs():
66 """create user's larch directories"""
67 files = {'init.lar': 'put custom startup larch commands:',
68 'history.lar': 'history of commands for larch CLI',
69 'history_larchgui.lar': 'history of commands for larch GUI',
70 }
71 subdirs = {'matplotlib': 'matplotlib may put cache files here',
72 'feff': 'Feff files and subfolders here',
73 'fdmnes': 'FDMNES files and subfolders here',
74 }
76 def make_dir(dname):
77 "create directory"
78 dname = Path(dname).absolute()
79 if not dname.exists():
80 try:
81 dname.mkdir(mode=493, parents=True)
82 except PermissionError:
83 log_warning(f'no permission to create directory {dname.as_posix()}')
84 except (OSError, TypeError):
85 log_error(sys.exc_info()[1])
87 def write_file(fname, text):
88 "write wrapper"
89 if not Path(fname).exists():
90 try:
91 with open(fname, 'w', encoding=sys.getdefaultencoding()) as fileh:
92 fileh.write(f'# {text}\n')
93 except IOError:
94 log_error(sys.exc_info()[1])
96 make_dir(user_larchdir)
97 for fname, text in files.items():
98 write_file(pjoin(user_larchdir, fname), text)
100 for sdir, text in subdirs.items():
101 sdir = pjoin(user_larchdir, sdir)
102 make_dir(sdir)
103 write_file(pjoin(sdir, 'README'), text)
106def site_config():
107 "retutn string of site config"
108 return f"""#== Larch Configuration:
109 Release version: {__release_version__}
110 Development version: {__version__}
111 Python executable: {sys.executable}
112 User larch dir: {user_larchdir}
113 User history_file: {history_file}
114#========================
115"""
117def show_site_config():
118 "print stie_config"
119 print(site_config())
121def system_settings():
122 """set system-specific Environmental Variables, and make sure
123 that the user larchdirs exist.
124 This is run by the interpreter on startup."""
125 # ubuntu / unity hack
126 if uname.startswith('linux'):
127 if 'ubuntu' in os.uname()[3].lower():
128 os.environ['UBUNTU_MENUPROXY'] = '0'
129 make_user_larchdirs()
132if __name__ == '__main__':
133 show_site_config()