File: python3HB.py

package info (click to toggle)
klayout 0.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292,204 kB
  • sloc: cpp: 2,068,428; ruby: 47,823; xml: 26,924; python: 14,404; sh: 1,812; tcl: 212; perl: 170; makefile: 112; ansic: 42
file content (175 lines) | stat: -rwxr-xr-x 6,568 bytes parent folder | download
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#==============================================================================
# File: macbuild/python3HB.py
#
# Descriptions: A handy tool to setup the standardized directory structures
#               for Homebrew's Python 3.x
#==============================================================================
import os
import sys
import platform
import optparse

#------------------------------------------------------------------------------
# Set global variables
#------------------------------------------------------------------------------
def SetGlobals():
    global DefaultHomebrewRoot
    global Usage

    (System, Node, Release, MacVersion, Machine, Processor) = platform.uname()
    if Machine == "arm64": # Apple Silicon!
      DefaultHomebrewRoot = '/opt/homebrew'
    else:
      DefaultHomebrewRoot = '/usr/local'
    del System, Node, Release, MacVersion, Machine, Processor

    Usage  = "\n"
    Usage += "----------------------------------------------------------------------------------------\n"
    Usage += "<< Usage of 'python3HB.py' >>\n"
    Usage += "   to setup the standardized directory structures for Homebrew's Python 3.x on Mac\n"
    Usage += "\n"
    Usage += "   option & argument      : descriptions                              | default value\n"
    Usage += "   -------------------------------------------------------------------+---------------\n"
    Usage += "   <-v|--version <number>>: in ['3.8', '3.9', '3.10', '3.11', '3.12', | ''\n"
    Usage += "                                '3.13']                               |\n"
    Usage += "   [-u|-unlink]           : unlink only                               | disabled\n"
    Usage += "   [-?|--?]               : print this usage and exit                 | disabled\n"
    Usage += "----------------------------------------------------------------------+-----------------\n"

#------------------------------------------------------------------------------
# Parse the command line arguments
#------------------------------------------------------------------------------
def Parse_CLI_Args():
    global Version
    global UnlinkOnly

    p = optparse.OptionParser( usage=Usage )

    p.add_option( '-v', '--version',
                  dest='version',
                  help="python3 version=['3.8', '3.9', '3.10', '3.11', '3.12', '3.13']" )

    p.add_option( '-u', '--unlink',
                  action='store_true',
                  dest='unlink',
                  default=False,
                  help='unlink only' )

    p.add_option( '-?', '--??',
                  action='store_true',
                  dest='checkusage',
                  default=False,
                  help='check usage' )

    p.set_defaults( version    = "",
                    unlink     = False,
                    checkusage = False )

    opt, args = p.parse_args()
    if (opt.checkusage):
        print(Usage)
        sys.exit(0)

    Version    = opt.version
    UnlinkOnly = opt.unlink
    if not Version in [ '3.8', '3.9', '3.10', '3.11', '3.12', '3.13' ]:
        print( "! Unsupported Python 3 version <%s>" % Version )
        print(Usage)
        sys.exit(0)

#------------------------------------------------------------------------------
# Set the directory structures
#------------------------------------------------------------------------------
def SetDirectoryStructures():
    #----------------------------------------------------------
    # [1] Check the root directory of python@${Version}
    #----------------------------------------------------------
    root = "%s/opt/python@%s" % (DefaultHomebrewRoot, Version)
    if not os.path.isdir(root):
        print( "! Found no such a directory <%s>" % root )
        sys.exit(0)

    #----------------------------------------------------------
    # [2] Go to "lib/" and make
    #     Python.framework -> ../Frameworks/Python.framework/
    #----------------------------------------------------------
    os.chdir( root )
    os.chdir( "lib/" )
    try:
        os.remove( "Python.framework" )
    except FileNotFoundError:
        pass
    if not UnlinkOnly:
        os.symlink( "../Frameworks/Python.framework/", "Python.framework" )

    #----------------------------------------------------------
    # [3] Go to "bin/" and make
    #     ./python${version} -> python3
    #     ./pip${version}    -> pip3
    #----------------------------------------------------------
    os.chdir( root )
    os.chdir( "bin/" )
    try:
        os.remove( "python3" )
        os.remove( "pip3" )
    except FileNotFoundError:
        pass
    if not UnlinkOnly:
        os.symlink( "./python%s" % Version, "python3" )
        os.symlink( "./pip%s"    % Version, "pip3" )

    #----------------------------------------------------------
    # [4] Go to "Frameworks/Python.framework/" and delete
    #     three symbolic links
    #----------------------------------------------------------
    os.chdir( root )
    os.chdir( "Frameworks/Python.framework/" )
    try:
        os.remove( "Headers" )
        os.remove( "Resources" )
        os.remove( "Python" )
    except FileNotFoundError:
        pass

    #----------------------------------------------------------
    # [5] Go to "Versions/" and make
    #     Current -> ${Version}/
    #----------------------------------------------------------
    os.chdir( root )
    os.chdir( "Frameworks/Python.framework/Versions/" )
    try:
        os.remove( "Current" )
    except FileNotFoundError:
        pass
    if not UnlinkOnly:
        os.symlink( "%s/" % Version, "Current" )

    #----------------------------------------------------------
    # [6] Go to "Frameworks/Python.framework/" and make
    #     three symbolic links
    #----------------------------------------------------------
    if not UnlinkOnly:
        os.chdir( root )
        os.chdir( "Frameworks/Python.framework/" )
        os.symlink( "Versions/Current/Headers/",   "Headers" )
        os.symlink( "Versions/Current/Resources/", "Resources" )
        os.symlink( "Versions/Current/Python",     "Python" )

#------------------------------------------------------------------------------
# The main function
#------------------------------------------------------------------------------
def Main():
    SetGlobals()
    Parse_CLI_Args()
    SetDirectoryStructures()

#===================================================================================
if __name__ == "__main__":
    Main()

#---------------
# End of file
#---------------