-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_CompressTars_2_7z-Linux.py
139 lines (103 loc) · 5.39 KB
/
_CompressTars_2_7z-Linux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
##################################################################################
# This script uses 7-zip (standard windows installation path) to compress the #
# uncompressed tar-archives which were captured by the PyCam2-Server as 7z. #
# Note: #
# Tar-archives with a size of 21GB can be compressed to 1.4GB as most of the #
# images are dark. #
# #
# How to use (variable explanation): #
# xCmd: Path to the 7-zip executable. #
# xArg: 7-zip arguments (see options below). #
# globSearch4TarName: The script filters the files using this filter-string #
# (can contian reg-exp). #
# targetArchiveName: 7-zip compresses the filtered files into an 7z-archive #
# with this filename into the same folder. #
# verbose: When true, extra information is printed to the console. #
# wds: A list of strings with the working-directories which are #
# scanned iteratively for tar-archives using the #
# globSearch4Tarname filter. #
# #
# 2023 © haum (OTH-Regensburg) #
##################################################################################
# Imports
import sys
import os
from os.path import join, basename, dirname, abspath
import subprocess
import glob
import natsort
##########################################################
# Script to extract the PiCam tar.gz to a Pics folder #
# Tested on Win10 with standard-installation-path #
##########################################################
### Command-Line generator (GIT): https://github.com/axelstudios/7z
### Command-Line generator: https://axelstudios.github.io/7z/#!/
## Left side
# Archive format: 7z
# Compression Level: Ultra
# Compression Method: LZMA2
# Dictionary size: 64M
# Word size: 64
# Solid Block Size: 4G
# Number of CPU Threads: 16
# RAM-Usage Compression: 8797M
# RAM-Usage Decompression: 66M
## Right side
# Update mode: Add an replace files
# Path Mode: Relative pathnames
# Options - Create SFX: false
# Options - Compress shared: false
# Options - Delete files after compression: true
# Encryption: None
## Resulting Cmd-Line
# 7z a -mx9 -md128m -mfb128 -mmt24 -sdel <archive_name> [<file_names>...]
###### USER AREA ######
xCmd = '/usr/bin/7z' # Path to 7zip
xArg = 'a -mx9 -sdel' # 7-zip will delete the compressed tar-archives after successful compression
# xArg = 'a -mx9' # 7-zip will keep the tars (test-flags for debug-reasons)
user = 'smbuser' # When using another user to compress, the python-script needs to be run by a sudo-user (without password!)
globSearch4TarName = '*_rPiHQCam2-*.tar'
targetArchiveName = 'Dev101_rPiHQCam2-tars.7z'
verbose = True
wds = [
# Your (parent)-folderpaths go here
r"<Drive>\<Input Pics folderpath here>", # Topmost Parent --> Scans the child-folders iteratively
]
SkipBadSubdirs = False
###### DO NOT TOUCH AREA ######
_XXBadDirs = list()
for wd in wds:
os.chdir(wd)
wd = os.getcwd()
print(f"wd is now: {wd}")
for root, dirs, files in os.walk("."):
# Firstly check if path contains one of the already marked bad measurement-folders
if any(root.__contains__(_bDir) for _bDir in _XXBadDirs):
print("Bad parent - skipped:" + root)
continue
# Folder marked as bad measurement -> Skip
if root.endswith("_XX"):
if SkipBadSubdirs == True:
_XXBadDirs.append(root)
print("Skipping:" + root)
continue
print(f"Scanning for tars in: \"{root}\"".ljust(100), end="")
tarFiles = glob.glob(join(root, globSearch4TarName))
tarFiles = natsort.os_sorted(tarFiles)
nTars = len(tarFiles)
if nTars > 0:
print("") # new line
print(f" - Found {nTars} tar-archives")
archivePath = join(root, targetArchiveName)
print(f" - Archiving to: {archivePath}")
# cmprsFiles = '" "'.join(tarFiles) # First and last " is missing, but added in cmd-concatenation!
cmd = f'sudo -u "{user}" "{xCmd}" {xArg} "{archivePath}" "{root}/*.tar"'
if verbose:
print(f" - Cmd: {cmd}")
print(f" - Starting compression...")
subprocess.call(cmd, shell=True, cwd=wd) #, stdout=sys.stdout, stderr=sys.stderr)
print(" - Compression done")
else:
print(f"Nothing found, going on...")
continue
print(f"Done with {root}. Going on...")