| 
									
										
										
										
											2010-03-11 22:53:45 +00:00
										 |  |  | #! /usr/bin/env python3 | 
					
						
							| 
									
										
										
										
											1991-06-04 20:36:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # mkreal | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # turn a symlink to a directory into a real directory | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											1992-03-30 11:13:59 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											1991-06-04 20:36:54 +00:00
										 |  |  | from stat import * | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-03-30 11:13:59 +00:00
										 |  |  | join = os.path.join | 
					
						
							| 
									
										
										
										
											1991-06-04 20:36:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | error = 'mkreal error' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BUFSIZE = 32*1024 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def mkrealfile(name): | 
					
						
							| 
									
										
										
										
											2001-01-17 08:48:39 +00:00
										 |  |  |     st = os.stat(name) # Get the mode | 
					
						
							|  |  |  |     mode = S_IMODE(st[ST_MODE]) | 
					
						
							|  |  |  |     linkto = os.readlink(name) # Make sure again it's a symlink | 
					
						
							| 
									
										
										
										
											2019-03-30 08:33:02 +02:00
										 |  |  |     with open(name, 'rb') as f_in: # This ensures it's a file | 
					
						
							|  |  |  |         os.unlink(name) | 
					
						
							|  |  |  |         with open(name, 'wb') as f_out: | 
					
						
							|  |  |  |             while 1: | 
					
						
							|  |  |  |                 buf = f_in.read(BUFSIZE) | 
					
						
							|  |  |  |                 if not buf: break | 
					
						
							|  |  |  |                 f_out.write(buf) | 
					
						
							| 
									
										
										
										
											2001-01-17 08:48:39 +00:00
										 |  |  |     os.chmod(name, mode) | 
					
						
							| 
									
										
										
										
											1991-06-04 20:36:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def mkrealdir(name): | 
					
						
							| 
									
										
										
										
											2001-01-17 08:48:39 +00:00
										 |  |  |     st = os.stat(name) # Get the mode | 
					
						
							|  |  |  |     mode = S_IMODE(st[ST_MODE]) | 
					
						
							|  |  |  |     linkto = os.readlink(name) | 
					
						
							|  |  |  |     files = os.listdir(name) | 
					
						
							|  |  |  |     os.unlink(name) | 
					
						
							|  |  |  |     os.mkdir(name, mode) | 
					
						
							|  |  |  |     os.chmod(name, mode) | 
					
						
							|  |  |  |     linkto = join(os.pardir, linkto) | 
					
						
							|  |  |  |     # | 
					
						
							| 
									
										
										
										
											2003-05-13 18:14:25 +00:00
										 |  |  |     for filename in files: | 
					
						
							|  |  |  |         if filename not in (os.curdir, os.pardir): | 
					
						
							|  |  |  |             os.symlink(join(linkto, filename), join(name, filename)) | 
					
						
							| 
									
										
										
										
											1991-06-04 20:36:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2001-01-17 08:48:39 +00:00
										 |  |  |     sys.stdout = sys.stderr | 
					
						
							|  |  |  |     progname = os.path.basename(sys.argv[0]) | 
					
						
							|  |  |  |     if progname == '-c': progname = 'mkreal' | 
					
						
							|  |  |  |     args = sys.argv[1:] | 
					
						
							|  |  |  |     if not args: | 
					
						
							| 
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 |  |  |         print('usage:', progname, 'path ...') | 
					
						
							| 
									
										
										
										
											2001-01-17 08:48:39 +00:00
										 |  |  |         sys.exit(2) | 
					
						
							|  |  |  |     status = 0 | 
					
						
							|  |  |  |     for name in args: | 
					
						
							|  |  |  |         if not os.path.islink(name): | 
					
						
							| 
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 |  |  |             print(progname+':', name+':', 'not a symlink') | 
					
						
							| 
									
										
										
										
											2001-01-17 08:48:39 +00:00
										 |  |  |             status = 1 | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             if os.path.isdir(name): | 
					
						
							|  |  |  |                 mkrealdir(name) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 mkrealfile(name) | 
					
						
							|  |  |  |     sys.exit(status) | 
					
						
							| 
									
										
										
										
											1991-06-04 20:36:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-09 17:27:55 +00:00
										 |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     main() |