-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (102 loc) · 4.57 KB
/
main.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
import os
import glob
import sys
import stat
import argparse
import shutil
import git
from git import Repo
import yaml
import sqlite3
fdroid_repo_remote = "https://gitlab.com/fdroid/fdroiddata.git"
fdroid_repo_local = 'tmp_fdroid'
fdroid_repo_local_metadata = fdroid_repo_local+'/metadata'
results_output = 'output'
results_output_db_name = 'results.sqlite'
results_output_db_path = results_output+'/' + results_output_db_name
parser = argparse.ArgumentParser(description='F-Droid Metadata Extractor')
parser.add_argument('-o', action="store_true", default=False,
dest='arg_overwrite', help='Overwrite all data')
args = parser.parse_args()
def remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)
if args.arg_overwrite:
if os.path.exists(fdroid_repo_local):
print('removing fdroid directory...', end ='', flush=True)
shutil.rmtree(fdroid_repo_local, onerror=remove_readonly)
print('done')
if os.path.exists(results_output):
print('removing output directory...', end ='', flush=True)
shutil.rmtree(results_output, onerror=remove_readonly)
print('done')
if not os.path.exists(fdroid_repo_local):
print('creating fdroid directory...', end ='', flush=True)
os.makedirs(fdroid_repo_local)
print('done')
if not os.path.exists(results_output):
print('creating output directory...', end ='', flush=True)
os.makedirs(results_output)
print('done')
if not os.path.exists(results_output_db_path):
print('creating empty database...', end ='', flush=True)
conn = sqlite3.connect(results_output_db_path)
c = conn.cursor()
c.execute('''CREATE TABLE App
([Id] INTEGER PRIMARY KEY AUTOINCREMENT,[UniqueName] text,[Name] text,[AutoName] text,[WebSite] text,[AuthorName] text,[AuthorEmail] text,[Categories] text,[Repo] text,[SourceCode] text,[RepoType] text,[Changelog] text,[License] text,[IssueTracker] text,[Description] text)''')
conn.commit()
conn.close()
print('done')
try:
repo = git.Repo(fdroid_repo_local, search_parent_directories=False)
except git.exc.InvalidGitRepositoryError:
print('cloning fdroid repo...', end ='', flush=True)
repo = Repo.clone_from(fdroid_repo_remote, fdroid_repo_local)
print('done')
print('extracting metadata...', end ='', flush=True)
conn = sqlite3.connect(results_output_db_path)
c = conn.cursor()
os.chdir(fdroid_repo_local_metadata)
for file in glob.glob("*.yml"):
with open(file, 'rt', encoding='utf8') as stream:
try:
data = yaml.safe_load(stream)
data_repotype = data_changelog = data_license = data_issuetracker = data_sourcecode = data_repo = data_autoname = data_name = data_description = data_categories = data_website = data_authorname = data_authoremail = ''
data_uniquename = file.rsplit(".", 1)[0]
if 'RepoType' in data:
data_repotype = data['RepoType']
if 'Changelog' in data:
data_changelog = data['Changelog']
if 'License' in data:
data_license = data['License']
if 'IssueTracker' in data:
data_issuetracker = data['IssueTracker']
if 'SourceCode' in data:
data_sourcecode = data['SourceCode']
if 'Repo' in data:
data_repo = data['Repo']
if 'AutoName' in data:
data_autoname = data['AutoName']
if 'Name' in data:
data_name = data['Name']
if 'Description' in data:
data_description = data['Description']
if 'Categories' in data:
data_categories = data['Categories']
if 'WebSite' in data:
data_website = data['WebSite']
if 'AuthorName' in data:
data_authorname = data['AuthorName']
if 'AuthorEmail' in data:
data_authoremail = data['AuthorEmail']
c.execute('INSERT INTO App (Repo,AutoName,Name,Description,WebSite,AuthorName,AuthorEmail,SourceCode,IssueTracker,License,Changelog,RepoType,UniqueName,Categories) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?);',
(data_repo, data_autoname, data_name, data_description, data_website, data_authorname, data_authoremail, data_sourcecode, data_issuetracker, data_license, data_changelog, data_repotype, data_uniquename, ','.join(data_categories)))
conn.commit()
except yaml.YAMLError as exc:
print(exc)
except:
e = sys.exc_info()[0]
print(e)
conn.close()
print('done')
print('execution completed!')