Skip to content

Commit 0d441f9

Browse files
committed
Move code to proper place #660
Signed-off-by: Jono Yang <[email protected]>
1 parent 810956f commit 0d441f9

File tree

2 files changed

+45
-70
lines changed

2 files changed

+45
-70
lines changed

minecode_pipelines/miners/__init__.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,3 @@
66
# See https://github.com/aboutcode-org/purldb for support or download.
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
9-
10-
import os
11-
import saneyaml
12-
13-
from pathlib import Path
14-
15-
from aboutcode import hashid
16-
from packageurl import PackageURL
17-
from scanpipe.pipes import federatedcode
18-
19-
20-
def write_packageurls_to_file(repo, base_dir, packageurls):
21-
purl_file_rel_path = os.path.join(base_dir, hashid.PURLS_FILENAME)
22-
purl_file_full_path = Path(repo.working_dir) / purl_file_rel_path
23-
write_data_to_file(path=purl_file_full_path, data=packageurls)
24-
return purl_file_rel_path
25-
26-
27-
def write_data_to_file(path, data):
28-
path.parent.mkdir(parents=True, exist_ok=True)
29-
with open(path, encoding="utf-8", mode="w") as f:
30-
f.write(saneyaml.dump(data))
31-
32-
33-
def write_purls_to_repo(repo, package, packages, push_commit=False):
34-
# save purls to yaml
35-
path_elements = hashid.package_path_elements(package)
36-
_, core_path, _, _ = path_elements
37-
ppath = core_path / hashid.PURLS_FILENAME
38-
purls = [p.purl for p in packages]
39-
federatedcode.write_data_as_yaml(
40-
base_path=repo.working_dir,
41-
file_path=ppath,
42-
data=purls,
43-
)
44-
45-
change_type = "Add" if ppath in repo.untracked_files else "Update"
46-
commit_message = f"""\
47-
{change_type} list of available {package} versions
48-
"""
49-
federatedcode.commit_changes(
50-
repo=repo,
51-
files_to_commit=[ppath],
52-
commit_message=commit_message,
53-
)
54-
55-
# see if we should push
56-
if push_commit:
57-
federatedcode.push_changes(repo=repo)

minecode_pipelines/pipes/__init__.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,16 @@
1010
import json
1111
import os
1212
import requests
13-
import saneyaml
14-
1513
from datetime import datetime
16-
from pathlib import Path
1714

18-
from aboutcode.hashid import PURLS_FILENAME
19-
from scanpipe.pipes.federatedcode import clone_repository
20-
from scanpipe.pipes.federatedcode import commit_and_push_changes
15+
import saneyaml
16+
from aboutcode import hashid
17+
from scanpipe.pipes import federatedcode
2118

2219

2320
MINECODE_SETTINGS_REPO = "https://github.com/AyanSinhaMahapatra/minecode-test/"
2421

2522

26-
def write_packageurls_to_file(repo, base_dir, packageurls):
27-
purl_file_rel_path = os.path.join(base_dir, PURLS_FILENAME)
28-
purl_file_full_path = Path(repo.working_dir) / purl_file_rel_path
29-
write_data_to_file(path=purl_file_full_path, data=packageurls)
30-
return purl_file_rel_path
31-
32-
33-
def write_data_to_file(path, data):
34-
path.parent.mkdir(parents=True, exist_ok=True)
35-
with open(path, encoding="utf-8", mode="w") as f:
36-
f.write(saneyaml.dump(data))
37-
38-
3923
def fetch_last_serial_mined(
4024
settings_repo=MINECODE_SETTINGS_REPO,
4125
settings_path=None,
@@ -59,6 +43,12 @@ def fetch_last_serial_mined(
5943
return settings_data.get("last_serial")
6044

6145

46+
def write_data_to_file(path, data):
47+
path.parent.mkdir(parents=True, exist_ok=True)
48+
with open(path, encoding="utf-8", mode="w") as f:
49+
f.write(saneyaml.dump(data))
50+
51+
6252
def update_last_serial_mined(
6353
last_serial,
6454
settings_repo=MINECODE_SETTINGS_REPO,
@@ -68,7 +58,41 @@ def update_last_serial_mined(
6858
"date": str(datetime.now()),
6959
"last_serial": last_serial,
7060
}
71-
cloned_repo = clone_repository(repo_url=settings_repo)
61+
cloned_repo = federatedcode.clone_repository(repo_url=settings_repo)
7262
settings_path = os.path.join(cloned_repo.working_dir, settings_path)
7363
write_data_to_file(path=settings_path, data=settings_data)
74-
commit_and_push_changes(repo=cloned_repo, file_to_commit=settings_path)
64+
federatedcode.commit_and_push_changes(repo=cloned_repo, file_to_commit=settings_path)
65+
66+
67+
def create_package_path(package):
68+
path_elements = hashid.package_path_elements(package)
69+
_, core_path, _, _ = path_elements
70+
ppath = core_path / hashid.PURLS_FILENAME
71+
return ppath
72+
73+
74+
def write_purls_to_repo(repo, package, packages, commit_message="",push_commit=False):
75+
# save purls to yaml
76+
path_elements = hashid.package_path_elements(package)
77+
_, core_path, _, _ = path_elements
78+
ppath = core_path / hashid.PURLS_FILENAME
79+
purls = [p.purl for p in packages]
80+
federatedcode.write_data_as_yaml(
81+
base_path=repo.working_dir,
82+
file_path=ppath,
83+
data=purls,
84+
)
85+
86+
change_type = "Add" if ppath in repo.untracked_files else "Update"
87+
commit_message = f"""\
88+
{change_type} list of available {package} versions
89+
"""
90+
federatedcode.commit_changes(
91+
repo=repo,
92+
files_to_commit=[ppath],
93+
commit_message=commit_message,
94+
)
95+
96+
# see if we should push
97+
if push_commit:
98+
federatedcode.push_changes(repo=repo)

0 commit comments

Comments
 (0)