Skip to content

Commit d529466

Browse files
committed
Manually merged PR89 with some modifications in line with review comments on github: fixed a comment typo, a PEP8 style recommendation and rearranged a few lines for tighter clustering of related code lines.
git-svn-id: svn+ssh://svn.code.sf.net/p/migrid/code/trunk@6096 b75ad72c-e7d7-11dd-a971-7dbc132099af
1 parent 445051a commit d529466

File tree

2 files changed

+108
-15
lines changed

2 files changed

+108
-15
lines changed

mig/shared/install.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import sys
5151

5252
from mig.shared.defaults import default_http_port, default_https_port, \
53-
mig_user, mig_group, default_source, default_destination, \
53+
MIG_BASE, mig_user, mig_group, default_source, default_destination, \
5454
auth_openid_mig_db, auth_openid_ext_db, STRONG_TLS_CIPHERS, \
5555
STRONG_TLS_CURVES, STRONG_SSH_KEXALGOS, STRONG_SSH_LEGACY_KEXALGOS, \
5656
STRONG_SSH_CIPHERS, STRONG_SSH_LEGACY_CIPHERS, STRONG_SSH_MACS, \
@@ -74,6 +74,15 @@ def _override_apache_initd(template_name, user_dict):
7474
return "%s-%s" % (file_name, user_dict['__MIG_USER__'])
7575

7676

77+
def abspath(path, start):
78+
"""Return an absolute path as per os.path.abspath() - from an explicit
79+
starting cwd if necessary.
80+
"""
81+
if os.path.isabs(path):
82+
return path
83+
return os.path.normpath(os.path.join(start, path))
84+
85+
7786
def determine_timezone(_environ=os.environ, _path_exists=os.path.exists, _print=print):
7887
"""Attempt to detect the timezone in various known portable ways."""
7988

@@ -255,7 +264,11 @@ def template_remove(template_file, remove_pattern):
255264
'group',
256265
'user',
257266
'timezone',
267+
'_getcwd',
258268
'_getpwnam',
269+
'_prepare',
270+
'_writefiles',
271+
'_instructions',
259272
]
260273

261274

@@ -485,36 +498,55 @@ def generate_confs(
485498
ca_fqdn='',
486499
ca_user='mig-ca',
487500
ca_smtp='localhost',
501+
_getcwd=os.getcwd,
488502
_getpwnam=pwd.getpwnam,
503+
_prepare=None,
504+
_writefiles=None,
505+
_instructions=None,
489506
):
490507
"""Generate Apache and MiG server confs with specified variables"""
491508

509+
# TODO: override in signature as a non-functional follow-up change
510+
if _prepare is None:
511+
_prepare = _generate_confs_prepare
512+
if _writefiles is None:
513+
_writefiles = _generate_confs_writefiles
514+
if _instructions is None:
515+
_instructions = _generate_confs_instructions
516+
492517
# Read out dictionary of args with defaults and overrides
493518

494519
thelocals = locals()
495520
expanded = {k: v for k, v in thelocals.items() if k not in
496521
_GENERATE_CONFS_NOFORWARD_KEYS}
497522

498523
# expand any directory path specific as "auto" relative to CWD
524+
thecwd = _getcwd()
525+
499526
if source == keyword_auto:
500-
template_path = os.path.dirname(sys.argv[0])
527+
# use the templates from this copy of the code tree
528+
template_dir = os.path.join(MIG_BASE, "mig/install")
501529
else:
502-
template_path = source
530+
# construct a path using the supplied value made absolute
531+
template_dir = abspath(source, start=thecwd)
503532

504533
if destination == keyword_auto:
505-
destination_link = os.path.dirname(sys.argv[0])
534+
# write output into a confs folder within the CWD
535+
destination = os.path.join(thecwd, 'confs')
506536
else:
507-
destination_link = destination
537+
# construct a path from the supplied value made absolute
538+
destination = abspath(destination, start=thecwd)
539+
540+
# finalize destination paths up-front
541+
destination_link = destination
542+
destination_dir = "%s%s" % (destination, destination_suffix)
508543

509544
# expand any user information marked as "auto" based on the environment
510545
if user == keyword_auto:
511546
user = pwd.getpwuid(os.getuid())[0]
512547
if group == keyword_auto:
513548
group = grp.getgrgid(os.getgid())[0]
514549

515-
# finalize a destination path up-front
516-
destination_dir = "%s%s" % (destination, destination_suffix)
517-
518550
# Backwards compatibility with old name
519551
if public_port and not public_http_port:
520552
public_http_port = public_port
@@ -528,16 +560,16 @@ def generate_confs(
528560
'command_line': generateconfs_command,
529561
'destination_dir': destination_dir,
530562
'destination_link': destination_link,
531-
'template_dir': template_path,
563+
'template_dir': template_dir,
532564
'timezone': timezone,
533565
'user_gid': user_pw_info.pw_gid,
534566
'user_group': group,
535567
'user_uid': user_pw_info.pw_uid,
536568
'user_uname': user,
537569
}
538-
user_dict = _generate_confs_prepare(options, **expanded)
539-
_generate_confs_write(options, user_dict)
540-
_generate_confs_instructions(options, user_dict)
570+
user_dict = _prepare(options, **expanded)
571+
_writefiles(options, user_dict)
572+
_instructions(options, user_dict)
541573
return options
542574

543575

@@ -2131,7 +2163,7 @@ def _generate_confs_prepare(
21312163
return user_dict
21322164

21332165

2134-
def _generate_confs_write(options, user_dict, insert_list=[], cleanup_list=[]):
2166+
def _generate_confs_writefiles(options, user_dict, insert_list=[], cleanup_list=[]):
21352167
"""Actually write generated confs"""
21362168
assert os.path.isabs(options['destination_dir'])
21372169
assert os.path.isabs(options['template_dir'])

tests/test_mig_shared_install.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535

3636
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
3737

38-
from support import MigTestCase, testmain, temppath, cleanpath, fixturepath
38+
from support import MIG_BASE, MigTestCase, testmain, temppath, cleanpath, fixturepath
3939

40+
from mig.shared.defaults import keyword_auto
4041
from mig.shared.install import determine_timezone, generate_confs
4142

4243

@@ -56,7 +57,9 @@ def create_dummy_gpwnam(pw_uid, pw_gid):
5657

5758

5859
def noop(*args, **kwargs):
59-
pass
60+
if args:
61+
return args[0]
62+
return None
6063

6164

6265
class MigSharedInstall__determine_timezone(MigTestCase):
@@ -161,6 +164,64 @@ def test_creates_output_directory_containing_a_standard_local_configuration(self
161164
expected_file = os.path.join(fixture_dir, file_name)
162165
self.assertFileContentIdentical(actual_file, expected_file)
163166

167+
def test_options_for_source_auto(self):
168+
options = generate_confs(
169+
source=keyword_auto,
170+
_getcwd=lambda: '/some/arbitrary/path',
171+
_getpwnam=create_dummy_gpwnam(4321, 1234),
172+
_prepare=noop,
173+
_writefiles=noop,
174+
_instructions=noop,
175+
)
176+
expected_template_dir = os.path.join(MIG_BASE, 'mig/install')
177+
178+
self.assertEqual(options['template_dir'], expected_template_dir)
179+
180+
def test_options_for_source_relative(self):
181+
options = generate_confs(
182+
source='.',
183+
_getcwd=lambda: '/current/working/directory/mig/install',
184+
_getpwnam=create_dummy_gpwnam(4321, 1234),
185+
_prepare=noop,
186+
_writefiles=noop,
187+
_instructions=noop,
188+
)
189+
190+
self.assertEqual(options['template_dir'],
191+
'/current/working/directory/mig/install')
192+
193+
def test_options_for_destination_auto(self):
194+
options = generate_confs(
195+
destination=keyword_auto,
196+
destination_suffix='_suffix',
197+
_getcwd=lambda: '/some/arbitrary/path',
198+
_getpwnam=create_dummy_gpwnam(4321, 1234),
199+
_prepare=noop,
200+
_writefiles=noop,
201+
_instructions=noop,
202+
)
203+
204+
self.assertEqual(options['destination_link'],
205+
'/some/arbitrary/path/confs')
206+
self.assertEqual(options['destination_dir'],
207+
'/some/arbitrary/path/confs_suffix')
208+
209+
def test_options_for_destination_relative(self):
210+
options = generate_confs(
211+
destination='generate-confs',
212+
destination_suffix='_suffix',
213+
_getcwd=lambda: '/current/working/directory',
214+
_getpwnam=create_dummy_gpwnam(4321, 1234),
215+
_prepare=noop,
216+
_writefiles=noop,
217+
_instructions=noop,
218+
)
219+
220+
self.assertEqual(options['destination_link'],
221+
'/current/working/directory/generate-confs')
222+
self.assertEqual(options['destination_dir'],
223+
'/current/working/directory/generate-confs_suffix')
224+
164225

165226
if __name__ == '__main__':
166227
testmain()

0 commit comments

Comments
 (0)