|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +r""" |
| 4 | +Python script to create a tarball for the sage-package ``database_knotinfo`` |
| 5 | +in the given path. This utility should be used in case of a switch to a |
| 6 | +new version of the data files (that is if the original files on the |
| 7 | +KnotInfo LinkInfo web-page have changed). In that case an invocation of |
| 8 | +``sage -package update database_knotinfo <new version>`` and |
| 9 | +``sage -package fix-checksum database_knotinfo`` will be necessary. |
| 10 | +
|
| 11 | +..NOTE:: |
| 12 | +
|
| 13 | + This function demands the Python package ``pandas``, ``xlrd`` and |
| 14 | + ``xlsx2csv`` to be installed. If not you have to run:: |
| 15 | +
|
| 16 | + pip install pandas |
| 17 | + pip install xlrd |
| 18 | + pip install xlsx2csv |
| 19 | +
|
| 20 | + before using this function. |
| 21 | +
|
| 22 | +INPUT: |
| 23 | +
|
| 24 | +- ``version`` -- string, name of the new version to be created |
| 25 | + (by default date of the day of creation) |
| 26 | +- ``path`` -- string of the path where the tarball should be stored |
| 27 | + (by default ``pwd``) |
| 28 | +
|
| 29 | +EXAMPLES:: |
| 30 | +
|
| 31 | + ~/sage $ build/pkgs/database_knotinfo/create_knotinfo_tarball.py 20210201 upstream |
| 32 | + src/ |
| 33 | + src/knotinfo_data_complete.csv |
| 34 | + src/linkinfo_data_complete.csv |
| 35 | +""" |
| 36 | + |
| 37 | +import sys, os |
| 38 | +from xlsx2csv import Xlsx2csv |
| 39 | +from pandas import read_excel |
| 40 | + |
| 41 | +############################################################################## |
| 42 | +# Copyright (C) 2021 Sebastian Oehms <[email protected]> |
| 43 | +# |
| 44 | +# This program is free software: you can redistribute it and/or modify |
| 45 | +# it under the terms of the GNU General Public License as published by |
| 46 | +# the Free Software Foundation, either version 2 of the License, or |
| 47 | +# (at your option) any later version. |
| 48 | +# http://www.gnu.org/licenses/ |
| 49 | +############################################################################## |
| 50 | + |
| 51 | + |
| 52 | +cmdline_args = sys.argv[1:] |
| 53 | + |
| 54 | +version = None |
| 55 | +path = None |
| 56 | + |
| 57 | +if len(cmdline_args) > 1: |
| 58 | + path = cmdline_args[1] |
| 59 | + |
| 60 | +if len(cmdline_args) > 0: |
| 61 | + version = cmdline_args[0] |
| 62 | + |
| 63 | + |
| 64 | +if not version: |
| 65 | + from datetime import datetime |
| 66 | + version = str(datetime.today().date()).replace('-','') |
| 67 | + |
| 68 | +if not path: |
| 69 | + path = os.environ['PWD'] |
| 70 | + |
| 71 | +path_temp = os.path.join(path, 'special_knotinfo_spkg_temp_dir') |
| 72 | +path_src = os.path.join(path_temp, 'src') |
| 73 | +os.makedirs(path_temp) |
| 74 | +os.makedirs(path_src) |
| 75 | + |
| 76 | +def convert(path_src, url, filen, reader): |
| 77 | + if reader == Xlsx2csv: |
| 78 | + excel = filen + '.xlsx' |
| 79 | + else: |
| 80 | + excel = filen + '.xls' |
| 81 | + csv = filen + '.csv' |
| 82 | + inp = os.path.join(url, excel) |
| 83 | + out = os.path.join(path_src, csv) |
| 84 | + if reader == Xlsx2csv: |
| 85 | + from six.moves.urllib.request import urlopen |
| 86 | + f = urlopen(inp) |
| 87 | + url_data = f.read() |
| 88 | + temp_file = os.path.join(path_temp, 'temp.xlsx') |
| 89 | + f = open(temp_file, 'wt') |
| 90 | + f.write(url_data) |
| 91 | + f.close() |
| 92 | + data = reader(temp_file, delimiter='|', skip_empty_lines=True) |
| 93 | + data.convert(out) |
| 94 | + else: |
| 95 | + data = reader(inp) |
| 96 | + data.to_csv(out, sep='|', index=False) |
| 97 | + |
| 98 | +# first KnotInfo (using pandas and xlrd) |
| 99 | +convert(path_src, 'https://knotinfo.math.indiana.edu/', 'knotinfo_data_complete', read_excel) |
| 100 | + |
| 101 | +# now LinkInfo (using xlsx2csv) |
| 102 | +convert(path_src, 'https://linkinfo.sitehost.iu.edu/', 'linkinfo_data_complete', Xlsx2csv) |
| 103 | + |
| 104 | +tar_file = 'knotinfo-%s.tar.bz2' %version |
| 105 | +path_tar = os.path.join(path_temp, tar_file) |
| 106 | + |
| 107 | +os.system('cd %s; tar -cvjSf %s src' %(path_temp, tar_file)) |
| 108 | +os.system('mv %s %s; rm -rf %s' %(path_tar, path, path_temp)) |
0 commit comments