Created
March 26, 2019 13:42
-
-
Save AdrienHorgnies/74988da5ef6c39cfde0d6ff2fa392118 to your computer and use it in GitHub Desktop.
Script to update symbolic links after republication (no longer used, here for posteriority)
This file contains 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/env python3 | |
import argparse | |
import logging | |
import os | |
import re | |
from datetime import datetime | |
republication_path = '/data/activity_resources/republication' | |
languages = {'AR_EG', 'BG_BG', 'CS_CZ', 'DA_DK', 'DE_DE', 'EL_GR', 'EN_GB', 'EN_US', 'ES_ES', 'ET_EE', 'FI_FI', 'FR_FR', 'GA_IE', 'HR_HR', 'HU_HU', 'ID_ID', 'IT_IT', 'LT_LT', 'LV_LV', 'MT_MT', 'NL_NL', 'PT_BR', 'PT_PT', 'PL_PL', 'RO_RO', 'RU_RU', 'SK_SK', 'SL_SI', 'SV_SE', 'TR_TR', 'NB_NO'} | |
def main(): | |
content_dir = get_content_dir() | |
log.debug('using content directory {}'.format(content_dir)) | |
available_dirs = os.listdir(os.path.join(republication_path, content_dir)) | |
log.debug('available language dirs : {}'.format(available_dirs)) | |
if args.languages is not None and not set(args.languages).issubset(set(available_dirs)): | |
log.debug('asked dirs : {}'.format(args.languages)) | |
log.critical('user specified languages {} that are not available in directory {}. Aborting, did nothing, retry with correct parameters'.format(set(args.languages) - set(available_dirs), content_dir)) | |
exit(1) | |
lg_dirs = args.languages if args.languages is not None else available_dirs | |
log.debug('using language directories: {}'.format(lg_dirs)) | |
for lg_dir in lg_dirs: | |
src_dir = os.path.join(republication_path, content_dir, lg_dir) | |
link = os.path.join(republication_path, 'current', lg_dir) | |
if not os.path.exists(link): | |
log.warning('{} does not exist and will be created'.format(link)) | |
elif not os.path.islink(link): | |
log.error('{} is not a symlink, it is skipped and left untouched'.format(link)) | |
continue | |
else: | |
log.debug('unlink {} -> {}'.format(link, os.readlink(link))) | |
if args.do: | |
os.unlink(link) | |
log.info('create symlink {} -> {}'.format(link, src_dir)) | |
if args.do: | |
os.symlink(src_dir, link) | |
if not args.do: | |
log.info('By default, script does nothing but logging, add option --do to do above actions') | |
def get_content_dir(): | |
if args.content_dir: | |
return args.content_dir | |
items = os.listdir(republication_path) | |
dirs = [item for item in items if os.path.isdir(item) and re.match(r'^[0-9]{12}$', item)] | |
latest = max(dirs, key=lambda x: datetime.strptime(x, '%Y%m%d%H%M')) | |
return latest | |
def configure_logging(): | |
log = logging.getLogger('relink') | |
log.setLevel(logging.DEBUG) | |
log_format = logging.Formatter(fmt='%(asctime)s %(name)s:%(lineno)d %(levelname)s %(message)s') | |
sh = logging.StreamHandler() | |
sh.setLevel(logging.INFO) | |
sh.setFormatter(log_format) | |
log.addHandler(sh) | |
if args.do: | |
fh = logging.FileHandler('relink.log') | |
fh.setLevel(logging.DEBUG) | |
fh.setFormatter(log_format) | |
log.addHandler(fh) | |
return log | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='This script updates symbolic links for the activities after republication.') | |
parser.add_argument('--languages', nargs='+', choices=list(languages), metavar='LOCALE_TAG', help='By default this scripts updates all languages available in content directory, this option specifies which languages to update.') | |
parser.add_argument('--content_dir', help='By default, this script takes the latest content directory, this option specifies which one to use.') | |
parser.add_argument('--do', default=False, action='store_true', help='By default, this script makes a dry run, this option makes it active. It will modify filesystem with this option') | |
args = parser.parse_args() | |
log = configure_logging() | |
log.info('starting at {}'.format(datetime.now().isoformat())) | |
main() | |
log.info('completed at {}'.format(datetime.now().isoformat())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment