Skip to content

Instantly share code, notes, and snippets.

@yebt
Last active August 22, 2024 15:29
Show Gist options
  • Save yebt/9729a2ba13f0fc7e42e010e261bcd5b9 to your computer and use it in GitHub Desktop.
Save yebt/9729a2ba13f0fc7e42e010e261bcd5b9 to your computer and use it in GitHub Desktop.
This script was created as an helps to recover the require dependencies inside vendor directory of project, if you lost the main composer.json
#!/bin/python
# -*- coding: utf-8 -*-
import json
import sys
# Constant vars
if len(sys.argv) != 2:
print('Error!! -- No file specified ')
print('\t get_composer_deps.py <path/to/vendor/composer/installed.json>')
exit()
filename =''
try:
filename = sys.argv[1]
json_file= open(filename, 'r')
except Exception as e:
print(f"Error!! -- The file '{filename}' dont exist" )
exit()
# get the data
json_load = json.load(json_file)
# interactive vars
direct_dependences_dict = {}
indirect_dependences_list = []
# This manage the indirect dependences and
# delete or add this from dictionary or list
def manage_indirect_deps( ind_deps_list :list) -> None:
# traverse list of indirect dependences
for inddep in ind_deps_list:
if not(inddep in indirect_dependences_list):
# delete 'direct' dependence calls from 'indirect' dependences
if inddep in direct_dependences_dict:
del direct_dependences_dict[inddep]
# add 'indirect' dependence to list
indirect_dependences_list.append(inddep)
# traverse the data list
for obj in json_load:
# get the dep data
dep_name = obj['name']
dep_version = obj['version_normalized']
dep_dev = (lambda x: ((lambda y: y.get('dev-master') if y else None ) (x.get('branch-alias'))) if x else None ) ( obj.get('extra') )
# verify direct dependeces
if not (dep_name in indirect_dependences_list):
direct_dependences_dict[dep_name] = (dep_version, dep_dev)
# get indeirect dependecnes
ind_deps_reqs = obj.get('require')
ind_deps_reqs_dev = obj.get('require-dev')
# manage indirect dependences
manage_indirect_deps(ind_deps_reqs if ind_deps_reqs else [] )
manage_indirect_deps(ind_deps_reqs_dev if ind_deps_reqs_dev else [])
# traverse the obtained direct dependencies
str_req = '"require": {'
str_req_dev = '"require-dev": {'
for dep in direct_dependences_dict:
if direct_dependences_dict[dep][1]:
str_req_dev += '\n \"'+dep + '\": \"^' + direct_dependences_dict[dep][0] + '\"'
else:
str_req += '\n \"'+dep + '\": \"^' + direct_dependences_dict[dep][0] + '\"'
str_req += '\n},'
str_req_dev += '\n}'
print(str_req)
print(str_req_dev)
#!/bin/python
# -*- coding: utf-8 -*-
import json
import sys
from pygments import highlight
from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.lexers.web import JsonLexer
# Constant vars
if len(sys.argv) != 2:
print('Error!! -- No file specified ')
print('\t get_composer_deps.py <path/to/vendor/composer/installed.json>')
exit()
filename =''
try:
filename = sys.argv[1]
json_file= open(filename, 'r')
except Exception as e:
print(f"Error!! -- The file '{filename}' dont exist" )
exit()
# get the data
json_load = json.load(json_file)
# interactive vars
direct_dependences_dict = {}
indirect_dependences_list = []
# This manage the indirect dependences and
# delete or add this from dictionary or list
def manage_indirect_deps( ind_deps_list :list) -> None:
# traverse list of indirect dependences
for inddep in ind_deps_list:
if not(inddep in indirect_dependences_list):
# delete 'direct' dependence calls from 'indirect' dependences
if inddep in direct_dependences_dict:
del direct_dependences_dict[inddep]
# add 'indirect' dependence to list
indirect_dependences_list.append(inddep)
# traverse the data list
for obj in json_load:
# get the dep data
dep_name = obj['name']
dep_version = obj['version_normalized']
dep_dev = (lambda x: ((lambda y: y.get('dev-master') if y else None ) (x.get('branch-alias'))) if x else None ) ( obj.get('extra') )
# verify direct dependeces
if not (dep_name in indirect_dependences_list):
direct_dependences_dict[dep_name] = (dep_version, dep_dev)
# get indeirect dependecnes
ind_deps_reqs = obj.get('require')
ind_deps_reqs_dev = obj.get('require-dev')
# manage indirect dependences
manage_indirect_deps(ind_deps_reqs if ind_deps_reqs else [] )
manage_indirect_deps(ind_deps_reqs_dev if ind_deps_reqs_dev else [])
# traverse the obtained direct dependencies
req_json_dic = {}
req_dev_json_dic = {}
for dep in direct_dependences_dict:
if direct_dependences_dict[dep][1]:
req_dev_json_dic[dep] = "^" + direct_dependences_dict[dep][0]
else:
req_json_dic[dep] = "^" + direct_dependences_dict[dep][0]
# get all deps
compser_json_dic = {"require": req_json_dic, "require-dev":req_dev_json_dic}
# Generate JSON
raw_json = json.dumps(compser_json_dic, indent=4)
# Colorize it
colorful = highlight(
raw_json,
lexer=JsonLexer(),
formatter=Terminal256Formatter(),
)
# Print to console
print(colorful)
@yebt
Copy link
Author

yebt commented Nov 3, 2023

No problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment