Skip to content

Commit a32c900

Browse files
committed
Add new args to create_data_tar.py
Signed-off-by: Keerthi Gowda <kbalehal@qti.qualcomm.com>
1 parent ada317d commit a32c900

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

create_data_tar.py

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
create_data_tar.py
77
88
Standalone utility to:
9-
- Parse a .changes file (provided via --changes-file, or auto-detected)
10-
- Extract each referenced .deb into data/<pkg>/<arch>/
11-
- Pack the data/ directory as <changes_basename>.tar.gz in the same directory as the .changes file
9+
- Locate a .changes file via --path-to-changes (file path or directory; if directory, the newest .changes is selected)
10+
- Extract each referenced .deb into data/<pkg>/<arch>/ under the directory containing the .changes file
11+
- Pack the data/ directory as <changes_basename>.tar.gz
12+
- Place the tarball under <output-tar>/prebuilt_<distro>/ when --output-tar and --distro are provided; otherwise follow the fallback rules described in --output-tar help.
1213
"""
1314

1415
import os
@@ -28,46 +29,54 @@ def parse_arguments():
2829
description="Generate data.tar.gz by extracting deb contents to data/<pkg>/<arch>/ from a .changes file."
2930
)
3031
parser.add_argument(
31-
"--changes-file",
32+
"--path-to-changes",
3233
required=False,
33-
default="",
34-
help="Path to the .changes file. If not provided, the newest .changes in --output-dir will be used."
34+
default=".",
35+
help="Path to the .changes file or a directory containing .changes files. If a directory is provided, the newest .changes will be used."
3536
)
3637
parser.add_argument(
37-
"--output-dir",
38+
"--output-tar",
3839
required=False,
39-
default=".",
40-
help="Directory to search for the newest .changes when --changes-file is not provided. Also used as default working dir."
40+
default="",
41+
help="Base output directory where the tarball will be placed. When --distro is provided, the tarball will be written to <output-tar>/prebuilt_<distro>/"
4142
)
4243
parser.add_argument(
4344
"--arch",
4445
required=False,
4546
default="arm64",
4647
help="Architecture subfolder under each package directory (default: arm64)."
4748
)
49+
parser.add_argument(
50+
"--distro",
51+
required=False,
52+
default="",
53+
help="Target distro name (e.g., noble, questing). If provided, tar will be placed under <output-tar>/prebuilt_<distro>/"
54+
)
4855
return parser.parse_args()
4956

5057

51-
def find_changes_file(changes_file: str, output_dir: str) -> str:
58+
def find_changes_file(path_to_changes: str) -> str:
5259
"""
5360
Return the path to the .changes file to use.
54-
Priority:
55-
1) If changes_file is provided and exists, use it.
56-
2) Else, find newest *.changes in output_dir.
61+
If path_to_changes is a .changes file path, use it.
62+
If it is a directory, find the newest *.changes in that directory.
5763
"""
58-
if changes_file:
59-
if os.path.exists(changes_file):
60-
return os.path.abspath(changes_file)
61-
else:
62-
raise FileNotFoundError(f"Specified --changes-file not found: {changes_file}")
64+
if not path_to_changes:
65+
path_to_changes = '.'
6366

64-
# Search for newest .changes in output_dir
65-
candidates = glob.glob(os.path.join(output_dir or '.', '*.changes'))
66-
if not candidates:
67-
raise FileNotFoundError(f"No .changes files found in directory: {output_dir}")
67+
path_to_changes = os.path.abspath(path_to_changes)
6868

69-
newest = max(candidates, key=lambda p: os.path.getmtime(p))
70-
return os.path.abspath(newest)
69+
if os.path.isfile(path_to_changes) and path_to_changes.endswith('.changes'):
70+
return path_to_changes
71+
72+
if os.path.isdir(path_to_changes):
73+
candidates = glob.glob(os.path.join(path_to_changes, '*.changes'))
74+
if not candidates:
75+
raise FileNotFoundError(f"No .changes files found in directory: {path_to_changes}")
76+
newest = max(candidates, key=lambda p: os.path.getmtime(p))
77+
return os.path.abspath(newest)
78+
79+
raise FileNotFoundError(f"Invalid --path-to-changes: {path_to_changes}. Provide a .changes file or a directory containing .changes files.")
7180

7281

7382
def collect_debs_from_changes(changes_path: str):
@@ -136,17 +145,17 @@ def extract_debs_to_data(deb_names, work_dir, arch) -> bool:
136145
return True
137146

138147

139-
def create_tar_of_data(work_dir: str, tar_name: str) -> str:
148+
def create_tar_of_data(work_dir: str, tar_path: str) -> str:
140149
"""
141-
Create work_dir/<tar_name> containing the data/ directory.
150+
Create tarball at tar_path containing the data/ directory from work_dir.
142151
Returns the path to the tarball on success.
143152
"""
144153
data_root = os.path.join(work_dir, 'data')
145154
if not os.path.isdir(data_root):
146155
raise RuntimeError(f"Missing data directory to archive: {data_root}")
147156

148-
tar_path = os.path.join(work_dir, tar_name)
149157
logger.debug(f"Creating tarball: {tar_path}")
158+
os.makedirs(os.path.dirname(tar_path) or '.', exist_ok=True)
150159
with tarfile.open(tar_path, 'w:gz') as tar:
151160
tar.add(data_root, arcname='data')
152161
return tar_path
@@ -157,7 +166,7 @@ def main():
157166

158167
# Determine the .changes file
159168
try:
160-
changes_path = find_changes_file(args.changes_file, args.output_dir)
169+
changes_path = find_changes_file(args.path_to_changes)
161170
except Exception as e:
162171
logger.critical(str(e))
163172
sys.exit(1)
@@ -185,7 +194,16 @@ def main():
185194
tar_name = re.sub(r'\.changes$', '.tar.gz', base)
186195
if tar_name == base:
187196
tar_name = base + '.tar.gz'
188-
tar_path = create_tar_of_data(work_dir, tar_name)
197+
# Determine destination tar path based on --output-tar and --distro
198+
if args.output_tar:
199+
base_output_dir = os.path.abspath(args.output_tar)
200+
dest_dir = os.path.join(base_output_dir, f'prebuilt_{args.distro}') if args.distro else base_output_dir
201+
tar_path = os.path.join(dest_dir, tar_name)
202+
else:
203+
# Fallback to work_dir if no explicit output tar path is provided
204+
dest_dir = os.path.join(work_dir, f'prebuilt_{args.distro}') if args.distro else work_dir
205+
tar_path = os.path.join(dest_dir, tar_name)
206+
tar_path = create_tar_of_data(work_dir, tar_path)
189207
logger.info(f"Created tarball: {tar_path}")
190208
except Exception as e:
191209
logger.critical(f"Failed to create tarball: {e}")

0 commit comments

Comments
 (0)