-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetReq.py
75 lines (64 loc) · 2.84 KB
/
getReq.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
# -*- coding: utf-8 -*-
#!/usr/bin/python
import sys
import argparse, requests
from pip.operations import freeze
from argparse import RawTextHelpFormatter
header ="""
___ ___ ___ ___ ___ ___
/\ \ /\ \ /\ \ /\ \ /\ \ /\ \
/::\ \ /::\ \ \:\ \ /::\ \ /::\ \ /::\ \
/:/\:\ \ /:/\:\ \ \:\ \ /:/\:\ \ /:/\:\ \ /:/\:\ \
/:/ \:\ \ /::\~\:\ \ /::\ \ /::\~\:\ \ /::\~\:\ \ \:\~\:\ \
/:/__/_\:\__\ /:/\:\ \:\__\ /:/\:\__\ /:/\:\ \:\__\ /:/\:\ \:\__\ \:\ \:\_\\
\:\ /\ \/__/ \:\~\:\ \/__/ /:/ \/__/ \/_|::\/:/ / \:\~\:\ \/__/ \:\/:/ /
\:\ \:\__\ \:\ \:\__\ /:/ / |:|::/ / \:\ \:\__\ \::/ /
\:\/:/ / \:\ \/__/ \/__/ |:|\/__/ \:\ \/__/ /:/ /
\::/ / \:\__\ |:| | \:\__\ /:/ /
\/__/ \/__/ \|__| \/__/ \/__/
"""
reload(sys)
sys.setdefaultencoding('utf8')
parser = argparse.ArgumentParser(
description='Script to get requirements used in a python script.',
epilog='python getReq.py -f fichero.py -o salida.txt'
)
# parser.add_argument('--date', "-d", type=str, help='Envio de fecha del fichero DD/MM/YYYY')
parser.add_argument('--fichero', "-i", type=str, help='Name of the file to analize')
parser.add_argument('--salida', "-o", type=str, help='Output file to write the requirements')
VERBOSE = False
args = parser.parse_args()
def getReqs(filename, salida):
toImport = []
#Imports necesarios
with open(filename) as f:
for line in f.readlines():
if '\n' == line:
break
else:
if 'import' in line.split()[0]:
if ',' in line:
words = line.split()
words = [s.replace(',', '') for s in words]
words = words[words.index('import')+1:len(words)]
[toImport.append(s) for s in words]
else:
words = line.split()
toImport.append(words[words.index('import') + 1])
elif 'from' in line.split()[0]:
toImport.append(line.split()[1])
print toImport
fileSalida = open(salida,"w")
x = freeze.freeze()
for p in x:
#print p
if any(s.lower() in p for s in toImport):
fileSalida.write(p+"\n")
print p
fileSalida.close()
f.close()
print header
if args.fichero and args.salida:
getReqs(args.fichero, args.salida)
else:
print "Need more arguments. Look the help (-h)"