Last active
June 9, 2020 07:45
-
-
Save maxfischer2781/98158807ed21449cc1659ffaaaa75972 to your computer and use it in GitHub Desktop.
Copy a gridmapdir to a new location, preserving its mappings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Copy a gridmapdir to a new location, preserving its mappings | |
This script clones a gridmapdir, taking into account relative hardlinks. | |
This allows copying a gridmapdir to a different device, without losing mappings | |
from identities to accounts. | |
.. code:: bash | |
python clone_gridmapdir.py /etc/grid-security/gridmapdir/ /tmp/gridmapdir/ | |
""" | |
from __future__ import print_function | |
import sys | |
import os | |
import argparse | |
def touch(path, times=None): | |
with open(path, 'a'): | |
os.utime(path, times) | |
CLI = argparse.ArgumentParser( | |
description='Copy a gridmapdir and preserve its mappings', | |
) | |
CLI.add_argument( | |
'ORIGIN', | |
help='path of the original gridmapdir such as "/etc/grid-security/gridmapdir/"', | |
) | |
CLI.add_argument( | |
'DESTINATION', | |
help='path to copy gridmapdir into such as "/tmp/gridmapdir/', | |
) | |
options = CLI.parse_args() | |
origin = options.ORIGIN | |
destination = options.DESTINATION | |
assert os.path.exists(destination), ('Destination path %r must exist!' % destination) | |
accounts = {} | |
identities = {} | |
for fname in os.listdir(origin): | |
inode = os.stat(os.path.join(origin, fname)).st_ino | |
if fname.startswith('%'): | |
identities[inode] = fname | |
else: | |
accounts[inode] = fname | |
for step, fname in enumerate(accounts.values(), start=1): | |
touch(os.path.join(destination, fname)) | |
sys.stderr.write('%s/%s\r' % (step, len(accounts))) | |
print('created', len(accounts), 'account files') | |
for inode, fname in identities.items(): | |
account_fname = accounts[inode] | |
os.link( | |
os.path.join(destination, account_fname), | |
os.path.join(destination, fname), | |
) | |
print('alias', fname, '=>', account_fname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment