Source code for libka.utils.get_orig_tarball_extension
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# pylint: disable=line-too-long
# kate: space-indent on; indent-width 4; replace-tabs on; indent-mode python; remove-trailing-space modified;
# vim: expandtab ts=4
# pylint: enable=line-too-long
############################################################################
# Copyright © 2023 José Manuel Santamaría Lema <panfaust@gmail.com> #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
############################################################################
"""
This module provides the `get_orig_tarball_extension` function,
useful to find out the upstream tarball extension.
"""
[docs]
def get_orig_tarball_extension(path):
"""
This function returns the extension for the given path. Examples:
```
get_orig_tarball_extension('/my/file.odf') # returns 'odf'
get_orig_tarball_extension('/my/file.tgz') # returns 'tgz'
get_orig_tarball_extension('/my/file.tar.gz') # returns 'tar.gz'
get_orig_tarball_extension('/my/file.orig.tar.gz') # returns 'tar.gz'
```
"""
#Split path and find out length
split_path = path.split('.')
len_split_path = len(split_path)
#Find out extension
if len_split_path == 1:
extension = ''
elif len_split_path == 2:
extension = split_path[-1]
else: #len_split_path > 2
if split_path[-2] == 'orig':
#Discard orig in things such as file.orig.tgz
extension = split_path[-1]
else:
extension = '.'.join(split_path[-2:])
#Return result
return extension
[docs]
def main():
"""quick test of function"""
assert(get_orig_tarball_extension('/my/file') == '')
assert(get_orig_tarball_extension('/my/file.odf') == 'odf')
assert(get_orig_tarball_extension('/my/file.tgz') == 'tgz')
assert(get_orig_tarball_extension('/my/file.tar.gz') == 'tar.gz')
assert(get_orig_tarball_extension('/my/file.orig.tgz') == 'tgz')
assert(get_orig_tarball_extension('/my/file.orig.tar.gz') == 'tar.gz')
if __name__ == "__main__":
main()