Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
iterate through possible config file locations
Browse files Browse the repository at this point in the history
incorporate other PR comments from Vic:
1. use os.path.join properly
2. put config.yml in /usr/share/calyptos

this also copies config.yml to the ~/.calyptos directory
if there IS NOT a calyptos RPM package installed. this
indicates that the user has used setup.py to install as
opposed to rpm/yum, and we are allowing for the possibility
that in a dev environment they may want to edit the config.
  • Loading branch information
mbacchi committed Jun 3, 2015
1 parent b251390 commit c878e0e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
52 changes: 37 additions & 15 deletions bin/calyptos
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,46 @@ import os
import shutil

dot_dir = os.path.expanduser('~/.calyptos')
default_cfgfile = os.path.join(dot_dir + "/" + "config.yml")
# by default we look in users home dir first
default_cfgfile = os.path.join(dot_dir, "config.yml")
cfglist = []
cfglist.append(default_cfgfile)
cfglist.append('/usr/share/calyptos/config.yml')

def locate_cfgfile():
if not os.path.isfile(default_cfgfile):
if not os.path.exists(dot_dir):
try:
os.makedirs(dot_dir)
except OSError as e:
print('Error: cannot create directory ~/.calyptos: %s' % e)
try:
shutil.copy('/usr/share/calyptos/etc/config.yml', default_cfgfile)
except shutil.Error as e:
print('Error: %s' % e)
except IOError as e:
print('Error: %s' % e.strerror)
# if we find that the config file is in /usr/share
# but the rpm is not installed, we copy it to ~/.calyptos
# because that means calyptos was installed by setup.py
for item in cfglist:
if os.path.isfile(item):
if item != default_cfgfile:
import rpm # I think this is required by other parts of python so I'm safe using it
ts = rpm.TransactionSet()
mi = ts.dbMatch("name","calyptos")
if (len(mi) == 0):
if not os.path.isfile(default_cfgfile):
if not os.path.exists(dot_dir):
try:
os.makedirs(dot_dir)
except OSError as e:
print('Error: cannot create directory ~/.calyptos: %s' % e)
try:
# if this copy works, return default_cfgfile
# because it will be used going forward
shutil.copy('/usr/share/calyptos/config.yml', default_cfgfile)
return default_cfgfile
except shutil.Error as e:
print('Error: %s' % e)
except IOError as e:
print('Error: %s' % e.strerror)
# this is not in an else: block because its a
# last resort for the case where import rpm
# doesn't succeed above, because we still need
# to return a valid config file name
return item

if __name__ == '__main__':
locate_cfgfile()
cfgfile = locate_cfgfile()
parser = argparse.ArgumentParser()
default_repo = 'https://github.com/eucalyptus/eucalyptus-cookbook'
parser.add_argument('operation', choices=['validate',
Expand All @@ -33,7 +55,7 @@ if __name__ == '__main__':
'provision',
'debug',
'uninstall'])
parser.add_argument('-c', '--config', default=default_cfgfile)
parser.add_argument('-c', '--config', default=cfgfile)
parser.add_argument('-e', '--environment', default='etc/environment.yml')
parser.add_argument('-p', '--password', default='foobar')
parser.add_argument('-b', '--branch', default='euca-4.2')
Expand Down
2 changes: 1 addition & 1 deletion calyptos.spec
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ rm -rf $RPM_BUILD_ROOT
/usr/bin/calyptos
%{python_sitelib}/calyptos/*
%{python_sitelib}/*.egg-info
/usr/share/calyptos/etc/*
/usr/share/calyptos/config.yml
/usr/share/calyptos/examples/*
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ def make_release_tree(self, base_dir, files):
'build_py': build_py_with_git_version,
'sdist': sdist_with_git_version
},
data_files=[('/usr/share/calyptos/etc', ['etc/config.yml']),
data_files=[('/usr/share/calyptos', ['etc/config.yml']),
('/usr/share/calyptos/examples/', example_items)],
)

0 comments on commit c878e0e

Please sign in to comment.