Skip to content

Instantly share code, notes, and snippets.

@m0wn1ka
Created February 28, 2024 03:15
Show Gist options
  • Save m0wn1ka/29cd8025a703b94e349c9a0a5bc6f50f to your computer and use it in GitHub Desktop.
Save m0wn1ka/29cd8025a703b94e349c9a0a5bc6f50f to your computer and use it in GitHub Desktop.
check_version :given a package.json file we tell the latest versions and it wil be usefule for comparing the dependencies versions
'''
1: user gives a package.json file
2:script fetches the latest version
3:tells the need of updates
4:uses python urllib,csv
5:we can have a bash script as well to run it
6:it outputs a file with results.csv
'''
#check_version.py
import json
import urllib3
import csv
from bs4 import BeautifulSoup
def check_version():
a=""" ___ __ _
/ _ \ /_ | |
_ __ ___ | | | |_ ___ __ | | | ____ _
| '_ ` _ \| | | \ \ /\ / / '_ \| | |/ / _` |
| | | | | | |_| |\ V V /| | | | | < (_| |
|_| |_| |_|\___/ \_/\_/ |_| |_|_|_|\_\__,_|"""
print(a)
file1=open('package.json')
data=(file1.read())
data_json=json.loads(data)['dependencies']
file1.close()
file_hanlder=open("result.csv",'w')
csv_handler=csv.writer(file_hanlder)
csv_handler.writerow(["npm module ","current version","latest version","up to date?"])
for name,version in data_json.items():
try:
url='https://www.npmjs.com/package/'
url+=name
print("chekcing "+name)
resp=urllib3.request("GET",url)
soup = BeautifulSoup(resp.data, 'html.parser')
class1=soup.find(class_='_76473bea f6 dib ph0 pv2 mb2-ns black-80 nowrap f5 fw4 lh-copy')
latest_version=(class1.text)[:-2]
if("^" in version):
version=version.replace("^","")
print("your version is ",version,"and latet version is ",latest_version)
ver=int(version.replace(".",""))
l_ver=int(latest_version.replace(".",""))
up_to_date=l_ver<=ver
csv_handler.writerow([name,version,latest_version,up_to_date])
if(not up_to_date):
print("u need to update ",name)
else:
print("for ",name,"u are uptodate")
except Exception as e:
print("can load version of ",name,e)
file_hanlder.close()
if __name__ == "__main__":
try:
check_version()
except:
print("all requiements must be satisfied")
#bash script to run
'''
#!/bin/bash
echo "----it is a version chekcin----"
echo "----have a packge.json in this folder---"
echo "make sure the python script name is check_ver.py"
python check_version.py
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment