[go: up one dir, main page]

File: osxbuild.py

package info (click to toggle)
dipy 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 14,828 kB
  • sloc: python: 63,790; makefile: 258; pascal: 167; sh: 131; ansic: 106
file content (152 lines) | stat: -rw-r--r-- 4,357 bytes parent folder | download | duplicates (2)
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
"""Python script to build the OSX universal binaries.

Stolen with thankfulness from the numpy distribution

This is a simple script, most of the heavy lifting is done in bdist_mpkg.

To run this script:  'python build.py'

Installer is built using sudo so file permissions are correct when installed on
user system.  Script will prompt for sudo pwd.

"""

import os
import sys
import shutil
import subprocess
from optparse import OptionParser
from getpass import getuser

# USER_README = 'docs/README.rst'
# DEV_README = SRC_DIR + 'README.rst'

BUILD_DIR = 'build'
DIST_DIR = 'dist'
DIST_DMG_DIR = 'dist-dmg'


def remove_dirs(sudo):
    print('Removing old build and distribution directories...')
    print("""The distribution is built as root, so the files have the correct
    permissions when installed by the user.  Chown them to user for removal.""")
    if os.path.exists(BUILD_DIR):
        cmd = 'chown -R %s %s' % (getuser(), BUILD_DIR)
        if sudo:
            cmd = 'sudo ' + cmd
        shellcmd(cmd)
        shutil.rmtree(BUILD_DIR)
    if os.path.exists(DIST_DIR):
        cmd = 'sudo chown -R %s %s' % (getuser(), DIST_DIR)
        if sudo:
            cmd = 'sudo ' + cmd
        shellcmd(cmd)
        shutil.rmtree(DIST_DIR)


def build_dist(readme, python_exe, sudo):
    print('Building distribution... (using sudo)')
    cmd = '%s setup_egg.py bdist_mpkg --readme=%s' % (
        python_exe, readme)
    if sudo:
        cmd = 'sudo ' + cmd
    shellcmd(cmd)


def build_dmg(sudo):
    print('Building disk image...')
    # Since we removed the dist directory at the start of the script,
    # our pkg should be the only file there.
    pkg = os.listdir(DIST_DIR)[0]
    fn, ext = os.path.splitext(pkg)
    dmg = fn + '.dmg'
    srcfolder = os.path.join(DIST_DIR, pkg)
    dstfolder = os.path.join(DIST_DMG_DIR, dmg)
    # build disk image
    try:
        os.mkdir(DIST_DMG_DIR)
    except OSError:
        pass
    try:
        os.unlink(dstfolder)
    except OSError:
        pass
    cmd = 'hdiutil create -srcfolder %s %s' % (srcfolder, dstfolder)
    if sudo:
        cmd = 'sudo ' + cmd
    shellcmd(cmd)


def copy_readme():
    """Copy a user README with info regarding the website, instead of
    the developer README which tells one how to build the source.
    """
    print('Copy user README.rst for installer.')
    shutil.copy(USER_README, DEV_README)


def revert_readme():
    """Revert the developer README."""
    print('Reverting README.rst...')
    cmd = 'svn revert %s' % DEV_README
    shellcmd(cmd)


def shellcmd(cmd, verbose=True):
    """Call a shell command."""
    if verbose:
        print(cmd)
    try:
        subprocess.check_call(cmd, shell=True)
    except subprocess.CalledProcessError as err:
        msg = """
        Error while executing a shell command.
        %s
        """ % str(err)
        raise Exception(msg)


def build():
    parser = OptionParser()
    parser.add_option("-p", "--python", dest="python",
                      default=sys.executable,
                      help="python interpreter executable",
                      metavar="PYTHON_EXE")
    parser.add_option("-r", "--readme", dest="readme",
                      default='README.rst',
                      help="README file",
                      metavar="README")
    parser.add_option("-s", "--sudo", dest="sudo",
                      default=False,
                      help="Run as sudo or no",
                      metavar="SUDO")
    (options, args) = parser.parse_args()
    try:
        src_dir = args[0]
    except IndexError:
        src_dir = '.'
    # Check source directory
    if not os.path.isfile(os.path.join(src_dir, 'setup.py')):
        raise RuntimeError('Run this script from directory '
                           'with setup.py, or pass in this '
                           'directory on command line')
    # update end-user documentation
    # copy_readme()
    # shellcmd("svn stat %s"%DEV_README)

    # change to source directory
    cwd = os.getcwd()
    os.chdir(src_dir)

    # build distribution
    remove_dirs(options.sudo)
    build_dist(options.readme, options.python, options.sudo)
    build_dmg(options.sudo)

    # change back to original directory
    os.chdir(cwd)
    # restore developer documentation
    # revert_readme()

if __name__ == '__main__':
    build()