-
Notifications
You must be signed in to change notification settings - Fork 4
/
management.py
126 lines (86 loc) · 3.06 KB
/
management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Handle session management.
A session is an ordinary Python module that is stored in a per-user
directory."""
import os
import sys
import imp
import shutil
_CACHED_PATH = None
_SESSION_TEMPLATE = """\"\"\"# *{doc}* session manual
## Usage
## Notes
\"\"\"
import logging
import concert
concert.require("{version}")
from concert.quantities import q
from concert.session.utils import ddoc, dstate, pdoc, code_of, abort
LOG = logging.getLogger(__name__)
"""
def path(session=None):
"""
Get absolute path of *session* module or base path if *session* is None.
"""
global _CACHED_PATH
if _CACHED_PATH is None:
env = os.environ
if "VIRTUAL_ENV" in env:
env["XDG_DATA_HOME"] = os.path.join(env["VIRTUAL_ENV"], "share")
import xdg.BaseDirectory
_CACHED_PATH = xdg.BaseDirectory.save_data_path('concert')
if session is None:
return _CACHED_PATH
return os.path.join(_CACHED_PATH, session + '.py')
def logfile_path():
return os.path.join(path(), 'concert.log')
def create(session, imports=()):
"""Create a template with *session* name and write it.
For each name in *imports* try to load it and insert `from
concert.processes.name import *` into the session file.
.. note:: This will *always* overwrite session.
"""
from concert import get_canonical_version
template = _SESSION_TEMPLATE.format(version=get_canonical_version(), doc=session)
def _module_exists(module_name):
try:
__import__(module_name)
return True
except ImportError:
return False
for module in imports:
module_name = 'concert.processes.{0}'.format(module)
if _module_exists(module_name):
template += 'from {0} import *'.format(module_name)
else:
print("{0} not found.".format(module_name))
if not os.path.exists(path()):
os.mkdir(path())
with open(path(session), 'w') as session_file:
session_file.write(template)
def remove(session):
"""Remove a *session*."""
if exists(session):
os.unlink(path(session))
def move(source, target):
"""Move *source* to *target*."""
os.rename(path(source), path(target))
def copy(source, target):
"""Copy *source* to *target*."""
shutil.copy(path(source), path(target))
def load(session, from_file=False):
"""Load *session* and return the module."""
if not from_file:
return imp.load_source(session, path(session))
return imp.load_source('somename', os.path.abspath(session))
def get_existing():
"""Get all existing session names."""
sessions = [f for f in os.listdir(path()) if f.endswith('.py')]
return [os.path.splitext(f)[0] for f in sessions]
def exists(session):
"""Check if *session* already exists."""
return os.access(path(session), os.R_OK)
def exit_if_not_exists(session):
"""Exit if *session* does not exist with a message."""
if not exists(session):
message = "Session `{0}' does not exist. Run `concert init {0}' first."
sys.exit(message.format(session))