-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added command check_file_format to pydabu.py
(just a small start to be enhanced)
- Loading branch information
1 parent
e37d8e3
commit e177f06
Showing
7 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
""" | ||
|
||
import os | ||
import re | ||
|
||
|
||
def check_file_available(files, key): | ||
|
@@ -19,10 +20,11 @@ def check_file_available(files, key): | |
for f in files: | ||
if f.lower().startswith(key): | ||
res = f | ||
break | ||
return res | ||
|
||
|
||
def analyse_data_structure(path): | ||
def analyse_data_structure(path, result=dict()): | ||
""" | ||
:Author: Daniel Mohr | ||
:Email: [email protected] | ||
|
@@ -31,12 +33,36 @@ def analyse_data_structure(path): | |
Analyse the data structure of the given path. | ||
:param path: directory path to analyse | ||
:param result: you can give a dict, where the results are appended | ||
or overridden | ||
""" | ||
result = dict() | ||
files = os.listdir(path) | ||
analysed_files = [] | ||
# find README, LICENSE, MANIFEST | ||
for key in ['README', 'LICENSE', 'MANIFEST']: | ||
result[key] = check_file_available(files, key) | ||
result['author'] = [{'name': 'foo', 'email': 'bar'}, | ||
{'name': 'a', 'email': 'b'}] | ||
for key in ['readme', 'license', 'manifest']: | ||
res = check_file_available(files, key) | ||
result[key] = res | ||
if res is not None: | ||
analysed_files.append(res) | ||
# analyse if directory is a repository | ||
result['repository'] = None | ||
for f in files: | ||
if (f in ['.git', '.bzr']) and os.path.isdir(f): | ||
# assume repository | ||
result['repository'] = f | ||
analysed_files.append(f) | ||
break | ||
# analyse if checksums are available (look for checksums) | ||
result['checksums'] = None | ||
regexp = re.compile( | ||
'.*checksum.*|.*\.md5|.*\.sha256|.*\.sha512|.*\.sha1', | ||
flags=re.IGNORECASE) | ||
for f in files: | ||
if regexp.findall(f): | ||
result['CHECKSUMS'] = f | ||
analysed_files.append(f) | ||
break | ||
result['data'] = list(set(files).difference(analysed_files)) | ||
# result['author'] = [{'name': 'foo', 'email': 'bar'}, | ||
# {'name': 'a', 'email': 'b'}] | ||
return result |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
:mod:`dabu.analyse_file_format` | ||
=============================== | ||
.. moduleauthor:: Daniel Mohr | ||
.. contents:: | ||
functions | ||
--------- | ||
.. currentmodule:: dabu.analyse_file_format | ||
.. autofunction:: analyse_file_format | ||
.. autofunction:: analyse_file_format_dict | ||
copyright + license | ||
=================== | ||
Author: Daniel Mohr | ||
Date: 2021-01-19 (last change). | ||
License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007. | ||
Copyright (C) 2021 Daniel Mohr | ||
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 3 of | ||
the License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, see | ||
http://www.gnu.org/licenses/ | ||
""" | ||
from .analyse_file_format import analyse_file_format | ||
from .analyse_file_format_dict import analyse_file_format_dict |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
:Author: Daniel Mohr | ||
:Email: [email protected] | ||
:Date: 2021-01-19 (last change). | ||
:License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007. | ||
""" | ||
|
||
import os.path | ||
|
||
|
||
def analyse_file_format(path): | ||
""" | ||
:Author: Daniel Mohr | ||
:Email: [email protected] | ||
:Date: 2021-01-19 (last change). | ||
Analyse the file format of the files stored in result. | ||
:param path: directory path to analyse | ||
""" | ||
_, file_extension = os.path.splitext(path) | ||
return file_extension |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
:Author: Daniel Mohr | ||
:Email: [email protected] | ||
:Date: 2021-01-19 (last change). | ||
:License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007. | ||
""" | ||
|
||
import os | ||
import re | ||
|
||
from .analyse_file_format import analyse_file_format | ||
|
||
|
||
def analyse_file_format_dict(path, result): | ||
""" | ||
:Author: Daniel Mohr | ||
:Email: [email protected] | ||
:Date: 2021-01-19 (last change). | ||
Analyse the file format of the files stored in result. | ||
:param path: directory path to analyse | ||
:param result: a dict; only the key 'data' will be read | ||
""" | ||
files = result['data'].copy() | ||
result['data'] = [] | ||
for f in files: | ||
result['data'].append({'name': f, 'format': analyse_file_format(f)}) | ||
return result |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
:Author: Daniel Mohr | ||
:Email: [email protected] | ||
:Date: 2021-01-19 (last change). | ||
:License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007. | ||
""" | ||
|
||
import json | ||
|
||
from dabu.analyse_data_structure import analyse_data_structure | ||
from dabu.analyse_file_format import analyse_file_format_dict | ||
|
||
|
||
def run_check_file_format(args): | ||
""" | ||
:Author: Daniel Mohr | ||
:Email: [email protected] | ||
:Date: 2021-01-19 (last change). | ||
:param args: namespace return from ArgumentParser.parse_args | ||
""" | ||
# print(args);exit() | ||
for path in args.directory: # for every given directory | ||
result = analyse_data_structure(path) | ||
result = analyse_file_format_dict(path, result) | ||
# print(result) | ||
if 'json' in args.output_format: | ||
print(json.dumps(result)) | ||
if 'human_readable' in args.output_format: | ||
print(json.dumps(result, indent=1)) |