-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
4.redo_language.py
164 lines (129 loc) · 4.3 KB
/
4.redo_language.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# This is a basic parsing script that will generate output files
# organized / named by repo, along with a "super credit" summary of all repos.
__author__ = "Vanessa Sochat"
__copyright__ = "Copyright 2022, Vanessa Sochat"
__license__ = "MPL 2.0"
from rse.main import Encyclopedia
from rse.utils.command import Command
from rse.utils.file import recursive_find
import citelang.utils as utils
import citelang.main.parser as parser
import citelang.main.packages as packages
import tempfile
import shutil
import argparse
import sys
import os
def clone(url, dest):
dest = os.path.join(dest, os.path.basename(url))
cmd = Command("git clone --depth 1 %s %s" % (url, dest))
cmd.execute()
if cmd.returncode != 0:
print("Issue cloning %s" % url)
return
return dest
def get_parser():
parser = argparse.ArgumentParser(
description="Research Software Encyclopedia Analyzer",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--settings-file",
dest="settings_file",
help="custom path to settings file.",
)
parser.add_argument(
"--language-file",
dest="language_files",
action="append",
help="Language requirements file basename to re-parse.",
choices=[
"DESCRIPTION",
"requirements.txt",
"setup.py",
"go.mod",
"package.json",
],
)
parser.add_argument(
"-o",
"--outdir",
help="Output directory for data.",
default=os.path.join(os.getcwd(), "_repos"),
)
return parser
repo_header = """---
title: %s
layout: repo
tipue_search_active: true
exclude_from_search: true
---
"""
def main():
p = get_parser()
args, extra = p.parse_known_args()
# Create a base temporary folder to work from
tempdir = tempfile.mkdtemp()
# Make sure output directory exists
outdir = os.path.abspath(args.outdir)
if not os.path.exists(outdir):
sys.exit("%s does not exist!" % args.outdir)
# Create a base temporary folder to work from
tempdir = tempfile.mkdtemp()
pedia = Encyclopedia(args.settings_file)
repos = list(pedia.list())
for i, reponame in enumerate(repos):
repo = pedia.get(reponame[0])
destdir = os.path.join(outdir, repo.uid)
if not os.path.exists(destdir):
continue
destfile = os.path.join(destdir, "data.json")
found = False
for lang_file in args.language_files:
reqfile = os.path.join(destdir, lang_file)
if os.path.exists(reqfile):
found = True
break
if not found:
continue
# Re-clone, because we need reqs files in context of their code!
dest = clone(repo.url, tempdir)
if not dest:
continue
# By here we've already filtered to requirements files
found = None
for filename in recursive_find(dest, pattern="*"):
basename = os.path.basename(filename)
if basename in packages.filesystem_manager_names:
found = filename
break
if not found:
continue
reqfile = os.path.join(destdir, os.path.basename(found))
# copy updated found file into folder
shutil.copyfile(found, reqfile)
# Files that are weirdly binaries are going to error here
try:
cli = parser.RequirementsParser(filename=found, min_credit=0.001)
result = cli.gen(name=repo.uid, min_credit=0.001)
except UnicodeDecodeError:
print("Issue parsing %s because of unicode, skipping." % repo.uid)
continue
continue
utils.write_json(cli.data, destfile)
destfile = os.path.join(destdir, "README.md")
utils.write_file(
repo_header % repo.uid + result.render(start_end_blocks=False), destfile
)
# use the root to derive a badge (this is how done internally)
destfile = os.path.join(destdir, "badge.png")
os.system(
"citelang badge %s %s --min-credit 0.001 --outfile %s --force"
% (repo.uid, found, destfile)
)
shutil.rmtree(dest)
# Clean up
shutil.rmtree(tempdir)
if __name__ == "__main__":
main()