/
init_project.py
182 lines (158 loc) · 5.47 KB
/
init_project.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import os
import stat
import argparse
from ..common.config import load_config, get_config, read_config_file
from .. import VERSION_TUPLE
from .templates import TEMPLATES
from . import get_project
logger = logging.getLogger(__name__)
DEFAULT_WORKSPACE = 'workspace'
DEFAULT_STORAGE = 'storage'
SCRIPT_HEADER = "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n"
DEFAULT_VERSION = VERSION_TUPLE
MSG_SUCCESS = "Successfully created project '{project_name}'"\
"in directory '{project_dir}'.\n"\
"Execute `signac check` to check your configuration."
MSG_AUTHOR_INCOMPLETE = "Author information is incomplete. "\
"This will lead to problems during project execution."\
" Execute `signac config` to create "\
"missing author information."
def check_for_existing_project(args):
try:
project = get_project()
root_dir = project.root_directory()
except (LookupError, FileNotFoundError, NotADirectoryError, KeyError):
pass
else:
if os.path.abspath(root_dir) == os.path.abspath(args.directory):
print("Project in directory '{}' already exists. "
"Use '-f' or '--force' argument to ignore this warning and "
"create a project anyways. This will lead to potential "
"data loss!".format(os.path.abspath(args.directory)))
return True
return False
def adjust_args(args):
if args.workspace:
args['workspace_dir'] = os.path.abspath(args.workspace)
if args.storage:
args['filestorage_dir'] = os.path.abspath(args.storage)
def make_dir(dirname):
try:
os.makedirs(dirname)
except OSError:
pass
def generate_config(args):
global_config = load_config()
if not args.workspace and global_config.get('workspace_dir') is None:
args.workspace = DEFAULT_WORKSPACE
if not args.storage and global_config.get('filestorage_dir') is None:
args.storage = DEFAULT_STORAGE
c_args = {
'project': args.project_name,
'signac_version': args.signac_version,
}
if args.workspace:
make_dir(args.workspace)
c_args['workspace_dir'] = args.workspace
if args.storage:
make_dir(args.storage)
c_args['filestorage_dir'] = args.storage
if args.db_host:
c_args['database_host'] = args.db_host
fn_config = os.path.join(args.directory, 'signac.rc')
try:
config = read_config_file(fn_config)
except FileNotFoundError:
config = get_config()
config.update(c_args)
config.verify()
return config
def get_templates():
return TEMPLATES.keys()
def copy_templates(args):
template = TEMPLATES[args.template]
for filename, content in template.items():
fn = os.path.join(args.directory, filename)
if os.path.isfile(fn):
logger.warning("Skipping template file '{}' because a file with "
"the same name already exists.".format(fn))
else:
with open(fn, 'wb') as file:
c = SCRIPT_HEADER + content
file.write(c.encode('utf-8'))
os.chmod(fn, os.stat(fn).st_mode | stat.S_IEXEC)
def init_project(args):
adjust_args(args)
if not args.force:
if check_for_existing_project(args):
return
config = generate_config(args)
copy_templates(args)
config.filename = os.path.join(args.directory, 'signac.rc')
config.write()
print(MSG_SUCCESS.format(
project_name=args.project_name,
project_dir=os.path.abspath(args.directory)))
def setup_parser(parser):
parser.add_argument(
'project_name',
type=str,
help="The project unique identifier.",
)
parser.add_argument(
'-d', '--directory',
type=str,
default='.',
help="The project's root directory. "
"Defaults to the current working directory.",
)
parser.add_argument(
'-w', '--workspace',
type=str,
help="The project's workspace directory. Defaults to directory '{}' "
"within the project's root directory.".format(DEFAULT_WORKSPACE),
)
parser.add_argument(
'-s', '--storage',
type=str,
help="The project's filestorage directory. Defaults to directory '{}' "
"within the project's root directory.".format(DEFAULT_STORAGE),
)
parser.add_argument(
'--db-host',
type=str,
help="The MongoDB database host address.",
)
parser.add_argument(
'-t', '--template',
type=str,
default='minimal',
choices=get_templates(),
help="Choose the template for this project.",
)
parser.add_argument(
'-f', '--force',
action='store_true',
help="Ignore warnings that prevent project creation. "
"This might lead to potential data loss!"
)
parser.add_argument(
'--signac-version',
type=str,
default=DEFAULT_VERSION,
help="The signac version, to use for this new project. "
"Defaults to '{}'.".format(DEFAULT_VERSION)
)
def main(arguments=None):
logging.basicConfig(level=logging.DEBUG)
parser = argparse.ArgumentParser(
description="Make a new mock signac project.",
)
setup_parser(parser)
args = parser.parse_args(arguments)
init_project(args)
if __name__ == '__main__':
main()