detect site conf file, use /etc/mic/mic.conf by default
[tools/mic.git] / setup.py
1 #!/usr/bin/env python
2
3 import os, sys
4 import glob
5 from distutils.core import setup
6 try:
7     import setuptools
8     # enable "setup.py develop", optional
9 except ImportError:
10     pass
11
12 MOD_NAME = 'mic'
13
14 version_path = 'VERSION'
15 if not os.path.isfile(version_path):
16     print 'No VERSION file in topdir, abort'
17     sys.exit(1)
18
19 try:
20     # first line should be the version number
21     version = open(version_path).readline().strip()
22     if not version:
23         print 'VERSION file is invalid, abort'
24         sys.exit(1)
25
26     ver_file = open('%s/__version__.py' % MOD_NAME, 'w')
27     ver_file.write("VERSION = \"%s\"\n" % version)
28     ver_file.close()
29 except IOError:
30     print 'WARNING: Cannot write version number file'
31
32 # --install-layout is recognized after 2.5
33 if sys.version_info[:2] > (2, 5):
34     if len(sys.argv) > 1 and 'install' in sys.argv:
35         try:
36             import platform
37             (dist, ver, id) = platform.linux_distribution()
38
39             # for debian-like distros, mods will be installed to
40             # ${PYTHONLIB}/dist-packages
41             if dist in ('debian', 'Ubuntu'):
42                 sys.argv.append('--install-layout=deb')
43         except:
44             pass
45
46 PACKAGES = [MOD_NAME,
47             MOD_NAME + '/utils',
48             MOD_NAME + '/imager',
49             MOD_NAME + '/kickstart',
50             MOD_NAME + '/kickstart/custom_commands',
51             MOD_NAME + '/3rdparty/pykickstart',
52             MOD_NAME + '/3rdparty/pykickstart/commands',
53             MOD_NAME + '/3rdparty/pykickstart/handlers',
54             MOD_NAME + '/3rdparty/pykickstart/urlgrabber',
55            ]
56
57 IMAGER_PLUGINS = glob.glob(os.path.join("plugins", "imager", "*.py"))
58 BACKEND_PLUGINS = glob.glob(os.path.join("plugins", "backend", "*.py"))
59
60 # the following code to do a simple parse for '--prefix' opts
61 prefix = sys.prefix
62 is_next = False
63 for arg in sys.argv:
64     if is_next:
65         prefix = arg
66         break
67     if '--prefix=' in arg:
68         prefix = arg[9:]
69         break
70     elif '--prefix' == arg:
71         is_next = True
72
73 # expand the path, for user may type some unexcepted format
74 prefix = os.path.abspath(os.path.expanduser(prefix)).rstrip('/')
75 if prefix.lstrip('/') == 'usr':
76     etc_prefix = '/etc'
77 else:
78     etc_prefix = os.path.join(prefix, 'etc')
79
80 # apply prefix to mic.conf.in to generate actual mic.conf
81 conf_str = file('distfiles/mic.conf.in').read()
82 conf_str = conf_str.replace('@PREFIX@', prefix)
83 with file('distfiles/mic.conf', 'w') as wf:
84     wf.write(conf_str)
85
86 try:
87     setup(name=MOD_NAME,
88           version = version,
89           description = 'Image Creator for Linux Distributions',
90           author='Jian-feng Ding, Qiang Zhang, Gui Chen',
91           author_email='jian-feng.ding@intel.com, qiang.z.zhang@intel.com, gui.chen@intel.com',
92           url='https://github.com/jfding/mic',
93           scripts=[
94               'tools/mic',
95               ],
96           packages = PACKAGES,
97           data_files = [("%s/lib/mic/plugins/imager" % prefix, IMAGER_PLUGINS),
98                         ("%s/lib/mic/plugins/backend" % prefix, BACKEND_PLUGINS),
99                         ("%s/mic" % etc_prefix, ["distfiles/mic.conf"])]
100     )
101 finally:
102     # remove dynamic file distfiles/mic.conf
103     os.unlink('distfiles/mic.conf')
104