Last active
December 26, 2019 08:32
-
-
Save psachin/68276c05322c28c017401a1f3568b5c2 to your computer and use it in GitHub Desktop.
Test _expand_paths of insights-core: https://github.com/redhataccess/insights-client/blob/master/insights_client/utilities.py#L176
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
def _expand_paths(path): | |
""" | |
Expand wildcarded paths | |
""" | |
import re | |
dir_name = os.path.dirname(path) | |
paths = [] | |
print("Attempting to expand:", path) | |
if os.path.isdir(dir_name): | |
files = os.listdir(dir_name) | |
match = os.path.basename(path) | |
for file_path in files: | |
if re.match(match, file_path): | |
expanded_path = os.path.join(dir_name, file_path) | |
paths.append(expanded_path) | |
return paths | |
else: | |
return("Could not expand", path) | |
if __name__ == "__main__": | |
with open('test_uploader.json', 'r') as fh: | |
specs = json.load(fh) | |
root = '/home/psachin/github/insights/insights-core-assets/test' | |
paths = None | |
for spec in specs['files']: | |
print('\n=== Spec {}: "{}" ==='.format(spec['symbolic_name'], | |
spec['file'])) | |
if spec['file'][0] == '/': | |
path = root + spec['file'] | |
else: | |
path = root + '/' + spec['file'] | |
print('Paths: {}'.format(_expand_paths(path))) | |
for spec in specs['globs']: | |
print('\n=== Spec {}: "{}" ==='.format(spec['symbolic_name'], | |
spec['glob'])) | |
if spec['glob'][0] == '/': | |
path = root + spec['glob'] | |
else: | |
path = root + '/' + spec['glob'] | |
print('Paths: {}'.format(_expand_paths(path))) |
tree test/
Output:
$ tree test
test
└── var
└── log
├── ceph
│ ├── ceph.log.1
│ ├── ceph-osd.1.log
│ └── ceph-osd.2.log
└── libvirt
├── libvirtd.log
└── qemu
├── bar.log
└── foo.log
5 directories, 6 files
$ python ./test_expand_path.py
=== Spec libvirtd_qemu_log: "/var/log/libvirt/qemu/(.*.log)" ===
Attempting to expand: /home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/qemu/(.*.log)
Paths: ['/home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/qemu/bar.log', '/home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/qemu/foo.log']
=== Spec libvirtd_log: "/var/log/libvirt/libvirtd.log" ===
Attempting to expand: /home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/libvirtd.log
Paths: ['/home/psachin/github/insights/insights-core-assets/test/var/log/libvirt/libvirtd.log']
=== Spec ceph_osd_log: "var/log/ceph/()*ceph-osd.*\.log$" ===
Attempting to expand: /home/psachin/github/insights/insights-core-assets/test/var/log/ceph/()*ceph-osd.*\.log$
Paths: ['/home/psachin/github/insights/insights-core-assets/test/var/log/ceph/ceph-osd.1.log', '/home/psachin/github/insights/insights-core-assets/test/var/log/ceph/ceph-osd.2.log']
=== Spec ceph_log: "/var/log/ceph/()ceph.log*" ===
Attempting to expand: /home/psachin/github/insights/insights-core-assets/test/var/log/ceph/()ceph.log*
Paths: ['/home/psachin/github/insights/insights-core-assets/test/var/log/ceph/ceph.log.1']
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test_uploader.json