cpython/Tools/scripts/mkreal.py

66 lines
1.4 KiB
Python
Raw Normal View History

1993-04-01 16:46:40 -04:00
#! /usr/local/bin/python
1991-06-04 17:36:54 -03:00
# mkreal
#
# turn a symlink to a directory into a real directory
import sys
1992-03-30 07:13:59 -04:00
import os
1991-06-04 17:36:54 -03:00
from stat import *
1992-03-30 07:13:59 -04:00
join = os.path.join
1991-06-04 17:36:54 -03:00
error = 'mkreal error'
BUFSIZE = 32*1024
def mkrealfile(name):
1992-03-30 07:13:59 -04:00
st = os.stat(name) # Get the mode
1991-06-04 17:36:54 -03:00
mode = S_IMODE(st[ST_MODE])
1992-03-30 07:13:59 -04:00
linkto = os.readlink(name) # Make sure again it's a symlink
1991-06-04 17:36:54 -03:00
f_in = open(name, 'r') # This ensures it's a file
1992-03-30 07:13:59 -04:00
os.unlink(name)
1991-06-04 17:36:54 -03:00
f_out = open(name, 'w')
while 1:
buf = f_in.read(BUFSIZE)
if not buf: break
f_out.write(buf)
del f_out # Flush data to disk before changing mode
1992-03-30 07:13:59 -04:00
os.chmod(name, mode)
1991-06-04 17:36:54 -03:00
def mkrealdir(name):
1992-03-30 07:13:59 -04:00
st = os.stat(name) # Get the mode
1991-06-04 17:36:54 -03:00
mode = S_IMODE(st[ST_MODE])
1992-03-30 07:13:59 -04:00
linkto = os.readlink(name)
files = os.listdir(name)
os.unlink(name)
os.mkdir(name, mode)
os.chmod(name, mode)
linkto = join(os.pardir, linkto)
1991-06-04 17:36:54 -03:00
#
for file in files:
1992-03-30 07:13:59 -04:00
if file not in (os.curdir, os.pardir):
os.symlink(join(linkto, file), join(name, file))
1991-06-04 17:36:54 -03:00
def main():
sys.stdout = sys.stderr
1992-03-30 07:13:59 -04:00
progname = os.path.basename(sys.argv[0])
1993-04-01 16:46:40 -04:00
if progname == '-c': progname = 'mkreal'
1991-06-04 17:36:54 -03:00
args = sys.argv[1:]
if not args:
print 'usage:', progname, 'path ...'
sys.exit(2)
status = 0
for name in args:
1992-03-30 07:13:59 -04:00
if not os.path.islink(name):
1991-06-04 17:36:54 -03:00
print progname+':', name+':', 'not a symlink'
status = 1
else:
1992-03-30 07:13:59 -04:00
if os.path.isdir(name):
1991-06-04 17:36:54 -03:00
mkrealdir(name)
else:
mkrealfile(name)
sys.exit(status)
main()