1010import json
1111import os
1212import requests
13- import saneyaml
14-
1513from 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
2320MINECODE_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-
3923def 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+
6252def 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