Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. def extract_cmd(filepath, overwrite=False):
  2. """
  3. Determines the file type of file at filepath, returns extract cmd based on file suffix
  4. """
  5. filename = os.path.basename(filepath)
  6. exts = [x.lower() for x in filename.split('.')]
  7. target = '.'.join(exts[:-1])
  8. cmd_tmpl = None
  9.  
  10. extract_cmds = {
  11. # gzipped or gzipped tarball
  12. '.gz': "gunzip -c %(filepath)s > %(target)s",
  13. '.tar.gz': "tar xzf %(filepath)s",
  14. '.tgz': "tar xzf %(filepath)s",
  15. '.gtgz': "tar xzf %(filepath)s",
  16. # bzipped or bzipped tarball
  17. '.bz2': "bunzip2 %(filepath)s",
  18. '.tar.bz2': "tar xjf %(filepath)s",
  19. '.tbz': "tar xjf %(filepath)s",
  20. '.tbz2': "tar xjf %(filepath)s",
  21. '.tb2': "tar xjf %(filepath)s",
  22. # xzipped or xzipped tarball
  23. '.xz': "unxz %(filepath)s",
  24. '.tar.xz': "unxz %(filepath)s --stdout | tar x",
  25. '.txz': "unxz %(filepath)s --stdout | tar x",
  26. # tarball
  27. '.tar': "tar xf %(filepath)s",
  28. # zip file
  29. '.zip': "unzip -qq -o %(filepath)s" if overwrite else "unzip -qq %(filepath)s",
  30. # iso file
  31. '.iso': "7z x %(filepath)s",
  32. }
  33.  
  34. for i in extract_cmds.keys():
  35. if filename[-len(i):] == i:
  36. cmd_tmpl=extract_cmds[i]
  37.  
  38. if cmd_tmpl is None:
  39. raise EasyBuildError('Unknown file type for file %s (%s)', filepath, exts)
  40.  
  41. return cmd_tmpl % {'filepath': filepath, 'target': target}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement